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 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
19 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
20 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
22 d1f6d47b 2019-02-04 stsp #include <dirent.h>
23 12ce7a6c 2019-08-12 stsp #include <limits.h>
24 f5c49f82 2019-01-06 stsp #include <stddef.h>
25 86c3caaf 2018-03-09 stsp #include <string.h>
26 86c3caaf 2018-03-09 stsp #include <stdio.h>
27 86c3caaf 2018-03-09 stsp #include <stdlib.h>
28 303e14b5 2019-09-23 stsp #include <time.h>
29 86c3caaf 2018-03-09 stsp #include <fcntl.h>
30 86c3caaf 2018-03-09 stsp #include <errno.h>
31 86c3caaf 2018-03-09 stsp #include <unistd.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>
36 86c3caaf 2018-03-09 stsp #include "got_error.h"
37 86c3caaf 2018-03-09 stsp #include "got_repository.h"
38 5261c201 2018-04-01 stsp #include "got_reference.h"
39 9d31a1d8 2018-03-11 stsp #include "got_object.h"
40 1dd54920 2019-05-11 stsp #include "got_path.h"
41 e6209546 2019-08-22 stsp #include "got_cancel.h"
42 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 234035bc 2019-06-01 stsp #include "got_diff.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
47 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
53 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
54 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
55 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
56 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
59 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 a807c50b 2023-07-05 thomas static mode_t
66 a807c50b 2023-07-05 thomas apply_umask(mode_t mode)
68 a807c50b 2023-07-05 thomas mode_t um;
70 a807c50b 2023-07-05 thomas um = umask(000);
71 a807c50b 2023-07-05 thomas umask(um);
72 a807c50b 2023-07-05 thomas return mode & ~um;
75 99724ed4 2018-03-10 stsp static const struct got_error *
76 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
78 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
81 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
82 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
84 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
89 507dc3bb 2018-12-29 stsp static const struct got_error *
90 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
92 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
93 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
94 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
95 507dc3bb 2018-12-29 stsp char *path = NULL;
97 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
98 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
99 507dc3bb 2018-12-29 stsp path = NULL;
103 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &tmpfile, path, "");
107 507dc3bb 2018-12-29 stsp if (content) {
108 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
109 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
110 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
115 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
116 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
117 2a57020b 2019-02-20 stsp unlink(tmppath);
122 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
123 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
124 230a42bd 2019-05-11 jcs free(tmppath);
125 09fe317a 2018-03-11 stsp return err;
128 024e9686 2019-05-14 stsp static const struct got_error *
129 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
131 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
132 024e9686 2019-05-14 stsp char *refstr = NULL;
134 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
135 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
136 024e9686 2019-05-14 stsp if (refstr == NULL)
137 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
139 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
140 024e9686 2019-05-14 stsp if (refstr == NULL)
141 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
143 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
144 024e9686 2019-05-14 stsp free(refstr);
145 024e9686 2019-05-14 stsp return err;
148 86c3caaf 2018-03-09 stsp const struct got_error *
149 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
150 ad10f64e 2023-07-19 thomas const char *prefix, const char *meta_dir, struct got_repository *repo)
152 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
153 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
154 ec22038e 2019-03-10 stsp uuid_t uuid;
155 ec22038e 2019-03-10 stsp uint32_t uuid_status;
156 65596e15 2018-12-24 stsp int obj_type;
157 7ac97322 2018-03-11 stsp char *path_got = NULL;
158 1451e70d 2018-03-10 stsp char *formatstr = NULL;
159 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
160 65596e15 2018-12-24 stsp char *basestr = NULL;
161 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
163 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
164 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
168 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
170 65596e15 2018-12-24 stsp return err;
171 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
173 65596e15 2018-12-24 stsp return err;
174 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
175 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
177 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
178 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
179 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
182 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
183 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
184 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
188 ad10f64e 2023-07-19 thomas /* Create .got/.cvg directory (may already exist). */
189 ad10f64e 2023-07-19 thomas if (asprintf(&path_got, "%s/%s", path, meta_dir) == -1) {
190 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
193 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
194 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
198 056e7441 2018-03-11 stsp /* Create an empty lock file. */
199 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
203 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
204 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
208 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
209 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
213 65596e15 2018-12-24 stsp /* Record our base commit. */
214 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
217 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
221 1451e70d 2018-03-10 stsp /* Store path to repository. */
222 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
223 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
227 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
228 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
229 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
233 ec22038e 2019-03-10 stsp /* Generate UUID. */
234 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
235 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
236 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
239 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
240 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
241 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
244 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
248 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
249 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
250 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
253 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
258 65596e15 2018-12-24 stsp free(commit_id);
259 7ac97322 2018-03-11 stsp free(path_got);
260 1451e70d 2018-03-10 stsp free(formatstr);
261 0bb8a95e 2018-03-12 stsp free(absprefix);
262 65596e15 2018-12-24 stsp free(basestr);
263 ec22038e 2019-03-10 stsp free(uuidstr);
264 86c3caaf 2018-03-09 stsp return err;
267 247140b2 2019-02-05 stsp const struct got_error *
268 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
269 e5dc7198 2018-12-29 stsp const char *path_prefix)
271 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
273 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
274 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
275 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
277 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
278 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
279 e5dc7198 2018-12-29 stsp free(absprefix);
280 e5dc7198 2018-12-29 stsp return NULL;
283 bc70eb79 2019-05-09 stsp const char *
284 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
286 36a38700 2019-05-10 stsp return worktree->head_ref_name;
289 024e9686 2019-05-14 stsp const struct got_error *
290 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
291 024e9686 2019-05-14 stsp struct got_reference *head_ref)
293 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
294 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
296 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
297 ad10f64e 2023-07-19 thomas worktree->meta_dir) == -1) {
298 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
299 024e9686 2019-05-14 stsp path_got = NULL;
303 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
304 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
305 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
309 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
313 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
314 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
316 024e9686 2019-05-14 stsp free(path_got);
318 024e9686 2019-05-14 stsp free(head_ref_name);
319 024e9686 2019-05-14 stsp return err;
322 b72f483a 2019-02-05 stsp struct got_object_id *
323 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
325 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
328 507dc3bb 2018-12-29 stsp const struct got_error *
329 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
330 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
332 507dc3bb 2018-12-29 stsp const struct got_error *err;
333 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
334 507dc3bb 2018-12-29 stsp char *id_str = NULL;
335 507dc3bb 2018-12-29 stsp char *path_got = NULL;
337 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
338 ad10f64e 2023-07-19 thomas worktree->meta_dir) == -1) {
339 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
340 507dc3bb 2018-12-29 stsp path_got = NULL;
344 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
346 507dc3bb 2018-12-29 stsp return err;
348 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
349 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
353 507dc3bb 2018-12-29 stsp /* Record our base commit. */
354 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
357 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
361 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
362 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
363 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
364 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
369 507dc3bb 2018-12-29 stsp got_object_close(obj);
370 507dc3bb 2018-12-29 stsp free(id_str);
371 507dc3bb 2018-12-29 stsp free(path_got);
372 507dc3bb 2018-12-29 stsp return err;
375 50b0790e 2020-09-11 stsp const struct got_gotconfig *
376 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
378 50b0790e 2020-09-11 stsp return worktree->gotconfig;
381 9d31a1d8 2018-03-11 stsp static const struct got_error *
382 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
384 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
385 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
386 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
387 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
388 86c3caaf 2018-03-09 stsp return NULL;
391 21908da4 2019-01-13 stsp static const struct got_error *
392 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
394 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
395 4a1ddfc2 2019-01-12 stsp char *abspath;
397 59530ea7 2023-07-17 thomas /* We only accept worktree-relative paths here. */
398 59530ea7 2023-07-17 thomas if (got_path_is_absolute(path)) {
399 59530ea7 2023-07-17 thomas return got_error_fmt(GOT_ERR_BAD_PATH,
400 59530ea7 2023-07-17 thomas "%s does not accept absolute paths: %s",
401 59530ea7 2023-07-17 thomas __func__, path);
404 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
405 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
407 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
408 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
409 ddcd8544 2019-03-11 stsp struct stat sb;
410 ddcd8544 2019-03-11 stsp err = NULL;
411 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
412 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
413 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
414 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
415 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
418 4a1ddfc2 2019-01-12 stsp free(abspath);
419 68c76935 2019-02-19 stsp return err;
422 68c76935 2019-02-19 stsp static const struct got_error *
423 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
425 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
426 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
427 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
428 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
433 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
434 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
435 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
438 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
439 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
440 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
443 68c76935 2019-02-19 stsp if (flen1 == 0) {
444 68c76935 2019-02-19 stsp if (flen2 != 0)
447 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
448 68c76935 2019-02-19 stsp if (flen1 != 0)
451 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
452 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
462 68c76935 2019-02-19 stsp return err;
465 68c76935 2019-02-19 stsp static const struct got_error *
466 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
468 68c76935 2019-02-19 stsp struct stat sb;
469 68c76935 2019-02-19 stsp size_t size1, size2;
473 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
474 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
475 68c76935 2019-02-19 stsp size1 = sb.st_size;
477 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
478 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
479 68c76935 2019-02-19 stsp size2 = sb.st_size;
481 68c76935 2019-02-19 stsp if (size1 != size2) {
483 68c76935 2019-02-19 stsp return NULL;
486 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
487 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
488 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
489 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
491 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
494 24f136e0 2021-11-20 thomas static const struct got_error *
495 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
497 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
498 24f136e0 2021-11-20 thomas size_t flen;
499 24f136e0 2021-11-20 thomas ssize_t outlen;
501 24f136e0 2021-11-20 thomas *outsize = 0;
503 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
504 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
506 24f136e0 2021-11-20 thomas for (;;) {
507 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
508 24f136e0 2021-11-20 thomas if (flen == 0) {
509 24f136e0 2021-11-20 thomas if (ferror(f))
510 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
511 24f136e0 2021-11-20 thomas if (feof(f))
514 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
515 24f136e0 2021-11-20 thomas if (outlen == -1)
516 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
517 24f136e0 2021-11-20 thomas if (outlen != flen)
518 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
519 24f136e0 2021-11-20 thomas *outsize += outlen;
522 24f136e0 2021-11-20 thomas return NULL;
525 24f136e0 2021-11-20 thomas static const struct got_error *
526 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
527 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
528 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
529 24f136e0 2021-11-20 thomas const char *ondisk_path)
531 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
532 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
533 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
534 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
535 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
536 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
537 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
539 24f136e0 2021-11-20 thomas *overlapcnt = 0;
541 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
543 24f136e0 2021-11-20 thomas return err;
545 24f136e0 2021-11-20 thomas if (same_content)
546 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
548 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
550 24f136e0 2021-11-20 thomas return err;
551 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
552 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
554 24f136e0 2021-11-20 thomas return err;
555 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
557 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
558 24f136e0 2021-11-20 thomas *overlapcnt = 1;
559 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
560 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
561 24f136e0 2021-11-20 thomas goto done;
563 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
564 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
565 24f136e0 2021-11-20 thomas goto done;
567 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
568 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
569 24f136e0 2021-11-20 thomas goto done;
571 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
572 fc2a50f2 2022-11-01 thomas base_path_orig, "");
574 24f136e0 2021-11-20 thomas goto done;
575 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
576 fc2a50f2 2022-11-01 thomas base_path_deriv, "");
578 24f136e0 2021-11-20 thomas goto done;
579 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
580 fc2a50f2 2022-11-01 thomas base_path_deriv2, "");
582 24f136e0 2021-11-20 thomas goto done;
583 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
585 24f136e0 2021-11-20 thomas goto done;
586 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
588 24f136e0 2021-11-20 thomas goto done;
589 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
591 24f136e0 2021-11-20 thomas goto done;
592 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
593 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
594 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
595 24f136e0 2021-11-20 thomas goto done;
597 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
598 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
599 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
600 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
601 24f136e0 2021-11-20 thomas path_deriv) < 0) {
602 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
603 24f136e0 2021-11-20 thomas goto done;
605 24f136e0 2021-11-20 thomas if (size_orig > 0) {
606 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
607 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
608 24f136e0 2021-11-20 thomas label_orig ? " " : "",
609 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
610 24f136e0 2021-11-20 thomas path_orig) < 0) {
611 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
612 24f136e0 2021-11-20 thomas goto done;
615 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
616 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
617 24f136e0 2021-11-20 thomas path_deriv2,
618 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
619 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
620 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
621 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
622 24f136e0 2021-11-20 thomas goto done;
624 24f136e0 2021-11-20 thomas } else if (changed_deriv)
625 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
626 24f136e0 2021-11-20 thomas else if (changed_deriv2)
627 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
629 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
630 24f136e0 2021-11-20 thomas err == NULL)
631 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
632 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
633 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
634 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
635 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
636 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
637 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
638 24f136e0 2021-11-20 thomas free(path_orig);
639 24f136e0 2021-11-20 thomas free(path_deriv);
640 24f136e0 2021-11-20 thomas free(path_deriv2);
641 24f136e0 2021-11-20 thomas free(base_path_orig);
642 24f136e0 2021-11-20 thomas free(base_path_deriv);
643 24f136e0 2021-11-20 thomas free(base_path_deriv2);
644 24f136e0 2021-11-20 thomas return err;
648 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
649 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
650 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
651 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
652 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
654 6353ad76 2019-02-08 stsp static const struct got_error *
655 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
656 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
657 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
658 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
659 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
660 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
662 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
663 6353ad76 2019-02-08 stsp int merged_fd = -1;
664 db590691 2021-06-02 stsp FILE *f_merged = NULL;
665 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
666 234035bc 2019-06-01 stsp int overlapcnt = 0;
667 3524bbf9 2020-10-20 stsp char *parent = NULL;
669 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
671 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
673 3524bbf9 2020-10-20 stsp return err;
675 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
676 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
680 fc2a50f2 2022-11-01 thomas err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
684 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
685 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
686 24f136e0 2021-11-20 thomas if (err) {
687 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
688 24f136e0 2021-11-20 thomas goto done;
689 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
690 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
691 24f136e0 2021-11-20 thomas ondisk_path);
693 24f136e0 2021-11-20 thomas goto done;
696 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
697 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
701 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
702 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
706 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
707 db590691 2021-06-02 stsp if (f_merged == NULL) {
708 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
711 db590691 2021-06-02 stsp merged_fd = -1;
713 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
714 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
715 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
721 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
722 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
726 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
727 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
728 230a42bd 2019-05-11 jcs ondisk_path);
733 fdcb7daf 2019-12-15 stsp if (merged_path)
734 fdcb7daf 2019-12-15 stsp unlink(merged_path);
736 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
737 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
738 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
739 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
740 6353ad76 2019-02-08 stsp free(merged_path);
741 af54ae4a 2019-02-19 stsp free(base_path);
742 3524bbf9 2020-10-20 stsp free(parent);
743 af57b12a 2020-07-23 stsp return err;
746 af57b12a 2020-07-23 stsp static const struct got_error *
747 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
748 af57b12a 2020-07-23 stsp size_t target_len)
750 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
751 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
752 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
753 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
754 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
755 af57b12a 2020-07-23 stsp ondisk_path);
756 af57b12a 2020-07-23 stsp return NULL;
760 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
761 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
762 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
763 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
765 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
766 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
768 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
769 993e2a1b 2020-07-23 stsp * has deleted this symlink.
771 11cc08c1 2020-07-23 stsp static const struct got_error *
772 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
773 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
774 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
776 11cc08c1 2020-07-23 stsp const struct got_error *err;
777 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
778 11cc08c1 2020-07-23 stsp FILE *f = NULL;
780 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
782 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
784 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
785 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
786 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
790 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
792 3818e3c4 2020-11-01 naddy goto done;
794 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
795 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
799 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
800 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
801 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
802 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
803 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
804 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
805 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
806 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
807 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
808 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
812 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
813 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
816 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
817 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
821 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
822 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
823 11cc08c1 2020-07-23 stsp free(path);
824 11cc08c1 2020-07-23 stsp free(id_str);
825 11cc08c1 2020-07-23 stsp free(label_deriv);
826 11cc08c1 2020-07-23 stsp return err;
829 d219f183 2020-07-23 stsp /* forward declaration */
830 d219f183 2020-07-23 stsp static const struct got_error *
831 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
832 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
833 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
834 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
837 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
838 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
839 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
840 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
842 af57b12a 2020-07-23 stsp static const struct got_error *
843 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
844 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
845 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
846 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
847 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
849 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
850 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
851 af57b12a 2020-07-23 stsp struct stat sb;
852 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
853 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
854 11cc08c1 2020-07-23 stsp int have_local_change = 0;
855 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
857 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
858 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
860 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
861 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
862 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
863 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
864 af57b12a 2020-07-23 stsp ondisk_path);
867 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
869 526a746f 2020-07-23 stsp if (blob_orig) {
870 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
875 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
876 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
877 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
878 11cc08c1 2020-07-23 stsp have_local_change = 1;
880 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
881 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
882 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
883 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
884 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
886 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
887 11cc08c1 2020-07-23 stsp if (ancestor_target) {
888 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
889 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
891 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
892 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
893 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
894 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
897 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
898 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
899 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
900 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
903 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
906 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
907 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
908 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
909 af57b12a 2020-07-23 stsp strlen(deriv_target));
912 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
913 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
914 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
915 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
916 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
917 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
920 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
921 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
922 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
925 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
931 af57b12a 2020-07-23 stsp free(ancestor_target);
932 eec2f5a9 2021-06-03 stsp return err;
935 eec2f5a9 2021-06-03 stsp static const struct got_error *
936 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
938 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
939 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
940 eec2f5a9 2021-06-03 stsp ssize_t target_len;
944 eec2f5a9 2021-06-03 stsp *outfile = NULL;
946 eec2f5a9 2021-06-03 stsp f = got_opentemp();
947 eec2f5a9 2021-06-03 stsp if (f == NULL)
948 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
949 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
950 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
951 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
954 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
955 eec2f5a9 2021-06-03 stsp if (n != target_len) {
956 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
959 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
960 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
963 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
964 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
971 eec2f5a9 2021-06-03 stsp *outfile = f;
972 14c901f1 2019-08-08 stsp return err;
976 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
977 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
978 14c901f1 2019-08-08 stsp * acts as the second derived version.
980 14c901f1 2019-08-08 stsp static const struct got_error *
981 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
982 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
983 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
984 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
985 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
986 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
988 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
989 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
990 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
991 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
992 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
994 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
996 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
998 ed6b5030 2020-10-20 stsp return err;
1000 67a66647 2021-05-31 stsp if (blob_orig) {
1001 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
1002 67a66647 2021-05-31 stsp parent) == -1) {
1003 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
1004 67a66647 2021-05-31 stsp base_path = NULL;
1005 67a66647 2021-05-31 stsp goto done;
1008 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_orig_path, &f_orig,
1009 fc2a50f2 2022-11-01 thomas base_path, "");
1011 67a66647 2021-05-31 stsp goto done;
1012 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1013 67a66647 2021-05-31 stsp blob_orig);
1015 67a66647 2021-05-31 stsp goto done;
1016 67a66647 2021-05-31 stsp free(base_path);
1019 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1020 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1021 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1023 db590691 2021-06-02 stsp f_orig = got_opentemp();
1024 db590691 2021-06-02 stsp if (f_orig == NULL) {
1025 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1026 db590691 2021-06-02 stsp goto done;
1030 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1031 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1032 14c901f1 2019-08-08 stsp base_path = NULL;
1033 14c901f1 2019-08-08 stsp goto done;
1036 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1038 14c901f1 2019-08-08 stsp goto done;
1039 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1040 14c901f1 2019-08-08 stsp blob_deriv);
1042 14c901f1 2019-08-08 stsp goto done;
1044 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1046 14c901f1 2019-08-08 stsp goto done;
1047 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1048 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1049 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1050 14c901f1 2019-08-08 stsp goto done;
1054 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1055 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1057 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1058 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1060 eec2f5a9 2021-06-03 stsp goto done;
1063 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1064 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1065 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1066 eec2f5a9 2021-06-03 stsp goto done;
1068 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1069 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1070 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1071 eec2f5a9 2021-06-03 stsp close(fd);
1072 eec2f5a9 2021-06-03 stsp goto done;
1076 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1077 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1078 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1080 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1081 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1082 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1083 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1084 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1085 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1086 14c901f1 2019-08-08 stsp free(base_path);
1087 67a66647 2021-05-31 stsp if (blob_orig_path) {
1088 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1089 67a66647 2021-05-31 stsp free(blob_orig_path);
1091 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1092 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1093 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1095 6353ad76 2019-02-08 stsp free(id_str);
1096 818c7501 2019-07-11 stsp free(label_deriv);
1097 ed6b5030 2020-10-20 stsp free(parent);
1098 4a1ddfc2 2019-01-12 stsp return err;
1101 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1102 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1103 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1104 aaf972e2 2023-09-18 thomas int wt_fd, const char *path, struct got_object_id *blob_id,
1105 aaf972e2 2023-09-18 thomas int update_timestamps)
1107 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1108 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1110 65b05cec 2020-07-23 stsp *new_iep = NULL;
1112 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1114 054041d0 2020-06-24 stsp return err;
1116 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1117 aaf972e2 2023-09-18 thomas blob_id->sha1, base_commit_id->sha1, update_timestamps);
1119 054041d0 2020-06-24 stsp goto done;
1121 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1124 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1126 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1127 13d9040b 2019-03-27 stsp return err;
1130 1ebedb77 2019-10-19 stsp static mode_t
1131 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1133 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1135 1ebedb77 2019-10-19 stsp if (executable) {
1136 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1137 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1138 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1139 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1140 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1141 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1144 a2c162eb 2022-10-30 thomas return st_mode;
1147 8ba819a3 2020-07-23 stsp /* forward declaration */
1148 13d9040b 2019-03-27 stsp static const struct got_error *
1149 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1150 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1151 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1152 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1153 aaf972e2 2023-09-18 thomas int path_is_unversioned, int *update_timestamps,
1154 aaf972e2 2023-09-18 thomas struct got_repository *repo,
1155 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1158 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1159 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1161 8ba819a3 2020-07-23 stsp static const struct got_error *
1162 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1163 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1165 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1166 5a1fbc73 2020-07-23 stsp ssize_t elen;
1167 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1170 c6e8a826 2021-04-05 stsp *did_something = 0;
1173 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1174 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1175 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1176 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1177 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1178 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1180 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1181 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1182 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1183 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1185 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1186 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1187 5a1fbc73 2020-07-23 stsp if (elen == -1)
1188 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1190 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1191 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1192 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1195 c6e8a826 2021-04-05 stsp *did_something = 1;
1196 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1197 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1198 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1199 5a1fbc73 2020-07-23 stsp return err;
1202 3c1ec782 2020-07-23 stsp static const struct got_error *
1203 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1204 ad10f64e 2023-07-19 thomas size_t target_len, const char *ondisk_path, const char *wtroot_path,
1205 ad10f64e 2023-07-19 thomas const char *meta_dir)
1207 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1208 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1209 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1211 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1213 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1214 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1215 3c1ec782 2020-07-23 stsp return NULL;
1219 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1220 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1221 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1222 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1223 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1225 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1226 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1227 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1229 ce031e9e 2020-10-20 stsp return err;
1230 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1231 ce031e9e 2020-10-20 stsp free(parent);
1232 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1234 ce031e9e 2020-10-20 stsp free(parent);
1235 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1236 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1237 3c1ec782 2020-07-23 stsp free(abspath);
1238 3c1ec782 2020-07-23 stsp return err;
1240 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1241 3c1ec782 2020-07-23 stsp free(abspath);
1243 3c1ec782 2020-07-23 stsp return err;
1245 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1247 3c1ec782 2020-07-23 stsp return err;
1250 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1251 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1252 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1253 3c1ec782 2020-07-23 stsp return NULL;
1256 ad10f64e 2023-07-19 thomas /* Do not allow symlinks pointing into the meta directory. */
1257 ad10f64e 2023-07-19 thomas if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1258 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1259 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1260 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1262 3c1ec782 2020-07-23 stsp free(path_got);
1263 3c1ec782 2020-07-23 stsp return NULL;
1266 5a1fbc73 2020-07-23 stsp static const struct got_error *
1267 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1268 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1269 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1270 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1271 ace90326 2021-09-27 thomas struct got_repository *repo,
1272 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1274 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1275 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1276 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1277 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1278 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1280 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1283 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1284 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1285 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1286 8ba819a3 2020-07-23 stsp * in the blob object.
1289 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1290 3521b466 2022-07-21 thomas if (err)
1291 3521b466 2022-07-21 thomas return err;
1293 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1294 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1295 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1296 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1297 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1298 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1299 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1300 aaf972e2 2023-09-18 thomas 1, path_is_unversioned, NULL, repo, progress_cb,
1301 3b9f0f87 2020-07-23 stsp progress_arg);
1303 8ba819a3 2020-07-23 stsp if (len > 0) {
1304 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1305 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1306 8ba819a3 2020-07-23 stsp len - hdrlen);
1307 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1308 8ba819a3 2020-07-23 stsp hdrlen = 0;
1310 8ba819a3 2020-07-23 stsp } while (len != 0);
1311 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1313 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1314 ad10f64e 2023-07-19 thomas ondisk_path, worktree->root_path, worktree->meta_dir);
1316 3c1ec782 2020-07-23 stsp return err;
1318 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1319 906c123b 2020-07-23 stsp /* install as a regular file */
1320 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1321 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1322 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1323 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1324 aaf972e2 2023-09-18 thomas path_is_unversioned, NULL, repo,
1325 aaf972e2 2023-09-18 thomas progress_cb, progress_arg);
1326 9e39ca77 2022-07-21 thomas return err;
1329 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1330 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1331 c6e8a826 2021-04-05 stsp int symlink_replaced;
1332 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1333 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1334 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1335 9e39ca77 2022-07-21 thomas return err;
1337 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1338 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1340 9e39ca77 2022-07-21 thomas return err;
1341 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1342 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1343 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1344 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1345 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1346 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1348 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1349 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1352 9e39ca77 2022-07-21 thomas return err; /* Nothing else to do. */
1355 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1356 f4994adc 2020-10-20 stsp char *parent;
1357 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1359 9e39ca77 2022-07-21 thomas return err;
1360 59530ea7 2023-07-17 thomas err = got_path_mkdir(parent);
1361 f4994adc 2020-10-20 stsp free(parent);
1363 9e39ca77 2022-07-21 thomas return err;
1365 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1366 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1368 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1369 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1370 59530ea7 2023-07-17 thomas if (progress_cb) {
1371 59530ea7 2023-07-17 thomas err = (*progress_cb)(progress_arg,
1372 59530ea7 2023-07-17 thomas reverting_versioned_file ?
1373 59530ea7 2023-07-17 thomas GOT_STATUS_REVERT : GOT_STATUS_ADD,
1376 9e39ca77 2022-07-21 thomas return err;
1380 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1381 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1382 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1383 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1384 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1385 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1386 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1387 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1388 aaf972e2 2023-09-18 thomas path_is_unversioned, NULL, repo,
1389 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1390 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1391 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1392 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1394 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1395 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1397 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1398 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1399 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1400 8ba819a3 2020-07-23 stsp return err;
1403 8ba819a3 2020-07-23 stsp static const struct got_error *
1404 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1405 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1406 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1407 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1408 aaf972e2 2023-09-18 thomas int path_is_unversioned, int *update_timestamps,
1409 aaf972e2 2023-09-18 thomas struct got_repository *repo,
1410 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1412 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1413 507dc3bb 2018-12-29 stsp int fd = -1;
1414 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1415 507dc3bb 2018-12-29 stsp int update = 0;
1416 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1417 a2c162eb 2022-10-30 thomas mode_t mode;
1419 aaf972e2 2023-09-18 thomas if (update_timestamps)
1420 aaf972e2 2023-09-18 thomas *update_timestamps = 1;
1422 a2c162eb 2022-10-30 thomas mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1423 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1424 a2c162eb 2022-10-30 thomas O_CLOEXEC, mode);
1425 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1426 9f323212 2023-03-10 thomas if (errno == ENOENT || errno == ENOTDIR) {
1427 f5375317 2020-10-20 stsp char *parent;
1428 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1430 f5375317 2020-10-20 stsp return err;
1431 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1432 9f323212 2023-03-10 thomas if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1433 9f323212 2023-03-10 thomas err = got_error_path(path, err->code);
1434 f5375317 2020-10-20 stsp free(parent);
1436 21908da4 2019-01-13 stsp return err;
1437 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1438 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1440 21908da4 2019-01-13 stsp if (fd == -1)
1441 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1442 230a42bd 2019-05-11 jcs ondisk_path);
1443 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1444 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1445 aaf972e2 2023-09-18 thomas if (update_timestamps)
1446 aaf972e2 2023-09-18 thomas *update_timestamps = 0;
1447 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1448 aaf972e2 2023-09-18 thomas GOT_STATUS_EXISTS, path);
1449 3b9f0f87 2020-07-23 stsp goto done;
1451 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1452 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1453 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1454 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1455 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1456 507dc3bb 2018-12-29 stsp goto done;
1458 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1459 fc2a50f2 2022-11-01 thomas ondisk_path, "");
1461 507dc3bb 2018-12-29 stsp goto done;
1462 507dc3bb 2018-12-29 stsp update = 1;
1464 a2c162eb 2022-10-30 thomas if (fchmod(fd, apply_umask(mode)) == -1) {
1465 a2c162eb 2022-10-30 thomas err = got_error_from_errno2("fchmod",
1466 a2c162eb 2022-10-30 thomas tmppath);
1467 a2c162eb 2022-10-30 thomas goto done;
1471 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1474 bd6aa359 2020-07-23 stsp if (progress_cb) {
1475 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1476 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1478 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1479 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1482 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1483 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1485 bd6aa359 2020-07-23 stsp goto done;
1488 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1490 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1491 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1494 9d31a1d8 2018-03-11 stsp if (len > 0) {
1495 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1496 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1497 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1498 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1499 b87c6f83 2018-12-24 stsp goto done;
1500 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1501 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1502 b87c6f83 2018-12-24 stsp goto done;
1504 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1506 9d31a1d8 2018-03-11 stsp } while (len != 0);
1508 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1509 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1510 816dc654 2019-02-16 stsp goto done;
1513 507dc3bb 2018-12-29 stsp if (update) {
1514 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1515 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1516 f6d8c0ac 2020-11-09 stsp goto done;
1518 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1519 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1520 230a42bd 2019-05-11 jcs ondisk_path);
1521 68ed9ba5 2019-02-10 stsp goto done;
1523 63df146d 2020-11-09 stsp free(tmppath);
1524 63df146d 2020-11-09 stsp tmppath = NULL;
1528 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1529 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1530 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1531 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1532 507dc3bb 2018-12-29 stsp free(tmppath);
1533 6353ad76 2019-02-08 stsp return err;
1537 be94c032 2023-02-20 thomas * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1538 be94c032 2023-02-20 thomas * conflict marker is found in newly added lines only.
1540 6353ad76 2019-02-08 stsp static const struct got_error *
1541 be94c032 2023-02-20 thomas get_modified_file_content_status(unsigned char *status,
1542 be94c032 2023-02-20 thomas struct got_blob_object *blob, const char *path, struct stat *sb,
1543 be94c032 2023-02-20 thomas FILE *ondisk_file)
1545 8de9d8ad 2023-02-20 thomas const struct got_error *err, *free_err;
1546 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1547 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1548 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1549 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1551 8de9d8ad 2023-02-20 thomas FILE *f1 = NULL;
1552 8de9d8ad 2023-02-20 thomas struct got_diffreg_result *diffreg_result = NULL;
1553 8de9d8ad 2023-02-20 thomas struct diff_result *r;
1554 8de9d8ad 2023-02-20 thomas int nchunks_parsed, n, i = 0, ln = 0;
1555 9bdd68dd 2020-01-02 naddy char *line = NULL;
1556 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1557 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1559 8de9d8ad 2023-02-20 thomas if (*status != GOT_STATUS_MODIFY)
1560 8de9d8ad 2023-02-20 thomas return NULL;
1562 8de9d8ad 2023-02-20 thomas f1 = got_opentemp();
1563 8de9d8ad 2023-02-20 thomas if (f1 == NULL)
1564 8de9d8ad 2023-02-20 thomas return got_error_from_errno("got_opentemp");
1566 8de9d8ad 2023-02-20 thomas if (blob) {
1567 8de9d8ad 2023-02-20 thomas got_object_blob_rewind(blob);
1568 8de9d8ad 2023-02-20 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1569 be94c032 2023-02-20 thomas if (err)
1570 be94c032 2023-02-20 thomas goto done;
1573 8de9d8ad 2023-02-20 thomas err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1574 8de9d8ad 2023-02-20 thomas 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1575 8de9d8ad 2023-02-20 thomas if (err)
1576 8de9d8ad 2023-02-20 thomas goto done;
1578 8de9d8ad 2023-02-20 thomas r = diffreg_result->result;
1580 8de9d8ad 2023-02-20 thomas for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1581 8de9d8ad 2023-02-20 thomas struct diff_chunk *c;
1582 8de9d8ad 2023-02-20 thomas struct diff_chunk_context cc = {};
1583 450d9f6b 2023-02-20 thomas off_t pos;
1586 8de9d8ad 2023-02-20 thomas * We can optimise a little by advancing straight
1587 8de9d8ad 2023-02-20 thomas * to the next chunk if this one has no added lines.
1589 8de9d8ad 2023-02-20 thomas c = diff_chunk_get(r, n);
1591 47c7ee21 2023-02-20 thomas if (diff_chunk_type(c) != CHUNK_PLUS) {
1592 8de9d8ad 2023-02-20 thomas nchunks_parsed = 1;
1593 450d9f6b 2023-02-20 thomas continue; /* removed or unchanged lines */
1596 450d9f6b 2023-02-20 thomas pos = diff_chunk_get_right_start_pos(c);
1597 450d9f6b 2023-02-20 thomas if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1598 450d9f6b 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1599 450d9f6b 2023-02-20 thomas goto done;
1602 450d9f6b 2023-02-20 thomas diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1603 450d9f6b 2023-02-20 thomas ln = cc.right.start;
1605 8de9d8ad 2023-02-20 thomas while (ln < cc.right.end) {
1606 8de9d8ad 2023-02-20 thomas linelen = getline(&line, &linesize, ondisk_file);
1607 8de9d8ad 2023-02-20 thomas if (linelen == -1) {
1608 8de9d8ad 2023-02-20 thomas if (feof(ondisk_file))
1610 8de9d8ad 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1614 8de9d8ad 2023-02-20 thomas if (line && strncmp(line, markers[i],
1615 8de9d8ad 2023-02-20 thomas strlen(markers[i])) == 0) {
1616 8de9d8ad 2023-02-20 thomas if (strcmp(markers[i],
1617 8de9d8ad 2023-02-20 thomas GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1618 8de9d8ad 2023-02-20 thomas *status = GOT_STATUS_CONFLICT;
1619 8de9d8ad 2023-02-20 thomas goto done;
1628 9bdd68dd 2020-01-02 naddy free(line);
1629 be94c032 2023-02-20 thomas if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1630 be94c032 2023-02-20 thomas err = got_error_from_errno("fclose");
1631 8de9d8ad 2023-02-20 thomas free_err = got_diffreg_result_free(diffreg_result);
1632 8de9d8ad 2023-02-20 thomas if (err == NULL)
1633 8de9d8ad 2023-02-20 thomas err = free_err;
1635 7154f6ce 2019-03-27 stsp return err;
1638 e2b1e152 2019-07-13 stsp static int
1639 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1641 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1642 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1645 1ebedb77 2019-10-19 stsp static int
1646 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1648 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1649 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1650 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1651 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1652 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1653 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1656 c363b2c1 2019-08-03 stsp static unsigned char
1657 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1659 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1660 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1661 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1662 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1663 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1664 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1665 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1667 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1671 a919d5c4 2020-07-23 stsp static const struct got_error *
1672 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1673 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1674 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1676 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1677 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1678 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1679 a919d5c4 2020-07-23 stsp ssize_t elen;
1680 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1681 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1682 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1684 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1686 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1688 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1690 a919d5c4 2020-07-23 stsp return err;
1691 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1693 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1694 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1696 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1698 a919d5c4 2020-07-23 stsp if (len > 0) {
1699 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1700 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1701 a919d5c4 2020-07-23 stsp len - hdrlen);
1702 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1703 a919d5c4 2020-07-23 stsp hdrlen = 0;
1705 a919d5c4 2020-07-23 stsp } while (len != 0);
1706 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1708 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1709 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1710 a919d5c4 2020-07-23 stsp if (elen == -1)
1711 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1713 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1714 a919d5c4 2020-07-23 stsp if (elen == -1)
1715 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1718 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1719 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1721 a919d5c4 2020-07-23 stsp return NULL;
1724 7154f6ce 2019-03-27 stsp static const struct got_error *
1725 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1726 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1727 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1729 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1730 6353ad76 2019-02-08 stsp struct got_object_id id;
1731 6353ad76 2019-02-08 stsp size_t hdrlen;
1732 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1733 6353ad76 2019-02-08 stsp FILE *f = NULL;
1734 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1735 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1736 6353ad76 2019-02-08 stsp size_t flen, blen;
1737 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
1739 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
1740 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1741 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1744 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1745 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1746 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1748 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1749 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1750 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1751 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1752 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1754 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1755 882ef1b9 2019-12-13 stsp goto done;
1757 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1758 3d35a492 2019-12-13 stsp goto done;
1761 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1762 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1763 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1764 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1765 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1766 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1767 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1768 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1769 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1770 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1771 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1773 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1774 3d35a492 2019-12-13 stsp goto done;
1776 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1777 1338848f 2019-12-13 stsp goto done;
1781 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1782 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1783 1338848f 2019-12-13 stsp goto done;
1786 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1787 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1788 1338848f 2019-12-13 stsp goto done;
1789 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1790 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1791 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1792 1338848f 2019-12-13 stsp goto done;
1795 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1796 f179e66d 2020-07-23 stsp goto done;
1798 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1799 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1800 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1801 1338848f 2019-12-13 stsp goto done;
1804 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1805 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1806 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
1808 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
1810 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1811 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1812 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1813 f4ae6ddb 2022-07-01 thomas goto done;
1815 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1817 1338848f 2019-12-13 stsp goto done;
1819 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1820 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1821 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1822 a919d5c4 2020-07-23 stsp goto done;
1825 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1826 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1827 ab0d4361 2019-12-13 stsp if (fd == -1) {
1828 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1829 ab0d4361 2019-12-13 stsp goto done;
1833 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1834 6353ad76 2019-02-08 stsp if (f == NULL) {
1835 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1836 6353ad76 2019-02-08 stsp goto done;
1839 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1841 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1842 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1844 7154f6ce 2019-03-27 stsp goto done;
1845 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1846 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1847 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1848 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1849 7154f6ce 2019-03-27 stsp goto done;
1851 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1852 6353ad76 2019-02-08 stsp if (flen != 0)
1853 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1855 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1856 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1857 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1859 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1860 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1861 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1862 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1866 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1869 6353ad76 2019-02-08 stsp hdrlen = 0;
1872 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1873 7154f6ce 2019-03-27 stsp rewind(f);
1874 be94c032 2023-02-20 thomas err = get_modified_file_content_status(status, blob, ie->path,
1876 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1877 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1879 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1880 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1882 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1883 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1884 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1885 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1886 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1887 9d31a1d8 2018-03-11 stsp return err;
1891 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1892 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1894 e2b1e152 2019-07-13 stsp static const struct got_error *
1895 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1896 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1898 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1899 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1900 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1902 e2b1e152 2019-07-13 stsp return NULL;
1905 9f323212 2023-03-10 thomas static const struct got_error *remove_ondisk_file(const char *, const char *);
1907 9d31a1d8 2018-03-11 stsp static const struct got_error *
1908 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1909 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1910 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1911 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1912 0584f854 2019-04-06 stsp void *progress_arg)
1914 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1915 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1916 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1917 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1918 b8f41171 2019-02-10 stsp struct stat sb;
1919 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1920 4f46fcf8 2024-02-13 thomas int update_timestamps = 0;
1922 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1923 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1926 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1927 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1928 a76c42e6 2019-08-03 stsp goto done;
1930 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1933 abb4604f 2019-07-27 stsp goto done;
1934 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1935 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1937 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1938 9f323212 2023-03-10 thomas if (errno != ENOENT && errno != ENOTDIR) {
1939 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1940 e6f4ba31 2021-09-24 thomas ondisk_path);
1941 e6f4ba31 2021-09-24 thomas goto done;
1943 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1944 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1945 e6f4ba31 2021-09-24 thomas } else {
1946 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1947 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1949 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1953 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1955 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1956 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1957 b8f41171 2019-02-10 stsp goto done;
1959 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1961 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1962 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1964 5036ab18 2020-04-18 stsp goto done;
1967 9f323212 2023-03-10 thomas if (S_ISDIR(te->mode)) { /* file changing into a directory */
1968 9f323212 2023-03-10 thomas if (status == GOT_STATUS_UNVERSIONED) {
1969 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, status, path);
1970 9f323212 2023-03-10 thomas } else if (status != GOT_STATUS_NO_CHANGE &&
1971 9f323212 2023-03-10 thomas status != GOT_STATUS_DELETE &&
1972 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1973 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1974 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg,
1975 9f323212 2023-03-10 thomas GOT_STATUS_CANNOT_DELETE, path);
1976 9f323212 2023-03-10 thomas } else if (ie) {
1977 9f323212 2023-03-10 thomas if (status != GOT_STATUS_DELETE &&
1978 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1979 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1980 9f323212 2023-03-10 thomas err = remove_ondisk_file(worktree->root_path,
1981 9f323212 2023-03-10 thomas ie->path);
1982 9f323212 2023-03-10 thomas if (err && !(err->code == GOT_ERR_ERRNO &&
1983 9f323212 2023-03-10 thomas errno == ENOENT))
1984 9f323212 2023-03-10 thomas goto done;
1986 9f323212 2023-03-10 thomas got_fileindex_entry_remove(fileindex, ie);
1987 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1988 9f323212 2023-03-10 thomas ie->path);
1990 9f323212 2023-03-10 thomas goto done; /* nothing else to do */
1993 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1994 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1995 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1997 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1998 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1999 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
2000 c6e8a826 2021-04-05 stsp * updating contents of this file.
2002 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
2003 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
2004 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
2005 c6e8a826 2021-04-05 stsp /* Same commit. */
2006 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2007 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2009 e2b1e152 2019-07-13 stsp goto done;
2010 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2012 6353ad76 2019-02-08 stsp goto done;
2014 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
2015 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
2016 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
2017 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
2018 b36a9774 2023-04-14 thomas if (got_fileindex_entry_has_commit(ie)) {
2019 b36a9774 2023-04-14 thomas /* Update the base commit ID of this file. */
2020 b36a9774 2023-04-14 thomas memcpy(ie->commit_sha1,
2021 b36a9774 2023-04-14 thomas worktree->base_commit_id->sha1,
2022 b36a9774 2023-04-14 thomas sizeof(ie->commit_sha1));
2024 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2025 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2027 0f58026f 2021-04-05 stsp goto done;
2028 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2030 b8f41171 2019-02-10 stsp goto done;
2034 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
2035 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
2036 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2037 f4ae6ddb 2022-07-01 thomas goto done;
2039 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2041 6353ad76 2019-02-08 stsp goto done;
2043 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2044 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2045 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2046 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2047 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
2048 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
2049 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2050 f4ae6ddb 2022-07-01 thomas goto done;
2052 234035bc 2019-06-01 stsp struct got_object_id id2;
2053 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id2, ie);
2054 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2057 f69721c3 2019-10-21 stsp goto done;
2059 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2060 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2061 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2062 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2063 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2064 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2065 f69721c3 2019-10-21 stsp goto done;
2067 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2068 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2069 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2070 234035bc 2019-06-01 stsp goto done;
2073 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2074 36bf999c 2020-07-23 stsp char *link_target;
2075 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2077 36bf999c 2020-07-23 stsp goto done;
2078 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2079 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2080 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2081 36bf999c 2020-07-23 stsp free(link_target);
2083 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2084 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2085 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2086 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2088 f69721c3 2019-10-21 stsp free(label_orig);
2089 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2090 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2091 f4ae6ddb 2022-07-01 thomas goto done;
2093 234035bc 2019-06-01 stsp if (blob2)
2094 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2096 909d120e 2019-09-22 stsp goto done;
2098 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2099 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2100 234035bc 2019-06-01 stsp * unmodified files again.
2102 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2103 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2104 234035bc 2019-06-01 stsp update_timestamps);
2105 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2106 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2107 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2108 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2109 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2111 1ee397ad 2019-07-12 stsp goto done;
2112 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2113 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2115 13d9040b 2019-03-27 stsp goto done;
2117 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2118 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2119 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2120 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2121 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2122 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2123 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2125 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2126 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2127 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2128 aaf972e2 2023-09-18 thomas status == GOT_STATUS_UNVERSIONED,
2129 aaf972e2 2023-09-18 thomas &update_timestamps,
2130 aaf972e2 2023-09-18 thomas repo, progress_cb, progress_arg);
2133 13d9040b 2019-03-27 stsp goto done;
2136 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2137 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2138 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2140 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2141 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2142 aaf972e2 2023-09-18 thomas &blob->id, update_timestamps);
2145 13d9040b 2019-03-27 stsp goto done;
2147 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2148 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2149 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2153 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2154 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2155 f4ae6ddb 2022-07-01 thomas goto done;
2157 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2159 6353ad76 2019-02-08 stsp free(ondisk_path);
2160 8da9e5f4 2019-01-12 stsp return err;
2163 90285c3b 2019-01-08 stsp static const struct got_error *
2164 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2166 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2167 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2169 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2170 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2172 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2173 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2174 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2176 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2177 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2179 1c4cdd89 2021-06-20 stsp goto done;
2180 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2181 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2182 fddefe3b 2020-10-20 stsp free(ondisk_path);
2183 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2184 1c4cdd89 2021-06-20 stsp parent = NULL;
2185 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2186 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2187 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2188 fddefe3b 2020-10-20 stsp ondisk_path);
2191 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2197 90285c3b 2019-01-08 stsp free(ondisk_path);
2198 1c4cdd89 2021-06-20 stsp free(parent);
2199 90285c3b 2019-01-08 stsp return err;
2202 708d8e67 2019-03-27 stsp static const struct got_error *
2203 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2204 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2205 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2207 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2208 708d8e67 2019-03-27 stsp unsigned char status;
2209 708d8e67 2019-03-27 stsp struct stat sb;
2210 708d8e67 2019-03-27 stsp char *ondisk_path;
2212 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2213 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2215 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2217 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2219 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2221 5a58a424 2020-06-23 stsp goto done;
2223 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2224 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2225 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2226 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2227 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2228 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2229 993e2a1b 2020-07-23 stsp goto done;
2231 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2232 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2233 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2234 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2236 993e2a1b 2020-07-23 stsp goto done;
2237 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2238 993e2a1b 2020-07-23 stsp ie->path);
2239 993e2a1b 2020-07-23 stsp goto done;
2242 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2243 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2244 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2246 5a58a424 2020-06-23 stsp goto done;
2248 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2249 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2251 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2252 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2254 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2256 5a58a424 2020-06-23 stsp goto done;
2257 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2258 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2260 5a58a424 2020-06-23 stsp goto done;
2262 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2265 5a58a424 2020-06-23 stsp free(ondisk_path);
2266 fc6346c4 2019-03-27 stsp return err;
2269 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2270 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2271 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2272 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2273 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2274 8da9e5f4 2019-01-12 stsp void *progress_arg;
2275 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2276 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2279 512f0d0e 2019-01-02 stsp static const struct got_error *
2280 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2281 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2283 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
2284 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2286 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
2287 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
2288 eb6fb9cd 2023-07-26 thomas if (err)
2289 eb6fb9cd 2023-07-26 thomas return err;
2292 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2293 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2296 8da9e5f4 2019-01-12 stsp static const struct got_error *
2297 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2299 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
2300 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2302 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
2303 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
2304 eb6fb9cd 2023-07-26 thomas if (err)
2305 eb6fb9cd 2023-07-26 thomas return err;
2308 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2309 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2312 9d31a1d8 2018-03-11 stsp static const struct got_error *
2313 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2315 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
2316 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2317 8da9e5f4 2019-01-12 stsp char *path;
2319 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
2320 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
2321 eb6fb9cd 2023-07-26 thomas if (err)
2322 eb6fb9cd 2023-07-26 thomas return err;
2325 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2326 59530ea7 2023-07-17 thomas return NULL;
2328 59530ea7 2023-07-17 thomas if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2329 63c5ca5d 2019-08-24 stsp return NULL;
2331 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2332 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2334 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2336 59530ea7 2023-07-17 thomas err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2337 59530ea7 2023-07-17 thomas a->repo, a->progress_cb, a->progress_arg);
2339 8da9e5f4 2019-01-12 stsp free(path);
2340 0cd1c46a 2019-03-11 stsp return err;
2343 b2118c49 2020-07-28 stsp const struct got_error *
2344 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2346 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2348 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2349 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2350 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2351 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2354 b2118c49 2020-07-28 stsp return NULL;
2357 818c7501 2019-07-11 stsp static const struct got_error *
2358 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2360 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2361 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2363 517bab73 2019-03-11 stsp *refname = NULL;
2365 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2367 b2118c49 2020-07-28 stsp return err;
2369 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2370 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2371 0647c563 2019-03-11 stsp *refname = NULL;
2373 517bab73 2019-03-11 stsp free(uuidstr);
2374 517bab73 2019-03-11 stsp return err;
2377 818c7501 2019-07-11 stsp const struct got_error *
2378 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2379 f6cd0243 2023-01-28 thomas const char *prefix)
2381 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2384 fcfaa6d5 2023-07-05 thomas static const struct got_error *
2385 fcfaa6d5 2023-07-05 thomas get_base_ref_name(char **refname, struct got_worktree *worktree)
2387 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2390 818c7501 2019-07-11 stsp static const struct got_error *
2391 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2393 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2394 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2397 818c7501 2019-07-11 stsp static const struct got_error *
2398 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2400 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2403 818c7501 2019-07-11 stsp static const struct got_error *
2404 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2406 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2407 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2410 818c7501 2019-07-11 stsp static const struct got_error *
2411 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2413 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2414 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2417 0ebf8283 2019-07-24 stsp static const struct got_error *
2418 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2420 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2421 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2424 0ebf8283 2019-07-24 stsp static const struct got_error *
2425 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2427 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2428 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2431 0ebf8283 2019-07-24 stsp static const struct got_error *
2432 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2434 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2435 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2438 0ebf8283 2019-07-24 stsp static const struct got_error *
2439 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2441 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2442 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2445 0ebf8283 2019-07-24 stsp const struct got_error *
2446 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2447 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2449 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2450 ad10f64e 2023-07-19 thomas worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2451 0ebf8283 2019-07-24 stsp *path = NULL;
2452 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2454 0ebf8283 2019-07-24 stsp return NULL;
2457 10604dce 2021-09-24 thomas static const struct got_error *
2458 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2460 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2461 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2464 10604dce 2021-09-24 thomas static const struct got_error *
2465 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2467 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2468 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2472 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2473 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2475 517bab73 2019-03-11 stsp static const struct got_error *
2476 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2478 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2479 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2480 517bab73 2019-03-11 stsp char *refname;
2482 fcfaa6d5 2023-07-05 thomas err = get_base_ref_name(&refname, worktree);
2484 517bab73 2019-03-11 stsp return err;
2486 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2488 0cd1c46a 2019-03-11 stsp goto done;
2490 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2492 0cd1c46a 2019-03-11 stsp free(refname);
2494 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2495 3e3a69f1 2019-07-25 stsp return err;
2498 3e3a69f1 2019-07-25 stsp static const struct got_error *
2499 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2501 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2503 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2504 ad10f64e 2023-07-19 thomas worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2505 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2506 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2508 9d31a1d8 2018-03-11 stsp return err;
2512 ebf99748 2019-05-09 stsp static const struct got_error *
2513 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2514 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2516 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2517 ebf99748 2019-05-09 stsp FILE *index = NULL;
2519 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2520 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2521 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2522 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2524 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2526 ebf99748 2019-05-09 stsp goto done;
2528 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2529 ebf99748 2019-05-09 stsp if (index == NULL) {
2530 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2531 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2533 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2534 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2535 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2538 ebf99748 2019-05-09 stsp if (err) {
2539 ebf99748 2019-05-09 stsp free(*fileindex_path);
2540 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2541 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2542 ebf99748 2019-05-09 stsp *fileindex = NULL;
2544 ebf99748 2019-05-09 stsp return err;
2547 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2548 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2549 c932eeeb 2019-05-22 stsp const char *path;
2550 c932eeeb 2019-05-22 stsp size_t path_len;
2551 c932eeeb 2019-05-22 stsp const char *entry_name;
2552 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2553 a484d721 2019-06-10 stsp void *progress_arg;
2556 c932eeeb 2019-05-22 stsp static const struct got_error *
2557 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2559 1ee397ad 2019-07-12 stsp const struct got_error *err;
2560 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2562 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2563 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2564 c932eeeb 2019-05-22 stsp return NULL;
2565 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2566 102ff934 2019-06-11 stsp return NULL;
2568 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2569 a769b60b 2021-06-27 stsp return NULL;
2571 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2572 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2573 c932eeeb 2019-05-22 stsp return NULL;
2575 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2576 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2577 edd02c5e 2019-07-11 stsp ie->path);
2579 1ee397ad 2019-07-12 stsp return err;
2581 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2582 c932eeeb 2019-05-22 stsp return NULL;
2585 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2586 a615e0e7 2020-12-16 stsp static const struct got_error *
2587 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2588 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2589 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2591 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2593 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2594 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2595 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2596 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2597 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2598 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2600 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2601 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2604 9c6338c4 2019-06-02 stsp static const struct got_error *
2605 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2607 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2608 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2609 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2610 867630bb 2020-01-17 stsp struct timespec timeout;
2612 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2613 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2615 9c6338c4 2019-06-02 stsp goto done;
2617 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2619 9c6338c4 2019-06-02 stsp goto done;
2621 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2622 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2623 9c6338c4 2019-06-02 stsp fileindex_path);
2624 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2628 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2629 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2630 867630bb 2020-01-17 stsp * was recorded in the file index.
2632 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2633 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2634 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2636 9c6338c4 2019-06-02 stsp if (new_index)
2637 9c6338c4 2019-06-02 stsp fclose(new_index);
2638 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2639 9c6338c4 2019-06-02 stsp return err;
2642 6ced4176 2019-07-12 stsp static const struct got_error *
2643 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2644 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2645 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2646 945f9229 2022-04-16 thomas struct got_repository *repo)
2648 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2649 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2650 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2651 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2653 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2654 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2655 6ced4176 2019-07-12 stsp *tree_id = NULL;
2657 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2658 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2659 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2660 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2661 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2662 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2663 6ced4176 2019-07-12 stsp goto done;
2665 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2666 945f9229 2022-04-16 thomas worktree->path_prefix);
2668 6ced4176 2019-07-12 stsp goto done;
2669 6ced4176 2019-07-12 stsp return NULL;
2672 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2674 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2675 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2676 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2677 6ced4176 2019-07-12 stsp goto done;
2680 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2682 6ced4176 2019-07-12 stsp goto done;
2684 6ced4176 2019-07-12 stsp free(in_repo_path);
2685 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2687 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2689 6ced4176 2019-07-12 stsp goto done;
2691 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2692 6ced4176 2019-07-12 stsp /* Check out a single file. */
2693 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2694 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2695 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2696 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2697 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2698 6ced4176 2019-07-12 stsp goto done;
2700 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2701 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2702 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2703 6ced4176 2019-07-12 stsp goto done;
2706 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2707 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2709 6ced4176 2019-07-12 stsp return err;
2710 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2711 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2712 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2713 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2714 6ced4176 2019-07-12 stsp goto done;
2717 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2718 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2720 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2721 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2722 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2723 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2724 6ced4176 2019-07-12 stsp goto done;
2726 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2727 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2728 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2729 6ced4176 2019-07-12 stsp goto done;
2734 6ced4176 2019-07-12 stsp free(in_repo_path);
2735 6ced4176 2019-07-12 stsp if (err) {
2736 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2737 6ced4176 2019-07-12 stsp free(*tree_relpath);
2738 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2739 6ced4176 2019-07-12 stsp free(*tree_id);
2740 6ced4176 2019-07-12 stsp *tree_id = NULL;
2742 07ed472d 2019-07-12 stsp return err;
2745 07ed472d 2019-07-12 stsp static const struct got_error *
2746 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2747 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2748 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2749 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2751 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2752 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2753 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2754 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2755 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2757 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2758 7f47418f 2019-12-20 stsp if (err) {
2759 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2760 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2761 7f47418f 2019-12-20 stsp goto done;
2762 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2763 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2765 7f47418f 2019-12-20 stsp return err;
2768 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2769 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2771 07ed472d 2019-07-12 stsp goto done;
2773 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2775 07ed472d 2019-07-12 stsp goto done;
2777 07ed472d 2019-07-12 stsp if (entry_name &&
2778 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2779 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2780 07ed472d 2019-07-12 stsp goto done;
2783 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2784 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2785 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2786 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2787 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2788 07ed472d 2019-07-12 stsp arg.repo = repo;
2789 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2790 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2791 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2792 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2793 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2794 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2797 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2798 07ed472d 2019-07-12 stsp if (commit)
2799 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2800 6ced4176 2019-07-12 stsp return err;
2803 9d31a1d8 2018-03-11 stsp const struct got_error *
2804 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2805 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2806 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2807 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2809 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2810 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2811 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2812 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2813 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2814 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2815 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2816 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2817 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2818 f2ea84fa 2019-07-27 stsp int entry_type;
2819 f2ea84fa 2019-07-27 stsp char *relpath;
2820 f2ea84fa 2019-07-27 stsp char *entry_name;
2821 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2822 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2824 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2826 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2828 9d31a1d8 2018-03-11 stsp return err;
2830 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2831 945f9229 2022-04-16 thomas worktree->base_commit_id);
2832 945f9229 2022-04-16 thomas if (err)
2833 945f9229 2022-04-16 thomas goto done;
2835 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2836 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2837 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2838 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2839 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2840 f2ea84fa 2019-07-27 stsp goto done;
2843 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2844 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2845 945f9229 2022-04-16 thomas worktree, repo);
2846 f2ea84fa 2019-07-27 stsp if (err) {
2847 f2ea84fa 2019-07-27 stsp free(tpd);
2848 6ced4176 2019-07-12 stsp goto done;
2851 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2852 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2853 f2ea84fa 2019-07-27 stsp if (err) {
2854 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2855 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2856 f2ea84fa 2019-07-27 stsp free(tpd);
2857 f2ea84fa 2019-07-27 stsp goto done;
2860 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2862 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2866 51514078 2018-12-25 stsp * Read the file index.
2867 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2868 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2870 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2872 c4cdcb68 2019-04-03 stsp goto done;
2874 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2875 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2876 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2878 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2879 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2880 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2884 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2885 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2886 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2887 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2888 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2889 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2890 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2891 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2895 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2897 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2898 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2899 af12c6b9 2019-06-04 stsp err = sync_err;
2901 9c6338c4 2019-06-02 stsp free(fileindex_path);
2903 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2904 9d31a1d8 2018-03-11 stsp if (commit)
2905 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2906 5ade8233 2019-07-12 stsp if (fileindex)
2907 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2908 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2909 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2910 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2911 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2912 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2913 f2ea84fa 2019-07-27 stsp free(tpd);
2915 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2916 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2917 9d31a1d8 2018-03-11 stsp err = unlockerr;
2918 f8d1f275 2019-02-04 stsp return err;
2921 0529f8df 2023-03-10 thomas static const struct got_error *
2922 0529f8df 2023-03-10 thomas add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2923 0529f8df 2023-03-10 thomas struct got_fileindex_entry *ie, const char *ondisk_path,
2924 0529f8df 2023-03-10 thomas const char *path2, struct got_blob_object *blob2, mode_t mode2,
2925 0529f8df 2023-03-10 thomas int restoring_missing_file, int reverting_versioned_file,
2926 0529f8df 2023-03-10 thomas int path_is_unversioned, int allow_bad_symlinks,
2927 0529f8df 2023-03-10 thomas struct got_repository *repo,
2928 0529f8df 2023-03-10 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
2930 0529f8df 2023-03-10 thomas const struct got_error *err = NULL;
2931 0529f8df 2023-03-10 thomas int is_bad_symlink = 0;
2933 0529f8df 2023-03-10 thomas if (S_ISLNK(mode2)) {
2934 0529f8df 2023-03-10 thomas err = install_symlink(&is_bad_symlink,
2935 0529f8df 2023-03-10 thomas worktree, ondisk_path, path2, blob2,
2936 0529f8df 2023-03-10 thomas restoring_missing_file,
2937 0529f8df 2023-03-10 thomas reverting_versioned_file,
2938 0529f8df 2023-03-10 thomas path_is_unversioned, allow_bad_symlinks,
2939 0529f8df 2023-03-10 thomas repo, progress_cb, progress_arg);
2940 0529f8df 2023-03-10 thomas } else {
2941 0529f8df 2023-03-10 thomas err = install_blob(worktree, ondisk_path, path2,
2942 0529f8df 2023-03-10 thomas mode2, GOT_DEFAULT_FILE_MODE, blob2,
2943 0529f8df 2023-03-10 thomas restoring_missing_file, reverting_versioned_file, 0,
2944 aaf972e2 2023-09-18 thomas path_is_unversioned, NULL, repo,
2945 aaf972e2 2023-09-18 thomas progress_cb, progress_arg);
2947 0529f8df 2023-03-10 thomas if (err)
2948 0529f8df 2023-03-10 thomas return err;
2949 0529f8df 2023-03-10 thomas if (ie == NULL) {
2950 0529f8df 2023-03-10 thomas /* Adding an unversioned file. */
2951 0529f8df 2023-03-10 thomas err = got_fileindex_entry_alloc(&ie, path2);
2952 0529f8df 2023-03-10 thomas if (err)
2953 0529f8df 2023-03-10 thomas return err;
2954 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2955 0529f8df 2023-03-10 thomas worktree->root_fd, path2, NULL, NULL, 1);
2956 0529f8df 2023-03-10 thomas if (err) {
2957 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2958 0529f8df 2023-03-10 thomas return err;
2960 0529f8df 2023-03-10 thomas err = got_fileindex_entry_add(fileindex, ie);
2961 0529f8df 2023-03-10 thomas if (err) {
2962 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2963 0529f8df 2023-03-10 thomas return err;
2965 0529f8df 2023-03-10 thomas } else {
2966 0529f8df 2023-03-10 thomas /* Re-adding a locally deleted file. */
2967 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2968 0529f8df 2023-03-10 thomas worktree->root_fd, path2, ie->blob_sha1,
2969 0529f8df 2023-03-10 thomas worktree->base_commit_id->sha1, 0);
2970 0529f8df 2023-03-10 thomas if (err)
2971 0529f8df 2023-03-10 thomas return err;
2974 0529f8df 2023-03-10 thomas if (is_bad_symlink) {
2975 0529f8df 2023-03-10 thomas got_fileindex_entry_filetype_set(ie,
2976 0529f8df 2023-03-10 thomas GOT_FILEIDX_MODE_BAD_SYMLINK);
2979 0529f8df 2023-03-10 thomas return NULL;
2982 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2983 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2984 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2985 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2986 234035bc 2019-06-01 stsp void *progress_arg;
2987 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2988 234035bc 2019-06-01 stsp void *cancel_arg;
2989 f69721c3 2019-10-21 stsp const char *label_orig;
2990 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2991 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2994 234035bc 2019-06-01 stsp static const struct got_error *
2995 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2996 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2997 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2998 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2999 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3001 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
3002 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
3003 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
3004 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
3005 234035bc 2019-06-01 stsp struct stat sb;
3006 234035bc 2019-06-01 stsp unsigned char status;
3007 234035bc 2019-06-01 stsp int local_changes_subsumed;
3008 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3009 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
3010 4c3671a9 2023-07-26 thomas struct got_object_id *id = NULL;
3012 234035bc 2019-06-01 stsp if (blob1 && blob2) {
3013 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3014 d6c87207 2019-08-02 stsp strlen(path2));
3015 1ee397ad 2019-07-12 stsp if (ie == NULL)
3016 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3017 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
3019 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3020 234035bc 2019-06-01 stsp path2) == -1)
3021 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3023 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3026 234035bc 2019-06-01 stsp goto done;
3028 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3029 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3030 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
3031 234035bc 2019-06-01 stsp goto done;
3033 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3034 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3035 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3036 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3037 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3038 234035bc 2019-06-01 stsp goto done;
3041 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3042 36bf999c 2020-07-23 stsp char *link_target2;
3043 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3045 36bf999c 2020-07-23 stsp goto done;
3046 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3047 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3048 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3049 36bf999c 2020-07-23 stsp free(link_target2);
3053 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3054 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3055 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3056 1af628f4 2021-06-03 stsp goto done;
3058 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3059 1af628f4 2021-06-03 stsp f_orig, blob1);
3061 1af628f4 2021-06-03 stsp goto done;
3063 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3064 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3065 1af628f4 2021-06-03 stsp goto done;
3066 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3067 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3069 1af628f4 2021-06-03 stsp goto done;
3071 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
3072 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3073 1af628f4 2021-06-03 stsp if (fd == -1) {
3074 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3075 1af628f4 2021-06-03 stsp ondisk_path);
3076 1af628f4 2021-06-03 stsp goto done;
3078 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3079 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3080 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3081 1af628f4 2021-06-03 stsp ondisk_path);
3082 1af628f4 2021-06-03 stsp close(fd);
3083 1af628f4 2021-06-03 stsp goto done;
3085 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3087 1af628f4 2021-06-03 stsp goto done;
3088 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3089 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3090 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3091 1af628f4 2021-06-03 stsp goto done;
3093 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3094 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3095 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
3096 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3097 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3099 234035bc 2019-06-01 stsp } else if (blob1) {
3100 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3101 d6c87207 2019-08-02 stsp strlen(path1));
3102 1ee397ad 2019-07-12 stsp if (ie == NULL)
3103 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3104 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3106 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3107 2b92fad7 2019-06-02 stsp path1) == -1)
3108 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3110 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3113 2b92fad7 2019-06-02 stsp goto done;
3115 2b92fad7 2019-06-02 stsp switch (status) {
3116 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3117 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3118 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3120 1ee397ad 2019-07-12 stsp goto done;
3121 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3123 2b92fad7 2019-06-02 stsp goto done;
3125 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3127 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3128 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3129 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3130 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3132 1ee397ad 2019-07-12 stsp goto done;
3134 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3136 4c3671a9 2023-07-26 thomas case GOT_STATUS_MODIFY: {
3137 4c3671a9 2023-07-26 thomas FILE *blob1_f;
3138 4c3671a9 2023-07-26 thomas off_t blob1_size;
3139 4c3671a9 2023-07-26 thomas int obj_type;
3141 4c3671a9 2023-07-26 thomas * Delete the file only if its content already
3142 4c3671a9 2023-07-26 thomas * exists in the repository.
3144 4c3671a9 2023-07-26 thomas err = got_object_blob_file_create(&id, &blob1_f,
3145 4c3671a9 2023-07-26 thomas &blob1_size, path1);
3146 4c3671a9 2023-07-26 thomas if (err)
3147 4c3671a9 2023-07-26 thomas goto done;
3148 4c3671a9 2023-07-26 thomas if (fclose(blob1_f) == EOF) {
3149 4c3671a9 2023-07-26 thomas err = got_error_from_errno("fclose");
3150 4c3671a9 2023-07-26 thomas goto done;
3153 4c3671a9 2023-07-26 thomas /* Implied existence check. */
3154 4c3671a9 2023-07-26 thomas err = got_object_get_type(&obj_type, repo, id);
3155 4c3671a9 2023-07-26 thomas if (err) {
3156 4c3671a9 2023-07-26 thomas if (err->code != GOT_ERR_NO_OBJ)
3157 4c3671a9 2023-07-26 thomas goto done;
3158 4c3671a9 2023-07-26 thomas err = (*a->progress_cb)(a->progress_arg,
3159 4c3671a9 2023-07-26 thomas GOT_STATUS_CANNOT_DELETE, path1);
3160 4c3671a9 2023-07-26 thomas goto done;
3161 4c3671a9 2023-07-26 thomas } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3162 4c3671a9 2023-07-26 thomas err = (*a->progress_cb)(a->progress_arg,
3163 4c3671a9 2023-07-26 thomas GOT_STATUS_CANNOT_DELETE, path1);
3164 4c3671a9 2023-07-26 thomas goto done;
3166 4c3671a9 2023-07-26 thomas err = (*a->progress_cb)(a->progress_arg,
3167 4c3671a9 2023-07-26 thomas GOT_STATUS_DELETE, path1);
3168 4c3671a9 2023-07-26 thomas if (err)
3169 4c3671a9 2023-07-26 thomas goto done;
3170 4c3671a9 2023-07-26 thomas err = remove_ondisk_file(a->worktree->root_path,
3172 4c3671a9 2023-07-26 thomas if (err)
3173 4c3671a9 2023-07-26 thomas goto done;
3175 4c3671a9 2023-07-26 thomas got_fileindex_entry_mark_deleted_from_disk(ie);
3178 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3179 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3180 e8f02263 2022-01-23 thomas off_t blob1_size;
3182 4c3671a9 2023-07-26 thomas * Delete the file only if its content already
3183 0a22ca1a 2020-09-23 stsp * exists in the repository.
3185 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
3186 e8f02263 2022-01-23 thomas &blob1_size, path1);
3188 0a22ca1a 2020-09-23 stsp goto done;
3189 4c3671a9 2023-07-26 thomas if (fclose(blob1_f) == EOF) {
3190 4c3671a9 2023-07-26 thomas err = got_error_from_errno("fclose");
3191 4c3671a9 2023-07-26 thomas goto done;
3193 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3194 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3195 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3197 0a22ca1a 2020-09-23 stsp goto done;
3198 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3201 0a22ca1a 2020-09-23 stsp goto done;
3203 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3206 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3207 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3210 0a22ca1a 2020-09-23 stsp goto done;
3213 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3214 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3215 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3217 1ee397ad 2019-07-12 stsp goto done;
3219 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3220 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3222 1ee397ad 2019-07-12 stsp goto done;
3227 234035bc 2019-06-01 stsp } else if (blob2) {
3228 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3229 234035bc 2019-06-01 stsp path2) == -1)
3230 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3231 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3232 d6c87207 2019-08-02 stsp strlen(path2));
3234 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3235 7f91a133 2019-12-13 stsp -1, NULL, repo);
3237 234035bc 2019-06-01 stsp goto done;
3238 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3239 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3240 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3241 0529f8df 2023-03-10 thomas status != GOT_STATUS_ADD &&
3242 0529f8df 2023-03-10 thomas status != GOT_STATUS_DELETE) {
3243 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3244 1ee397ad 2019-07-12 stsp status, path2);
3245 234035bc 2019-06-01 stsp goto done;
3247 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3248 36bf999c 2020-07-23 stsp char *link_target2;
3249 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3252 36bf999c 2020-07-23 stsp goto done;
3253 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3254 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3255 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3256 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3257 36bf999c 2020-07-23 stsp free(link_target2);
3258 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3259 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3260 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3261 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3262 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3263 526a746f 2020-07-23 stsp a->progress_arg);
3264 0529f8df 2023-03-10 thomas } else if (status != GOT_STATUS_DELETE) {
3265 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3266 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3269 dfe9fba0 2020-07-23 stsp goto done;
3270 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3271 0529f8df 2023-03-10 thomas /* Re-add file with content from new blob. */
3272 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, ie,
3273 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3274 0529f8df 2023-03-10 thomas 0, 0, 0, a->allow_bad_symlinks,
3275 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3277 234035bc 2019-06-01 stsp goto done;
3280 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, NULL,
3281 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3282 0529f8df 2023-03-10 thomas 0, 0, 1, a->allow_bad_symlinks,
3283 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3285 2b92fad7 2019-06-02 stsp goto done;
3289 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3290 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3291 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3292 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3293 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3294 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3295 1af628f4 2021-06-03 stsp free(id_str);
3296 4c3671a9 2023-07-26 thomas free(id);
3297 54d5be07 2021-06-03 stsp free(label_deriv2);
3298 234035bc 2019-06-01 stsp free(ondisk_path);
3299 234035bc 2019-06-01 stsp return err;
3302 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args {
3303 6ebb2263 2023-07-26 thomas struct got_worktree *worktree;
3304 6ebb2263 2023-07-26 thomas got_cancel_cb cancel_cb;
3305 6ebb2263 2023-07-26 thomas void *cancel_arg;
3308 69de9dd4 2021-09-03 stsp static const struct got_error *
3309 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3311 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
3312 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args *a = arg;
3314 6ebb2263 2023-07-26 thomas if (a->cancel_cb) {
3315 6ebb2263 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3316 6ebb2263 2023-07-26 thomas if (err)
3317 6ebb2263 2023-07-26 thomas return err;
3320 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3321 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3322 6ebb2263 2023-07-26 thomas memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3323 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3324 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3326 69de9dd4 2021-09-03 stsp return NULL;
3329 349dfd1e 2023-07-23 thomas const struct got_error *
3330 349dfd1e 2023-07-23 thomas got_worktree_get_state(char *state, struct got_repository *repo,
3331 6ebb2263 2023-07-26 thomas struct got_worktree *worktree,
3332 6ebb2263 2023-07-26 thomas got_cancel_cb cancel_cb, void *cancel_arg)
3334 349dfd1e 2023-07-23 thomas const struct got_error *err;
3335 349dfd1e 2023-07-23 thomas struct got_object_id *base_id, *head_id = NULL;
3336 349dfd1e 2023-07-23 thomas struct got_reference *head_ref;
3337 349dfd1e 2023-07-23 thomas struct got_fileindex *fileindex = NULL;
3338 349dfd1e 2023-07-23 thomas char *fileindex_path = NULL;
3339 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args cma;
3341 349dfd1e 2023-07-23 thomas if (worktree == NULL)
3342 349dfd1e 2023-07-23 thomas return got_error(GOT_ERR_NOT_WORKTREE);
3344 349dfd1e 2023-07-23 thomas err = got_ref_open(&head_ref, repo,
3345 349dfd1e 2023-07-23 thomas got_worktree_get_head_ref_name(worktree), 0);
3346 349dfd1e 2023-07-23 thomas if (err)
3347 349dfd1e 2023-07-23 thomas return err;
3349 349dfd1e 2023-07-23 thomas err = got_ref_resolve(&head_id, repo, head_ref);
3350 349dfd1e 2023-07-23 thomas if (err)
3351 349dfd1e 2023-07-23 thomas goto done;
3353 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_UNKNOWN;
3354 349dfd1e 2023-07-23 thomas base_id = got_worktree_get_base_commit_id(worktree);
3356 6ebb2263 2023-07-26 thomas cma.worktree = worktree;
3357 6ebb2263 2023-07-26 thomas cma.cancel_cb = cancel_cb;
3358 6ebb2263 2023-07-26 thomas cma.cancel_arg = cancel_arg;
3360 349dfd1e 2023-07-23 thomas if (got_object_id_cmp(base_id, head_id) == 0) {
3361 349dfd1e 2023-07-23 thomas err = open_fileindex(&fileindex, &fileindex_path, worktree);
3362 349dfd1e 2023-07-23 thomas if (err)
3363 349dfd1e 2023-07-23 thomas goto done;
3365 349dfd1e 2023-07-23 thomas err = got_fileindex_for_each_entry_safe(fileindex,
3366 6ebb2263 2023-07-26 thomas check_mixed_commits, &cma);
3367 349dfd1e 2023-07-23 thomas if (err == NULL)
3368 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_UPTODATE;
3369 92845f09 2023-07-26 thomas else if (err->code == GOT_ERR_MIXED_COMMITS) {
3370 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_OUTOFDATE;
3371 349dfd1e 2023-07-23 thomas err = NULL;
3374 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_OUTOFDATE;
3377 349dfd1e 2023-07-23 thomas free(head_id);
3378 349dfd1e 2023-07-23 thomas free(fileindex_path);
3379 349dfd1e 2023-07-23 thomas got_ref_close(head_ref);
3380 349dfd1e 2023-07-23 thomas if (fileindex != NULL)
3381 349dfd1e 2023-07-23 thomas got_fileindex_free(fileindex);
3382 349dfd1e 2023-07-23 thomas return err;
3385 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3386 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3387 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3388 234035bc 2019-06-01 stsp struct got_repository *repo;
3391 234035bc 2019-06-01 stsp static const struct got_error *
3392 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3393 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3394 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3395 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3396 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3398 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3399 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3400 234035bc 2019-06-01 stsp unsigned char status;
3401 234035bc 2019-06-01 stsp struct stat sb;
3402 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3403 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3404 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3405 234035bc 2019-06-01 stsp char *ondisk_path;
3407 69de9dd4 2021-09-03 stsp if (id == NULL)
3408 69de9dd4 2021-09-03 stsp return NULL;
3410 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3411 69de9dd4 2021-09-03 stsp if (ie == NULL)
3412 69de9dd4 2021-09-03 stsp return NULL;
3414 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3416 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3418 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3419 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3420 5546d466 2021-09-02 stsp free(ondisk_path);
3422 234035bc 2019-06-01 stsp return err;
3423 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3424 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3426 234035bc 2019-06-01 stsp return NULL;
3429 818c7501 2019-07-11 stsp static const struct got_error *
3430 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3431 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3432 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3433 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3434 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3436 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3437 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3438 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3439 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3440 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3441 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3442 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3443 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3444 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3446 03415a1a 2019-06-02 stsp if (commit_id1) {
3447 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3448 945f9229 2022-04-16 thomas if (err)
3449 945f9229 2022-04-16 thomas goto done;
3450 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3451 03415a1a 2019-06-02 stsp worktree->path_prefix);
3452 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3453 03415a1a 2019-06-02 stsp goto done;
3455 69d57f3d 2020-07-31 stsp if (tree_id1) {
3456 69d57f3d 2020-07-31 stsp char *id_str;
3458 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3460 03415a1a 2019-06-02 stsp goto done;
3462 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3464 f69721c3 2019-10-21 stsp goto done;
3466 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3467 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3468 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3469 f69721c3 2019-10-21 stsp free(id_str);
3470 f69721c3 2019-10-21 stsp goto done;
3472 f69721c3 2019-10-21 stsp free(id_str);
3474 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3475 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3476 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3477 a0f32f33 2022-06-13 thomas goto done;
3481 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3482 945f9229 2022-04-16 thomas if (err)
3483 945f9229 2022-04-16 thomas goto done;
3485 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3486 234035bc 2019-06-01 stsp worktree->path_prefix);
3488 234035bc 2019-06-01 stsp goto done;
3490 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3492 234035bc 2019-06-01 stsp goto done;
3494 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3495 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3496 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3497 19a6a6b5 2022-07-01 thomas goto done;
3500 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3501 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3502 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3503 a0f32f33 2022-06-13 thomas goto done;
3506 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3507 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3508 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3509 19a6a6b5 2022-07-01 thomas goto done;
3512 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3513 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3514 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3515 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3516 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3518 69de9dd4 2021-09-03 stsp goto done;
3520 234035bc 2019-06-01 stsp arg.worktree = worktree;
3521 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3522 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3523 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3524 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3525 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3526 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3527 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3528 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3529 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3530 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3531 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3532 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3533 af12c6b9 2019-06-04 stsp err = sync_err;
3535 945f9229 2022-04-16 thomas if (commit1)
3536 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3537 945f9229 2022-04-16 thomas if (commit2)
3538 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3539 234035bc 2019-06-01 stsp if (tree1)
3540 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3541 234035bc 2019-06-01 stsp if (tree2)
3542 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3543 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3544 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3545 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3546 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3547 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3548 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3549 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3550 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3551 f69721c3 2019-10-21 stsp free(label_orig);
3552 818c7501 2019-07-11 stsp return err;
3555 818c7501 2019-07-11 stsp const struct got_error *
3556 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3557 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3558 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3559 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3561 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3562 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3563 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3564 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args cma;
3566 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3568 818c7501 2019-07-11 stsp return err;
3570 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3572 818c7501 2019-07-11 stsp goto done;
3574 6ebb2263 2023-07-26 thomas cma.worktree = worktree;
3575 6ebb2263 2023-07-26 thomas cma.cancel_cb = cancel_cb;
3576 6ebb2263 2023-07-26 thomas cma.cancel_arg = cancel_arg;
3578 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3581 818c7501 2019-07-11 stsp goto done;
3583 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3584 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3585 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3587 818c7501 2019-07-11 stsp if (fileindex)
3588 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3589 818c7501 2019-07-11 stsp free(fileindex_path);
3590 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3591 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3592 234035bc 2019-06-01 stsp err = unlockerr;
3593 234035bc 2019-06-01 stsp return err;
3596 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3597 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3598 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3599 927df6b7 2019-02-10 stsp const char *status_path;
3600 927df6b7 2019-02-10 stsp size_t status_path_len;
3601 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3602 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3603 f8d1f275 2019-02-04 stsp void *status_arg;
3604 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3605 f8d1f275 2019-02-04 stsp void *cancel_arg;
3606 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3607 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3608 f2a9dc41 2019-12-13 tracey int report_unchanged;
3609 3143d852 2020-06-25 stsp int no_ignores;
3612 f8d1f275 2019-02-04 stsp static const struct got_error *
3613 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3614 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3615 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3616 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3618 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3619 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3620 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3621 927df6b7 2019-02-10 stsp struct stat sb;
3622 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3623 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3624 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3626 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3627 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3629 98eaaa12 2019-08-03 stsp return err;
3631 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3632 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3633 98eaaa12 2019-08-03 stsp return NULL;
3635 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3636 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3637 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3638 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3639 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3640 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3641 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3642 43010591 2023-02-17 thomas &staged_blob_id, ie);
3645 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3646 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3649 927df6b7 2019-02-10 stsp static const struct got_error *
3650 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3651 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3653 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3654 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3655 f8d1f275 2019-02-04 stsp char *abspath;
3657 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
3658 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3659 eb6fb9cd 2023-07-26 thomas if (err)
3660 eb6fb9cd 2023-07-26 thomas return err;
3663 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3664 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3665 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3666 927df6b7 2019-02-10 stsp return NULL;
3668 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3669 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3670 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3671 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3673 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3674 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3675 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3678 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3679 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3680 f8d1f275 2019-02-04 stsp free(abspath);
3681 9d31a1d8 2018-03-11 stsp return err;
3684 f8d1f275 2019-02-04 stsp static const struct got_error *
3685 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3687 eb6fb9cd 2023-07-26 thomas const struct got_error *err;
3688 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3689 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3690 2ec1f75b 2019-03-26 stsp unsigned char status;
3692 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
3693 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3694 eb6fb9cd 2023-07-26 thomas if (err)
3695 eb6fb9cd 2023-07-26 thomas return err;
3698 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3699 927df6b7 2019-02-10 stsp return NULL;
3701 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3702 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3703 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3704 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3706 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3707 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3708 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3711 ef20f542 2022-06-26 thomas static void
3712 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3714 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3716 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3717 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3719 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3721 5f42e012 2023-12-09 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_ALL);
3724 6841da00 2019-08-08 stsp static const struct got_error *
3725 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3727 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3728 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3729 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3730 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3731 6841da00 2019-08-08 stsp size_t linesize = 0;
3732 6841da00 2019-08-08 stsp ssize_t linelen;
3734 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3735 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3736 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3737 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3739 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3740 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3741 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3743 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3744 bd8de430 2019-10-04 stsp if (line[0] == '#')
3747 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3748 bd8de430 2019-10-04 stsp if (line[0] == '!')
3751 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3752 6841da00 2019-08-08 stsp line) == -1) {
3753 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3754 6841da00 2019-08-08 stsp goto done;
3756 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3758 6841da00 2019-08-08 stsp goto done;
3760 6841da00 2019-08-08 stsp if (ferror(f)) {
3761 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3762 6841da00 2019-08-08 stsp goto done;
3765 6841da00 2019-08-08 stsp dirpath = strdup(path);
3766 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3767 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3768 6841da00 2019-08-08 stsp goto done;
3770 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3772 6841da00 2019-08-08 stsp free(line);
3773 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3774 6841da00 2019-08-08 stsp free(dirpath);
3775 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3776 5f42e012 2023-12-09 thomas free(ignorelist);
3778 6841da00 2019-08-08 stsp return err;
3781 1b5d300f 2023-02-20 thomas static int
3782 1b5d300f 2023-02-20 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
3783 1b5d300f 2023-02-20 thomas int flags)
3785 1b5d300f 2023-02-20 thomas char buf[PATH_MAX];
3788 1b5d300f 2023-02-20 thomas * Trailing slashes signify directories.
3789 1b5d300f 2023-02-20 thomas * Append a * to make such patterns conform to fnmatch rules.
3791 1b5d300f 2023-02-20 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3792 1b5d300f 2023-02-20 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3793 1b5d300f 2023-02-20 thomas return FNM_NOMATCH; /* XXX */
3795 1b5d300f 2023-02-20 thomas return fnmatch(buf, path, flags);
3798 1b5d300f 2023-02-20 thomas return fnmatch(pattern, path, flags);
3801 ef20f542 2022-06-26 thomas static int
3802 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3804 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3806 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3807 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3808 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3809 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3811 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3812 1b5d300f 2023-02-20 thomas const char *p;
3814 1b5d300f 2023-02-20 thomas if (pi->path_len < 3 ||
3815 1b5d300f 2023-02-20 thomas strncmp(pi->path, "**/", 3) != 0)
3818 bd8de430 2019-10-04 stsp while (*p) {
3819 1b5d300f 2023-02-20 thomas if (match_path(pi->path + 3,
3820 1b5d300f 2023-02-20 thomas pi->path_len - 3, p,
3821 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3822 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3823 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3825 bd8de430 2019-10-04 stsp while (*p == '/')
3835 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3836 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3837 6841da00 2019-08-08 stsp * ignores backwards.
3839 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3840 6841da00 2019-08-08 stsp while (pe) {
3841 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3842 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3843 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3844 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3845 1b5d300f 2023-02-20 thomas int flags = FNM_LEADING_DIR;
3846 1b5d300f 2023-02-20 thomas if (strstr(pi->path, "/**/") == NULL)
3847 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3848 1b5d300f 2023-02-20 thomas if (match_path(pi->path, pi->path_len,
3849 1b5d300f 2023-02-20 thomas path, flags))
3854 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3860 6841da00 2019-08-08 stsp static const struct got_error *
3861 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3862 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3864 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3865 6841da00 2019-08-08 stsp char *ignorespath;
3866 886cec17 2019-12-15 stsp int fd = -1;
3867 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3869 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3870 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3871 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3873 886cec17 2019-12-15 stsp if (dirfd != -1) {
3874 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3875 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3876 886cec17 2019-12-15 stsp if (fd == -1) {
3877 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3878 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3879 886cec17 2019-12-15 stsp ignorespath);
3881 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3882 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3883 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3884 886cec17 2019-12-15 stsp ignorespath);
3887 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3891 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3892 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3893 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3894 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3895 886cec17 2019-12-15 stsp ignorespath);
3897 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3900 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3901 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3902 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3903 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3904 6841da00 2019-08-08 stsp free(ignorespath);
3905 6841da00 2019-08-08 stsp return err;
3908 f8d1f275 2019-02-04 stsp static const struct got_error *
3909 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3910 6092c299 2021-10-04 thomas int dirfd)
3912 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3913 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3914 f8d1f275 2019-02-04 stsp char *path = NULL;
3916 6092c299 2021-10-04 thomas if (ignore != NULL)
3917 6092c299 2021-10-04 thomas *ignore = 0;
3919 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
3920 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3921 eb6fb9cd 2023-07-26 thomas if (err)
3922 eb6fb9cd 2023-07-26 thomas return err;
3925 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3926 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3927 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3929 f8d1f275 2019-02-04 stsp path = de->d_name;
3932 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3933 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3934 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3935 6092c299 2021-10-04 thomas *ignore = 1;
3936 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3937 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3938 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3939 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3940 f8d1f275 2019-02-04 stsp if (parent_path[0])
3941 f8d1f275 2019-02-04 stsp free(path);
3942 b72f483a 2019-02-05 stsp return err;
3945 347d1d3e 2019-07-12 stsp static const struct got_error *
3946 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3948 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3949 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3951 3143d852 2020-06-25 stsp if (a->no_ignores)
3952 3143d852 2020-06-25 stsp return NULL;
3954 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3955 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3957 3143d852 2020-06-25 stsp return err;
3959 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3960 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3962 3143d852 2020-06-25 stsp return err;
3965 3143d852 2020-06-25 stsp static const struct got_error *
3966 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3967 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3968 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3969 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3971 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3972 abb4604f 2019-07-27 stsp struct stat sb;
3974 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3976 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3977 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3979 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3980 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3981 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3982 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3983 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3986 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3987 fe1d8685 2022-02-12 thomas return NULL;
3989 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3990 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3991 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3993 abb4604f 2019-07-27 stsp return NULL;
3996 3143d852 2020-06-25 stsp static const struct got_error *
3997 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3998 3143d852 2020-06-25 stsp const char *root_path, const char *path)
4000 3143d852 2020-06-25 stsp const struct got_error *err;
4001 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
4003 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
4004 3143d852 2020-06-25 stsp ".cvsignore");
4006 3143d852 2020-06-25 stsp return err;
4008 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
4009 3143d852 2020-06-25 stsp ".gitignore");
4011 3143d852 2020-06-25 stsp return err;
4013 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
4014 3143d852 2020-06-25 stsp if (err) {
4015 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
4016 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
4017 3143d852 2020-06-25 stsp return err;
4019 3143d852 2020-06-25 stsp for (;;) {
4020 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
4021 3143d852 2020-06-25 stsp ".cvsignore");
4024 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
4025 3143d852 2020-06-25 stsp ".gitignore");
4028 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
4029 3143d852 2020-06-25 stsp if (err) {
4030 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
4031 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
4034 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
4036 b737c85a 2020-06-26 stsp free(parent_path);
4037 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
4038 b737c85a 2020-06-26 stsp next_parent_path = NULL;
4041 b737c85a 2020-06-26 stsp free(parent_path);
4042 b737c85a 2020-06-26 stsp free(next_parent_path);
4043 3143d852 2020-06-25 stsp return err;
4046 f8836425 2023-04-14 thomas struct find_missing_children_args {
4047 f8836425 2023-04-14 thomas const char *parent_path;
4048 f8836425 2023-04-14 thomas size_t parent_len;
4049 f8836425 2023-04-14 thomas struct got_pathlist_head *children;
4050 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb;
4051 f8836425 2023-04-14 thomas void *cancel_arg;
4054 abb4604f 2019-07-27 stsp static const struct got_error *
4055 f8836425 2023-04-14 thomas find_missing_children(void *arg, struct got_fileindex_entry *ie)
4057 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
4058 f8836425 2023-04-14 thomas struct find_missing_children_args *a = arg;
4060 f8836425 2023-04-14 thomas if (a->cancel_cb) {
4061 f8836425 2023-04-14 thomas err = a->cancel_cb(a->cancel_arg);
4062 f8836425 2023-04-14 thomas if (err)
4063 f8836425 2023-04-14 thomas return err;
4066 f8836425 2023-04-14 thomas if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4067 f8836425 2023-04-14 thomas err = got_pathlist_append(a->children, ie->path, NULL);
4069 f8836425 2023-04-14 thomas return err;
4072 f8836425 2023-04-14 thomas static const struct got_error *
4073 f8836425 2023-04-14 thomas report_children(struct got_pathlist_head *children,
4074 f8836425 2023-04-14 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
4075 f8836425 2023-04-14 thomas struct got_repository *repo, int is_root_dir, int report_unchanged,
4076 f8836425 2023-04-14 thomas struct got_pathlist_head *ignores, int no_ignores,
4077 f8836425 2023-04-14 thomas got_worktree_status_cb status_cb, void *status_arg,
4078 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb, void *cancel_arg)
4080 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
4081 f8836425 2023-04-14 thomas struct got_pathlist_entry *pe;
4082 f8836425 2023-04-14 thomas char *ondisk_path = NULL;
4084 f8836425 2023-04-14 thomas TAILQ_FOREACH(pe, children, entry) {
4085 f8836425 2023-04-14 thomas if (cancel_cb) {
4086 f8836425 2023-04-14 thomas err = cancel_cb(cancel_arg);
4087 f8836425 2023-04-14 thomas if (err)
4091 f8836425 2023-04-14 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4092 f8836425 2023-04-14 thomas !is_root_dir ? "/" : "", pe->path) == -1) {
4093 f8836425 2023-04-14 thomas err = got_error_from_errno("asprintf");
4094 f8836425 2023-04-14 thomas ondisk_path = NULL;
4098 f8836425 2023-04-14 thomas err = report_single_file_status(pe->path, ondisk_path,
4099 f8836425 2023-04-14 thomas fileindex, status_cb, status_arg, repo, report_unchanged,
4100 f8836425 2023-04-14 thomas ignores, no_ignores);
4101 f8836425 2023-04-14 thomas if (err)
4104 f8836425 2023-04-14 thomas free(ondisk_path);
4105 f8836425 2023-04-14 thomas ondisk_path = NULL;
4108 f8836425 2023-04-14 thomas free(ondisk_path);
4109 f8836425 2023-04-14 thomas return err;
4112 f8836425 2023-04-14 thomas static const struct got_error *
4113 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
4114 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4115 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4116 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4117 f2a9dc41 2019-12-13 tracey int report_unchanged)
4119 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
4120 6fc93f37 2019-12-13 stsp int fd = -1;
4121 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
4122 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
4123 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
4124 f8836425 2023-04-14 thomas struct got_pathlist_head ignores, missing_children;
4125 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
4127 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
4128 f8836425 2023-04-14 thomas TAILQ_INIT(&missing_children);
4130 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
4131 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
4132 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
4134 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4135 a78810f8 2022-03-13 thomas if (ie) {
4136 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
4137 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
4138 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
4139 a78810f8 2022-03-13 thomas goto done;
4140 f8836425 2023-04-14 thomas } else {
4141 f8836425 2023-04-14 thomas struct find_missing_children_args fmca;
4142 f8836425 2023-04-14 thomas fmca.parent_path = path;
4143 f8836425 2023-04-14 thomas fmca.parent_len = strlen(path);
4144 f8836425 2023-04-14 thomas fmca.children = &missing_children;
4145 f8836425 2023-04-14 thomas fmca.cancel_cb = cancel_cb;
4146 f8836425 2023-04-14 thomas fmca.cancel_arg = cancel_arg;
4147 f8836425 2023-04-14 thomas err = got_fileindex_for_each_entry_safe(fileindex,
4148 f8836425 2023-04-14 thomas find_missing_children, &fmca);
4149 f8836425 2023-04-14 thomas if (err)
4150 f8836425 2023-04-14 thomas goto done;
4153 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4154 6fc93f37 2019-12-13 stsp if (fd == -1) {
4155 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4156 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
4157 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
4159 ff56836b 2021-07-08 stsp if (!no_ignores) {
4160 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4161 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
4163 ff56836b 2021-07-08 stsp goto done;
4165 f8836425 2023-04-14 thomas if (TAILQ_EMPTY(&missing_children)) {
4166 f8836425 2023-04-14 thomas err = report_single_file_status(path,
4167 f8836425 2023-04-14 thomas ondisk_path, fileindex,
4168 f8836425 2023-04-14 thomas status_cb, status_arg, repo,
4169 f8836425 2023-04-14 thomas report_unchanged, &ignores, no_ignores);
4170 f8836425 2023-04-14 thomas if (err)
4171 f8836425 2023-04-14 thomas goto done;
4172 f8836425 2023-04-14 thomas } else {
4173 f8836425 2023-04-14 thomas err = report_children(&missing_children,
4174 f8836425 2023-04-14 thomas worktree, fileindex, repo,
4175 f8836425 2023-04-14 thomas (path[0] == '\0'), report_unchanged,
4176 f8836425 2023-04-14 thomas &ignores, no_ignores,
4177 f8836425 2023-04-14 thomas status_cb, status_arg,
4178 f8836425 2023-04-14 thomas cancel_cb, cancel_arg);
4179 6f97aa83 2023-04-14 thomas if (err)
4180 6f97aa83 2023-04-14 thomas goto done;
4184 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4185 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4186 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4187 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4188 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4189 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4190 abb4604f 2019-07-27 stsp arg.status_path = path;
4191 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4192 abb4604f 2019-07-27 stsp arg.repo = repo;
4193 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4194 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4195 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4196 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4197 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4198 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4199 022fae89 2019-12-06 tracey if (!no_ignores) {
4200 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4201 3143d852 2020-06-25 stsp worktree->root_path, path);
4203 3143d852 2020-06-25 stsp goto done;
4205 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4206 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4207 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4210 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4211 b2dcbbc7 2023-12-05 thomas got_pathlist_free(&missing_children, GOT_PATHLIST_FREE_NONE);
4212 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4213 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4214 927df6b7 2019-02-10 stsp free(ondisk_path);
4215 347d1d3e 2019-07-12 stsp return err;
4218 347d1d3e 2019-07-12 stsp const struct got_error *
4219 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4220 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4221 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4222 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4224 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4225 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4226 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4227 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4229 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4231 347d1d3e 2019-07-12 stsp return err;
4233 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4234 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4235 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4236 f6343036 2021-06-22 stsp no_ignores, 0);
4240 f8d1f275 2019-02-04 stsp free(fileindex_path);
4241 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4242 f8d1f275 2019-02-04 stsp return err;
4245 6c7ab921 2019-03-18 stsp const struct got_error *
4246 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4247 6c7ab921 2019-03-18 stsp const char *arg)
4249 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4250 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4251 6c7ab921 2019-03-18 stsp size_t len;
4252 00bb5ea0 2020-07-23 stsp struct stat sb;
4253 01740607 2020-11-04 stsp char *abspath = NULL;
4254 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4256 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4258 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4259 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4260 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4262 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4263 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4264 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4265 00bb5ea0 2020-07-23 stsp goto done;
4267 727173c3 2020-11-06 stsp sb.st_mode = 0;
4269 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4271 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4272 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4273 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4274 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4276 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4277 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4278 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4279 00bb5ea0 2020-07-23 stsp goto done;
4283 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4284 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4286 d0710d08 2019-07-22 stsp goto done;
4287 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4288 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4289 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4290 00bb5ea0 2020-07-23 stsp goto done;
4293 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4294 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4295 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4296 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4297 00bb5ea0 2020-07-23 stsp goto done;
4299 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4300 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4301 01740607 2020-11-04 stsp goto done;
4303 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4304 01740607 2020-11-04 stsp sizeof(canonpath));
4306 00bb5ea0 2020-07-23 stsp goto done;
4307 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4308 01740607 2020-11-04 stsp if (resolved == NULL) {
4309 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4310 01740607 2020-11-04 stsp goto done;
4315 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4316 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4317 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4318 6c7ab921 2019-03-18 stsp goto done;
4321 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4322 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4323 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4325 f6d88e1a 2019-05-29 stsp goto done;
4327 f6d88e1a 2019-05-29 stsp path = strdup("");
4328 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4329 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4330 f6d88e1a 2019-05-29 stsp goto done;
4334 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4335 6c7ab921 2019-03-18 stsp len = strlen(path);
4336 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4337 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4341 01740607 2020-11-04 stsp free(abspath);
4342 6c7ab921 2019-03-18 stsp free(resolved);
4343 d0710d08 2019-07-22 stsp free(cwd);
4344 6c7ab921 2019-03-18 stsp if (err == NULL)
4345 6c7ab921 2019-03-18 stsp *wt_path = path;
4347 6c7ab921 2019-03-18 stsp free(path);
4348 d00136be 2019-03-26 stsp return err;
4351 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4352 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4353 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4354 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4355 4e68cba3 2019-11-23 stsp void *progress_arg;
4356 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4359 b21ebdb0 2023-06-22 thomas static int
4360 b21ebdb0 2023-06-22 thomas add_noop_status(unsigned char status)
4362 b21ebdb0 2023-06-22 thomas return (status == GOT_STATUS_ADD ||
4363 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODIFY ||
4364 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_CONFLICT ||
4365 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODE_CHANGE ||
4366 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_NO_CHANGE);
4369 4e68cba3 2019-11-23 stsp static const struct got_error *
4370 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4371 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4372 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4373 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4375 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4376 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4377 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4378 6d022e97 2019-08-04 stsp struct stat sb;
4379 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4381 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4382 dbb83fbd 2019-12-12 stsp relpath) == -1)
4383 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4385 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4387 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4388 12463d8b 2019-12-13 stsp de_name, a->repo);
4390 dbb83fbd 2019-12-12 stsp goto done;
4391 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4392 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE &&
4393 b21ebdb0 2023-06-22 thomas add_noop_status(status))
4394 dbb83fbd 2019-12-12 stsp goto done;
4395 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4397 dbb83fbd 2019-12-12 stsp goto done;
4400 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4401 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4402 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4404 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4405 dbb83fbd 2019-12-12 stsp goto done;
4408 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4410 4e68cba3 2019-11-23 stsp goto done;
4411 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4412 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4413 3969253a 2020-03-07 stsp if (err) {
4414 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4415 3969253a 2020-03-07 stsp goto done;
4417 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4418 07f5b47a 2019-06-02 stsp if (err) {
4419 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4420 4e68cba3 2019-11-23 stsp goto done;
4423 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4425 dbb83fbd 2019-12-12 stsp return err;
4426 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4427 dbb83fbd 2019-12-12 stsp return NULL;
4428 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4431 d00136be 2019-03-26 stsp const struct got_error *
4432 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4433 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4434 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4435 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4437 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4438 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4439 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4440 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4441 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4443 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4445 d00136be 2019-03-26 stsp return err;
4447 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4449 d00136be 2019-03-26 stsp goto done;
4451 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4452 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4453 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4454 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4455 4e68cba3 2019-11-23 stsp saa.repo = repo;
4457 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4458 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4459 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4463 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4464 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4465 af12c6b9 2019-06-04 stsp err = sync_err;
4467 fb399478 2019-07-12 stsp free(fileindex_path);
4468 d00136be 2019-03-26 stsp if (fileindex)
4469 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4470 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4471 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4472 d00136be 2019-03-26 stsp err = unlockerr;
4473 6c7ab921 2019-03-18 stsp return err;
4476 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4477 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4478 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4479 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4480 f2a9dc41 2019-12-13 tracey void *progress_arg;
4481 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4482 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4483 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4484 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4485 8ec7352a 2023-06-01 thomas const char *status_path;
4486 8ec7352a 2023-06-01 thomas size_t status_path_len;
4487 766841c2 2020-08-13 stsp const char *status_codes;
4490 f2a9dc41 2019-12-13 tracey static const struct got_error *
4491 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4492 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4493 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4494 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4496 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4497 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4498 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4499 17ed4618 2019-06-02 stsp struct stat sb;
4500 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4502 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4503 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4504 d64cc78a 2022-01-25 thomas return NULL;
4505 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4508 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4509 17ed4618 2019-06-02 stsp if (ie == NULL)
4510 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4512 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4513 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4514 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4515 9acbc4fa 2019-08-03 stsp return NULL;
4516 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4519 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4520 f2a9dc41 2019-12-13 tracey relpath) == -1)
4521 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4523 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4526 f2a9dc41 2019-12-13 tracey goto done;
4528 766841c2 2020-08-13 stsp if (a->status_codes) {
4529 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4531 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4532 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4535 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4536 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4537 766841c2 2020-08-13 stsp free(ondisk_path);
4538 766841c2 2020-08-13 stsp return NULL;
4540 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4541 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4542 766841c2 2020-08-13 stsp static char msg[64];
4543 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4544 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4545 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4546 766841c2 2020-08-13 stsp goto done;
4550 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4551 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4552 f2a9dc41 2019-12-13 tracey goto done;
4553 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4554 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4555 f2a9dc41 2019-12-13 tracey goto done;
4557 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4558 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4559 d64cc78a 2022-01-25 thomas goto done;
4561 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4562 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4563 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4564 f2a9dc41 2019-12-13 tracey goto done;
4568 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4569 20a2ad1c 2020-10-20 stsp size_t root_len;
4571 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4572 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4573 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4574 12463d8b 2019-12-13 stsp ondisk_path);
4575 12463d8b 2019-12-13 stsp goto done;
4577 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4578 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4579 12463d8b 2019-12-13 stsp goto done;
4582 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4584 20a2ad1c 2020-10-20 stsp char *parent;
4586 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4588 2513f20a 2020-10-20 stsp goto done;
4589 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4590 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4591 8ec7352a 2023-06-01 thomas if (got_path_cmp(ondisk_path, a->status_path,
4592 8ec7352a 2023-06-01 thomas strlen(ondisk_path), a->status_path_len) != 0 &&
4593 8ec7352a 2023-06-01 thomas !got_path_is_child(ondisk_path, a->status_path,
4594 8ec7352a 2023-06-01 thomas a->status_path_len))
4596 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4597 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4598 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4599 20a2ad1c 2020-10-20 stsp ondisk_path);
4602 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4603 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4606 97f9158f 2023-08-29 thomas if (got_fileindex_entry_has_blob(ie))
4607 97f9158f 2023-08-29 thomas got_fileindex_entry_mark_deleted_from_disk(ie);
4609 97f9158f 2023-08-29 thomas got_fileindex_entry_remove(a->fileindex, ie);
4611 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4612 f2a9dc41 2019-12-13 tracey if (err)
4613 f2a9dc41 2019-12-13 tracey return err;
4614 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4615 f2a9dc41 2019-12-13 tracey return NULL;
4616 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4617 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4620 2ec1f75b 2019-03-26 stsp const struct got_error *
4621 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4622 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4623 766841c2 2020-08-13 stsp const char *status_codes,
4624 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4625 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4627 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4628 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4629 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4630 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4631 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4633 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4635 2ec1f75b 2019-03-26 stsp return err;
4637 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4639 2ec1f75b 2019-03-26 stsp goto done;
4641 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4642 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4643 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4644 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4645 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4646 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4647 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4648 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4649 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4651 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4652 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
4654 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s%s%s",
4655 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree),
4656 8ec7352a 2023-06-01 thomas pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4657 8ec7352a 2023-06-01 thomas err = got_error_from_errno("asprintf");
4658 8ec7352a 2023-06-01 thomas goto done;
4660 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
4661 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
4662 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4663 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4664 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
4668 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4669 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4670 af12c6b9 2019-06-04 stsp err = sync_err;
4672 fb399478 2019-07-12 stsp free(fileindex_path);
4673 2ec1f75b 2019-03-26 stsp if (fileindex)
4674 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4675 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4676 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4677 2ec1f75b 2019-03-26 stsp err = unlockerr;
4678 33aa809d 2019-08-08 stsp return err;
4681 33aa809d 2019-08-08 stsp static const struct got_error *
4682 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4684 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4685 33aa809d 2019-08-08 stsp char *line = NULL;
4686 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4687 33aa809d 2019-08-08 stsp ssize_t linelen;
4689 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4690 33aa809d 2019-08-08 stsp if (linelen == -1) {
4691 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4692 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4693 33aa809d 2019-08-08 stsp goto done;
4695 33aa809d 2019-08-08 stsp return NULL;
4697 33aa809d 2019-08-08 stsp if (outfile) {
4698 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4699 33aa809d 2019-08-08 stsp if (n != linelen) {
4700 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4701 33aa809d 2019-08-08 stsp goto done;
4704 33aa809d 2019-08-08 stsp if (rejectfile) {
4705 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4706 33aa809d 2019-08-08 stsp if (n != linelen)
4707 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4710 33aa809d 2019-08-08 stsp free(line);
4711 2ec1f75b 2019-03-26 stsp return err;
4714 33aa809d 2019-08-08 stsp static const struct got_error *
4715 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4717 33aa809d 2019-08-08 stsp char *line = NULL;
4718 33aa809d 2019-08-08 stsp size_t linesize = 0;
4719 33aa809d 2019-08-08 stsp ssize_t linelen;
4721 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4722 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4723 500467ff 2019-09-25 hiltjo if (ferror(f))
4724 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4725 500467ff 2019-09-25 hiltjo return NULL;
4727 33aa809d 2019-08-08 stsp free(line);
4728 33aa809d 2019-08-08 stsp return NULL;
4731 33aa809d 2019-08-08 stsp static const struct got_error *
4732 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4733 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4734 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4736 33aa809d 2019-08-08 stsp const struct got_error *err;
4738 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4739 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4740 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4742 33aa809d 2019-08-08 stsp return err;
4743 33aa809d 2019-08-08 stsp (*line_cur1)++;
4745 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4746 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4747 33aa809d 2019-08-08 stsp if (rejectfile)
4748 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4750 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4752 33aa809d 2019-08-08 stsp return err;
4753 33aa809d 2019-08-08 stsp (*line_cur2)++;
4755 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4756 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4757 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4759 33aa809d 2019-08-08 stsp return err;
4760 33aa809d 2019-08-08 stsp (*line_cur2)++;
4762 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4763 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4764 33aa809d 2019-08-08 stsp if (rejectfile)
4765 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4767 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4769 33aa809d 2019-08-08 stsp return err;
4770 33aa809d 2019-08-08 stsp (*line_cur1)++;
4773 f1e81a05 2019-08-10 stsp return NULL;
4776 f1e81a05 2019-08-10 stsp static const struct got_error *
4777 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4778 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4780 f1e81a05 2019-08-10 stsp const struct got_error *err;
4782 f1e81a05 2019-08-10 stsp if (outfile) {
4783 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4784 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4785 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4787 f1e81a05 2019-08-10 stsp return err;
4788 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4791 f1e81a05 2019-08-10 stsp if (rejectfile) {
4792 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4793 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4794 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4796 f1e81a05 2019-08-10 stsp return err;
4797 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4801 33aa809d 2019-08-08 stsp return NULL;
4804 33aa809d 2019-08-08 stsp static const struct got_error *
4805 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4806 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4807 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4808 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4809 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4811 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4812 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4813 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4814 33aa809d 2019-08-08 stsp FILE *hunkfile;
4815 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4816 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4819 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4821 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4822 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4823 fe621944 2020-11-10 stsp start_old = cc.left.start;
4824 fe621944 2020-11-10 stsp end_old = cc.left.end;
4825 fe621944 2020-11-10 stsp start_new = cc.right.start;
4826 fe621944 2020-11-10 stsp end_new = cc.right.end;
4828 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4829 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4830 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4832 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4833 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4834 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4836 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4837 fe621944 2020-11-10 stsp if (diff_state == NULL)
4838 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4839 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4841 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4842 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4843 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4844 33aa809d 2019-08-08 stsp goto done;
4847 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4848 fe621944 2020-11-10 stsp diff_result, &cc);
4849 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4850 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4851 33aa809d 2019-08-08 stsp goto done;
4854 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4855 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4856 33aa809d 2019-08-08 stsp goto done;
4859 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4860 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4862 33aa809d 2019-08-08 stsp goto done;
4864 33aa809d 2019-08-08 stsp switch (*choice) {
4865 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4866 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4867 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4869 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4870 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4871 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4873 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4876 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4880 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4881 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4882 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4883 33aa809d 2019-08-08 stsp return err;
4886 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4887 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4888 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4889 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4890 1f1abb7e 2019-08-08 stsp void *progress_arg;
4891 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4892 33aa809d 2019-08-08 stsp void *patch_arg;
4893 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4894 10604dce 2021-09-24 thomas int unlink_added_files;
4895 8641a332 2023-04-14 thomas struct got_pathlist_head *added_files_to_unlink;
4898 e20a8b6f 2019-06-04 stsp static const struct got_error *
4899 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4900 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4901 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4902 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4903 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4905 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4906 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4907 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4908 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4909 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4910 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4911 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4912 fe621944 2020-11-10 stsp struct stat sb2;
4913 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4914 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4915 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4917 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4919 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4921 33aa809d 2019-08-08 stsp return err;
4923 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4924 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4925 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4926 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4927 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4928 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4929 fa3cef63 2020-07-23 stsp goto done;
4931 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4932 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4933 48b4f239 2021-12-31 thomas if (link_len == -1) {
4934 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4937 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4938 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4941 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4942 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4943 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4944 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4945 fa3cef63 2020-07-23 stsp goto done;
4947 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4948 fa3cef63 2020-07-23 stsp sizeof(link_target));
4949 fa3cef63 2020-07-23 stsp if (link_len == -1)
4950 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4951 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4952 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4955 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4956 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4957 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4958 fa3cef63 2020-07-23 stsp goto done;
4961 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4962 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4963 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4964 fa3cef63 2020-07-23 stsp goto done;
4969 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4970 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4971 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4972 fa3cef63 2020-07-23 stsp goto done;
4974 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4975 fa3cef63 2020-07-23 stsp if (n != link_len) {
4976 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4977 fa3cef63 2020-07-23 stsp goto done;
4979 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4980 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4981 fa3cef63 2020-07-23 stsp goto done;
4983 fa3cef63 2020-07-23 stsp rewind(f2);
4986 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4987 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4988 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4989 f4ae6ddb 2022-07-01 thomas goto done;
4992 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4994 33aa809d 2019-08-08 stsp goto done;
4996 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4998 33aa809d 2019-08-08 stsp goto done;
5000 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5002 33aa809d 2019-08-08 stsp goto done;
5004 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5005 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5007 33aa809d 2019-08-08 stsp goto done;
5009 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5012 33aa809d 2019-08-08 stsp goto done;
5014 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
5015 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
5016 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
5017 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
5019 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
5020 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5021 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
5022 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
5023 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
5024 fe621944 2020-11-10 stsp nchanges++;
5026 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5027 33aa809d 2019-08-08 stsp int choice;
5028 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
5029 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
5030 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
5031 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
5032 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
5033 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
5035 33aa809d 2019-08-08 stsp goto done;
5036 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
5037 33aa809d 2019-08-08 stsp have_content = 1;
5038 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
5041 1ebedb77 2019-10-19 stsp if (have_content) {
5042 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5043 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
5044 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
5046 1ebedb77 2019-10-19 stsp goto done;
5048 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
5049 a2c162eb 2022-10-30 thomas mode_t mode;
5051 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
5052 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
5053 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
5054 fa3cef63 2020-07-23 stsp goto done;
5059 33aa809d 2019-08-08 stsp free(id_str);
5060 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5061 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5063 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
5064 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
5065 fe621944 2020-11-10 stsp if (err == NULL)
5066 fe621944 2020-11-10 stsp err = free_err;
5067 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
5068 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
5069 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
5070 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
5071 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5072 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
5073 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
5074 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
5075 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
5076 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
5077 33aa809d 2019-08-08 stsp if (err || !have_content) {
5078 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5079 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
5080 33aa809d 2019-08-08 stsp free(*path_outfile);
5081 33aa809d 2019-08-08 stsp *path_outfile = NULL;
5083 33aa809d 2019-08-08 stsp free(path1);
5084 33aa809d 2019-08-08 stsp return err;
5087 33aa809d 2019-08-08 stsp static const struct got_error *
5088 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
5089 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
5090 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5091 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5093 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
5094 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
5095 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
5096 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
5097 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
5098 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
5099 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
5100 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
5101 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
5102 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
5103 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
5104 f4ae6ddb 2022-07-01 thomas int fd = -1;
5106 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
5107 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
5108 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
5109 d3bcc3d1 2019-08-08 stsp return NULL;
5111 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
5112 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
5113 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
5115 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5116 65084dad 2019-08-08 stsp if (ie == NULL)
5117 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
5119 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
5120 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
5121 a129376b 2019-03-28 stsp if (err) {
5122 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
5123 a129376b 2019-03-28 stsp goto done;
5124 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
5125 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
5126 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
5127 e20a8b6f 2019-06-04 stsp goto done;
5130 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
5131 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
5132 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5133 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5134 a129376b 2019-03-28 stsp goto done;
5137 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
5138 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
5139 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5140 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5141 a129376b 2019-03-28 stsp goto done;
5144 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
5145 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
5146 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5147 a129376b 2019-03-28 stsp goto done;
5152 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
5153 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
5154 945f9229 2022-04-16 thomas if (err)
5155 945f9229 2022-04-16 thomas goto done;
5157 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5158 a9fa2909 2019-07-27 stsp if (err) {
5159 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5160 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
5161 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
5162 a9fa2909 2019-07-27 stsp goto done;
5164 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
5166 a9fa2909 2019-07-27 stsp goto done;
5168 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
5170 a9fa2909 2019-07-27 stsp goto done;
5172 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
5173 1233e6b6 2020-10-19 stsp free(te_name);
5174 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
5175 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
5176 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5177 a9fa2909 2019-07-27 stsp goto done;
5181 a129376b 2019-03-28 stsp switch (status) {
5182 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
5183 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5184 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5185 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5186 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5188 33aa809d 2019-08-08 stsp goto done;
5189 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5192 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5193 1f1abb7e 2019-08-08 stsp ie->path);
5195 1ee397ad 2019-07-12 stsp goto done;
5196 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5197 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
5198 8641a332 2023-04-14 thomas int do_unlink = a->added_files_to_unlink ? 0 : 1;
5200 8641a332 2023-04-14 thomas if (a->added_files_to_unlink) {
5201 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
5203 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, a->added_files_to_unlink,
5204 8641a332 2023-04-14 thomas entry) {
5205 8641a332 2023-04-14 thomas if (got_path_cmp(pe->path, relpath,
5206 8641a332 2023-04-14 thomas pe->path_len, strlen(relpath)))
5207 8641a332 2023-04-14 thomas continue;
5208 8641a332 2023-04-14 thomas do_unlink = 1;
5213 8641a332 2023-04-14 thomas if (do_unlink) {
5214 8641a332 2023-04-14 thomas if (asprintf(&ondisk_path, "%s/%s",
5215 8641a332 2023-04-14 thomas got_worktree_get_root_path(a->worktree),
5216 8641a332 2023-04-14 thomas relpath) == -1) {
5217 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
5218 8641a332 2023-04-14 thomas goto done;
5220 8641a332 2023-04-14 thomas if (unlink(ondisk_path) == -1) {
5221 8641a332 2023-04-14 thomas err = got_error_from_errno2("unlink",
5222 8641a332 2023-04-14 thomas ondisk_path);
5228 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5229 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5230 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5231 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5232 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5234 33aa809d 2019-08-08 stsp goto done;
5235 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5238 33aa809d 2019-08-08 stsp /* fall through */
5239 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5240 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5241 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5242 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5243 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5244 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5245 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
5246 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
5248 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
5249 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5250 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5251 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5252 f4ae6ddb 2022-07-01 thomas goto done;
5255 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5257 65084dad 2019-08-08 stsp goto done;
5259 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5260 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5261 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5262 a129376b 2019-03-28 stsp goto done;
5265 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5266 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5267 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5268 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5269 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5270 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5271 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5273 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5274 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5275 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5276 369fd7e5 2020-07-23 stsp path_content);
5279 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5280 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5281 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5282 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5284 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5285 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5286 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5287 369fd7e5 2020-07-23 stsp goto done;
5291 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5292 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5293 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5294 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5295 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5296 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5298 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5300 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5301 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5302 aaf972e2 2023-09-18 thomas 0, 1, 0, 0, NULL, a->repo,
5303 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5306 a129376b 2019-03-28 stsp goto done;
5307 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5308 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5309 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5310 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5311 437adc9d 2020-12-10 yzhong blob->id.sha1,
5312 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5314 2e1fa222 2020-07-23 stsp goto done;
5316 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5317 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5318 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5327 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5328 33aa809d 2019-08-08 stsp free(path_content);
5329 e20a8b6f 2019-06-04 stsp free(parent_path);
5330 a129376b 2019-03-28 stsp free(tree_path);
5331 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5332 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5334 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5336 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5337 a129376b 2019-03-28 stsp free(tree_id);
5338 945f9229 2022-04-16 thomas if (base_commit)
5339 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
5340 e20a8b6f 2019-06-04 stsp return err;
5343 e20a8b6f 2019-06-04 stsp const struct got_error *
5344 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5345 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5346 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5347 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5348 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5350 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5351 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5352 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5353 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5354 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5355 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5357 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5359 e20a8b6f 2019-06-04 stsp return err;
5361 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5363 e20a8b6f 2019-06-04 stsp goto done;
5365 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5366 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5367 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5368 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5369 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5370 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5371 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5372 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
5373 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5374 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5375 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
5379 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5380 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5381 e20a8b6f 2019-06-04 stsp err = sync_err;
5383 fb399478 2019-07-12 stsp free(fileindex_path);
5384 a129376b 2019-03-28 stsp if (fileindex)
5385 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5386 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5387 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5388 a129376b 2019-03-28 stsp err = unlockerr;
5389 c4296144 2019-05-09 stsp return err;
5392 cf066bf8 2019-05-09 stsp static void
5393 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5395 24519714 2019-05-09 stsp free(ct->path);
5396 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5397 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5398 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5399 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5400 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5401 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5405 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5406 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5407 24519714 2019-05-09 stsp struct got_repository *repo;
5408 24519714 2019-05-09 stsp struct got_worktree *worktree;
5409 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5410 5f8a88c6 2019-08-03 stsp int have_staged_files;
5411 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5412 ef899790 2022-11-01 thomas int diff_header_shown;
5413 be94c032 2023-02-20 thomas int commit_conflicts;
5414 ef899790 2022-11-01 thomas FILE *diff_outfile;
5415 ef899790 2022-11-01 thomas FILE *f1;
5416 ef899790 2022-11-01 thomas FILE *f2;
5420 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5421 ef899790 2022-11-01 thomas * it as content to the diff engine.
5423 ef899790 2022-11-01 thomas static const struct got_error *
5424 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5425 ef899790 2022-11-01 thomas const char *abspath)
5427 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5428 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5429 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5431 ef899790 2022-11-01 thomas *fd = -1;
5433 ef899790 2022-11-01 thomas if (dirfd != -1) {
5434 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5435 ef899790 2022-11-01 thomas if (target_len == -1)
5436 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5437 ef899790 2022-11-01 thomas } else {
5438 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5439 ef899790 2022-11-01 thomas if (target_len == -1)
5440 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5443 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5444 ef899790 2022-11-01 thomas if (*fd == -1)
5445 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5447 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5448 ef899790 2022-11-01 thomas if (outlen == -1) {
5449 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5450 ef899790 2022-11-01 thomas goto done;
5453 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5454 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5455 ef899790 2022-11-01 thomas goto done;
5458 ef899790 2022-11-01 thomas if (err) {
5459 ef899790 2022-11-01 thomas close(*fd);
5460 ef899790 2022-11-01 thomas *fd = -1;
5462 ef899790 2022-11-01 thomas return err;
5465 ef899790 2022-11-01 thomas static const struct got_error *
5466 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5467 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5468 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5470 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5471 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5472 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5473 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5474 ef899790 2022-11-01 thomas char *label1 = NULL;
5475 ef899790 2022-11-01 thomas struct stat sb;
5476 ef899790 2022-11-01 thomas off_t size1 = 0;
5477 ef899790 2022-11-01 thomas int f2_exists = 0;
5478 ef899790 2022-11-01 thomas char *id_str = NULL;
5480 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5482 d259e491 2022-11-01 thomas if (diff_staged) {
5483 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5484 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5485 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5486 d259e491 2022-11-01 thomas return NULL;
5487 d259e491 2022-11-01 thomas } else {
5488 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5489 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5490 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5491 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5492 d259e491 2022-11-01 thomas return NULL;
5495 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5496 ef899790 2022-11-01 thomas if (err)
5497 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5498 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5499 ef899790 2022-11-01 thomas if (err)
5500 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5502 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5503 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5504 ef899790 2022-11-01 thomas if (err)
5505 ef899790 2022-11-01 thomas return err;
5506 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5507 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5508 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5509 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5510 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5511 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5512 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5515 ef899790 2022-11-01 thomas if (diff_staged) {
5516 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5517 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5518 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5519 ef899790 2022-11-01 thomas label1 = ct->path;
5520 ef899790 2022-11-01 thomas label2 = ct->path;
5522 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5523 ef899790 2022-11-01 thomas label2 = ct->path;
5525 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5526 ef899790 2022-11-01 thomas label1 = ct->path;
5528 ef899790 2022-11-01 thomas default:
5529 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5531 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5532 ef899790 2022-11-01 thomas if (fd1 == -1) {
5533 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5534 ef899790 2022-11-01 thomas goto done;
5536 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5537 ef899790 2022-11-01 thomas if (fd2 == -1) {
5538 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5539 ef899790 2022-11-01 thomas goto done;
5541 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5542 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5543 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5544 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5545 ef899790 2022-11-01 thomas goto done;
5548 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5549 ef899790 2022-11-01 thomas if (fd1 == -1) {
5550 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5551 ef899790 2022-11-01 thomas goto done;
5554 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5555 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5556 ef899790 2022-11-01 thomas 8192, fd1);
5557 ef899790 2022-11-01 thomas if (err)
5558 ef899790 2022-11-01 thomas goto done;
5561 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5562 ef899790 2022-11-01 thomas if (dirfd != -1) {
5563 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5564 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5565 ef899790 2022-11-01 thomas if (fd == -1) {
5566 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5567 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5568 ef899790 2022-11-01 thomas ct->ondisk_path);
5569 ef899790 2022-11-01 thomas goto done;
5571 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5572 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5573 ef899790 2022-11-01 thomas if (err)
5574 ef899790 2022-11-01 thomas goto done;
5576 ef899790 2022-11-01 thomas } else {
5577 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5578 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5579 ef899790 2022-11-01 thomas if (fd == -1) {
5580 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5581 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5582 ef899790 2022-11-01 thomas ct->ondisk_path);
5583 ef899790 2022-11-01 thomas goto done;
5585 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5586 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5587 ef899790 2022-11-01 thomas if (err)
5588 ef899790 2022-11-01 thomas goto done;
5591 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5592 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5593 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5594 ef899790 2022-11-01 thomas goto done;
5596 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5597 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5598 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5599 ef899790 2022-11-01 thomas goto done;
5601 ef899790 2022-11-01 thomas fd = -1;
5602 ef899790 2022-11-01 thomas f2_exists = 1;
5605 ef899790 2022-11-01 thomas if (blob1) {
5606 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5607 ef899790 2022-11-01 thomas f1, blob1);
5608 ef899790 2022-11-01 thomas if (err)
5609 ef899790 2022-11-01 thomas goto done;
5612 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5613 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5614 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5616 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5617 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5618 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5619 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5620 ef899790 2022-11-01 thomas if (blob1)
5621 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5622 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5623 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5624 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5625 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5626 ef899790 2022-11-01 thomas return err;
5629 c4296144 2019-05-09 stsp static const struct got_error *
5630 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5631 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5632 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5633 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5635 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5636 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5637 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5638 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5639 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5640 768aea60 2019-05-09 stsp struct stat sb;
5642 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5643 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5644 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5645 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5646 5f8a88c6 2019-08-03 stsp return NULL;
5648 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5649 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5650 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5653 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5654 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5655 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5656 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5657 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5658 5f8a88c6 2019-08-03 stsp return NULL;
5661 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5662 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5663 036813ee 2019-05-09 stsp goto done;
5665 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5666 036813ee 2019-05-09 stsp parent_path = strdup("");
5667 69960a46 2019-05-09 stsp if (parent_path == NULL)
5668 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5670 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5672 69960a46 2019-05-09 stsp return err;
5675 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5676 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5677 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5678 c4296144 2019-05-09 stsp goto done;
5681 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5682 768aea60 2019-05-09 stsp relpath) == -1) {
5683 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5684 768aea60 2019-05-09 stsp goto done;
5687 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5688 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5689 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5690 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5691 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5692 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5693 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5694 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5696 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5697 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5700 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5701 0aeb8099 2020-07-23 stsp goto done;
5703 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5704 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5705 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5706 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5707 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5708 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5709 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5710 12463d8b 2019-12-13 stsp ct->ondisk_path);
5711 12463d8b 2019-12-13 stsp goto done;
5713 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5714 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5715 768aea60 2019-05-09 stsp goto done;
5717 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5720 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5721 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5722 44d03001 2019-05-09 stsp relpath) == -1) {
5723 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5724 44d03001 2019-05-09 stsp goto done;
5727 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5728 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5729 35213c7c 2020-07-23 stsp int is_bad_symlink;
5730 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5731 35213c7c 2020-07-23 stsp ssize_t target_len;
5732 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5733 35213c7c 2020-07-23 stsp sizeof(target_path));
5734 35213c7c 2020-07-23 stsp if (target_len == -1) {
5735 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5736 35213c7c 2020-07-23 stsp ct->ondisk_path);
5737 35213c7c 2020-07-23 stsp goto done;
5739 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5740 ad10f64e 2023-07-19 thomas target_len, ct->ondisk_path, a->worktree->root_path,
5741 ad10f64e 2023-07-19 thomas a->worktree->meta_dir);
5743 35213c7c 2020-07-23 stsp goto done;
5744 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5745 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5746 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5747 35213c7c 2020-07-23 stsp goto done;
5752 cf066bf8 2019-05-09 stsp ct->status = status;
5753 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5754 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5755 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5756 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5757 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5758 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5759 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5760 b416585c 2019-05-13 stsp goto done;
5762 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5763 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5764 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5765 f0b75401 2019-08-03 stsp goto done;
5768 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5769 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5770 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5771 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5772 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5773 036813ee 2019-05-09 stsp goto done;
5776 24519714 2019-05-09 stsp ct->path = strdup(path);
5777 24519714 2019-05-09 stsp if (ct->path == NULL) {
5778 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5779 24519714 2019-05-09 stsp goto done;
5782 720b08bc 2023-08-29 thomas if (a->diff_outfile) {
5783 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5784 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5785 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5786 ef899790 2022-11-01 thomas if (err)
5787 ef899790 2022-11-01 thomas goto done;
5790 720b08bc 2023-08-29 thomas err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5792 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5793 ed175427 2019-05-09 stsp free_commitable(ct);
5794 24519714 2019-05-09 stsp free(parent_path);
5795 036813ee 2019-05-09 stsp free(path);
5796 a129376b 2019-03-28 stsp return err;
5799 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5800 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5801 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5802 036813ee 2019-05-09 stsp struct got_repository *);
5804 ed175427 2019-05-09 stsp static const struct got_error *
5805 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5806 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5807 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5808 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5809 afa376bf 2019-05-09 stsp struct got_repository *repo)
5811 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5812 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5813 036813ee 2019-05-09 stsp char *subpath;
5815 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5816 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5817 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5819 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5821 ed175427 2019-05-09 stsp return err;
5823 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5824 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5825 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5826 036813ee 2019-05-09 stsp free(subpath);
5827 036813ee 2019-05-09 stsp return err;
5830 036813ee 2019-05-09 stsp static const struct got_error *
5831 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5833 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5834 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5836 036813ee 2019-05-09 stsp *match = 0;
5838 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5839 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5840 0f63689d 2019-05-10 stsp return NULL;
5843 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5845 0f63689d 2019-05-10 stsp return err;
5846 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5847 036813ee 2019-05-09 stsp free(ct_parent_path);
5848 036813ee 2019-05-09 stsp return err;
5851 768aea60 2019-05-09 stsp static mode_t
5852 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5854 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5855 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5857 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5860 036813ee 2019-05-09 stsp static const struct got_error *
5861 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5862 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5864 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5866 036813ee 2019-05-09 stsp *new_te = NULL;
5868 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5870 036813ee 2019-05-09 stsp goto done;
5872 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5874 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5875 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5876 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5878 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5880 036813ee 2019-05-09 stsp if (err && *new_te) {
5881 56e0773d 2019-11-28 stsp free(*new_te);
5882 036813ee 2019-05-09 stsp *new_te = NULL;
5884 036813ee 2019-05-09 stsp return err;
5887 036813ee 2019-05-09 stsp static const struct got_error *
5888 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5889 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5891 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5892 102b254e 2020-10-19 stsp char *ct_name = NULL;
5894 9c57c4c6 2024-02-13 thomas *new_te = NULL;
5896 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5897 036813ee 2019-05-09 stsp if (*new_te == NULL)
5898 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5900 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5902 036813ee 2019-05-09 stsp goto done;
5903 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5904 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5905 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5906 036813ee 2019-05-09 stsp goto done;
5909 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5911 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5912 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5913 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5915 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5917 102b254e 2020-10-19 stsp free(ct_name);
5918 036813ee 2019-05-09 stsp if (err && *new_te) {
5919 56e0773d 2019-11-28 stsp free(*new_te);
5920 036813ee 2019-05-09 stsp *new_te = NULL;
5922 036813ee 2019-05-09 stsp return err;
5925 036813ee 2019-05-09 stsp static const struct got_error *
5926 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5927 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5929 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5930 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5932 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5934 036813ee 2019-05-09 stsp return err;
5935 036813ee 2019-05-09 stsp if (new_pe == NULL)
5936 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5937 036813ee 2019-05-09 stsp return NULL;
5940 afa376bf 2019-05-09 stsp static const struct got_error *
5941 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5942 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5944 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5945 5f8a88c6 2019-08-03 stsp unsigned char status;
5947 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5948 ae1e948a 2021-09-28 thomas return NULL;
5950 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5951 afa376bf 2019-05-09 stsp ct_path++;
5953 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5954 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5956 5f8a88c6 2019-08-03 stsp status = ct->status;
5958 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5959 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5962 036813ee 2019-05-09 stsp static const struct got_error *
5963 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5964 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5966 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5967 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5968 44d03001 2019-05-09 stsp char *te_path;
5970 44d03001 2019-05-09 stsp *modified = 0;
5972 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5973 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5974 44d03001 2019-05-09 stsp te->name) == -1)
5975 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5977 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5978 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5979 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5980 44d03001 2019-05-09 stsp strlen(te_path));
5981 62d463ca 2020-10-20 naddy if (*modified)
5985 44d03001 2019-05-09 stsp free(te_path);
5986 44d03001 2019-05-09 stsp return err;
5989 44d03001 2019-05-09 stsp static const struct got_error *
5990 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5991 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5992 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5994 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5995 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5997 036813ee 2019-05-09 stsp *ctp = NULL;
5999 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6000 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6001 036813ee 2019-05-09 stsp char *ct_name = NULL;
6002 036813ee 2019-05-09 stsp int path_matches;
6004 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6005 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
6006 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
6007 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
6008 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6011 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
6012 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
6016 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6019 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6021 036813ee 2019-05-09 stsp return err;
6022 036813ee 2019-05-09 stsp if (!path_matches)
6025 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
6027 d34b633e 2020-10-19 stsp return err;
6029 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
6030 d34b633e 2020-10-19 stsp free(ct_name);
6033 d34b633e 2020-10-19 stsp free(ct_name);
6035 036813ee 2019-05-09 stsp *ctp = ct;
6039 2b496619 2019-07-10 stsp return err;
6042 2b496619 2019-07-10 stsp static const struct got_error *
6043 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6044 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
6045 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
6046 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
6047 2b496619 2019-07-10 stsp struct got_repository *repo)
6049 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
6050 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
6051 2b496619 2019-07-10 stsp char *subtree_path;
6052 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
6053 ba580f68 2020-03-22 stsp int nentries;
6055 2b496619 2019-07-10 stsp *new_tep = NULL;
6057 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6058 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
6059 2b496619 2019-07-10 stsp child_path) == -1)
6060 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
6062 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
6063 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
6064 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
6065 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
6067 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6068 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
6069 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
6070 2b496619 2019-07-10 stsp goto done;
6072 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
6073 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
6074 2b496619 2019-07-10 stsp if (err) {
6075 56e0773d 2019-11-28 stsp free(new_te);
6076 2b496619 2019-07-10 stsp goto done;
6078 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
6081 2b496619 2019-07-10 stsp free(subtree_path);
6082 2b496619 2019-07-10 stsp if (err == NULL)
6083 2b496619 2019-07-10 stsp *new_tep = new_te;
6084 036813ee 2019-05-09 stsp return err;
6087 036813ee 2019-05-09 stsp static const struct got_error *
6088 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
6089 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
6090 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
6091 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6092 036813ee 2019-05-09 stsp struct got_repository *repo)
6094 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
6095 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
6096 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
6097 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
6099 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
6100 ba580f68 2020-03-22 stsp *nentries = 0;
6102 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
6103 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6104 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6105 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
6107 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
6108 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
6109 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
6112 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6113 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
6116 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6117 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
6119 036813ee 2019-05-09 stsp goto done;
6121 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
6122 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
6123 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
6125 036813ee 2019-05-09 stsp goto done;
6126 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
6128 afa376bf 2019-05-09 stsp goto done;
6129 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
6130 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6132 2f51b5b3 2019-05-09 stsp goto done;
6133 ba580f68 2020-03-22 stsp (*nentries)++;
6135 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
6136 2b496619 2019-07-10 stsp if (base_tree == NULL ||
6137 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
6138 2b496619 2019-07-10 stsp == NULL) {
6139 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
6140 2b496619 2019-07-10 stsp child_path, path_base_tree,
6141 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
6144 2b496619 2019-07-10 stsp goto done;
6145 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6147 2b496619 2019-07-10 stsp goto done;
6148 ba580f68 2020-03-22 stsp (*nentries)++;
6153 2f51b5b3 2019-05-09 stsp if (base_tree) {
6154 56e0773d 2019-11-28 stsp int i, nbase_entries;
6155 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
6156 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
6157 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
6158 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
6160 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
6161 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
6162 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
6163 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
6165 63c5ca5d 2019-08-24 stsp goto done;
6166 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
6168 63c5ca5d 2019-08-24 stsp goto done;
6169 ba580f68 2020-03-22 stsp (*nentries)++;
6173 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
6174 44d03001 2019-05-09 stsp int modified;
6175 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6177 036813ee 2019-05-09 stsp goto done;
6178 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
6179 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
6181 2f51b5b3 2019-05-09 stsp goto done;
6182 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
6183 44d03001 2019-05-09 stsp if (modified) {
6184 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
6185 ba580f68 2020-03-22 stsp int nsubentries;
6186 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
6187 ba580f68 2020-03-22 stsp &nsubentries, te,
6188 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
6189 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
6191 44d03001 2019-05-09 stsp goto done;
6192 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6193 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6194 ba580f68 2020-03-22 stsp free(new_id);
6197 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6198 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6199 56e0773d 2019-11-28 stsp free(new_id);
6201 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6203 036813ee 2019-05-09 stsp goto done;
6204 ba580f68 2020-03-22 stsp (*nentries)++;
6208 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6209 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6211 f66c734c 2019-09-22 stsp goto done;
6213 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6214 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6215 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6216 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
6217 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6218 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6219 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6221 2f51b5b3 2019-05-09 stsp goto done;
6222 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6224 2f51b5b3 2019-05-09 stsp goto done;
6225 ba580f68 2020-03-22 stsp (*nentries)++;
6227 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6228 b416585c 2019-05-13 stsp status_arg);
6230 afa376bf 2019-05-09 stsp goto done;
6232 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6233 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6235 2f51b5b3 2019-05-09 stsp goto done;
6236 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6238 2f51b5b3 2019-05-09 stsp goto done;
6239 ba580f68 2020-03-22 stsp (*nentries)++;
6244 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6245 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6247 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6248 2e1fa222 2020-07-23 stsp return err;
6251 2e1fa222 2020-07-23 stsp static const struct got_error *
6252 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6253 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6254 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6255 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6257 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6258 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6259 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6261 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6262 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6263 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6265 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6267 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6268 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6269 437adc9d 2020-12-10 yzhong if (err)
6270 437adc9d 2020-12-10 yzhong goto done;
6273 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6274 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6275 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6276 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6277 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6278 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6279 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6280 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6282 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6283 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6284 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6285 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6286 72fd46fa 2019-09-06 stsp !have_staged_files);
6288 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6289 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6290 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6291 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6292 72fd46fa 2019-09-06 stsp !have_staged_files);
6294 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6296 437adc9d 2020-12-10 yzhong goto done;
6297 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6298 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6299 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6300 3969253a 2020-03-07 stsp if (err) {
6301 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6302 437adc9d 2020-12-10 yzhong goto done;
6304 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6305 3969253a 2020-03-07 stsp if (err) {
6306 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6307 437adc9d 2020-12-10 yzhong goto done;
6310 437adc9d 2020-12-10 yzhong free(relpath);
6311 437adc9d 2020-12-10 yzhong relpath = NULL;
6314 437adc9d 2020-12-10 yzhong free(relpath);
6315 d56d26ce 2019-05-10 stsp return err;
6319 d56d26ce 2019-05-10 stsp static const struct got_error *
6320 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6321 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6322 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6323 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6324 735ef5ac 2019-08-03 stsp int ood_errcode)
6326 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6327 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6328 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6330 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6331 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6332 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6333 1a36436d 2019-06-10 stsp return NULL;
6335 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6336 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6338 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6339 945f9229 2022-04-16 thomas if (err)
6340 945f9229 2022-04-16 thomas goto done;
6341 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6342 9bead371 2019-07-28 stsp if (err) {
6343 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6344 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6345 1a36436d 2019-06-10 stsp goto done;
6346 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6347 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6349 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6350 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6351 945f9229 2022-04-16 thomas if (err)
6352 945f9229 2022-04-16 thomas goto done;
6353 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6354 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6355 1a36436d 2019-06-10 stsp goto done;
6356 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6360 945f9229 2022-04-16 thomas if (commit)
6361 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6362 1a36436d 2019-06-10 stsp return err;
6365 ef20f542 2022-06-26 thomas static const struct got_error *
6366 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6367 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6368 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
6369 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
6370 10604dce 2021-09-24 thomas struct got_worktree *worktree,
6371 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
6372 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6373 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6374 afa376bf 2019-05-09 stsp struct got_repository *repo)
6376 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6377 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6378 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6379 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6380 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6381 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6382 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6383 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6384 10604dce 2021-09-24 thomas int nentries, nparents = 0;
6385 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6386 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6387 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6388 892eab0a 2022-07-22 thomas time_t timestamp;
6390 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6392 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6394 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6396 588edf97 2019-05-10 stsp goto done;
6398 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6400 588edf97 2019-05-10 stsp goto done;
6402 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6403 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
6404 ef899790 2022-11-01 thomas &logmsg, commit_arg);
6409 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6410 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6414 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6415 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6416 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6417 cf066bf8 2019-05-09 stsp char *ondisk_path;
6419 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6420 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6421 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6424 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6425 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6426 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6427 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6430 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6431 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6432 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6433 cf066bf8 2019-05-09 stsp goto done;
6435 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6436 2e1fa222 2020-07-23 stsp free(ondisk_path);
6438 cf066bf8 2019-05-09 stsp goto done;
6441 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6442 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6443 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6445 036813ee 2019-05-09 stsp goto done;
6447 538aebb5 2023-04-22 thomas err = got_object_qid_alloc(&pid, head_commit_id);
6449 de18fc63 2019-05-09 stsp goto done;
6450 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6451 10604dce 2021-09-24 thomas nparents++;
6452 10604dce 2021-09-24 thomas if (parent_id2) {
6453 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6454 10604dce 2021-09-24 thomas if (err)
6455 10604dce 2021-09-24 thomas goto done;
6456 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6457 10604dce 2021-09-24 thomas nparents++;
6459 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6460 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6461 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6462 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6463 33ad4cbe 2019-05-12 jcs free(logmsg);
6465 9d40349a 2019-05-09 stsp goto done;
6467 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6468 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6469 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6470 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6471 09f5bd90 2019-05-09 stsp goto done;
6473 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6474 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6476 09f5bd90 2019-05-09 stsp goto done;
6477 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6479 ebf99748 2019-05-09 stsp goto done;
6480 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6481 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6482 09f5bd90 2019-05-09 stsp goto done;
6484 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6485 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6487 f2c16586 2019-05-09 stsp goto done;
6488 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6490 f2c16586 2019-05-09 stsp goto done;
6492 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6494 09f5bd90 2019-05-09 stsp goto done;
6496 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6498 ebf99748 2019-05-09 stsp goto done;
6500 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6501 588edf97 2019-05-10 stsp if (head_tree)
6502 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6503 588edf97 2019-05-10 stsp if (head_commit)
6504 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6505 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6506 2f17228e 2019-05-12 stsp if (head_ref2) {
6507 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6508 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6509 2f17228e 2019-05-12 stsp err = unlockerr;
6510 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6512 39cd0ff6 2019-07-12 stsp return err;
6515 5c1e53bc 2019-07-28 stsp static const struct got_error *
6516 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6517 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6519 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6520 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6522 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6523 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6524 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6526 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6527 5c1e53bc 2019-07-28 stsp ct_path++;
6529 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6530 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6534 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6535 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6537 5c1e53bc 2019-07-28 stsp return NULL;
6540 f0b75401 2019-08-03 stsp static const struct got_error *
6541 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6543 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6545 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6546 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6547 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6550 f0b75401 2019-08-03 stsp return NULL;
6553 5f8a88c6 2019-08-03 stsp static const struct got_error *
6554 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6555 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6557 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6558 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6560 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6561 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6563 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6564 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6565 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6566 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6567 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6568 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6571 5f8a88c6 2019-08-03 stsp return NULL;
6574 39cd0ff6 2019-07-12 stsp const struct got_error *
6575 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6576 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6577 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6578 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6579 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6580 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6581 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6583 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6584 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6585 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6586 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6587 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6588 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6589 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6590 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6591 ef899790 2022-11-01 thomas char *diff_path = NULL;
6592 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6594 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6596 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6597 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6599 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6601 39cd0ff6 2019-07-12 stsp goto done;
6603 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6605 39cd0ff6 2019-07-12 stsp goto done;
6607 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6609 39cd0ff6 2019-07-12 stsp goto done;
6611 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6613 39cd0ff6 2019-07-12 stsp goto done;
6615 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6616 5f8a88c6 2019-08-03 stsp &have_staged_files);
6617 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6618 5f8a88c6 2019-08-03 stsp goto done;
6619 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6620 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6622 5f8a88c6 2019-08-03 stsp goto done;
6625 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6626 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6627 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6628 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6629 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6630 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6631 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6632 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6633 ef899790 2022-11-01 thomas if (show_diff) {
6634 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6635 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6636 ef899790 2022-11-01 thomas if (err)
6637 ef899790 2022-11-01 thomas goto done;
6638 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6639 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6640 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6641 ef899790 2022-11-01 thomas goto done;
6643 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6644 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6645 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6646 ef899790 2022-11-01 thomas goto done;
6650 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6651 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6652 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6654 5c1e53bc 2019-07-28 stsp goto done;
6657 ef899790 2022-11-01 thomas if (show_diff) {
6658 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6659 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6660 ef899790 2022-11-01 thomas goto done;
6664 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6665 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6666 39cd0ff6 2019-07-12 stsp goto done;
6669 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6670 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6672 5c1e53bc 2019-07-28 stsp goto done;
6675 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6676 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6677 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6679 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6680 f0b75401 2019-08-03 stsp ct_path++;
6681 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6682 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6683 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6685 b50cabdf 2019-07-12 stsp goto done;
6689 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6690 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6691 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6692 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6694 39cd0ff6 2019-07-12 stsp goto done;
6696 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6697 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6698 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6699 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6700 39cd0ff6 2019-07-12 stsp err = sync_err;
6702 39cd0ff6 2019-07-12 stsp if (fileindex)
6703 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6704 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6705 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6706 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6707 39cd0ff6 2019-07-12 stsp err = unlockerr;
6708 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6709 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6711 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6713 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6714 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6715 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6716 ef899790 2022-11-01 thomas free(diff_path);
6717 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6718 ef899790 2022-11-01 thomas err == NULL)
6719 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6720 c4296144 2019-05-09 stsp return err;
6723 8656d6c4 2019-05-20 stsp const char *
6724 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6726 8656d6c4 2019-05-20 stsp return ct->path;
6729 8656d6c4 2019-05-20 stsp unsigned int
6730 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6732 8656d6c4 2019-05-20 stsp return ct->status;
6735 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6736 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6737 818c7501 2019-07-11 stsp struct got_repository *repo;
6740 818c7501 2019-07-11 stsp static const struct got_error *
6741 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6743 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6744 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6745 818c7501 2019-07-11 stsp unsigned char status;
6746 818c7501 2019-07-11 stsp struct stat sb;
6747 818c7501 2019-07-11 stsp char *ondisk_path;
6749 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6750 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6751 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6752 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6754 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6756 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6758 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6759 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6760 818c7501 2019-07-11 stsp free(ondisk_path);
6762 818c7501 2019-07-11 stsp return err;
6764 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6765 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6766 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6767 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6769 818c7501 2019-07-11 stsp return NULL;
6772 818c7501 2019-07-11 stsp const struct got_error *
6773 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6774 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6775 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6776 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6778 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6779 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6780 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6781 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6782 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6783 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6784 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6786 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6787 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6788 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6790 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6792 818c7501 2019-07-11 stsp return err;
6794 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6796 818c7501 2019-07-11 stsp goto done;
6798 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6799 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6800 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6803 818c7501 2019-07-11 stsp goto done;
6805 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6807 818c7501 2019-07-11 stsp goto done;
6809 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6811 818c7501 2019-07-11 stsp goto done;
6813 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6815 818c7501 2019-07-11 stsp goto done;
6817 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6820 e51d7b55 2020-01-04 stsp goto done;
6822 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6824 818c7501 2019-07-11 stsp goto done;
6825 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6826 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6827 e51d7b55 2020-01-04 stsp goto done;
6830 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6831 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6833 818c7501 2019-07-11 stsp goto done;
6834 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6836 818c7501 2019-07-11 stsp goto done;
6838 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6840 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6842 818c7501 2019-07-11 stsp goto done;
6844 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6846 818c7501 2019-07-11 stsp goto done;
6848 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6849 818c7501 2019-07-11 stsp worktree->base_commit_id);
6851 818c7501 2019-07-11 stsp goto done;
6852 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6854 818c7501 2019-07-11 stsp goto done;
6856 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6858 818c7501 2019-07-11 stsp goto done;
6860 818c7501 2019-07-11 stsp free(fileindex_path);
6861 818c7501 2019-07-11 stsp free(tmp_branch_name);
6862 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6863 818c7501 2019-07-11 stsp free(branch_ref_name);
6864 818c7501 2019-07-11 stsp if (branch_ref)
6865 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6866 818c7501 2019-07-11 stsp if (wt_branch)
6867 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6868 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6869 818c7501 2019-07-11 stsp if (err) {
6870 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6871 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6872 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6874 818c7501 2019-07-11 stsp if (*tmp_branch) {
6875 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6876 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6878 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6879 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6880 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6882 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6884 818c7501 2019-07-11 stsp return err;
6887 818c7501 2019-07-11 stsp const struct got_error *
6888 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6889 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6890 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6891 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6893 818c7501 2019-07-11 stsp const struct got_error *err;
6894 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6895 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6896 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6897 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6898 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6900 818c7501 2019-07-11 stsp *commit_id = NULL;
6901 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6902 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6903 3e3a69f1 2019-07-25 stsp *branch = NULL;
6904 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6906 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6908 3e3a69f1 2019-07-25 stsp return err;
6910 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6912 f032f1f7 2019-08-04 stsp goto done;
6914 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6915 f032f1f7 2019-08-04 stsp &have_staged_files);
6916 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6917 f032f1f7 2019-08-04 stsp goto done;
6918 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6919 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6920 3e3a69f1 2019-07-25 stsp goto done;
6923 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6925 f032f1f7 2019-08-04 stsp goto done;
6927 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6929 818c7501 2019-07-11 stsp goto done;
6931 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6933 818c7501 2019-07-11 stsp goto done;
6935 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6937 818c7501 2019-07-11 stsp goto done;
6939 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6941 818c7501 2019-07-11 stsp goto done;
6943 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6944 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6946 818c7501 2019-07-11 stsp goto done;
6948 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6950 818c7501 2019-07-11 stsp goto done;
6952 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6954 818c7501 2019-07-11 stsp goto done;
6956 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6957 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6959 818c7501 2019-07-11 stsp goto done;
6961 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6963 818c7501 2019-07-11 stsp goto done;
6965 818c7501 2019-07-11 stsp free(commit_ref_name);
6966 818c7501 2019-07-11 stsp free(branch_ref_name);
6967 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6968 818c7501 2019-07-11 stsp if (commit_ref)
6969 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6970 818c7501 2019-07-11 stsp if (branch_ref)
6971 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6972 818c7501 2019-07-11 stsp if (err) {
6973 818c7501 2019-07-11 stsp free(*commit_id);
6974 818c7501 2019-07-11 stsp *commit_id = NULL;
6975 818c7501 2019-07-11 stsp if (*tmp_branch) {
6976 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6977 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6979 818c7501 2019-07-11 stsp if (*new_base_branch) {
6980 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6981 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6983 818c7501 2019-07-11 stsp if (*branch) {
6984 818c7501 2019-07-11 stsp got_ref_close(*branch);
6985 818c7501 2019-07-11 stsp *branch = NULL;
6987 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6988 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6989 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6991 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6993 818c7501 2019-07-11 stsp return err;
6996 818c7501 2019-07-11 stsp const struct got_error *
6997 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6999 818c7501 2019-07-11 stsp const struct got_error *err;
7000 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
7002 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7004 818c7501 2019-07-11 stsp return err;
7006 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7007 818c7501 2019-07-11 stsp free(tmp_branch_name);
7008 818c7501 2019-07-11 stsp return NULL;
7011 818c7501 2019-07-11 stsp static const struct got_error *
7012 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7013 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
7015 0ebf8283 2019-07-24 stsp *logmsg = arg;
7016 818c7501 2019-07-11 stsp return NULL;
7019 818c7501 2019-07-11 stsp static const struct got_error *
7020 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7021 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7022 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7023 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7025 818c7501 2019-07-11 stsp return NULL;
7028 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
7029 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
7030 01757395 2019-07-12 stsp void *progress_arg;
7031 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
7034 01757395 2019-07-12 stsp static const struct got_error *
7035 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
7037 01757395 2019-07-12 stsp const struct got_error *err;
7038 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
7040 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
7042 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
7044 01757395 2019-07-12 stsp return err;
7046 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
7047 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
7048 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
7049 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
7050 01757395 2019-07-12 stsp return NULL;
7052 01757395 2019-07-12 stsp p = strdup(path);
7053 01757395 2019-07-12 stsp if (p == NULL)
7054 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
7056 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7057 01757395 2019-07-12 stsp if (err || new == NULL)
7059 01757395 2019-07-12 stsp return err;
7062 0ebf8283 2019-07-24 stsp static const struct got_error *
7063 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7064 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
7066 818c7501 2019-07-11 stsp const struct got_error *err;
7067 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
7069 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7070 818c7501 2019-07-11 stsp if (err) {
7071 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
7072 818c7501 2019-07-11 stsp goto done;
7073 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7075 818c7501 2019-07-11 stsp goto done;
7076 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
7078 818c7501 2019-07-11 stsp goto done;
7079 de05890f 2020-03-05 stsp } else if (is_rebase) {
7080 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
7083 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
7085 818c7501 2019-07-11 stsp goto done;
7086 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
7087 818c7501 2019-07-11 stsp free(stored_id);
7088 818c7501 2019-07-11 stsp if (cmp != 0) {
7089 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7090 818c7501 2019-07-11 stsp goto done;
7094 0ebf8283 2019-07-24 stsp if (commit_ref)
7095 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7096 0ebf8283 2019-07-24 stsp return err;
7099 0ebf8283 2019-07-24 stsp static const struct got_error *
7100 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
7101 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
7102 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7103 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
7104 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7105 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7107 0ebf8283 2019-07-24 stsp const struct got_error *err;
7108 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7109 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
7110 3e3a69f1 2019-07-25 stsp char *fileindex_path;
7112 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7114 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7116 0ebf8283 2019-07-24 stsp return err;
7118 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
7119 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
7120 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
7121 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
7122 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
7123 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
7124 818c7501 2019-07-11 stsp if (commit_ref)
7125 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
7126 818c7501 2019-07-11 stsp return err;
7129 818c7501 2019-07-11 stsp const struct got_error *
7130 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7131 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7132 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7133 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7134 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7135 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7137 0ebf8283 2019-07-24 stsp const struct got_error *err;
7138 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7140 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7142 0ebf8283 2019-07-24 stsp return err;
7144 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7146 0ebf8283 2019-07-24 stsp goto done;
7148 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7149 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7150 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7152 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7153 0ebf8283 2019-07-24 stsp return err;
7156 0ebf8283 2019-07-24 stsp const struct got_error *
7157 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7158 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7159 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7160 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7161 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7162 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7164 0ebf8283 2019-07-24 stsp const struct got_error *err;
7165 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7167 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7169 0ebf8283 2019-07-24 stsp return err;
7171 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7173 0ebf8283 2019-07-24 stsp goto done;
7175 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7176 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7177 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7179 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7180 0ebf8283 2019-07-24 stsp return err;
7183 0ebf8283 2019-07-24 stsp static const struct got_error *
7184 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
7185 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7186 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7187 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
7188 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
7189 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7191 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7192 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7193 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7194 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7195 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7196 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7197 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7199 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7200 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7201 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7203 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7205 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7207 3e3a69f1 2019-07-25 stsp return err;
7209 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7210 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7211 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7212 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7213 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
7215 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7216 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7218 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7219 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7220 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7221 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7222 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7223 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7225 01757395 2019-07-12 stsp if (merged_paths) {
7226 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7227 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7228 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7229 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7232 01757395 2019-07-12 stsp goto done;
7235 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7236 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7238 01757395 2019-07-12 stsp goto done;
7241 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7242 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7243 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7245 a0e95631 2019-07-12 stsp goto done;
7246 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7247 a0e95631 2019-07-12 stsp goto done;
7250 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7252 edd02c5e 2019-07-11 stsp goto done;
7254 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7256 818c7501 2019-07-11 stsp goto done;
7258 5943eee2 2019-08-13 stsp if (new_logmsg) {
7259 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7260 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7261 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7262 5943eee2 2019-08-13 stsp goto done;
7265 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7267 5943eee2 2019-08-13 stsp goto done;
7270 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7271 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7272 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
7273 8db00f97 2022-07-22 thomas committer ? committer :
7274 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
7275 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7277 a0e95631 2019-07-12 stsp goto done;
7279 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7281 a0e95631 2019-07-12 stsp goto done;
7283 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7285 a0e95631 2019-07-12 stsp goto done;
7287 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7288 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7289 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7290 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7291 a0e95631 2019-07-12 stsp err = sync_err;
7293 a0e95631 2019-07-12 stsp free(fileindex_path);
7294 a0e95631 2019-07-12 stsp free(head_commit_id);
7295 a0e95631 2019-07-12 stsp if (head_ref)
7296 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7297 818c7501 2019-07-11 stsp if (err) {
7298 818c7501 2019-07-11 stsp free(*new_commit_id);
7299 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7301 818c7501 2019-07-11 stsp return err;
7304 818c7501 2019-07-11 stsp const struct got_error *
7305 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7306 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7307 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7308 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7309 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
7310 be94c032 2023-02-20 thomas struct got_repository *repo)
7312 0ebf8283 2019-07-24 stsp const struct got_error *err;
7313 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7314 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7315 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7317 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7319 0ebf8283 2019-07-24 stsp return err;
7321 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7323 0ebf8283 2019-07-24 stsp goto done;
7324 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7326 0ebf8283 2019-07-24 stsp goto done;
7327 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7328 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7329 0ebf8283 2019-07-24 stsp goto done;
7332 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7333 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7334 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
7336 0ebf8283 2019-07-24 stsp if (commit_ref)
7337 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7338 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7339 0ebf8283 2019-07-24 stsp free(commit_id);
7340 0ebf8283 2019-07-24 stsp return err;
7343 0ebf8283 2019-07-24 stsp const struct got_error *
7344 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7345 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7346 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7347 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7348 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7349 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7351 0ebf8283 2019-07-24 stsp const struct got_error *err;
7352 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7353 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7355 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7357 0ebf8283 2019-07-24 stsp return err;
7359 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7361 0ebf8283 2019-07-24 stsp goto done;
7363 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7364 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7365 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
7367 0ebf8283 2019-07-24 stsp if (commit_ref)
7368 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7369 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7370 0ebf8283 2019-07-24 stsp return err;
7373 0ebf8283 2019-07-24 stsp const struct got_error *
7374 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7375 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7377 3e3a69f1 2019-07-25 stsp if (fileindex)
7378 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7379 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7382 69844fba 2019-07-11 stsp static const struct got_error *
7383 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7385 69844fba 2019-07-11 stsp const struct got_error *err;
7386 69844fba 2019-07-11 stsp struct got_reference *ref;
7388 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7389 69844fba 2019-07-11 stsp if (err) {
7390 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7391 69844fba 2019-07-11 stsp return NULL;
7392 69844fba 2019-07-11 stsp return err;
7395 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7396 69844fba 2019-07-11 stsp got_ref_close(ref);
7397 69844fba 2019-07-11 stsp return err;
7400 69844fba 2019-07-11 stsp static const struct got_error *
7401 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7403 69844fba 2019-07-11 stsp const struct got_error *err;
7404 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7405 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7407 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7409 69844fba 2019-07-11 stsp goto done;
7410 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7412 69844fba 2019-07-11 stsp goto done;
7414 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7416 69844fba 2019-07-11 stsp goto done;
7417 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7419 69844fba 2019-07-11 stsp goto done;
7421 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7423 69844fba 2019-07-11 stsp goto done;
7424 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7426 69844fba 2019-07-11 stsp goto done;
7428 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7430 69844fba 2019-07-11 stsp goto done;
7431 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7433 69844fba 2019-07-11 stsp goto done;
7436 69844fba 2019-07-11 stsp free(tmp_branch_name);
7437 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7438 69844fba 2019-07-11 stsp free(branch_ref_name);
7439 69844fba 2019-07-11 stsp free(commit_ref_name);
7440 e600f124 2021-03-21 stsp return err;
7443 ef20f542 2022-06-26 thomas static const struct got_error *
7444 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7445 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7447 e600f124 2021-03-21 stsp const struct got_error *err;
7448 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7449 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7450 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7451 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7452 e600f124 2021-03-21 stsp char *refname = NULL;
7454 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7455 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7456 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7457 e600f124 2021-03-21 stsp branch_name += 11;
7459 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7461 e600f124 2021-03-21 stsp return err;
7463 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7464 e600f124 2021-03-21 stsp new_id_str) == -1) {
7465 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7466 e600f124 2021-03-21 stsp goto done;
7469 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7471 e600f124 2021-03-21 stsp goto done;
7473 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7475 e600f124 2021-03-21 stsp goto done;
7477 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7479 e600f124 2021-03-21 stsp free(new_id_str);
7480 e600f124 2021-03-21 stsp free(refname);
7481 e600f124 2021-03-21 stsp free(old_commit_id);
7483 e600f124 2021-03-21 stsp got_ref_close(ref);
7484 69844fba 2019-07-11 stsp return err;
7487 818c7501 2019-07-11 stsp const struct got_error *
7488 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7489 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7490 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7491 f08f28be 2023-02-03 thomas int create_backup)
7493 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7494 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7495 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7497 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7499 818c7501 2019-07-11 stsp return err;
7501 e600f124 2021-03-21 stsp if (create_backup) {
7502 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7503 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7505 e600f124 2021-03-21 stsp goto done;
7508 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7510 818c7501 2019-07-11 stsp goto done;
7512 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7514 818c7501 2019-07-11 stsp goto done;
7516 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7518 818c7501 2019-07-11 stsp goto done;
7520 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7522 a615e0e7 2020-12-16 stsp goto done;
7524 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7526 a615e0e7 2020-12-16 stsp goto done;
7527 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7528 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7529 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7530 a615e0e7 2020-12-16 stsp err = sync_err;
7532 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7533 a615e0e7 2020-12-16 stsp free(fileindex_path);
7534 818c7501 2019-07-11 stsp free(new_head_commit_id);
7535 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7536 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7537 818c7501 2019-07-11 stsp err = unlockerr;
7538 818c7501 2019-07-11 stsp return err;
7541 8641a332 2023-04-14 thomas static const struct got_error *
7542 8641a332 2023-04-14 thomas get_paths_changed_between_commits(struct got_pathlist_head *paths,
7543 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7544 8641a332 2023-04-14 thomas struct got_repository *repo)
7546 8641a332 2023-04-14 thomas const struct got_error *err;
7547 8641a332 2023-04-14 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7548 8641a332 2023-04-14 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7550 8641a332 2023-04-14 thomas if (id1) {
7551 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit1, repo, id1);
7552 8641a332 2023-04-14 thomas if (err)
7553 8641a332 2023-04-14 thomas goto done;
7555 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree1, repo,
7556 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit1));
7557 8641a332 2023-04-14 thomas if (err)
7558 8641a332 2023-04-14 thomas goto done;
7561 8641a332 2023-04-14 thomas if (id2) {
7562 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit2, repo, id2);
7563 8641a332 2023-04-14 thomas if (err)
7564 8641a332 2023-04-14 thomas goto done;
7566 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree2, repo,
7567 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit2));
7568 8641a332 2023-04-14 thomas if (err)
7569 8641a332 2023-04-14 thomas goto done;
7572 8641a332 2023-04-14 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7573 8641a332 2023-04-14 thomas got_diff_tree_collect_changed_paths, paths, 0);
7574 8641a332 2023-04-14 thomas if (err)
7575 8641a332 2023-04-14 thomas goto done;
7577 8641a332 2023-04-14 thomas if (commit1)
7578 8641a332 2023-04-14 thomas got_object_commit_close(commit1);
7579 8641a332 2023-04-14 thomas if (commit2)
7580 8641a332 2023-04-14 thomas got_object_commit_close(commit2);
7581 8641a332 2023-04-14 thomas if (tree1)
7582 8641a332 2023-04-14 thomas got_object_tree_close(tree1);
7583 8641a332 2023-04-14 thomas if (tree2)
7584 8641a332 2023-04-14 thomas got_object_tree_close(tree2);
7585 8641a332 2023-04-14 thomas return err;
7588 8641a332 2023-04-14 thomas static const struct got_error *
7589 8641a332 2023-04-14 thomas get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7590 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7591 8641a332 2023-04-14 thomas const char *path_prefix, struct got_repository *repo)
7593 8641a332 2023-04-14 thomas const struct got_error *err;
7594 8641a332 2023-04-14 thomas struct got_pathlist_head merged_paths;
7595 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
7596 8641a332 2023-04-14 thomas char *abspath = NULL, *wt_path = NULL;
7598 8641a332 2023-04-14 thomas TAILQ_INIT(&merged_paths);
7600 8641a332 2023-04-14 thomas err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7601 8641a332 2023-04-14 thomas if (err)
7602 8641a332 2023-04-14 thomas goto done;
7604 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, &merged_paths, entry) {
7605 8641a332 2023-04-14 thomas struct got_diff_changed_path *change = pe->data;
7607 8641a332 2023-04-14 thomas if (change->status != GOT_STATUS_ADD)
7608 8641a332 2023-04-14 thomas continue;
7610 8641a332 2023-04-14 thomas if (got_path_is_root_dir(path_prefix)) {
7611 8641a332 2023-04-14 thomas wt_path = strdup(pe->path);
7612 8641a332 2023-04-14 thomas if (wt_path == NULL) {
7613 8641a332 2023-04-14 thomas err = got_error_from_errno("strdup");
7614 8641a332 2023-04-14 thomas goto done;
7616 8641a332 2023-04-14 thomas } else {
7617 8641a332 2023-04-14 thomas if (asprintf(&abspath, "/%s", pe->path) == -1) {
7618 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
7619 8641a332 2023-04-14 thomas goto done;
7622 8641a332 2023-04-14 thomas err = got_path_skip_common_ancestor(&wt_path,
7623 8641a332 2023-04-14 thomas path_prefix, abspath);
7624 8641a332 2023-04-14 thomas if (err)
7625 8641a332 2023-04-14 thomas goto done;
7626 8641a332 2023-04-14 thomas free(abspath);
7627 8641a332 2023-04-14 thomas abspath = NULL;
7630 8641a332 2023-04-14 thomas err = got_pathlist_append(added_paths, wt_path, NULL);
7631 8641a332 2023-04-14 thomas if (err)
7632 8641a332 2023-04-14 thomas goto done;
7633 8641a332 2023-04-14 thomas wt_path = NULL;
7637 8641a332 2023-04-14 thomas got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7638 8641a332 2023-04-14 thomas free(abspath);
7639 8641a332 2023-04-14 thomas free(wt_path);
7640 8641a332 2023-04-14 thomas return err;
7643 8641a332 2023-04-14 thomas static const struct got_error *
7644 8641a332 2023-04-14 thomas get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7645 8641a332 2023-04-14 thomas struct got_object_id *id, const char *path_prefix,
7646 8641a332 2023-04-14 thomas struct got_repository *repo)
7648 8641a332 2023-04-14 thomas const struct got_error *err;
7649 8641a332 2023-04-14 thomas struct got_commit_object *commit = NULL;
7650 8641a332 2023-04-14 thomas struct got_object_qid *pid;
7652 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo, id);
7653 8641a332 2023-04-14 thomas if (err)
7654 8641a332 2023-04-14 thomas goto done;
7656 8641a332 2023-04-14 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7658 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(added_paths,
7659 8641a332 2023-04-14 thomas pid ? &pid->id : NULL, id, path_prefix, repo);
7660 8641a332 2023-04-14 thomas if (err)
7661 8641a332 2023-04-14 thomas goto done;
7663 8641a332 2023-04-14 thomas if (commit)
7664 8641a332 2023-04-14 thomas got_object_commit_close(commit);
7665 8641a332 2023-04-14 thomas return err;
7668 818c7501 2019-07-11 stsp const struct got_error *
7669 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7670 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7671 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7672 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7674 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7675 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7676 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7677 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7678 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7679 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7680 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7681 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7682 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7683 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7684 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7686 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7688 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7690 818c7501 2019-07-11 stsp return err;
7692 8641a332 2023-04-14 thomas err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7693 8641a332 2023-04-14 thomas if (err)
7694 8641a332 2023-04-14 thomas goto done;
7696 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7697 8641a332 2023-04-14 thomas if (err)
7698 8641a332 2023-04-14 thomas goto done;
7700 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7701 8641a332 2023-04-14 thomas if (err)
7702 8641a332 2023-04-14 thomas goto done;
7705 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7706 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7707 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
7708 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
7710 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7711 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7712 8641a332 2023-04-14 thomas if (err)
7713 8641a332 2023-04-14 thomas goto done;
7715 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7716 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7718 818c7501 2019-07-11 stsp goto done;
7720 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7722 818c7501 2019-07-11 stsp goto done;
7725 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7726 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7727 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7729 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7731 818c7501 2019-07-11 stsp goto done;
7733 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7734 25a102ed 2023-04-14 thomas if (err)
7735 25a102ed 2023-04-14 thomas goto done;
7737 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7738 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7740 818c7501 2019-07-11 stsp goto done;
7742 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7743 945f9229 2022-04-16 thomas worktree->path_prefix);
7745 ca355955 2019-07-12 stsp goto done;
7747 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7749 ca355955 2019-07-12 stsp goto done;
7751 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7753 818c7501 2019-07-11 stsp goto done;
7755 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7756 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7757 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7758 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7759 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7760 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7761 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7762 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
7763 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
7764 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7765 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7767 1f1abb7e 2019-08-08 stsp goto sync;
7769 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7770 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7772 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7773 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7774 ca355955 2019-07-12 stsp err = sync_err;
7776 8641a332 2023-04-14 thomas got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7777 818c7501 2019-07-11 stsp got_ref_close(resolved);
7778 a3a2faf2 2019-07-12 stsp free(tree_id);
7779 818c7501 2019-07-11 stsp free(commit_id);
7780 8641a332 2023-04-14 thomas free(merged_commit_id);
7781 945f9229 2022-04-16 thomas if (commit)
7782 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7783 0ebf8283 2019-07-24 stsp if (fileindex)
7784 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7785 0ebf8283 2019-07-24 stsp free(fileindex_path);
7786 8641a332 2023-04-14 thomas free(commit_ref_name);
7787 8641a332 2023-04-14 thomas if (commit_ref)
7788 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
7790 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7791 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7792 0ebf8283 2019-07-24 stsp err = unlockerr;
7793 0ebf8283 2019-07-24 stsp return err;
7796 0ebf8283 2019-07-24 stsp const struct got_error *
7797 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7798 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7799 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7800 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7802 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7803 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7804 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7805 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7806 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7807 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7808 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7809 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7811 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7812 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7813 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7814 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7816 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7818 0ebf8283 2019-07-24 stsp return err;
7820 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7822 0ebf8283 2019-07-24 stsp goto done;
7824 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7825 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7826 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7829 0ebf8283 2019-07-24 stsp goto done;
7831 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7833 0ebf8283 2019-07-24 stsp goto done;
7835 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7837 0ebf8283 2019-07-24 stsp goto done;
7839 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7840 0ebf8283 2019-07-24 stsp worktree);
7842 0ebf8283 2019-07-24 stsp goto done;
7844 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7847 0ebf8283 2019-07-24 stsp goto done;
7849 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7851 0ebf8283 2019-07-24 stsp goto done;
7853 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7855 0ebf8283 2019-07-24 stsp goto done;
7857 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7858 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7860 0ebf8283 2019-07-24 stsp goto done;
7861 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7863 0ebf8283 2019-07-24 stsp goto done;
7864 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7865 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7866 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7867 0ebf8283 2019-07-24 stsp goto done;
7870 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7871 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7873 0ebf8283 2019-07-24 stsp goto done;
7874 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7876 0ebf8283 2019-07-24 stsp goto done;
7878 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7880 0ebf8283 2019-07-24 stsp goto done;
7882 0ebf8283 2019-07-24 stsp free(fileindex_path);
7883 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7884 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7885 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7886 0ebf8283 2019-07-24 stsp if (wt_branch)
7887 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7888 0ebf8283 2019-07-24 stsp if (err) {
7889 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7890 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7891 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7893 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7894 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7895 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7897 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7898 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7899 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7900 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7902 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7904 0ebf8283 2019-07-24 stsp return err;
7907 0ebf8283 2019-07-24 stsp const struct got_error *
7908 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7909 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7911 3e3a69f1 2019-07-25 stsp if (fileindex)
7912 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7913 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7916 0ebf8283 2019-07-24 stsp const struct got_error *
7917 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7918 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7920 0ebf8283 2019-07-24 stsp const struct got_error *err;
7921 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7923 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7925 0ebf8283 2019-07-24 stsp return err;
7927 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7928 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7929 0ebf8283 2019-07-24 stsp return NULL;
7932 0ebf8283 2019-07-24 stsp const struct got_error *
7933 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7934 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7935 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7936 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7938 0ebf8283 2019-07-24 stsp const struct got_error *err;
7939 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7940 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7941 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7942 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7943 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7944 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7946 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7947 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7948 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7949 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7951 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7953 3e3a69f1 2019-07-25 stsp return err;
7955 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7957 f032f1f7 2019-08-04 stsp goto done;
7959 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7960 f032f1f7 2019-08-04 stsp &have_staged_files);
7961 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7962 f032f1f7 2019-08-04 stsp goto done;
7963 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7964 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7965 3e3a69f1 2019-07-25 stsp goto done;
7968 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7970 f032f1f7 2019-08-04 stsp goto done;
7972 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7974 0ebf8283 2019-07-24 stsp goto done;
7976 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7978 0ebf8283 2019-07-24 stsp goto done;
7980 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7981 0ebf8283 2019-07-24 stsp worktree);
7983 0ebf8283 2019-07-24 stsp goto done;
7985 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7987 0ebf8283 2019-07-24 stsp goto done;
7989 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7991 0ebf8283 2019-07-24 stsp goto done;
7992 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7994 0ebf8283 2019-07-24 stsp goto done;
7996 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7998 0ebf8283 2019-07-24 stsp goto done;
7999 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
8001 0ebf8283 2019-07-24 stsp goto done;
8003 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
8005 0ebf8283 2019-07-24 stsp goto done;
8007 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8008 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8009 3e3a69f1 2019-07-25 stsp free(fileindex_path);
8010 0ebf8283 2019-07-24 stsp if (commit_ref)
8011 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
8012 0ebf8283 2019-07-24 stsp if (base_commit_ref)
8013 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
8014 0ebf8283 2019-07-24 stsp if (err) {
8015 0ebf8283 2019-07-24 stsp free(*commit_id);
8016 0ebf8283 2019-07-24 stsp *commit_id = NULL;
8017 0ebf8283 2019-07-24 stsp free(*base_commit_id);
8018 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
8019 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
8020 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
8021 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
8023 3e3a69f1 2019-07-25 stsp if (*fileindex) {
8024 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
8025 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
8027 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
8029 0ebf8283 2019-07-24 stsp return err;
8032 0ebf8283 2019-07-24 stsp static const struct got_error *
8033 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8035 0ebf8283 2019-07-24 stsp const struct got_error *err;
8036 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8037 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
8039 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8041 0ebf8283 2019-07-24 stsp goto done;
8042 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
8044 0ebf8283 2019-07-24 stsp goto done;
8046 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8047 0ebf8283 2019-07-24 stsp worktree);
8049 0ebf8283 2019-07-24 stsp goto done;
8050 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
8052 0ebf8283 2019-07-24 stsp goto done;
8054 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8056 0ebf8283 2019-07-24 stsp goto done;
8057 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
8059 0ebf8283 2019-07-24 stsp goto done;
8061 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8063 0ebf8283 2019-07-24 stsp goto done;
8064 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8066 0ebf8283 2019-07-24 stsp goto done;
8068 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
8069 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
8070 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8071 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8072 0ebf8283 2019-07-24 stsp return err;
8075 0ebf8283 2019-07-24 stsp const struct got_error *
8076 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
8077 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8078 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
8079 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8081 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8082 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8083 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
8084 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
8085 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8086 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
8087 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
8088 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
8089 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
8090 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
8092 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
8094 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
8096 0ebf8283 2019-07-24 stsp return err;
8098 8641a332 2023-04-14 thomas err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8099 8641a332 2023-04-14 thomas if (err)
8100 8641a332 2023-04-14 thomas goto done;
8102 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8103 8641a332 2023-04-14 thomas if (err) {
8104 8641a332 2023-04-14 thomas if (err->code != GOT_ERR_NOT_REF)
8105 8641a332 2023-04-14 thomas goto done;
8106 8641a332 2023-04-14 thomas /* Can happen on early abort due to invalid histedit script. */
8107 8641a332 2023-04-14 thomas commit_ref = NULL;
8110 8641a332 2023-04-14 thomas if (commit_ref) {
8111 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8112 8641a332 2023-04-14 thomas if (err)
8113 8641a332 2023-04-14 thomas goto done;
8116 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
8117 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
8118 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files added by the
8119 8641a332 2023-04-14 thomas * user during conflict resolution or during histedit -e.
8121 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8122 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
8123 8641a332 2023-04-14 thomas if (err)
8124 8641a332 2023-04-14 thomas goto done;
8127 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8128 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
8130 0ebf8283 2019-07-24 stsp goto done;
8132 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8134 0ebf8283 2019-07-24 stsp goto done;
8136 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8138 0ebf8283 2019-07-24 stsp goto done;
8140 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
8141 25a102ed 2023-04-14 thomas worktree->base_commit_id);
8142 25a102ed 2023-04-14 thomas if (err)
8143 25a102ed 2023-04-14 thomas goto done;
8145 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8146 0ebf8283 2019-07-24 stsp worktree->path_prefix);
8148 0ebf8283 2019-07-24 stsp goto done;
8150 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8152 0ebf8283 2019-07-24 stsp goto done;
8154 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
8156 0ebf8283 2019-07-24 stsp goto done;
8158 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
8159 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
8160 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
8161 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
8162 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
8163 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
8164 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
8165 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
8166 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8167 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8168 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8170 1f1abb7e 2019-08-08 stsp goto sync;
8172 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8173 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8175 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8176 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
8177 0ebf8283 2019-07-24 stsp err = sync_err;
8179 8641a332 2023-04-14 thomas if (resolved)
8180 8641a332 2023-04-14 thomas got_ref_close(resolved);
8181 8641a332 2023-04-14 thomas if (commit_ref)
8182 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8183 8641a332 2023-04-14 thomas free(merged_commit_id);
8184 0ebf8283 2019-07-24 stsp free(tree_id);
8185 818c7501 2019-07-11 stsp free(fileindex_path);
8186 8641a332 2023-04-14 thomas free(commit_ref_name);
8188 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8189 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
8190 818c7501 2019-07-11 stsp err = unlockerr;
8191 818c7501 2019-07-11 stsp return err;
8194 0ebf8283 2019-07-24 stsp const struct got_error *
8195 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8196 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8197 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8199 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8200 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8201 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8202 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8204 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8206 0ebf8283 2019-07-24 stsp return err;
8208 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8209 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8211 e600f124 2021-03-21 stsp goto done;
8213 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8214 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8216 0ebf8283 2019-07-24 stsp goto done;
8218 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8220 0ebf8283 2019-07-24 stsp goto done;
8222 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
8224 0ebf8283 2019-07-24 stsp goto done;
8226 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8228 0ebf8283 2019-07-24 stsp goto done;
8230 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8232 a615e0e7 2020-12-16 stsp goto done;
8234 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8236 a615e0e7 2020-12-16 stsp goto done;
8237 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8238 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8239 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8240 a615e0e7 2020-12-16 stsp err = sync_err;
8242 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8243 a615e0e7 2020-12-16 stsp free(fileindex_path);
8244 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8245 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8246 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8247 0ebf8283 2019-07-24 stsp err = unlockerr;
8248 0ebf8283 2019-07-24 stsp return err;
8251 0ebf8283 2019-07-24 stsp const struct got_error *
8252 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8253 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8255 0ebf8283 2019-07-24 stsp const struct got_error *err;
8256 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8258 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8260 0ebf8283 2019-07-24 stsp return err;
8262 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8264 0ebf8283 2019-07-24 stsp goto done;
8266 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8268 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8269 2822a352 2019-10-15 stsp return err;
8272 2822a352 2019-10-15 stsp const struct got_error *
8273 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8274 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8275 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8276 2822a352 2019-10-15 stsp struct got_repository *repo)
8278 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8279 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8280 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8282 2822a352 2019-10-15 stsp *fileindex = NULL;
8283 2822a352 2019-10-15 stsp *branch_ref = NULL;
8284 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8286 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8288 2822a352 2019-10-15 stsp return err;
8290 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8291 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8292 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8293 2822a352 2019-10-15 stsp "update -b or different branch name required");
8294 2822a352 2019-10-15 stsp goto done;
8297 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8299 2822a352 2019-10-15 stsp goto done;
8301 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8302 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8303 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8304 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8307 2822a352 2019-10-15 stsp goto done;
8309 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8311 2822a352 2019-10-15 stsp goto done;
8313 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8314 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8316 2822a352 2019-10-15 stsp if (err) {
8317 2822a352 2019-10-15 stsp if (*branch_ref) {
8318 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8319 2822a352 2019-10-15 stsp *branch_ref = NULL;
8321 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8322 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8323 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8325 2822a352 2019-10-15 stsp if (*fileindex) {
8326 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8327 2822a352 2019-10-15 stsp *fileindex = NULL;
8329 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8331 0cb83759 2019-08-03 stsp return err;
8334 2822a352 2019-10-15 stsp const struct got_error *
8335 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8336 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8337 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8338 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8339 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8341 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8342 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8343 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8344 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8346 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8348 2822a352 2019-10-15 stsp goto done;
8350 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8352 2822a352 2019-10-15 stsp goto done;
8354 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
8355 945f9229 2022-04-16 thomas if (err)
8356 945f9229 2022-04-16 thomas goto done;
8358 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8359 2822a352 2019-10-15 stsp worktree->path_prefix);
8361 2822a352 2019-10-15 stsp goto done;
8363 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8365 2822a352 2019-10-15 stsp goto done;
8367 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8368 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8370 2822a352 2019-10-15 stsp goto sync;
8372 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8374 2822a352 2019-10-15 stsp goto sync;
8376 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8378 6e210706 2021-01-22 stsp goto sync;
8380 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8382 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8383 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8384 2822a352 2019-10-15 stsp err = sync_err;
8387 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8388 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8389 2822a352 2019-10-15 stsp err = unlockerr;
8390 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8392 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8393 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8394 2822a352 2019-10-15 stsp err = unlockerr;
8395 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8397 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8398 2822a352 2019-10-15 stsp free(fileindex_path);
8399 2822a352 2019-10-15 stsp free(tree_id);
8400 945f9229 2022-04-16 thomas if (commit)
8401 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8403 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8404 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8405 2822a352 2019-10-15 stsp err = unlockerr;
8406 2822a352 2019-10-15 stsp return err;
8409 2822a352 2019-10-15 stsp const struct got_error *
8410 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8411 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8412 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8414 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8416 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8418 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8420 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8421 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8422 8b692cd0 2019-10-21 stsp err = unlockerr;
8423 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8425 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8426 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8427 8b692cd0 2019-10-21 stsp err = unlockerr;
8428 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8430 10604dce 2021-09-24 thomas return err;
8433 10604dce 2021-09-24 thomas const struct got_error *
8434 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
8435 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
8437 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
8438 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8440 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8441 10604dce 2021-09-24 thomas if (err)
8442 10604dce 2021-09-24 thomas goto done;
8444 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8446 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
8447 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8448 10604dce 2021-09-24 thomas err = sync_err;
8450 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8451 10604dce 2021-09-24 thomas free(fileindex_path);
8452 8b692cd0 2019-10-21 stsp return err;
8455 10604dce 2021-09-24 thomas static const struct got_error *
8456 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8458 10604dce 2021-09-24 thomas const struct got_error *err;
8459 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8461 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8462 10604dce 2021-09-24 thomas if (err)
8463 10604dce 2021-09-24 thomas goto done;
8464 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
8465 10604dce 2021-09-24 thomas if (err)
8466 10604dce 2021-09-24 thomas goto done;
8468 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8469 10604dce 2021-09-24 thomas if (err)
8470 10604dce 2021-09-24 thomas goto done;
8471 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
8472 10604dce 2021-09-24 thomas if (err)
8473 10604dce 2021-09-24 thomas goto done;
8476 10604dce 2021-09-24 thomas free(branch_refname);
8477 10604dce 2021-09-24 thomas free(commit_refname);
8478 10604dce 2021-09-24 thomas return err;
8481 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
8482 10604dce 2021-09-24 thomas struct got_worktree *worktree;
8483 10604dce 2021-09-24 thomas const char *branch_name;
8486 10604dce 2021-09-24 thomas static const struct got_error *
8487 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8488 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
8490 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
8492 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8493 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
8494 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
8496 10604dce 2021-09-24 thomas return NULL;
8500 10604dce 2021-09-24 thomas const struct got_error *
8501 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
8502 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
8503 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
8504 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
8505 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8506 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8508 10604dce 2021-09-24 thomas const struct got_error *err;
8509 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8510 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args cma;
8512 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8513 10604dce 2021-09-24 thomas if (err)
8514 10604dce 2021-09-24 thomas goto done;
8516 6ebb2263 2023-07-26 thomas cma.worktree = worktree;
8517 6ebb2263 2023-07-26 thomas cma.cancel_cb = cancel_cb;
8518 6ebb2263 2023-07-26 thomas cma.cancel_arg = cancel_arg;
8520 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8522 10604dce 2021-09-24 thomas if (err)
8523 10604dce 2021-09-24 thomas goto done;
8525 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8526 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
8527 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
8529 10604dce 2021-09-24 thomas free(fileindex_path);
8530 10604dce 2021-09-24 thomas return err;
8533 10604dce 2021-09-24 thomas const struct got_error *
8534 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
8535 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
8536 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
8537 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
8538 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
8539 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
8542 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
8543 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
8544 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
8545 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
8546 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
8547 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
8548 10604dce 2021-09-24 thomas int have_staged_files = 0;
8549 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
8550 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8552 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
8553 10604dce 2021-09-24 thomas *new_commit_id = NULL;
8555 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
8557 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8558 10604dce 2021-09-24 thomas if (err)
8559 10604dce 2021-09-24 thomas goto done;
8561 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8562 10604dce 2021-09-24 thomas if (err)
8563 10604dce 2021-09-24 thomas goto done;
8565 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
8566 10604dce 2021-09-24 thomas if (err)
8567 10604dce 2021-09-24 thomas goto done;
8569 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8570 10604dce 2021-09-24 thomas &have_staged_files);
8571 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8572 10604dce 2021-09-24 thomas goto done;
8573 10604dce 2021-09-24 thomas if (have_staged_files) {
8574 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8575 10604dce 2021-09-24 thomas goto done;
8578 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
8579 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
8580 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
8581 10604dce 2021-09-24 thomas cc_arg.repo = repo;
8582 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
8583 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8584 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
8585 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8586 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8587 10604dce 2021-09-24 thomas if (err)
8588 10604dce 2021-09-24 thomas goto done;
8590 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
8591 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
8592 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
8593 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
8594 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8595 10604dce 2021-09-24 thomas if (err)
8596 10604dce 2021-09-24 thomas goto done;
8598 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
8599 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
8600 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8601 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8602 10604dce 2021-09-24 thomas err = sync_err;
8604 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8605 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8607 10604dce 2021-09-24 thomas free_commitable(ct);
8609 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8610 10604dce 2021-09-24 thomas free(fileindex_path);
8611 10604dce 2021-09-24 thomas return err;
8614 10604dce 2021-09-24 thomas const struct got_error *
8615 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8616 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8618 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8619 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8621 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8622 10604dce 2021-09-24 thomas if (err)
8623 10604dce 2021-09-24 thomas goto done;
8625 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8626 10604dce 2021-09-24 thomas if (err)
8627 10604dce 2021-09-24 thomas goto done;
8628 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8629 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8630 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8631 10604dce 2021-09-24 thomas err = sync_err;
8633 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8634 10604dce 2021-09-24 thomas free(fileindex_path);
8635 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8636 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8637 10604dce 2021-09-24 thomas err = unlockerr;
8638 10604dce 2021-09-24 thomas return err;
8641 10604dce 2021-09-24 thomas const struct got_error *
8642 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8643 10604dce 2021-09-24 thomas struct got_repository *repo)
8645 10604dce 2021-09-24 thomas const struct got_error *err;
8646 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8647 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8649 10604dce 2021-09-24 thomas *in_progress = 0;
8651 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8652 10604dce 2021-09-24 thomas if (err)
8653 10604dce 2021-09-24 thomas return err;
8654 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8655 dfe86d1f 2021-09-24 thomas free(branch_refname);
8656 10604dce 2021-09-24 thomas if (err) {
8657 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8658 10604dce 2021-09-24 thomas return err;
8660 10604dce 2021-09-24 thomas *in_progress = 1;
8662 10604dce 2021-09-24 thomas return NULL;
8665 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8666 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8667 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8669 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8670 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8671 2b72f32d 2023-06-22 thomas struct got_reference *wt_branch = NULL;
8672 2b72f32d 2023-06-22 thomas struct got_object_id *wt_branch_tip = NULL;
8673 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8675 10604dce 2021-09-24 thomas *fileindex = NULL;
8677 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8678 10604dce 2021-09-24 thomas if (err)
8679 10604dce 2021-09-24 thomas return err;
8681 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8682 10604dce 2021-09-24 thomas if (err)
8683 10604dce 2021-09-24 thomas goto done;
8685 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8686 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8687 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8688 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8689 10604dce 2021-09-24 thomas &ok_arg);
8690 10604dce 2021-09-24 thomas if (err)
8691 10604dce 2021-09-24 thomas goto done;
8693 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8695 10604dce 2021-09-24 thomas if (err)
8696 10604dce 2021-09-24 thomas goto done;
8698 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8699 10604dce 2021-09-24 thomas if (err)
8700 10604dce 2021-09-24 thomas goto done;
8702 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8703 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8704 10604dce 2021-09-24 thomas goto done;
8708 2b72f32d 2023-06-22 thomas free(fileindex_path);
8709 2b72f32d 2023-06-22 thomas if (wt_branch)
8710 2b72f32d 2023-06-22 thomas got_ref_close(wt_branch);
8711 2b72f32d 2023-06-22 thomas free(wt_branch_tip);
8712 2b72f32d 2023-06-22 thomas if (err) {
8713 2b72f32d 2023-06-22 thomas if (*fileindex) {
8714 2b72f32d 2023-06-22 thomas got_fileindex_free(*fileindex);
8715 2b72f32d 2023-06-22 thomas *fileindex = NULL;
8717 2b72f32d 2023-06-22 thomas lock_worktree(worktree, LOCK_SH);
8719 2b72f32d 2023-06-22 thomas return err;
8722 2b72f32d 2023-06-22 thomas const struct got_error *got_worktree_merge_write_refs(
8723 2b72f32d 2023-06-22 thomas struct got_worktree *worktree, struct got_reference *branch,
8724 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8726 2b72f32d 2023-06-22 thomas const struct got_error *err = NULL;
8727 2b72f32d 2023-06-22 thomas char *branch_refname = NULL, *commit_refname = NULL;
8728 2b72f32d 2023-06-22 thomas struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8729 2b72f32d 2023-06-22 thomas struct got_object_id *branch_tip = NULL;
8731 2b72f32d 2023-06-22 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8732 2b72f32d 2023-06-22 thomas if (err)
8733 2b72f32d 2023-06-22 thomas return err;
8735 2b72f32d 2023-06-22 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8736 2b72f32d 2023-06-22 thomas if (err)
8737 2b72f32d 2023-06-22 thomas return err;
8739 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8740 10604dce 2021-09-24 thomas if (err)
8741 10604dce 2021-09-24 thomas goto done;
8743 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8744 10604dce 2021-09-24 thomas if (err)
8745 10604dce 2021-09-24 thomas goto done;
8746 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8747 10604dce 2021-09-24 thomas if (err)
8748 10604dce 2021-09-24 thomas goto done;
8750 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8751 10604dce 2021-09-24 thomas if (err)
8752 10604dce 2021-09-24 thomas goto done;
8753 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8754 10604dce 2021-09-24 thomas if (err)
8755 10604dce 2021-09-24 thomas goto done;
8758 10604dce 2021-09-24 thomas free(branch_refname);
8759 10604dce 2021-09-24 thomas free(commit_refname);
8760 10604dce 2021-09-24 thomas if (branch_ref)
8761 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8762 10604dce 2021-09-24 thomas if (commit_ref)
8763 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8764 2b72f32d 2023-06-22 thomas free(branch_tip);
8765 10604dce 2021-09-24 thomas return err;
8768 10604dce 2021-09-24 thomas const struct got_error *
8769 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8770 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8771 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8773 10604dce 2021-09-24 thomas const struct got_error *err;
8774 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8775 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8776 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8777 10604dce 2021-09-24 thomas int have_staged_files = 0;
8779 10604dce 2021-09-24 thomas *branch_name = NULL;
8780 10604dce 2021-09-24 thomas *branch_tip = NULL;
8781 10604dce 2021-09-24 thomas *fileindex = NULL;
8783 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8784 10604dce 2021-09-24 thomas if (err)
8785 10604dce 2021-09-24 thomas return err;
8787 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8788 10604dce 2021-09-24 thomas if (err)
8789 10604dce 2021-09-24 thomas goto done;
8791 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8792 10604dce 2021-09-24 thomas &have_staged_files);
8793 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8794 10604dce 2021-09-24 thomas goto done;
8795 10604dce 2021-09-24 thomas if (have_staged_files) {
8796 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8797 10604dce 2021-09-24 thomas goto done;
8800 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8801 10604dce 2021-09-24 thomas if (err)
8802 10604dce 2021-09-24 thomas goto done;
8804 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8805 10604dce 2021-09-24 thomas if (err)
8806 10604dce 2021-09-24 thomas goto done;
8808 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8809 10604dce 2021-09-24 thomas if (err)
8810 10604dce 2021-09-24 thomas goto done;
8812 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8813 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8814 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8815 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8816 10604dce 2021-09-24 thomas goto done;
8818 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8819 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8820 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8821 10604dce 2021-09-24 thomas goto done;
8824 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8825 10604dce 2021-09-24 thomas if (err)
8826 10604dce 2021-09-24 thomas goto done;
8828 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8829 10604dce 2021-09-24 thomas if (err)
8830 10604dce 2021-09-24 thomas goto done;
8832 10604dce 2021-09-24 thomas free(commit_refname);
8833 10604dce 2021-09-24 thomas free(branch_refname);
8834 10604dce 2021-09-24 thomas free(fileindex_path);
8835 10604dce 2021-09-24 thomas if (commit_ref)
8836 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8837 10604dce 2021-09-24 thomas if (branch_ref)
8838 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8839 10604dce 2021-09-24 thomas if (err) {
8840 10604dce 2021-09-24 thomas if (*branch_name) {
8841 10604dce 2021-09-24 thomas free(*branch_name);
8842 10604dce 2021-09-24 thomas *branch_name = NULL;
8844 10604dce 2021-09-24 thomas free(*branch_tip);
8845 10604dce 2021-09-24 thomas *branch_tip = NULL;
8846 10604dce 2021-09-24 thomas if (*fileindex) {
8847 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8848 10604dce 2021-09-24 thomas *fileindex = NULL;
8850 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8852 10604dce 2021-09-24 thomas return err;
8855 10604dce 2021-09-24 thomas const struct got_error *
8856 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8857 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8858 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8860 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8861 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8862 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8863 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8864 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
8865 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
8866 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
8867 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8868 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
8870 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
8872 8641a332 2023-04-14 thomas err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8873 8641a332 2023-04-14 thomas if (err)
8874 8641a332 2023-04-14 thomas goto done;
8876 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8877 8641a332 2023-04-14 thomas if (err)
8878 8641a332 2023-04-14 thomas goto done;
8880 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8881 8641a332 2023-04-14 thomas if (err)
8882 8641a332 2023-04-14 thomas goto done;
8885 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
8886 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
8887 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
8888 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
8890 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(&added_paths,
8891 8641a332 2023-04-14 thomas got_worktree_get_base_commit_id(worktree), merged_commit_id,
8892 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
8893 8641a332 2023-04-14 thomas if (err)
8894 8641a332 2023-04-14 thomas goto done;
8897 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8898 945f9229 2022-04-16 thomas worktree->base_commit_id);
8899 945f9229 2022-04-16 thomas if (err)
8900 945f9229 2022-04-16 thomas goto done;
8902 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8903 945f9229 2022-04-16 thomas worktree->path_prefix);
8904 10604dce 2021-09-24 thomas if (err)
8905 10604dce 2021-09-24 thomas goto done;
8907 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8908 10604dce 2021-09-24 thomas if (err)
8909 10604dce 2021-09-24 thomas goto done;
8911 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8912 10604dce 2021-09-24 thomas if (err)
8913 10604dce 2021-09-24 thomas goto done;
8915 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8916 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8917 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8918 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8919 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8920 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8921 10604dce 2021-09-24 thomas rfa.repo = repo;
8922 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8923 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8924 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8925 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8926 10604dce 2021-09-24 thomas if (err)
8927 10604dce 2021-09-24 thomas goto sync;
8929 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8930 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8932 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8933 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8934 10604dce 2021-09-24 thomas err = sync_err;
8936 10604dce 2021-09-24 thomas free(tree_id);
8937 8641a332 2023-04-14 thomas free(merged_commit_id);
8938 945f9229 2022-04-16 thomas if (commit)
8939 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8940 10604dce 2021-09-24 thomas if (fileindex)
8941 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8942 10604dce 2021-09-24 thomas free(fileindex_path);
8943 8641a332 2023-04-14 thomas if (commit_ref)
8944 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8945 8641a332 2023-04-14 thomas free(commit_ref_name);
8947 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8948 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8949 10604dce 2021-09-24 thomas err = unlockerr;
8950 10604dce 2021-09-24 thomas return err;
8953 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8954 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8955 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8956 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8957 2db2652d 2019-08-07 stsp struct got_repository *repo;
8958 2db2652d 2019-08-07 stsp int have_changes;
8961 ef20f542 2022-06-26 thomas static const struct got_error *
8962 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8963 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8964 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8965 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8967 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8968 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8969 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8970 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8971 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8972 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8974 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8975 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8976 8b13ce36 2019-08-08 stsp return NULL;
8977 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8978 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8980 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8981 735ef5ac 2019-08-03 stsp if (ie == NULL)
8982 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8984 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8985 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8986 735ef5ac 2019-08-03 stsp relpath) == -1)
8987 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8989 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8990 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
8991 43010591 2023-02-17 thomas &base_commit_id, ie);
8994 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8995 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8996 3aa5969e 2019-08-06 stsp goto done;
8997 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8998 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8999 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
9000 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9001 735ef5ac 2019-08-03 stsp goto done;
9004 2db2652d 2019-08-07 stsp a->have_changes = 1;
9006 735ef5ac 2019-08-03 stsp p = in_repo_path;
9007 735ef5ac 2019-08-03 stsp while (p[0] == '/')
9009 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
9010 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
9011 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
9013 735ef5ac 2019-08-03 stsp free(in_repo_path);
9014 dc424a06 2019-08-07 stsp return err;
9017 2db2652d 2019-08-07 stsp struct stage_path_arg {
9018 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
9019 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
9020 2db2652d 2019-08-07 stsp struct got_repository *repo;
9021 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
9022 2db2652d 2019-08-07 stsp void *status_arg;
9023 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
9024 2db2652d 2019-08-07 stsp void *patch_arg;
9025 7b5dc508 2019-10-28 stsp int staged_something;
9026 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
9029 2db2652d 2019-08-07 stsp static const struct got_error *
9030 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
9031 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
9032 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9033 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9035 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
9036 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
9037 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
9038 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
9039 0cb83759 2019-08-03 stsp uint32_t stage;
9040 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
9041 0aeb8099 2020-07-23 stsp struct stat sb;
9043 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
9044 8b13ce36 2019-08-08 stsp return NULL;
9046 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9047 d3e7c587 2019-08-03 stsp if (ie == NULL)
9048 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9050 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9051 2db2652d 2019-08-07 stsp relpath)== -1)
9052 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
9054 0cb83759 2019-08-03 stsp switch (status) {
9055 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
9056 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
9057 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
9058 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
9059 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
9062 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9063 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
9064 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9065 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9066 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
9069 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
9072 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
9073 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
9074 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
9075 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
9076 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
9080 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
9081 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
9084 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9085 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
9086 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9087 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
9089 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
9090 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9091 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
9092 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
9093 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
9094 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
9095 35213c7c 2020-07-23 stsp ssize_t target_len;
9096 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
9097 35213c7c 2020-07-23 stsp sizeof(target_path));
9098 35213c7c 2020-07-23 stsp if (target_len == -1) {
9099 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
9100 35213c7c 2020-07-23 stsp ondisk_path);
9103 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
9104 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
9105 ad10f64e 2023-07-19 thomas a->worktree->root_path,
9106 ad10f64e 2023-07-19 thomas a->worktree->meta_dir);
9109 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
9110 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
9111 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
9115 35213c7c 2020-07-23 stsp if (is_bad_symlink)
9116 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9117 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
9119 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9120 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
9122 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9123 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
9125 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9126 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9128 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9129 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
9130 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
9131 f289c82c 2022-06-13 thomas if (err)
9134 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
9135 f289c82c 2022-06-13 thomas * implicitly unstage the file.
9137 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9138 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
9139 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
9140 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
9143 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
9144 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
9146 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9147 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9148 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
9149 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
9152 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9154 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9155 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9159 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
9160 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9161 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9162 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9164 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9165 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9168 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
9170 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
9171 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9173 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
9174 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
9177 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9181 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
9182 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
9183 dc424a06 2019-08-07 stsp free(path_content);
9184 2db2652d 2019-08-07 stsp free(ondisk_path);
9185 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
9186 0ebf8283 2019-07-24 stsp return err;
9189 0cb83759 2019-08-03 stsp const struct got_error *
9190 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
9191 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
9192 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
9193 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9194 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
9196 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9197 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9198 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9199 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9200 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9201 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9202 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9203 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9205 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9207 0cb83759 2019-08-03 stsp return err;
9209 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9210 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9212 735ef5ac 2019-08-03 stsp goto done;
9213 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9215 735ef5ac 2019-08-03 stsp goto done;
9216 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9218 0cb83759 2019-08-03 stsp goto done;
9220 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9221 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9222 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9223 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9224 2db2652d 2019-08-07 stsp oka.repo = repo;
9225 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9226 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9227 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9228 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
9230 735ef5ac 2019-08-03 stsp goto done;
9232 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9233 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9234 2db2652d 2019-08-07 stsp goto done;
9237 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9238 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9239 2db2652d 2019-08-07 stsp spa.repo = repo;
9240 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9241 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9242 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9243 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9244 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9245 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9246 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9247 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9248 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
9250 2db2652d 2019-08-07 stsp goto done;
9252 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9253 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9254 7b5dc508 2019-10-28 stsp goto done;
9257 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9258 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9259 0cb83759 2019-08-03 stsp err = sync_err;
9261 735ef5ac 2019-08-03 stsp if (head_ref)
9262 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9263 735ef5ac 2019-08-03 stsp free(head_commit_id);
9264 0cb83759 2019-08-03 stsp free(fileindex_path);
9265 0cb83759 2019-08-03 stsp if (fileindex)
9266 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9267 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9268 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9269 0cb83759 2019-08-03 stsp err = unlockerr;
9270 0cb83759 2019-08-03 stsp return err;
9273 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9274 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9275 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9276 ad493afc 2019-08-03 stsp struct got_repository *repo;
9277 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9278 ad493afc 2019-08-03 stsp void *progress_arg;
9279 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9280 2e1f37b0 2019-08-08 stsp void *patch_arg;
9283 2e1f37b0 2019-08-08 stsp static const struct got_error *
9284 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9285 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9286 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9287 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9288 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9290 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9291 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9292 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9293 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9294 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9295 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9296 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9297 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9299 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9300 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9302 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9304 2e1f37b0 2019-08-08 stsp return err;
9306 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9307 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9308 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9309 f4ae6ddb 2022-07-01 thomas goto done;
9311 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9312 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9313 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9314 f4ae6ddb 2022-07-01 thomas goto done;
9317 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9319 2e1f37b0 2019-08-08 stsp goto done;
9321 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9323 2e1f37b0 2019-08-08 stsp goto done;
9325 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9327 2e1f37b0 2019-08-08 stsp goto done;
9329 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9332 2e1f37b0 2019-08-08 stsp goto done;
9334 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9336 2e1f37b0 2019-08-08 stsp goto done;
9338 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9340 2e1f37b0 2019-08-08 stsp goto done;
9342 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9343 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9345 2e1f37b0 2019-08-08 stsp goto done;
9347 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9348 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
9350 2e1f37b0 2019-08-08 stsp goto done;
9351 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9352 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
9354 2e1f37b0 2019-08-08 stsp goto done;
9356 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9357 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9358 2e1f37b0 2019-08-08 stsp goto done;
9360 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9361 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9362 2e1f37b0 2019-08-08 stsp goto done;
9364 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9365 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9366 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
9367 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9368 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9369 fe621944 2020-11-10 stsp nchanges++;
9371 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9372 2e1f37b0 2019-08-08 stsp int choice;
9373 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9374 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9375 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9376 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9378 2e1f37b0 2019-08-08 stsp goto done;
9379 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9380 2e1f37b0 2019-08-08 stsp have_content = 1;
9382 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9383 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9386 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9387 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9388 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9390 2e1f37b0 2019-08-08 stsp free(label1);
9391 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9392 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9394 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9395 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9396 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9397 2e1f37b0 2019-08-08 stsp if (staged_blob)
9398 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9399 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9400 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9401 fe621944 2020-11-10 stsp err = free_err;
9402 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9403 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9404 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9405 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9406 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9407 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9408 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9409 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9410 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9411 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9412 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9413 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9414 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9415 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9416 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9417 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9418 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9419 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9420 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9422 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9423 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9424 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9425 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9426 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9427 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9428 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9430 2e1f37b0 2019-08-08 stsp free(path1);
9431 2e1f37b0 2019-08-08 stsp free(path2);
9432 fda8017d 2020-07-23 stsp return err;
9435 fda8017d 2020-07-23 stsp static const struct got_error *
9436 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9437 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9438 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9439 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9440 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9441 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9442 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9444 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9445 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9446 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9447 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9448 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9449 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9450 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9451 fda8017d 2020-07-23 stsp struct stat sb;
9453 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9454 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9455 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9457 fda8017d 2020-07-23 stsp return err;
9459 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9460 fda8017d 2020-07-23 stsp return NULL;
9462 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9463 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9464 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9466 fda8017d 2020-07-23 stsp goto done;
9469 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
9470 fda8017d 2020-07-23 stsp if (f == NULL) {
9471 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9472 fda8017d 2020-07-23 stsp path_unstaged_content);
9473 fda8017d 2020-07-23 stsp goto done;
9475 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9476 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9477 fda8017d 2020-07-23 stsp goto done;
9479 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9480 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9481 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9483 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9484 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9485 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9486 fda8017d 2020-07-23 stsp goto done;
9488 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9489 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9490 fda8017d 2020-07-23 stsp goto done;
9492 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9493 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9494 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9495 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9496 fda8017d 2020-07-23 stsp progress_arg);
9498 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9500 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9502 67a66647 2021-05-31 stsp return err;
9504 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9505 67a66647 2021-05-31 stsp parent) == -1) {
9506 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9507 67a66647 2021-05-31 stsp base_path = NULL;
9508 67a66647 2021-05-31 stsp goto done;
9511 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
9512 fc2a50f2 2022-11-01 thomas base_path, "");
9514 67a66647 2021-05-31 stsp goto done;
9515 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9516 67a66647 2021-05-31 stsp blob_base);
9518 67a66647 2021-05-31 stsp goto done;
9521 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9522 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9524 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9525 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9526 eec2f5a9 2021-06-03 stsp ondisk_path);
9528 eec2f5a9 2021-06-03 stsp goto done;
9531 06340621 2021-12-31 thomas fd = open(ondisk_path,
9532 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9533 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9534 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9535 eec2f5a9 2021-06-03 stsp goto done;
9537 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9538 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9539 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9540 eec2f5a9 2021-06-03 stsp close(fd);
9541 eec2f5a9 2021-06-03 stsp goto done;
9545 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9546 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9547 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9548 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9549 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9552 fda8017d 2020-07-23 stsp goto done;
9554 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9555 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9556 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9558 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9559 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9562 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9563 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9564 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9565 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9566 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9567 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9568 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9569 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9570 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9571 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9572 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9573 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9574 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9575 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9576 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9577 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9578 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9579 67a66647 2021-05-31 stsp free(blob_base_path);
9580 67a66647 2021-05-31 stsp free(parent);
9581 67a66647 2021-05-31 stsp free(base_path);
9582 2e1f37b0 2019-08-08 stsp return err;
9585 ad493afc 2019-08-03 stsp static const struct got_error *
9586 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9587 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9588 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9589 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9591 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9592 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9593 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9594 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9595 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9596 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9597 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9598 9bc94a15 2019-08-03 stsp struct stat sb;
9599 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9601 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9602 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9603 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9604 2e1f37b0 2019-08-08 stsp return NULL;
9606 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9607 ad493afc 2019-08-03 stsp if (ie == NULL)
9608 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9610 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9612 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9614 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9615 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9617 f69721c3 2019-10-21 stsp goto done;
9618 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9619 f69721c3 2019-10-21 stsp id_str) == -1) {
9620 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9621 f4ae6ddb 2022-07-01 thomas goto done;
9624 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9625 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9626 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9627 f4ae6ddb 2022-07-01 thomas goto done;
9629 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9630 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9631 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9632 f69721c3 2019-10-21 stsp goto done;
9635 ad493afc 2019-08-03 stsp switch (staged_status) {
9636 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9637 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9638 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
9641 ad493afc 2019-08-03 stsp /* fall through */
9642 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9643 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9644 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9645 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9646 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9647 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9650 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9653 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9654 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9655 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9656 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9657 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9658 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9661 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9662 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9665 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9666 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9667 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9668 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9669 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9670 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9671 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9672 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9673 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9675 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9676 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9677 36bf999c 2020-07-23 stsp char *staged_target;
9678 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9679 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9681 36bf999c 2020-07-23 stsp goto done;
9682 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9683 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9684 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9685 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9686 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9687 36bf999c 2020-07-23 stsp free(staged_target);
9689 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9690 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9691 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9692 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9693 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9694 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9695 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9699 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9702 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9703 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9704 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9705 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9708 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9709 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9710 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9711 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9712 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9715 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9717 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9718 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9722 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9723 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9724 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9725 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9728 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9732 ad493afc 2019-08-03 stsp free(ondisk_path);
9733 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9734 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9735 ad493afc 2019-08-03 stsp if (blob_base)
9736 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9737 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9738 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9739 ad493afc 2019-08-03 stsp if (blob_staged)
9740 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9741 f69721c3 2019-10-21 stsp free(id_str);
9742 f69721c3 2019-10-21 stsp free(label_orig);
9743 ad493afc 2019-08-03 stsp return err;
9746 ad493afc 2019-08-03 stsp const struct got_error *
9747 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9748 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9749 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9750 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9751 ad493afc 2019-08-03 stsp struct got_repository *repo)
9753 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9754 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9755 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9756 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9757 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9759 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9761 ad493afc 2019-08-03 stsp return err;
9763 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9765 ad493afc 2019-08-03 stsp goto done;
9767 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9768 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9769 ad493afc 2019-08-03 stsp upa.repo = repo;
9770 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9771 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9772 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9773 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9774 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9775 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9776 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9778 ad493afc 2019-08-03 stsp goto done;
9781 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9782 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9783 ad493afc 2019-08-03 stsp err = sync_err;
9785 ad493afc 2019-08-03 stsp free(fileindex_path);
9786 ad493afc 2019-08-03 stsp if (fileindex)
9787 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9788 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9789 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9790 ad493afc 2019-08-03 stsp err = unlockerr;
9791 ad493afc 2019-08-03 stsp return err;
9794 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9795 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9796 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9797 b2118c49 2020-07-28 stsp void *info_arg;
9798 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9799 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9800 b2118c49 2020-07-28 stsp void *cancel_arg;
9803 b2118c49 2020-07-28 stsp static const struct got_error *
9804 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9806 eb6fb9cd 2023-07-26 thomas const struct got_error *err;
9807 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9808 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9809 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9810 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9811 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9812 b2118c49 2020-07-28 stsp int stage;
9814 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
9815 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
9816 eb6fb9cd 2023-07-26 thomas if (err)
9817 eb6fb9cd 2023-07-26 thomas return err;
9820 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9821 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9822 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9825 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9826 b2118c49 2020-07-28 stsp return NULL;
9828 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9829 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9830 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9831 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9832 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9833 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9834 43010591 2023-02-17 thomas &staged_blob_id, ie);
9837 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9838 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9840 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9841 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9844 b2118c49 2020-07-28 stsp const struct got_error *
9845 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9846 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9847 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9848 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9851 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9852 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9853 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9854 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9856 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9858 b2118c49 2020-07-28 stsp return err;
9860 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9862 b2118c49 2020-07-28 stsp goto done;
9864 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9865 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9866 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9867 b2118c49 2020-07-28 stsp arg.paths = paths;
9868 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9869 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9870 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9873 b2118c49 2020-07-28 stsp free(fileindex_path);
9874 b2118c49 2020-07-28 stsp if (fileindex)
9875 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9876 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9877 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9878 b2118c49 2020-07-28 stsp err = unlockerr;
9879 b2118c49 2020-07-28 stsp return err;
9882 814624e7 2022-03-22 thomas static const struct got_error *
9883 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9884 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9885 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9887 814624e7 2022-03-22 thomas const struct got_error *err;
9888 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9889 814624e7 2022-03-22 thomas struct stat sb;
9890 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9892 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9893 814624e7 2022-03-22 thomas if (err)
9894 814624e7 2022-03-22 thomas return err;
9896 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9897 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9898 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9900 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9901 814624e7 2022-03-22 thomas if (ie) {
9902 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9903 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9905 814624e7 2022-03-22 thomas if (err)
9906 814624e7 2022-03-22 thomas goto done;
9907 814624e7 2022-03-22 thomas } else {
9908 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9909 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9910 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9911 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9912 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9913 814624e7 2022-03-22 thomas ondisk_path);
9914 814624e7 2022-03-22 thomas goto done;
9916 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9921 814624e7 2022-03-22 thomas free(ondisk_path);
9922 814624e7 2022-03-22 thomas return err;
9925 814624e7 2022-03-22 thomas static const struct got_error *
9926 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
9927 814624e7 2022-03-22 thomas unsigned char staged_status)
9929 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9930 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9931 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9932 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9933 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9934 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9935 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9936 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9937 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9938 814624e7 2022-03-22 thomas return NULL;
9941 814624e7 2022-03-22 thomas static const struct got_error *
9942 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9944 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9945 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9946 814624e7 2022-03-22 thomas return NULL;
9949 814624e7 2022-03-22 thomas static const struct got_error *
9950 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9951 814624e7 2022-03-22 thomas unsigned char staged_status)
9953 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9954 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9955 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9956 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9957 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9958 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9959 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9960 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9961 814624e7 2022-03-22 thomas return NULL;
9964 814624e7 2022-03-22 thomas const struct got_error *
9965 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9966 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9968 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9971 814624e7 2022-03-22 thomas const struct got_error *
9972 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9973 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9974 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9976 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9977 814624e7 2022-03-22 thomas int file_renamed = 0;
9978 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9979 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9981 814624e7 2022-03-22 thomas *oldpath = NULL;
9982 814624e7 2022-03-22 thomas *newpath = NULL;
9984 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9985 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9986 814624e7 2022-03-22 thomas if (err)
9987 814624e7 2022-03-22 thomas goto done;
9989 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9990 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9991 814624e7 2022-03-22 thomas if (err)
9992 814624e7 2022-03-22 thomas goto done;
9994 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9995 814624e7 2022-03-22 thomas file_renamed = 1;
9997 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9998 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9999 814624e7 2022-03-22 thomas else if (file_renamed) {
10000 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
10001 814624e7 2022-03-22 thomas if (err == NULL)
10002 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
10003 814624e7 2022-03-22 thomas } else if (old == NULL)
10004 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
10006 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
10009 814624e7 2022-03-22 thomas if (err) {
10010 814624e7 2022-03-22 thomas free(*oldpath);
10011 814624e7 2022-03-22 thomas *oldpath = NULL;
10012 814624e7 2022-03-22 thomas free(*newpath);
10013 814624e7 2022-03-22 thomas *newpath = NULL;
10015 814624e7 2022-03-22 thomas return err;
10018 814624e7 2022-03-22 thomas const struct got_error *
10019 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10020 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
10021 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
10023 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
10025 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
10026 5e22b737 2022-05-31 thomas saa.worktree = worktree;
10027 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
10028 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
10029 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
10030 5e22b737 2022-05-31 thomas saa.repo = repo;
10032 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
10033 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
10036 5e22b737 2022-05-31 thomas const struct got_error *
10037 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10038 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
10039 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
10041 8ec7352a 2023-06-01 thomas const struct got_error *err;
10042 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
10043 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
10045 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
10046 5e22b737 2022-05-31 thomas sda.worktree = worktree;
10047 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
10048 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
10049 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
10050 5e22b737 2022-05-31 thomas sda.repo = repo;
10051 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
10052 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
10053 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
10054 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
10055 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s/%s",
10056 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree), path) == -1)
10057 8ec7352a 2023-06-01 thomas return got_error_from_errno("asprintf");
10058 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
10059 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
10061 8ec7352a 2023-06-01 thomas err = worktree_status(worktree, path, fileindex, repo,
10062 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10063 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
10064 8ec7352a 2023-06-01 thomas return err;
10067 5e22b737 2022-05-31 thomas const struct got_error *
10068 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
10069 36832a8e 2022-05-31 thomas const char *fileindex_path)
10071 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
10073 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
10074 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
10076 5e22b737 2022-05-31 thomas return err;