2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 5822e79e 2023-02-23 op #include <sha2.h>
33 9d31a1d8 2018-03-11 stsp #include <zlib.h>
34 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
35 512f0d0e 2019-01-02 stsp #include <libgen.h>
36 ec22038e 2019-03-10 stsp #include <uuid.h>
37 7154f6ce 2019-03-27 stsp #include <util.h>
39 86c3caaf 2018-03-09 stsp #include "got_error.h"
40 86c3caaf 2018-03-09 stsp #include "got_repository.h"
41 5261c201 2018-04-01 stsp #include "got_reference.h"
42 9d31a1d8 2018-03-11 stsp #include "got_object.h"
43 1dd54920 2019-05-11 stsp #include "got_path.h"
44 e6209546 2019-08-22 stsp #include "got_cancel.h"
45 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
46 511a516b 2018-05-19 stsp #include "got_opentemp.h"
47 234035bc 2019-06-01 stsp #include "got_diff.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
50 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
52 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
54 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
55 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
56 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
57 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
58 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
59 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
62 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
66 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
68 d556241a 2023-07-03 jrick static mode_t
69 d556241a 2023-07-03 jrick apply_umask(mode_t mode)
73 d556241a 2023-07-03 jrick um = umask(000);
75 d556241a 2023-07-03 jrick return mode & ~um;
78 99724ed4 2018-03-10 stsp static const struct got_error *
79 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
81 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
84 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
85 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
87 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
92 507dc3bb 2018-12-29 stsp static const struct got_error *
93 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
95 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
96 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
97 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
98 507dc3bb 2018-12-29 stsp char *path = NULL;
100 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
101 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
102 507dc3bb 2018-12-29 stsp path = NULL;
106 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &tmpfile, path, "");
110 507dc3bb 2018-12-29 stsp if (content) {
111 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
112 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
113 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
118 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
119 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
120 2a57020b 2019-02-20 stsp unlink(tmppath);
125 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
126 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
127 230a42bd 2019-05-11 jcs free(tmppath);
128 09fe317a 2018-03-11 stsp return err;
131 024e9686 2019-05-14 stsp static const struct got_error *
132 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
134 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
135 024e9686 2019-05-14 stsp char *refstr = NULL;
137 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
138 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
139 024e9686 2019-05-14 stsp if (refstr == NULL)
140 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
142 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
143 024e9686 2019-05-14 stsp if (refstr == NULL)
144 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
146 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
147 024e9686 2019-05-14 stsp free(refstr);
148 024e9686 2019-05-14 stsp return err;
151 86c3caaf 2018-03-09 stsp const struct got_error *
152 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
153 df6221c7 2023-07-19 stsp const char *prefix, const char *meta_dir, struct got_repository *repo)
155 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
156 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
157 ec22038e 2019-03-10 stsp uuid_t uuid;
158 ec22038e 2019-03-10 stsp uint32_t uuid_status;
159 65596e15 2018-12-24 stsp int obj_type;
160 7ac97322 2018-03-11 stsp char *path_got = NULL;
161 1451e70d 2018-03-10 stsp char *formatstr = NULL;
162 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
163 65596e15 2018-12-24 stsp char *basestr = NULL;
164 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
166 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
167 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
171 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
173 65596e15 2018-12-24 stsp return err;
174 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
176 65596e15 2018-12-24 stsp return err;
177 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
178 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
180 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
181 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
182 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
185 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
186 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
187 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
191 df6221c7 2023-07-19 stsp /* Create .got/.cvg directory (may already exist). */
192 df6221c7 2023-07-19 stsp if (asprintf(&path_got, "%s/%s", path, meta_dir) == -1) {
193 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
196 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
197 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
201 056e7441 2018-03-11 stsp /* Create an empty lock file. */
202 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
206 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
207 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
211 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
212 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
216 65596e15 2018-12-24 stsp /* Record our base commit. */
217 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
220 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
224 1451e70d 2018-03-10 stsp /* Store path to repository. */
225 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
226 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
230 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
231 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
232 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
236 ec22038e 2019-03-10 stsp /* Generate UUID. */
237 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
238 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
239 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
242 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
243 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
244 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
247 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
251 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
252 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
253 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
256 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
261 65596e15 2018-12-24 stsp free(commit_id);
262 7ac97322 2018-03-11 stsp free(path_got);
263 1451e70d 2018-03-10 stsp free(formatstr);
264 0bb8a95e 2018-03-12 stsp free(absprefix);
265 65596e15 2018-12-24 stsp free(basestr);
266 ec22038e 2019-03-10 stsp free(uuidstr);
267 86c3caaf 2018-03-09 stsp return err;
270 247140b2 2019-02-05 stsp const struct got_error *
271 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
272 e5dc7198 2018-12-29 stsp const char *path_prefix)
274 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
276 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
277 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
278 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
280 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
281 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
282 e5dc7198 2018-12-29 stsp free(absprefix);
283 e5dc7198 2018-12-29 stsp return NULL;
286 bc70eb79 2019-05-09 stsp const char *
287 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
289 36a38700 2019-05-10 stsp return worktree->head_ref_name;
292 024e9686 2019-05-14 stsp const struct got_error *
293 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
294 024e9686 2019-05-14 stsp struct got_reference *head_ref)
296 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
297 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
299 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
300 df6221c7 2023-07-19 stsp worktree->meta_dir) == -1) {
301 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
302 024e9686 2019-05-14 stsp path_got = NULL;
306 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
307 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
308 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
312 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
316 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
317 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
319 024e9686 2019-05-14 stsp free(path_got);
321 024e9686 2019-05-14 stsp free(head_ref_name);
322 024e9686 2019-05-14 stsp return err;
325 b72f483a 2019-02-05 stsp struct got_object_id *
326 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
328 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
331 507dc3bb 2018-12-29 stsp const struct got_error *
332 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
333 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
335 507dc3bb 2018-12-29 stsp const struct got_error *err;
336 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
337 507dc3bb 2018-12-29 stsp char *id_str = NULL;
338 507dc3bb 2018-12-29 stsp char *path_got = NULL;
340 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
341 df6221c7 2023-07-19 stsp worktree->meta_dir) == -1) {
342 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
343 507dc3bb 2018-12-29 stsp path_got = NULL;
347 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
349 507dc3bb 2018-12-29 stsp return err;
351 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
352 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
356 507dc3bb 2018-12-29 stsp /* Record our base commit. */
357 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
360 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
364 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
365 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
366 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
367 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
372 507dc3bb 2018-12-29 stsp got_object_close(obj);
373 507dc3bb 2018-12-29 stsp free(id_str);
374 507dc3bb 2018-12-29 stsp free(path_got);
375 507dc3bb 2018-12-29 stsp return err;
378 50b0790e 2020-09-11 stsp const struct got_gotconfig *
379 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
381 50b0790e 2020-09-11 stsp return worktree->gotconfig;
384 9d31a1d8 2018-03-11 stsp static const struct got_error *
385 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
387 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
388 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
389 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
390 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
391 86c3caaf 2018-03-09 stsp return NULL;
394 21908da4 2019-01-13 stsp static const struct got_error *
395 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
397 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
398 4a1ddfc2 2019-01-12 stsp char *abspath;
400 6a390967 2023-07-14 stsp /* We only accept worktree-relative paths here. */
401 6a390967 2023-07-14 stsp if (got_path_is_absolute(path)) {
402 6a390967 2023-07-14 stsp return got_error_fmt(GOT_ERR_BAD_PATH,
403 6a390967 2023-07-14 stsp "%s does not accept absolute paths: %s",
404 6a390967 2023-07-14 stsp __func__, path);
407 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
408 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
410 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
411 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
412 ddcd8544 2019-03-11 stsp struct stat sb;
413 ddcd8544 2019-03-11 stsp err = NULL;
414 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
415 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
416 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
417 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
418 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
421 4a1ddfc2 2019-01-12 stsp free(abspath);
422 68c76935 2019-02-19 stsp return err;
425 68c76935 2019-02-19 stsp static const struct got_error *
426 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
428 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
429 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
430 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
431 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
436 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
437 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
438 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
441 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
442 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
443 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
446 68c76935 2019-02-19 stsp if (flen1 == 0) {
447 68c76935 2019-02-19 stsp if (flen2 != 0)
450 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
451 68c76935 2019-02-19 stsp if (flen1 != 0)
454 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
455 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
465 68c76935 2019-02-19 stsp return err;
468 68c76935 2019-02-19 stsp static const struct got_error *
469 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
471 68c76935 2019-02-19 stsp struct stat sb;
472 68c76935 2019-02-19 stsp size_t size1, size2;
476 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
477 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
478 68c76935 2019-02-19 stsp size1 = sb.st_size;
480 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
481 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
482 68c76935 2019-02-19 stsp size2 = sb.st_size;
484 68c76935 2019-02-19 stsp if (size1 != size2) {
486 68c76935 2019-02-19 stsp return NULL;
489 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
490 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
491 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
492 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
494 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
497 0e039681 2021-11-15 stsp static const struct got_error *
498 0e039681 2021-11-15 stsp copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
500 0e039681 2021-11-15 stsp uint8_t fbuf[65536];
501 0e039681 2021-11-15 stsp size_t flen;
502 0e039681 2021-11-15 stsp ssize_t outlen;
504 0e039681 2021-11-15 stsp *outsize = 0;
506 0e039681 2021-11-15 stsp if (fseek(f, 0L, SEEK_SET) == -1)
507 0e039681 2021-11-15 stsp return got_ferror(f, GOT_ERR_IO);
510 0e039681 2021-11-15 stsp flen = fread(fbuf, 1, sizeof(fbuf), f);
511 0e039681 2021-11-15 stsp if (flen == 0) {
512 0e039681 2021-11-15 stsp if (ferror(f))
513 0e039681 2021-11-15 stsp return got_error_from_errno("fread");
514 0e039681 2021-11-15 stsp if (feof(f))
517 0e039681 2021-11-15 stsp outlen = write(outfd, fbuf, flen);
518 0e039681 2021-11-15 stsp if (outlen == -1)
519 0e039681 2021-11-15 stsp return got_error_from_errno("write");
520 0e039681 2021-11-15 stsp if (outlen != flen)
521 0e039681 2021-11-15 stsp return got_error(GOT_ERR_IO);
522 0e039681 2021-11-15 stsp *outsize += outlen;
525 0e039681 2021-11-15 stsp return NULL;
528 0e039681 2021-11-15 stsp static const struct got_error *
529 0e039681 2021-11-15 stsp merge_binary_file(int *overlapcnt, int merged_fd,
530 0e039681 2021-11-15 stsp FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
531 0e039681 2021-11-15 stsp const char *label_deriv, const char *label_orig, const char *label_deriv2,
532 0e039681 2021-11-15 stsp const char *ondisk_path)
534 0e039681 2021-11-15 stsp const struct got_error *err = NULL;
535 0e039681 2021-11-15 stsp int same_content, changed_deriv, changed_deriv2;
536 0e039681 2021-11-15 stsp int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
537 0e039681 2021-11-15 stsp off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
538 0e039681 2021-11-15 stsp char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
539 0e039681 2021-11-15 stsp char *base_path_orig = NULL, *base_path_deriv = NULL;
540 0e039681 2021-11-15 stsp char *base_path_deriv2 = NULL;
542 0e039681 2021-11-15 stsp *overlapcnt = 0;
544 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_deriv2);
546 0e039681 2021-11-15 stsp return err;
548 0e039681 2021-11-15 stsp if (same_content)
549 0e039681 2021-11-15 stsp return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
551 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_orig);
553 0e039681 2021-11-15 stsp return err;
554 0e039681 2021-11-15 stsp changed_deriv = !same_content;
555 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv2, f_orig);
557 0e039681 2021-11-15 stsp return err;
558 0e039681 2021-11-15 stsp changed_deriv2 = !same_content;
560 0e039681 2021-11-15 stsp if (changed_deriv && changed_deriv2) {
561 0e039681 2021-11-15 stsp *overlapcnt = 1;
562 0e039681 2021-11-15 stsp if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
563 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
566 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
567 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
570 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
571 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
574 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_orig, &fd_orig,
575 b90054ed 2022-11-01 stsp base_path_orig, "");
578 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
579 b90054ed 2022-11-01 stsp base_path_deriv, "");
582 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
583 b90054ed 2022-11-01 stsp base_path_deriv2, "");
586 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
589 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
592 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
595 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "Binary files differ and cannot be "
596 0e039681 2021-11-15 stsp "merged automatically:\n") < 0) {
597 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
600 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
601 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
602 0e039681 2021-11-15 stsp label_deriv ? " " : "",
603 0e039681 2021-11-15 stsp label_deriv ? label_deriv : "",
604 0e039681 2021-11-15 stsp path_deriv) < 0) {
605 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
608 0e039681 2021-11-15 stsp if (size_orig > 0) {
609 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
610 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_ORIG,
611 0e039681 2021-11-15 stsp label_orig ? " " : "",
612 0e039681 2021-11-15 stsp label_orig ? label_orig : "",
613 0e039681 2021-11-15 stsp path_orig) < 0) {
614 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
618 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
619 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
620 0e039681 2021-11-15 stsp path_deriv2,
621 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_END,
622 0e039681 2021-11-15 stsp label_deriv2 ? " " : "",
623 0e039681 2021-11-15 stsp label_deriv2 ? label_deriv2 : "") < 0) {
624 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
627 0e039681 2021-11-15 stsp } else if (changed_deriv)
628 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
629 0e039681 2021-11-15 stsp else if (changed_deriv2)
630 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
632 0e039681 2021-11-15 stsp if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
633 0e039681 2021-11-15 stsp err == NULL)
634 0e039681 2021-11-15 stsp err = got_error_from_errno2("unlink", path_orig);
635 0e039681 2021-11-15 stsp if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
636 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_orig);
637 0e039681 2021-11-15 stsp if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
638 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv);
639 0e039681 2021-11-15 stsp if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
640 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv2);
641 0e039681 2021-11-15 stsp free(path_orig);
642 0e039681 2021-11-15 stsp free(path_deriv);
643 0e039681 2021-11-15 stsp free(path_deriv2);
644 0e039681 2021-11-15 stsp free(base_path_orig);
645 0e039681 2021-11-15 stsp free(base_path_deriv);
646 0e039681 2021-11-15 stsp free(base_path_deriv2);
647 0e039681 2021-11-15 stsp return err;
651 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
652 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
653 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
654 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
655 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
657 6353ad76 2019-02-08 stsp static const struct got_error *
658 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
659 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
660 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
661 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
662 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
663 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
665 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
666 6353ad76 2019-02-08 stsp int merged_fd = -1;
667 db590691 2021-06-02 stsp FILE *f_merged = NULL;
668 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
669 234035bc 2019-06-01 stsp int overlapcnt = 0;
670 3524bbf9 2020-10-20 stsp char *parent = NULL;
672 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
674 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
676 3524bbf9 2020-10-20 stsp return err;
678 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
679 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
683 b90054ed 2022-11-01 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
687 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
688 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
690 0e039681 2021-11-15 stsp if (err->code != GOT_ERR_FILE_BINARY)
692 0e039681 2021-11-15 stsp err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
693 0e039681 2021-11-15 stsp f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
694 0e039681 2021-11-15 stsp ondisk_path);
699 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
700 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
704 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
705 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
709 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
710 db590691 2021-06-02 stsp if (f_merged == NULL) {
711 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
714 db590691 2021-06-02 stsp merged_fd = -1;
716 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
717 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
718 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
724 b2b3fce1 2022-10-29 op if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
725 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
729 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
730 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
731 230a42bd 2019-05-11 jcs ondisk_path);
736 fdcb7daf 2019-12-15 stsp if (merged_path)
737 fdcb7daf 2019-12-15 stsp unlink(merged_path);
739 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
740 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
741 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
742 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
743 6353ad76 2019-02-08 stsp free(merged_path);
744 af54ae4a 2019-02-19 stsp free(base_path);
745 3524bbf9 2020-10-20 stsp free(parent);
746 af57b12a 2020-07-23 stsp return err;
749 af57b12a 2020-07-23 stsp static const struct got_error *
750 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
751 af57b12a 2020-07-23 stsp size_t target_len)
753 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
754 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
755 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
756 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
757 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
758 af57b12a 2020-07-23 stsp ondisk_path);
759 af57b12a 2020-07-23 stsp return NULL;
763 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
764 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
765 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
766 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
768 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
769 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
771 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
772 993e2a1b 2020-07-23 stsp * has deleted this symlink.
774 11cc08c1 2020-07-23 stsp static const struct got_error *
775 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
776 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
777 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
779 11cc08c1 2020-07-23 stsp const struct got_error *err;
780 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
781 11cc08c1 2020-07-23 stsp FILE *f = NULL;
783 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
785 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
787 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
788 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
789 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
793 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
795 3818e3c4 2020-11-01 naddy goto done;
797 b2b3fce1 2022-10-29 op if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
798 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
802 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
803 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
804 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
805 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
806 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
807 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
808 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
809 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
810 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
811 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
815 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
816 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
819 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
820 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
824 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
825 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
826 11cc08c1 2020-07-23 stsp free(path);
827 11cc08c1 2020-07-23 stsp free(id_str);
828 11cc08c1 2020-07-23 stsp free(label_deriv);
829 11cc08c1 2020-07-23 stsp return err;
832 d219f183 2020-07-23 stsp /* forward declaration */
833 d219f183 2020-07-23 stsp static const struct got_error *
834 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
835 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
836 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
837 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
840 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
841 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
842 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
843 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
845 af57b12a 2020-07-23 stsp static const struct got_error *
846 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
847 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
848 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
849 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
850 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
852 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
853 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
854 af57b12a 2020-07-23 stsp struct stat sb;
855 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
856 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
857 11cc08c1 2020-07-23 stsp int have_local_change = 0;
858 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
860 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
861 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
863 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
864 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
865 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
866 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
867 af57b12a 2020-07-23 stsp ondisk_path);
870 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
872 526a746f 2020-07-23 stsp if (blob_orig) {
873 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
878 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
879 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
880 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
881 11cc08c1 2020-07-23 stsp have_local_change = 1;
883 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
884 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
885 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
886 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
887 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
889 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
890 11cc08c1 2020-07-23 stsp if (ancestor_target) {
891 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
892 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
894 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
895 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
896 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
897 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
900 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
901 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
902 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
903 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
906 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
909 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
910 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
911 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
912 af57b12a 2020-07-23 stsp strlen(deriv_target));
915 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
916 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
917 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
918 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
919 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
920 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
923 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
924 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
925 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
928 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
934 af57b12a 2020-07-23 stsp free(ancestor_target);
935 eec2f5a9 2021-06-03 stsp return err;
938 eec2f5a9 2021-06-03 stsp static const struct got_error *
939 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
941 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
942 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
943 eec2f5a9 2021-06-03 stsp ssize_t target_len;
947 eec2f5a9 2021-06-03 stsp *outfile = NULL;
949 eec2f5a9 2021-06-03 stsp f = got_opentemp();
950 eec2f5a9 2021-06-03 stsp if (f == NULL)
951 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
952 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
953 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
954 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
957 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
958 eec2f5a9 2021-06-03 stsp if (n != target_len) {
959 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
962 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
963 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
966 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
967 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
974 eec2f5a9 2021-06-03 stsp *outfile = f;
975 14c901f1 2019-08-08 stsp return err;
979 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
980 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
981 14c901f1 2019-08-08 stsp * acts as the second derived version.
983 14c901f1 2019-08-08 stsp static const struct got_error *
984 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
985 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
986 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
987 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
988 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
989 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
991 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
992 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
993 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
994 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
995 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
997 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
999 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1001 ed6b5030 2020-10-20 stsp return err;
1003 67a66647 2021-05-31 stsp if (blob_orig) {
1004 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
1005 67a66647 2021-05-31 stsp parent) == -1) {
1006 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
1007 67a66647 2021-05-31 stsp base_path = NULL;
1008 67a66647 2021-05-31 stsp goto done;
1011 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_orig_path, &f_orig,
1012 b90054ed 2022-11-01 stsp base_path, "");
1014 67a66647 2021-05-31 stsp goto done;
1015 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1016 67a66647 2021-05-31 stsp blob_orig);
1018 67a66647 2021-05-31 stsp goto done;
1019 67a66647 2021-05-31 stsp free(base_path);
1022 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1023 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1024 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1026 db590691 2021-06-02 stsp f_orig = got_opentemp();
1027 db590691 2021-06-02 stsp if (f_orig == NULL) {
1028 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1029 db590691 2021-06-02 stsp goto done;
1033 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1034 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1035 14c901f1 2019-08-08 stsp base_path = NULL;
1036 14c901f1 2019-08-08 stsp goto done;
1039 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1041 14c901f1 2019-08-08 stsp goto done;
1042 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1043 14c901f1 2019-08-08 stsp blob_deriv);
1045 14c901f1 2019-08-08 stsp goto done;
1047 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1049 14c901f1 2019-08-08 stsp goto done;
1050 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1051 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1052 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1053 14c901f1 2019-08-08 stsp goto done;
1057 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1058 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1060 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1061 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1063 eec2f5a9 2021-06-03 stsp goto done;
1066 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1067 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1068 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1069 eec2f5a9 2021-06-03 stsp goto done;
1071 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1072 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1073 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1074 eec2f5a9 2021-06-03 stsp close(fd);
1075 eec2f5a9 2021-06-03 stsp goto done;
1079 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1080 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1081 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1083 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1084 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1085 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1086 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1087 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1088 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1089 14c901f1 2019-08-08 stsp free(base_path);
1090 67a66647 2021-05-31 stsp if (blob_orig_path) {
1091 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1092 67a66647 2021-05-31 stsp free(blob_orig_path);
1094 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1095 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1096 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1098 6353ad76 2019-02-08 stsp free(id_str);
1099 818c7501 2019-07-11 stsp free(label_deriv);
1100 ed6b5030 2020-10-20 stsp free(parent);
1101 4a1ddfc2 2019-01-12 stsp return err;
1104 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1105 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1106 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1107 ef623445 2023-09-16 op int wt_fd, const char *path, struct got_object_id *blob_id,
1108 ef623445 2023-09-16 op int update_timestamps)
1110 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1111 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1113 65b05cec 2020-07-23 stsp *new_iep = NULL;
1115 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1117 054041d0 2020-06-24 stsp return err;
1119 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1120 ef623445 2023-09-16 op blob_id->sha1, base_commit_id->sha1, update_timestamps);
1122 054041d0 2020-06-24 stsp goto done;
1124 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1127 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1129 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1130 13d9040b 2019-03-27 stsp return err;
1133 1ebedb77 2019-10-19 stsp static mode_t
1134 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1136 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1138 1ebedb77 2019-10-19 stsp if (executable) {
1139 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1140 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1141 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1142 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1143 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1144 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1147 b2b3fce1 2022-10-29 op return st_mode;
1150 8ba819a3 2020-07-23 stsp /* forward declaration */
1151 13d9040b 2019-03-27 stsp static const struct got_error *
1152 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1153 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1154 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1155 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1156 ef623445 2023-09-16 op int path_is_unversioned, int *update_timestamps,
1157 ef623445 2023-09-16 op struct got_repository *repo,
1158 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1161 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1162 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1164 8ba819a3 2020-07-23 stsp static const struct got_error *
1165 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1166 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1168 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1169 5a1fbc73 2020-07-23 stsp ssize_t elen;
1170 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1173 c6e8a826 2021-04-05 stsp *did_something = 0;
1176 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1177 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1178 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1179 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1180 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1181 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1183 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1184 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1185 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
1186 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1188 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1189 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1190 5a1fbc73 2020-07-23 stsp if (elen == -1)
1191 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1193 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1194 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1195 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1198 c6e8a826 2021-04-05 stsp *did_something = 1;
1199 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1200 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1201 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1202 5a1fbc73 2020-07-23 stsp return err;
1205 3c1ec782 2020-07-23 stsp static const struct got_error *
1206 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1207 df6221c7 2023-07-19 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path,
1208 df6221c7 2023-07-19 stsp const char *meta_dir)
1210 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1211 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1212 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1214 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1216 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1217 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1218 3c1ec782 2020-07-23 stsp return NULL;
1222 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1223 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1224 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1225 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1226 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1228 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1229 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1230 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1232 ce031e9e 2020-10-20 stsp return err;
1233 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1234 ce031e9e 2020-10-20 stsp free(parent);
1235 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1237 ce031e9e 2020-10-20 stsp free(parent);
1238 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1239 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1240 3c1ec782 2020-07-23 stsp free(abspath);
1241 3c1ec782 2020-07-23 stsp return err;
1243 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1244 3c1ec782 2020-07-23 stsp free(abspath);
1246 3c1ec782 2020-07-23 stsp return err;
1248 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1250 3c1ec782 2020-07-23 stsp return err;
1253 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1254 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1255 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1256 3c1ec782 2020-07-23 stsp return NULL;
1259 df6221c7 2023-07-19 stsp /* Do not allow symlinks pointing into the meta directory. */
1260 df6221c7 2023-07-19 stsp if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1261 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1262 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1263 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1265 3c1ec782 2020-07-23 stsp free(path_got);
1266 3c1ec782 2020-07-23 stsp return NULL;
1269 5a1fbc73 2020-07-23 stsp static const struct got_error *
1270 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1271 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1272 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1273 5267b9e4 2021-09-26 stsp int path_is_unversioned, int allow_bad_symlinks,
1274 5267b9e4 2021-09-26 stsp struct got_repository *repo,
1275 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1277 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1278 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1279 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1280 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1281 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1283 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1286 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1287 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1288 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1289 8ba819a3 2020-07-23 stsp * in the blob object.
1292 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1293 538b6881 2022-07-21 florian if (err)
1294 538b6881 2022-07-21 florian return err;
1296 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1297 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1298 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1299 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1300 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1301 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1302 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1303 ef623445 2023-09-16 op 1, path_is_unversioned, NULL, repo, progress_cb,
1304 3b9f0f87 2020-07-23 stsp progress_arg);
1306 8ba819a3 2020-07-23 stsp if (len > 0) {
1307 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1308 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1309 8ba819a3 2020-07-23 stsp len - hdrlen);
1310 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1311 8ba819a3 2020-07-23 stsp hdrlen = 0;
1313 8ba819a3 2020-07-23 stsp } while (len != 0);
1314 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1316 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1317 df6221c7 2023-07-19 stsp ondisk_path, worktree->root_path, worktree->meta_dir);
1319 3c1ec782 2020-07-23 stsp return err;
1321 5267b9e4 2021-09-26 stsp if (*is_bad_symlink && !allow_bad_symlinks) {
1322 906c123b 2020-07-23 stsp /* install as a regular file */
1323 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1324 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1325 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1326 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1327 ef623445 2023-09-16 op path_is_unversioned, NULL, repo,
1328 ef623445 2023-09-16 op progress_cb, progress_arg);
1329 3c29341b 2022-07-21 florian return err;
1332 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1333 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1334 c6e8a826 2021-04-05 stsp int symlink_replaced;
1335 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1336 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1337 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1338 3c29341b 2022-07-21 florian return err;
1340 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1341 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1343 3c29341b 2022-07-21 florian return err;
1344 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1345 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1346 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1347 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1348 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1349 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1351 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1352 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1355 3c29341b 2022-07-21 florian return err; /* Nothing else to do. */
1358 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1359 f4994adc 2020-10-20 stsp char *parent;
1360 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1362 3c29341b 2022-07-21 florian return err;
1363 6a390967 2023-07-14 stsp err = got_path_mkdir(parent);
1364 f4994adc 2020-10-20 stsp free(parent);
1366 3c29341b 2022-07-21 florian return err;
1368 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1369 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1371 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1372 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1373 6a390967 2023-07-14 stsp if (progress_cb) {
1374 6a390967 2023-07-14 stsp err = (*progress_cb)(progress_arg,
1375 6a390967 2023-07-14 stsp reverting_versioned_file ?
1376 6a390967 2023-07-14 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD,
1379 3c29341b 2022-07-21 florian return err;
1383 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1384 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1385 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1386 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1387 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1388 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1389 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1390 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1391 ef623445 2023-09-16 op path_is_unversioned, NULL, repo,
1392 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1393 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1394 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1395 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1397 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1398 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1400 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1401 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1402 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1403 8ba819a3 2020-07-23 stsp return err;
1406 8ba819a3 2020-07-23 stsp static const struct got_error *
1407 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1408 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1409 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1410 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1411 ef623445 2023-09-16 op int path_is_unversioned, int *update_timestamps,
1412 ef623445 2023-09-16 op struct got_repository *repo,
1413 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1415 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1416 507dc3bb 2018-12-29 stsp int fd = -1;
1417 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1418 507dc3bb 2018-12-29 stsp int update = 0;
1419 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1420 b2b3fce1 2022-10-29 op mode_t mode;
1422 ef623445 2023-09-16 op if (update_timestamps)
1423 ef623445 2023-09-16 op *update_timestamps = 1;
1425 b2b3fce1 2022-10-29 op mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1426 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1427 b2b3fce1 2022-10-29 op O_CLOEXEC, mode);
1428 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1429 07fa9365 2023-03-10 stsp if (errno == ENOENT || errno == ENOTDIR) {
1430 f5375317 2020-10-20 stsp char *parent;
1431 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1433 f5375317 2020-10-20 stsp return err;
1434 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1435 07fa9365 2023-03-10 stsp if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1436 07fa9365 2023-03-10 stsp err = got_error_path(path, err->code);
1437 f5375317 2020-10-20 stsp free(parent);
1439 21908da4 2019-01-13 stsp return err;
1440 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1441 8bd0cdad 2021-12-31 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1443 21908da4 2019-01-13 stsp if (fd == -1)
1444 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1445 230a42bd 2019-05-11 jcs ondisk_path);
1446 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1447 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1448 ef623445 2023-09-16 op if (update_timestamps)
1449 ef623445 2023-09-16 op *update_timestamps = 0;
1450 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1451 ef623445 2023-09-16 op GOT_STATUS_EXISTS, path);
1452 3b9f0f87 2020-07-23 stsp goto done;
1454 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1455 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1456 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1457 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1458 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1459 507dc3bb 2018-12-29 stsp goto done;
1461 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1462 b90054ed 2022-11-01 stsp ondisk_path, "");
1464 507dc3bb 2018-12-29 stsp goto done;
1465 507dc3bb 2018-12-29 stsp update = 1;
1467 b2b3fce1 2022-10-29 op if (fchmod(fd, apply_umask(mode)) == -1) {
1468 b2b3fce1 2022-10-29 op err = got_error_from_errno2("fchmod",
1474 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1477 bd6aa359 2020-07-23 stsp if (progress_cb) {
1478 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1479 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1481 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1482 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1485 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1486 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1488 bd6aa359 2020-07-23 stsp goto done;
1491 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1493 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1494 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1497 9d31a1d8 2018-03-11 stsp if (len > 0) {
1498 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1499 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1500 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1501 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1502 b87c6f83 2018-12-24 stsp goto done;
1503 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1504 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1505 b87c6f83 2018-12-24 stsp goto done;
1507 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1509 9d31a1d8 2018-03-11 stsp } while (len != 0);
1511 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1512 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1513 816dc654 2019-02-16 stsp goto done;
1516 507dc3bb 2018-12-29 stsp if (update) {
1517 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1518 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1519 f6d8c0ac 2020-11-09 stsp goto done;
1521 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1522 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1523 230a42bd 2019-05-11 jcs ondisk_path);
1524 68ed9ba5 2019-02-10 stsp goto done;
1526 63df146d 2020-11-09 stsp free(tmppath);
1527 63df146d 2020-11-09 stsp tmppath = NULL;
1531 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1532 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1533 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1534 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1535 507dc3bb 2018-12-29 stsp free(tmppath);
1536 6353ad76 2019-02-08 stsp return err;
1540 12383673 2023-02-18 mark * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1541 12383673 2023-02-18 mark * conflict marker is found in newly added lines only.
1543 6353ad76 2019-02-08 stsp static const struct got_error *
1544 12383673 2023-02-18 mark get_modified_file_content_status(unsigned char *status,
1545 12383673 2023-02-18 mark struct got_blob_object *blob, const char *path, struct stat *sb,
1546 12383673 2023-02-18 mark FILE *ondisk_file)
1548 d952957d 2023-02-19 mark const struct got_error *err, *free_err;
1549 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1550 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1551 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1552 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1554 d952957d 2023-02-19 mark FILE *f1 = NULL;
1555 d952957d 2023-02-19 mark struct got_diffreg_result *diffreg_result = NULL;
1556 d952957d 2023-02-19 mark struct diff_result *r;
1557 d952957d 2023-02-19 mark int nchunks_parsed, n, i = 0, ln = 0;
1558 9bdd68dd 2020-01-02 naddy char *line = NULL;
1559 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1560 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1562 d952957d 2023-02-19 mark if (*status != GOT_STATUS_MODIFY)
1563 d952957d 2023-02-19 mark return NULL;
1565 d952957d 2023-02-19 mark f1 = got_opentemp();
1566 d952957d 2023-02-19 mark if (f1 == NULL)
1567 d952957d 2023-02-19 mark return got_error_from_errno("got_opentemp");
1569 d952957d 2023-02-19 mark if (blob) {
1570 d952957d 2023-02-19 mark got_object_blob_rewind(blob);
1571 d952957d 2023-02-19 mark err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1573 12383673 2023-02-18 mark goto done;
1576 d952957d 2023-02-19 mark err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1577 d952957d 2023-02-19 mark 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1579 d952957d 2023-02-19 mark goto done;
1581 d952957d 2023-02-19 mark r = diffreg_result->result;
1583 d952957d 2023-02-19 mark for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1584 d952957d 2023-02-19 mark struct diff_chunk *c;
1585 d952957d 2023-02-19 mark struct diff_chunk_context cc = {};
1586 7783f73c 2023-02-20 mark off_t pos;
1589 d952957d 2023-02-19 mark * We can optimise a little by advancing straight
1590 d952957d 2023-02-19 mark * to the next chunk if this one has no added lines.
1592 d952957d 2023-02-19 mark c = diff_chunk_get(r, n);
1594 45e6b2f4 2023-02-20 mark if (diff_chunk_type(c) != CHUNK_PLUS) {
1595 d952957d 2023-02-19 mark nchunks_parsed = 1;
1596 7783f73c 2023-02-20 mark continue; /* removed or unchanged lines */
1599 7783f73c 2023-02-20 mark pos = diff_chunk_get_right_start_pos(c);
1600 7783f73c 2023-02-20 mark if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1601 7783f73c 2023-02-20 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1602 7783f73c 2023-02-20 mark goto done;
1605 7783f73c 2023-02-20 mark diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1606 7783f73c 2023-02-20 mark ln = cc.right.start;
1608 d952957d 2023-02-19 mark while (ln < cc.right.end) {
1609 d952957d 2023-02-19 mark linelen = getline(&line, &linesize, ondisk_file);
1610 d952957d 2023-02-19 mark if (linelen == -1) {
1611 d952957d 2023-02-19 mark if (feof(ondisk_file))
1613 d952957d 2023-02-19 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1617 d952957d 2023-02-19 mark if (line && strncmp(line, markers[i],
1618 d952957d 2023-02-19 mark strlen(markers[i])) == 0) {
1619 d952957d 2023-02-19 mark if (strcmp(markers[i],
1620 d952957d 2023-02-19 mark GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1621 d952957d 2023-02-19 mark *status = GOT_STATUS_CONFLICT;
1622 d952957d 2023-02-19 mark goto done;
1631 9bdd68dd 2020-01-02 naddy free(line);
1632 12383673 2023-02-18 mark if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1633 12383673 2023-02-18 mark err = got_error_from_errno("fclose");
1634 d952957d 2023-02-19 mark free_err = got_diffreg_result_free(diffreg_result);
1635 d952957d 2023-02-19 mark if (err == NULL)
1636 d952957d 2023-02-19 mark err = free_err;
1638 7154f6ce 2019-03-27 stsp return err;
1641 e2b1e152 2019-07-13 stsp static int
1642 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1644 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1645 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1648 1ebedb77 2019-10-19 stsp static int
1649 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1651 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1652 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1653 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1654 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1655 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1656 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1659 c363b2c1 2019-08-03 stsp static unsigned char
1660 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1662 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1663 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1664 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1665 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1666 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1667 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1668 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1670 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1674 a919d5c4 2020-07-23 stsp static const struct got_error *
1675 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1676 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1677 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1679 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1680 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1681 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1682 a919d5c4 2020-07-23 stsp ssize_t elen;
1683 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1684 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1685 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1687 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1689 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1691 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1693 a919d5c4 2020-07-23 stsp return err;
1694 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1696 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1697 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1699 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1701 a919d5c4 2020-07-23 stsp if (len > 0) {
1702 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1703 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1704 a919d5c4 2020-07-23 stsp len - hdrlen);
1705 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1706 a919d5c4 2020-07-23 stsp hdrlen = 0;
1708 a919d5c4 2020-07-23 stsp } while (len != 0);
1709 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1711 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1712 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1713 a919d5c4 2020-07-23 stsp if (elen == -1)
1714 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1716 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1717 a919d5c4 2020-07-23 stsp if (elen == -1)
1718 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1721 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1722 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1724 a919d5c4 2020-07-23 stsp return NULL;
1727 7154f6ce 2019-03-27 stsp static const struct got_error *
1728 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1729 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1730 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1732 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1733 6353ad76 2019-02-08 stsp struct got_object_id id;
1734 6353ad76 2019-02-08 stsp size_t hdrlen;
1735 eb81bc23 2022-06-28 tracey int fd = -1, fd1 = -1;
1736 6353ad76 2019-02-08 stsp FILE *f = NULL;
1737 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1738 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1739 6353ad76 2019-02-08 stsp size_t flen, blen;
1740 2c4740ad 2023-02-12 mark unsigned char staged_status;
1742 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
1743 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1744 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1747 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1748 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1749 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1751 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1752 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1753 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1754 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1755 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1757 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1758 882ef1b9 2019-12-13 stsp goto done;
1760 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1761 3d35a492 2019-12-13 stsp goto done;
1764 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1765 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1766 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1767 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1768 5c02d2a5 2021-09-26 stsp else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1769 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1770 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1771 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1772 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1773 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1774 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1776 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1777 3d35a492 2019-12-13 stsp goto done;
1779 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1780 1338848f 2019-12-13 stsp goto done;
1784 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1785 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1786 1338848f 2019-12-13 stsp goto done;
1789 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1790 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1791 1338848f 2019-12-13 stsp goto done;
1792 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1793 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1794 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1795 1338848f 2019-12-13 stsp goto done;
1798 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1799 f179e66d 2020-07-23 stsp goto done;
1801 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1802 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1803 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1804 1338848f 2019-12-13 stsp goto done;
1807 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1808 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1809 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
1811 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
1813 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1814 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1815 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1816 eb81bc23 2022-06-28 tracey goto done;
1818 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1820 1338848f 2019-12-13 stsp goto done;
1822 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1823 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1824 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1825 a919d5c4 2020-07-23 stsp goto done;
1828 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1829 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1830 ab0d4361 2019-12-13 stsp if (fd == -1) {
1831 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1832 ab0d4361 2019-12-13 stsp goto done;
1836 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1837 6353ad76 2019-02-08 stsp if (f == NULL) {
1838 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1839 6353ad76 2019-02-08 stsp goto done;
1842 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1844 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1845 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1847 7154f6ce 2019-03-27 stsp goto done;
1848 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1849 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1850 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1851 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1852 7154f6ce 2019-03-27 stsp goto done;
1854 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1855 6353ad76 2019-02-08 stsp if (flen != 0)
1856 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1858 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1859 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1860 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1862 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1863 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1864 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1865 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1869 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1872 6353ad76 2019-02-08 stsp hdrlen = 0;
1875 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1876 7154f6ce 2019-03-27 stsp rewind(f);
1877 12383673 2023-02-18 mark err = get_modified_file_content_status(status, blob, ie->path,
1879 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1880 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1882 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1883 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1885 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1886 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1887 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1888 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1889 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1890 9d31a1d8 2018-03-11 stsp return err;
1894 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1895 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1897 e2b1e152 2019-07-13 stsp static const struct got_error *
1898 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1899 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1901 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1902 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1903 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1905 e2b1e152 2019-07-13 stsp return NULL;
1908 07fa9365 2023-03-10 stsp static const struct got_error *remove_ondisk_file(const char *, const char *);
1910 9d31a1d8 2018-03-11 stsp static const struct got_error *
1911 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1912 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1913 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1914 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1915 0584f854 2019-04-06 stsp void *progress_arg)
1917 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1918 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1919 eb81bc23 2022-06-28 tracey char *ondisk_path = NULL;
1920 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1921 b8f41171 2019-02-10 stsp struct stat sb;
1922 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
1923 ef623445 2023-09-16 op int update_timestamps;
1925 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1926 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1929 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1930 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1931 a76c42e6 2019-08-03 stsp goto done;
1933 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1936 abb4604f 2019-07-27 stsp goto done;
1937 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1938 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1940 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1941 07fa9365 2023-03-10 stsp if (errno != ENOENT && errno != ENOTDIR) {
1942 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1943 f6764181 2021-09-24 stsp ondisk_path);
1944 f6764181 2021-09-24 stsp goto done;
1946 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1947 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1949 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1950 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1952 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1956 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1958 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1959 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1960 b8f41171 2019-02-10 stsp goto done;
1962 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1964 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1965 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1967 5036ab18 2020-04-18 stsp goto done;
1970 07fa9365 2023-03-10 stsp if (S_ISDIR(te->mode)) { /* file changing into a directory */
1971 07fa9365 2023-03-10 stsp if (status == GOT_STATUS_UNVERSIONED) {
1972 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg, status, path);
1973 07fa9365 2023-03-10 stsp } else if (status != GOT_STATUS_NO_CHANGE &&
1974 07fa9365 2023-03-10 stsp status != GOT_STATUS_DELETE &&
1975 07fa9365 2023-03-10 stsp status != GOT_STATUS_NONEXISTENT &&
1976 07fa9365 2023-03-10 stsp status != GOT_STATUS_MISSING) {
1977 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg,
1978 07fa9365 2023-03-10 stsp GOT_STATUS_CANNOT_DELETE, path);
1979 07fa9365 2023-03-10 stsp } else if (ie) {
1980 07fa9365 2023-03-10 stsp if (status != GOT_STATUS_DELETE &&
1981 07fa9365 2023-03-10 stsp status != GOT_STATUS_NONEXISTENT &&
1982 07fa9365 2023-03-10 stsp status != GOT_STATUS_MISSING) {
1983 07fa9365 2023-03-10 stsp err = remove_ondisk_file(worktree->root_path,
1984 07fa9365 2023-03-10 stsp ie->path);
1985 07fa9365 2023-03-10 stsp if (err && !(err->code == GOT_ERR_ERRNO &&
1986 07fa9365 2023-03-10 stsp errno == ENOENT))
1987 07fa9365 2023-03-10 stsp goto done;
1989 07fa9365 2023-03-10 stsp got_fileindex_entry_remove(fileindex, ie);
1990 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1991 07fa9365 2023-03-10 stsp ie->path);
1993 07fa9365 2023-03-10 stsp goto done; /* nothing else to do */
1996 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1997 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1998 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
2000 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
2001 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
2002 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
2003 c6e8a826 2021-04-05 stsp * updating contents of this file.
2005 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
2006 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
2007 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
2008 c6e8a826 2021-04-05 stsp /* Same commit. */
2009 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2010 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2012 e2b1e152 2019-07-13 stsp goto done;
2013 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2015 6353ad76 2019-02-08 stsp goto done;
2017 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
2018 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
2019 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
2020 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
2021 fd785a9a 2023-04-12 stsp if (got_fileindex_entry_has_commit(ie)) {
2022 fd785a9a 2023-04-12 stsp /* Update the base commit ID of this file. */
2023 fd785a9a 2023-04-12 stsp memcpy(ie->commit_sha1,
2024 fd785a9a 2023-04-12 stsp worktree->base_commit_id->sha1,
2025 fd785a9a 2023-04-12 stsp sizeof(ie->commit_sha1));
2027 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2028 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2030 0f58026f 2021-04-05 stsp goto done;
2031 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2033 b8f41171 2019-02-10 stsp goto done;
2037 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
2038 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
2039 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
2040 eb81bc23 2022-06-28 tracey goto done;
2042 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2044 6353ad76 2019-02-08 stsp goto done;
2046 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2047 234035bc 2019-06-01 stsp int update_timestamps;
2048 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2049 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2050 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2051 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
2052 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
2053 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
2054 eb81bc23 2022-06-28 tracey goto done;
2056 234035bc 2019-06-01 stsp struct got_object_id id2;
2057 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id2, ie);
2058 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2061 f69721c3 2019-10-21 stsp goto done;
2063 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2064 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2065 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2066 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2067 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2068 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2069 f69721c3 2019-10-21 stsp goto done;
2071 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2072 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2073 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2074 234035bc 2019-06-01 stsp goto done;
2077 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2078 36bf999c 2020-07-23 stsp char *link_target;
2079 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2081 36bf999c 2020-07-23 stsp goto done;
2082 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2083 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2084 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2085 36bf999c 2020-07-23 stsp free(link_target);
2087 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2088 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2089 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2090 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2092 f69721c3 2019-10-21 stsp free(label_orig);
2093 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2094 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2095 eb81bc23 2022-06-28 tracey goto done;
2097 234035bc 2019-06-01 stsp if (blob2)
2098 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2100 909d120e 2019-09-22 stsp goto done;
2102 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2103 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2104 234035bc 2019-06-01 stsp * unmodified files again.
2106 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2107 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2108 234035bc 2019-06-01 stsp update_timestamps);
2109 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2110 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2111 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2112 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2113 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2115 1ee397ad 2019-07-12 stsp goto done;
2116 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2117 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2119 13d9040b 2019-03-27 stsp goto done;
2121 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2122 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2123 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2124 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2125 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2126 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
2127 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
2129 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2130 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2131 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2132 ef623445 2023-09-16 op status == GOT_STATUS_UNVERSIONED,
2133 ef623445 2023-09-16 op &update_timestamps,
2134 ef623445 2023-09-16 op repo, progress_cb, progress_arg);
2137 13d9040b 2019-03-27 stsp goto done;
2140 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2141 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2142 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2144 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2145 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2146 ef623445 2023-09-16 op &blob->id, update_timestamps);
2149 13d9040b 2019-03-27 stsp goto done;
2151 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2152 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2153 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2157 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2158 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2159 eb81bc23 2022-06-28 tracey goto done;
2161 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2163 6353ad76 2019-02-08 stsp free(ondisk_path);
2164 8da9e5f4 2019-01-12 stsp return err;
2167 90285c3b 2019-01-08 stsp static const struct got_error *
2168 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2170 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2171 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2173 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2174 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2176 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2177 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2178 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2180 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2181 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2183 1c4cdd89 2021-06-20 stsp goto done;
2184 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2185 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2186 fddefe3b 2020-10-20 stsp free(ondisk_path);
2187 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2188 1c4cdd89 2021-06-20 stsp parent = NULL;
2189 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2190 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2191 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2192 fddefe3b 2020-10-20 stsp ondisk_path);
2195 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2201 90285c3b 2019-01-08 stsp free(ondisk_path);
2202 1c4cdd89 2021-06-20 stsp free(parent);
2203 90285c3b 2019-01-08 stsp return err;
2206 708d8e67 2019-03-27 stsp static const struct got_error *
2207 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2208 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2209 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2211 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2212 708d8e67 2019-03-27 stsp unsigned char status;
2213 708d8e67 2019-03-27 stsp struct stat sb;
2214 708d8e67 2019-03-27 stsp char *ondisk_path;
2216 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2217 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2219 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2221 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2223 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2225 5a58a424 2020-06-23 stsp goto done;
2227 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2228 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2229 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2230 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2231 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2232 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2233 993e2a1b 2020-07-23 stsp goto done;
2235 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2236 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2237 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2238 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2240 993e2a1b 2020-07-23 stsp goto done;
2241 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2242 993e2a1b 2020-07-23 stsp ie->path);
2243 993e2a1b 2020-07-23 stsp goto done;
2246 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2247 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2248 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2250 5a58a424 2020-06-23 stsp goto done;
2252 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2253 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2255 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2256 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2258 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2260 5a58a424 2020-06-23 stsp goto done;
2261 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2262 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2264 5a58a424 2020-06-23 stsp goto done;
2266 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2269 5a58a424 2020-06-23 stsp free(ondisk_path);
2270 fc6346c4 2019-03-27 stsp return err;
2273 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2274 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2275 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2276 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2277 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2278 8da9e5f4 2019-01-12 stsp void *progress_arg;
2279 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2280 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2283 512f0d0e 2019-01-02 stsp static const struct got_error *
2284 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2285 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2287 f6b8c3c2 2023-07-24 stsp const struct got_error *err = NULL;
2288 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2290 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
2291 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
2293 f6b8c3c2 2023-07-24 stsp return err;
2296 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2297 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2300 8da9e5f4 2019-01-12 stsp static const struct got_error *
2301 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2303 f6b8c3c2 2023-07-24 stsp const struct got_error *err = NULL;
2304 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2306 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
2307 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
2309 f6b8c3c2 2023-07-24 stsp return err;
2312 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2313 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2316 9d31a1d8 2018-03-11 stsp static const struct got_error *
2317 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2319 f6b8c3c2 2023-07-24 stsp const struct got_error *err = NULL;
2320 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2321 8da9e5f4 2019-01-12 stsp char *path;
2323 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
2324 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
2326 f6b8c3c2 2023-07-24 stsp return err;
2329 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2330 6a390967 2023-07-14 stsp return NULL;
2332 6a390967 2023-07-14 stsp if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2333 63c5ca5d 2019-08-24 stsp return NULL;
2335 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2336 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2338 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2340 6a390967 2023-07-14 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2341 6a390967 2023-07-14 stsp a->repo, a->progress_cb, a->progress_arg);
2343 8da9e5f4 2019-01-12 stsp free(path);
2344 0cd1c46a 2019-03-11 stsp return err;
2347 b2118c49 2020-07-28 stsp const struct got_error *
2348 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2350 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2352 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2353 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2354 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2355 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2358 b2118c49 2020-07-28 stsp return NULL;
2361 818c7501 2019-07-11 stsp static const struct got_error *
2362 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2364 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2365 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2367 517bab73 2019-03-11 stsp *refname = NULL;
2369 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2371 b2118c49 2020-07-28 stsp return err;
2373 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2374 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2375 0647c563 2019-03-11 stsp *refname = NULL;
2377 517bab73 2019-03-11 stsp free(uuidstr);
2378 517bab73 2019-03-11 stsp return err;
2381 818c7501 2019-07-11 stsp const struct got_error *
2382 9587e6cc 2023-01-28 mark got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2383 9587e6cc 2023-01-28 mark const char *prefix)
2385 9587e6cc 2023-01-28 mark return get_ref_name(refname, worktree, prefix);
2388 5662d61d 2023-07-03 jrick static const struct got_error *
2389 5662d61d 2023-07-03 jrick get_base_ref_name(char **refname, struct got_worktree *worktree)
2391 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2394 818c7501 2019-07-11 stsp static const struct got_error *
2395 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2397 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2398 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2401 818c7501 2019-07-11 stsp static const struct got_error *
2402 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2404 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2407 818c7501 2019-07-11 stsp static const struct got_error *
2408 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2410 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2411 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2414 818c7501 2019-07-11 stsp static const struct got_error *
2415 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2417 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2418 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2421 0ebf8283 2019-07-24 stsp static const struct got_error *
2422 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2424 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2425 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2428 0ebf8283 2019-07-24 stsp static const struct got_error *
2429 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2431 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2432 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2435 0ebf8283 2019-07-24 stsp static const struct got_error *
2436 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2438 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2439 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2442 0ebf8283 2019-07-24 stsp static const struct got_error *
2443 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2445 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2446 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2449 0ebf8283 2019-07-24 stsp const struct got_error *
2450 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2451 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2453 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2454 df6221c7 2023-07-19 stsp worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2455 0ebf8283 2019-07-24 stsp *path = NULL;
2456 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2458 0ebf8283 2019-07-24 stsp return NULL;
2461 f259c4c1 2021-09-24 stsp static const struct got_error *
2462 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2464 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2465 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2468 f259c4c1 2021-09-24 stsp static const struct got_error *
2469 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2471 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2472 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2476 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2477 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2479 517bab73 2019-03-11 stsp static const struct got_error *
2480 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2482 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2483 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2484 517bab73 2019-03-11 stsp char *refname;
2486 5662d61d 2023-07-03 jrick err = get_base_ref_name(&refname, worktree);
2488 517bab73 2019-03-11 stsp return err;
2490 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2492 0cd1c46a 2019-03-11 stsp goto done;
2494 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2496 0cd1c46a 2019-03-11 stsp free(refname);
2498 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2499 3e3a69f1 2019-07-25 stsp return err;
2502 3e3a69f1 2019-07-25 stsp static const struct got_error *
2503 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2505 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2507 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2508 df6221c7 2023-07-19 stsp worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2509 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2510 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2512 9d31a1d8 2018-03-11 stsp return err;
2516 ebf99748 2019-05-09 stsp static const struct got_error *
2517 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2518 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2520 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2521 ebf99748 2019-05-09 stsp FILE *index = NULL;
2523 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2524 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2525 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2526 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2528 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2530 ebf99748 2019-05-09 stsp goto done;
2532 00fe21f2 2021-12-31 stsp index = fopen(*fileindex_path, "rbe");
2533 ebf99748 2019-05-09 stsp if (index == NULL) {
2534 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2535 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2537 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2538 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2539 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2542 ebf99748 2019-05-09 stsp if (err) {
2543 ebf99748 2019-05-09 stsp free(*fileindex_path);
2544 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2545 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2546 ebf99748 2019-05-09 stsp *fileindex = NULL;
2548 ebf99748 2019-05-09 stsp return err;
2551 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2552 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2553 c932eeeb 2019-05-22 stsp const char *path;
2554 c932eeeb 2019-05-22 stsp size_t path_len;
2555 c932eeeb 2019-05-22 stsp const char *entry_name;
2556 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2557 a484d721 2019-06-10 stsp void *progress_arg;
2560 c932eeeb 2019-05-22 stsp static const struct got_error *
2561 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2563 1ee397ad 2019-07-12 stsp const struct got_error *err;
2564 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2566 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2567 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2568 c932eeeb 2019-05-22 stsp return NULL;
2569 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2570 102ff934 2019-06-11 stsp return NULL;
2572 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2573 a769b60b 2021-06-27 stsp return NULL;
2575 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2576 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2577 c932eeeb 2019-05-22 stsp return NULL;
2579 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2580 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2581 edd02c5e 2019-07-11 stsp ie->path);
2583 1ee397ad 2019-07-12 stsp return err;
2585 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2586 c932eeeb 2019-05-22 stsp return NULL;
2589 b0a0c9ed 2023-02-06 op /* Bump base commit ID of all files within an updated part of the work tree. */
2590 a615e0e7 2020-12-16 stsp static const struct got_error *
2591 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2592 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2593 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2595 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2597 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2598 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2599 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2600 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2601 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2602 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2604 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2605 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2608 9c6338c4 2019-06-02 stsp static const struct got_error *
2609 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2611 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2612 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2613 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2614 867630bb 2020-01-17 stsp struct timespec timeout;
2616 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2617 b90054ed 2022-11-01 stsp fileindex_path, "");
2619 9c6338c4 2019-06-02 stsp goto done;
2621 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2623 9c6338c4 2019-06-02 stsp goto done;
2625 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2626 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2627 9c6338c4 2019-06-02 stsp fileindex_path);
2628 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2632 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2633 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2634 867630bb 2020-01-17 stsp * was recorded in the file index.
2636 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2637 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2638 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2640 9c6338c4 2019-06-02 stsp if (new_index)
2641 9c6338c4 2019-06-02 stsp fclose(new_index);
2642 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2643 9c6338c4 2019-06-02 stsp return err;
2646 6ced4176 2019-07-12 stsp static const struct got_error *
2647 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2648 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2649 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit, struct got_worktree *worktree,
2650 a44927cc 2022-04-07 stsp struct got_repository *repo)
2652 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2653 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2654 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2655 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2657 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2658 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2659 6ced4176 2019-07-12 stsp *tree_id = NULL;
2661 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2662 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2663 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2664 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2665 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2666 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2667 6ced4176 2019-07-12 stsp goto done;
2669 a44927cc 2022-04-07 stsp err = got_object_id_by_path(tree_id, repo, base_commit,
2670 a44927cc 2022-04-07 stsp worktree->path_prefix);
2672 6ced4176 2019-07-12 stsp goto done;
2673 6ced4176 2019-07-12 stsp return NULL;
2676 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2678 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2679 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2680 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2681 6ced4176 2019-07-12 stsp goto done;
2684 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2686 6ced4176 2019-07-12 stsp goto done;
2688 6ced4176 2019-07-12 stsp free(in_repo_path);
2689 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2691 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2693 6ced4176 2019-07-12 stsp goto done;
2695 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2696 6ced4176 2019-07-12 stsp /* Check out a single file. */
2697 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2698 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2699 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2700 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2701 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2702 6ced4176 2019-07-12 stsp goto done;
2704 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2705 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2706 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2707 6ced4176 2019-07-12 stsp goto done;
2710 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2711 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2713 6ced4176 2019-07-12 stsp return err;
2714 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2715 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2716 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2717 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2718 6ced4176 2019-07-12 stsp goto done;
2721 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2722 a44927cc 2022-04-07 stsp base_commit, in_repo_path);
2724 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2725 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2726 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2727 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2728 6ced4176 2019-07-12 stsp goto done;
2730 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2731 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2732 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2733 6ced4176 2019-07-12 stsp goto done;
2738 6ced4176 2019-07-12 stsp free(in_repo_path);
2739 6ced4176 2019-07-12 stsp if (err) {
2740 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2741 6ced4176 2019-07-12 stsp free(*tree_relpath);
2742 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2743 6ced4176 2019-07-12 stsp free(*tree_id);
2744 6ced4176 2019-07-12 stsp *tree_id = NULL;
2746 07ed472d 2019-07-12 stsp return err;
2749 07ed472d 2019-07-12 stsp static const struct got_error *
2750 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2751 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2752 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2753 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2755 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2756 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2757 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2758 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2759 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2761 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2762 7f47418f 2019-12-20 stsp if (err) {
2763 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2764 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2765 7f47418f 2019-12-20 stsp goto done;
2766 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2767 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2769 7f47418f 2019-12-20 stsp return err;
2772 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2773 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2775 07ed472d 2019-07-12 stsp goto done;
2777 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2779 07ed472d 2019-07-12 stsp goto done;
2781 07ed472d 2019-07-12 stsp if (entry_name &&
2782 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2783 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2784 07ed472d 2019-07-12 stsp goto done;
2787 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2788 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2789 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2790 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2791 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2792 07ed472d 2019-07-12 stsp arg.repo = repo;
2793 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2794 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2795 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2796 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2797 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2798 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2801 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2802 07ed472d 2019-07-12 stsp if (commit)
2803 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2804 6ced4176 2019-07-12 stsp return err;
2807 9d31a1d8 2018-03-11 stsp const struct got_error *
2808 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2809 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2810 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2811 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2813 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2814 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2815 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2816 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2817 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2818 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2819 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2820 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2821 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2822 f2ea84fa 2019-07-27 stsp int entry_type;
2823 f2ea84fa 2019-07-27 stsp char *relpath;
2824 f2ea84fa 2019-07-27 stsp char *entry_name;
2825 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2826 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2828 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2830 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2832 9d31a1d8 2018-03-11 stsp return err;
2834 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
2835 a44927cc 2022-04-07 stsp worktree->base_commit_id);
2837 a44927cc 2022-04-07 stsp goto done;
2839 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2840 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2841 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2842 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2843 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2844 f2ea84fa 2019-07-27 stsp goto done;
2847 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2848 a44927cc 2022-04-07 stsp &tpd->relpath, &tpd->tree_id, pe->path, commit,
2849 a44927cc 2022-04-07 stsp worktree, repo);
2850 f2ea84fa 2019-07-27 stsp if (err) {
2851 f2ea84fa 2019-07-27 stsp free(tpd);
2852 6ced4176 2019-07-12 stsp goto done;
2855 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2856 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2857 f2ea84fa 2019-07-27 stsp if (err) {
2858 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2859 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2860 f2ea84fa 2019-07-27 stsp free(tpd);
2861 f2ea84fa 2019-07-27 stsp goto done;
2864 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2866 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2870 51514078 2018-12-25 stsp * Read the file index.
2871 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2872 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2874 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2876 c4cdcb68 2019-04-03 stsp goto done;
2878 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2879 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2880 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2882 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2883 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2884 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2888 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2889 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2890 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2891 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2892 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2893 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2894 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2895 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2899 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2901 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2902 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2903 af12c6b9 2019-06-04 stsp err = sync_err;
2905 9c6338c4 2019-06-02 stsp free(fileindex_path);
2907 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2908 9d31a1d8 2018-03-11 stsp if (commit)
2909 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2910 5ade8233 2019-07-12 stsp if (fileindex)
2911 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2912 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2913 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2914 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2915 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2916 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2917 f2ea84fa 2019-07-27 stsp free(tpd);
2919 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2920 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2921 9d31a1d8 2018-03-11 stsp err = unlockerr;
2922 f8d1f275 2019-02-04 stsp return err;
2925 9a298e5c 2023-03-10 stsp static const struct got_error *
2926 9a298e5c 2023-03-10 stsp add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2927 9a298e5c 2023-03-10 stsp struct got_fileindex_entry *ie, const char *ondisk_path,
2928 9a298e5c 2023-03-10 stsp const char *path2, struct got_blob_object *blob2, mode_t mode2,
2929 9a298e5c 2023-03-10 stsp int restoring_missing_file, int reverting_versioned_file,
2930 9a298e5c 2023-03-10 stsp int path_is_unversioned, int allow_bad_symlinks,
2931 9a298e5c 2023-03-10 stsp struct got_repository *repo,
2932 9a298e5c 2023-03-10 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2934 9a298e5c 2023-03-10 stsp const struct got_error *err = NULL;
2935 9a298e5c 2023-03-10 stsp int is_bad_symlink = 0;
2937 9a298e5c 2023-03-10 stsp if (S_ISLNK(mode2)) {
2938 9a298e5c 2023-03-10 stsp err = install_symlink(&is_bad_symlink,
2939 9a298e5c 2023-03-10 stsp worktree, ondisk_path, path2, blob2,
2940 9a298e5c 2023-03-10 stsp restoring_missing_file,
2941 9a298e5c 2023-03-10 stsp reverting_versioned_file,
2942 9a298e5c 2023-03-10 stsp path_is_unversioned, allow_bad_symlinks,
2943 9a298e5c 2023-03-10 stsp repo, progress_cb, progress_arg);
2945 9a298e5c 2023-03-10 stsp err = install_blob(worktree, ondisk_path, path2,
2946 9a298e5c 2023-03-10 stsp mode2, GOT_DEFAULT_FILE_MODE, blob2,
2947 9a298e5c 2023-03-10 stsp restoring_missing_file, reverting_versioned_file, 0,
2948 ef623445 2023-09-16 op path_is_unversioned, NULL, repo,
2949 ef623445 2023-09-16 op progress_cb, progress_arg);
2952 9a298e5c 2023-03-10 stsp return err;
2953 9a298e5c 2023-03-10 stsp if (ie == NULL) {
2954 9a298e5c 2023-03-10 stsp /* Adding an unversioned file. */
2955 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_alloc(&ie, path2);
2957 9a298e5c 2023-03-10 stsp return err;
2958 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_update(ie,
2959 9a298e5c 2023-03-10 stsp worktree->root_fd, path2, NULL, NULL, 1);
2960 9a298e5c 2023-03-10 stsp if (err) {
2961 9a298e5c 2023-03-10 stsp got_fileindex_entry_free(ie);
2962 9a298e5c 2023-03-10 stsp return err;
2964 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_add(fileindex, ie);
2965 9a298e5c 2023-03-10 stsp if (err) {
2966 9a298e5c 2023-03-10 stsp got_fileindex_entry_free(ie);
2967 9a298e5c 2023-03-10 stsp return err;
2970 9a298e5c 2023-03-10 stsp /* Re-adding a locally deleted file. */
2971 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_update(ie,
2972 9a298e5c 2023-03-10 stsp worktree->root_fd, path2, ie->blob_sha1,
2973 9a298e5c 2023-03-10 stsp worktree->base_commit_id->sha1, 0);
2975 9a298e5c 2023-03-10 stsp return err;
2978 9a298e5c 2023-03-10 stsp if (is_bad_symlink) {
2979 9a298e5c 2023-03-10 stsp got_fileindex_entry_filetype_set(ie,
2980 9a298e5c 2023-03-10 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2983 9a298e5c 2023-03-10 stsp return NULL;
2986 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2987 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2988 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2989 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2990 234035bc 2019-06-01 stsp void *progress_arg;
2991 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2992 234035bc 2019-06-01 stsp void *cancel_arg;
2993 f69721c3 2019-10-21 stsp const char *label_orig;
2994 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2995 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2998 234035bc 2019-06-01 stsp static const struct got_error *
2999 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
3000 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3001 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3002 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3003 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3005 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
3006 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
3007 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
3008 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
3009 234035bc 2019-06-01 stsp struct stat sb;
3010 234035bc 2019-06-01 stsp unsigned char status;
3011 234035bc 2019-06-01 stsp int local_changes_subsumed;
3012 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3013 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
3014 c2ba7aa6 2023-07-26 stsp struct got_object_id *id = NULL;
3016 234035bc 2019-06-01 stsp if (blob1 && blob2) {
3017 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3018 d6c87207 2019-08-02 stsp strlen(path2));
3019 1ee397ad 2019-07-12 stsp if (ie == NULL)
3020 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3021 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
3023 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3024 234035bc 2019-06-01 stsp path2) == -1)
3025 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3027 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3030 234035bc 2019-06-01 stsp goto done;
3032 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3033 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3034 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
3035 234035bc 2019-06-01 stsp goto done;
3037 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3038 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3039 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3040 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3041 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3042 234035bc 2019-06-01 stsp goto done;
3045 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3046 36bf999c 2020-07-23 stsp char *link_target2;
3047 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3049 36bf999c 2020-07-23 stsp goto done;
3050 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3051 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3052 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3053 36bf999c 2020-07-23 stsp free(link_target2);
3057 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3058 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3059 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3060 1af628f4 2021-06-03 stsp goto done;
3062 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3063 1af628f4 2021-06-03 stsp f_orig, blob1);
3065 1af628f4 2021-06-03 stsp goto done;
3067 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3068 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3069 1af628f4 2021-06-03 stsp goto done;
3070 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3071 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3073 1af628f4 2021-06-03 stsp goto done;
3075 c0df5966 2021-12-31 stsp fd = open(ondisk_path,
3076 c0df5966 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3077 1af628f4 2021-06-03 stsp if (fd == -1) {
3078 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3079 1af628f4 2021-06-03 stsp ondisk_path);
3080 1af628f4 2021-06-03 stsp goto done;
3082 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3083 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3084 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3085 1af628f4 2021-06-03 stsp ondisk_path);
3086 1af628f4 2021-06-03 stsp close(fd);
3087 1af628f4 2021-06-03 stsp goto done;
3089 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3091 1af628f4 2021-06-03 stsp goto done;
3092 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3093 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3094 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3095 1af628f4 2021-06-03 stsp goto done;
3097 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3098 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3099 c48f94a4 2023-01-29 stsp mode2, a->label_orig, NULL, label_deriv2,
3100 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3101 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3103 234035bc 2019-06-01 stsp } else if (blob1) {
3104 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3105 d6c87207 2019-08-02 stsp strlen(path1));
3106 1ee397ad 2019-07-12 stsp if (ie == NULL)
3107 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3108 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3110 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3111 2b92fad7 2019-06-02 stsp path1) == -1)
3112 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3114 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3117 2b92fad7 2019-06-02 stsp goto done;
3119 2b92fad7 2019-06-02 stsp switch (status) {
3120 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3121 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3122 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3124 1ee397ad 2019-07-12 stsp goto done;
3125 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3127 2b92fad7 2019-06-02 stsp goto done;
3129 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3131 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3132 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3133 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3134 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3136 1ee397ad 2019-07-12 stsp goto done;
3138 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3140 c2ba7aa6 2023-07-26 stsp case GOT_STATUS_MODIFY: {
3141 c2ba7aa6 2023-07-26 stsp FILE *blob1_f;
3142 c2ba7aa6 2023-07-26 stsp off_t blob1_size;
3143 c2ba7aa6 2023-07-26 stsp int obj_type;
3145 c2ba7aa6 2023-07-26 stsp * Delete the file only if its content already
3146 c2ba7aa6 2023-07-26 stsp * exists in the repository.
3148 c2ba7aa6 2023-07-26 stsp err = got_object_blob_file_create(&id, &blob1_f,
3149 c2ba7aa6 2023-07-26 stsp &blob1_size, path1);
3151 c2ba7aa6 2023-07-26 stsp goto done;
3152 c2ba7aa6 2023-07-26 stsp if (fclose(blob1_f) == EOF) {
3153 c2ba7aa6 2023-07-26 stsp err = got_error_from_errno("fclose");
3154 c2ba7aa6 2023-07-26 stsp goto done;
3157 c2ba7aa6 2023-07-26 stsp /* Implied existence check. */
3158 c2ba7aa6 2023-07-26 stsp err = got_object_get_type(&obj_type, repo, id);
3159 c2ba7aa6 2023-07-26 stsp if (err) {
3160 c2ba7aa6 2023-07-26 stsp if (err->code != GOT_ERR_NO_OBJ)
3161 c2ba7aa6 2023-07-26 stsp goto done;
3162 c2ba7aa6 2023-07-26 stsp err = (*a->progress_cb)(a->progress_arg,
3163 c2ba7aa6 2023-07-26 stsp GOT_STATUS_CANNOT_DELETE, path1);
3164 c2ba7aa6 2023-07-26 stsp goto done;
3165 c2ba7aa6 2023-07-26 stsp } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3166 c2ba7aa6 2023-07-26 stsp err = (*a->progress_cb)(a->progress_arg,
3167 c2ba7aa6 2023-07-26 stsp GOT_STATUS_CANNOT_DELETE, path1);
3168 c2ba7aa6 2023-07-26 stsp goto done;
3170 c2ba7aa6 2023-07-26 stsp err = (*a->progress_cb)(a->progress_arg,
3171 c2ba7aa6 2023-07-26 stsp GOT_STATUS_DELETE, path1);
3173 c2ba7aa6 2023-07-26 stsp goto done;
3174 c2ba7aa6 2023-07-26 stsp err = remove_ondisk_file(a->worktree->root_path,
3177 c2ba7aa6 2023-07-26 stsp goto done;
3179 c2ba7aa6 2023-07-26 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3182 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3183 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3184 72840534 2022-01-19 stsp off_t blob1_size;
3186 c2ba7aa6 2023-07-26 stsp * Delete the file only if its content already
3187 0a22ca1a 2020-09-23 stsp * exists in the repository.
3189 72840534 2022-01-19 stsp err = got_object_blob_file_create(&id, &blob1_f,
3190 72840534 2022-01-19 stsp &blob1_size, path1);
3192 0a22ca1a 2020-09-23 stsp goto done;
3193 c2ba7aa6 2023-07-26 stsp if (fclose(blob1_f) == EOF) {
3194 c2ba7aa6 2023-07-26 stsp err = got_error_from_errno("fclose");
3195 c2ba7aa6 2023-07-26 stsp goto done;
3197 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3198 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3199 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3201 0a22ca1a 2020-09-23 stsp goto done;
3202 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3205 0a22ca1a 2020-09-23 stsp goto done;
3207 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3210 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3211 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3214 0a22ca1a 2020-09-23 stsp goto done;
3217 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3218 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3219 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3221 1ee397ad 2019-07-12 stsp goto done;
3223 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3224 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3226 1ee397ad 2019-07-12 stsp goto done;
3231 234035bc 2019-06-01 stsp } else if (blob2) {
3232 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3233 234035bc 2019-06-01 stsp path2) == -1)
3234 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3235 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3236 d6c87207 2019-08-02 stsp strlen(path2));
3238 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3239 7f91a133 2019-12-13 stsp -1, NULL, repo);
3241 234035bc 2019-06-01 stsp goto done;
3242 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3243 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3244 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3245 9a298e5c 2023-03-10 stsp status != GOT_STATUS_ADD &&
3246 9a298e5c 2023-03-10 stsp status != GOT_STATUS_DELETE) {
3247 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3248 1ee397ad 2019-07-12 stsp status, path2);
3249 234035bc 2019-06-01 stsp goto done;
3251 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3252 36bf999c 2020-07-23 stsp char *link_target2;
3253 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3256 36bf999c 2020-07-23 stsp goto done;
3257 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3258 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3259 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3260 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3261 36bf999c 2020-07-23 stsp free(link_target2);
3262 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3263 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3264 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3265 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3266 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3267 526a746f 2020-07-23 stsp a->progress_arg);
3268 9a298e5c 2023-03-10 stsp } else if (status != GOT_STATUS_DELETE) {
3269 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3270 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3273 dfe9fba0 2020-07-23 stsp goto done;
3274 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3275 9a298e5c 2023-03-10 stsp /* Re-add file with content from new blob. */
3276 9a298e5c 2023-03-10 stsp err = add_file(a->worktree, a->fileindex, ie,
3277 9a298e5c 2023-03-10 stsp ondisk_path, path2, blob2, mode2,
3278 9a298e5c 2023-03-10 stsp 0, 0, 0, a->allow_bad_symlinks,
3279 9a298e5c 2023-03-10 stsp repo, a->progress_cb, a->progress_arg);
3281 234035bc 2019-06-01 stsp goto done;
3284 9a298e5c 2023-03-10 stsp err = add_file(a->worktree, a->fileindex, NULL,
3285 9a298e5c 2023-03-10 stsp ondisk_path, path2, blob2, mode2,
3286 9a298e5c 2023-03-10 stsp 0, 0, 1, a->allow_bad_symlinks,
3287 9a298e5c 2023-03-10 stsp repo, a->progress_cb, a->progress_arg);
3289 2b92fad7 2019-06-02 stsp goto done;
3293 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3294 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3295 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3296 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3297 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3298 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3299 1af628f4 2021-06-03 stsp free(id_str);
3301 54d5be07 2021-06-03 stsp free(label_deriv2);
3302 234035bc 2019-06-01 stsp free(ondisk_path);
3303 234035bc 2019-06-01 stsp return err;
3306 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args {
3307 cdbfe5d2 2023-07-24 stsp struct got_worktree *worktree;
3308 cdbfe5d2 2023-07-24 stsp got_cancel_cb cancel_cb;
3309 cdbfe5d2 2023-07-24 stsp void *cancel_arg;
3312 69de9dd4 2021-09-03 stsp static const struct got_error *
3313 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3315 f6b8c3c2 2023-07-24 stsp const struct got_error *err = NULL;
3316 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args *a = arg;
3318 cdbfe5d2 2023-07-24 stsp if (a->cancel_cb) {
3319 cdbfe5d2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
3321 cdbfe5d2 2023-07-24 stsp return err;
3324 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3325 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3326 cdbfe5d2 2023-07-24 stsp memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3327 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3328 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3330 69de9dd4 2021-09-03 stsp return NULL;
3333 c935fd51 2023-07-23 mark const struct got_error *
3334 c935fd51 2023-07-23 mark got_worktree_get_state(char *state, struct got_repository *repo,
3335 cdbfe5d2 2023-07-24 stsp struct got_worktree *worktree,
3336 cdbfe5d2 2023-07-24 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3338 c935fd51 2023-07-23 mark const struct got_error *err;
3339 c935fd51 2023-07-23 mark struct got_object_id *base_id, *head_id = NULL;
3340 c935fd51 2023-07-23 mark struct got_reference *head_ref;
3341 c935fd51 2023-07-23 mark struct got_fileindex *fileindex = NULL;
3342 c935fd51 2023-07-23 mark char *fileindex_path = NULL;
3343 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args cma;
3345 c935fd51 2023-07-23 mark if (worktree == NULL)
3346 c935fd51 2023-07-23 mark return got_error(GOT_ERR_NOT_WORKTREE);
3348 c935fd51 2023-07-23 mark err = got_ref_open(&head_ref, repo,
3349 c935fd51 2023-07-23 mark got_worktree_get_head_ref_name(worktree), 0);
3351 c935fd51 2023-07-23 mark return err;
3353 c935fd51 2023-07-23 mark err = got_ref_resolve(&head_id, repo, head_ref);
3355 c935fd51 2023-07-23 mark goto done;
3357 99301cec 2023-07-24 stsp *state = GOT_WORKTREE_STATE_UNKNOWN;
3358 c935fd51 2023-07-23 mark base_id = got_worktree_get_base_commit_id(worktree);
3360 cdbfe5d2 2023-07-24 stsp cma.worktree = worktree;
3361 cdbfe5d2 2023-07-24 stsp cma.cancel_cb = cancel_cb;
3362 cdbfe5d2 2023-07-24 stsp cma.cancel_arg = cancel_arg;
3364 c935fd51 2023-07-23 mark if (got_object_id_cmp(base_id, head_id) == 0) {
3365 c935fd51 2023-07-23 mark err = open_fileindex(&fileindex, &fileindex_path, worktree);
3367 c935fd51 2023-07-23 mark goto done;
3369 c935fd51 2023-07-23 mark err = got_fileindex_for_each_entry_safe(fileindex,
3370 cdbfe5d2 2023-07-24 stsp check_mixed_commits, &cma);
3371 c935fd51 2023-07-23 mark if (err == NULL)
3372 99301cec 2023-07-24 stsp *state = GOT_WORKTREE_STATE_UPTODATE;
3373 99301cec 2023-07-24 stsp else if (err->code == GOT_ERR_MIXED_COMMITS) {
3374 99301cec 2023-07-24 stsp *state = GOT_WORKTREE_STATE_OUTOFDATE;
3375 c935fd51 2023-07-23 mark err = NULL;
3378 99301cec 2023-07-24 stsp *state = GOT_WORKTREE_STATE_OUTOFDATE;
3381 c935fd51 2023-07-23 mark free(head_id);
3382 c935fd51 2023-07-23 mark free(fileindex_path);
3383 c935fd51 2023-07-23 mark got_ref_close(head_ref);
3384 c935fd51 2023-07-23 mark if (fileindex != NULL)
3385 c935fd51 2023-07-23 mark got_fileindex_free(fileindex);
3386 c935fd51 2023-07-23 mark return err;
3389 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3390 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3391 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3392 234035bc 2019-06-01 stsp struct got_repository *repo;
3395 234035bc 2019-06-01 stsp static const struct got_error *
3396 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3397 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3398 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3399 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3400 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3402 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3403 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3404 234035bc 2019-06-01 stsp unsigned char status;
3405 234035bc 2019-06-01 stsp struct stat sb;
3406 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3407 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3408 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3409 234035bc 2019-06-01 stsp char *ondisk_path;
3411 69de9dd4 2021-09-03 stsp if (id == NULL)
3412 69de9dd4 2021-09-03 stsp return NULL;
3414 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3415 69de9dd4 2021-09-03 stsp if (ie == NULL)
3416 69de9dd4 2021-09-03 stsp return NULL;
3418 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3420 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3422 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3423 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3424 5546d466 2021-09-02 stsp free(ondisk_path);
3426 234035bc 2019-06-01 stsp return err;
3427 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3428 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3430 234035bc 2019-06-01 stsp return NULL;
3433 818c7501 2019-07-11 stsp static const struct got_error *
3434 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3435 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3436 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3437 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3438 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3440 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3441 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3442 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3443 a44927cc 2022-04-07 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3444 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3445 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3446 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3447 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
3448 f9d37699 2022-06-28 stsp int fd1 = -1, fd2 = -1;
3450 03415a1a 2019-06-02 stsp if (commit_id1) {
3451 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit1, repo, commit_id1);
3453 a44927cc 2022-04-07 stsp goto done;
3454 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id1, repo, commit1,
3455 03415a1a 2019-06-02 stsp worktree->path_prefix);
3456 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3457 03415a1a 2019-06-02 stsp goto done;
3459 69d57f3d 2020-07-31 stsp if (tree_id1) {
3460 69d57f3d 2020-07-31 stsp char *id_str;
3462 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3464 03415a1a 2019-06-02 stsp goto done;
3466 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3468 f69721c3 2019-10-21 stsp goto done;
3470 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3471 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3472 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3473 f69721c3 2019-10-21 stsp free(id_str);
3474 f69721c3 2019-10-21 stsp goto done;
3476 f69721c3 2019-10-21 stsp free(id_str);
3478 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3479 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3480 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3481 b72706c3 2022-06-01 stsp goto done;
3485 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit2, repo, commit_id2);
3487 a44927cc 2022-04-07 stsp goto done;
3489 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id2, repo, commit2,
3490 234035bc 2019-06-01 stsp worktree->path_prefix);
3492 234035bc 2019-06-01 stsp goto done;
3494 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3496 234035bc 2019-06-01 stsp goto done;
3498 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3499 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3500 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3501 f9d37699 2022-06-28 stsp goto done;
3504 f9d37699 2022-06-28 stsp fd1 = got_opentempfd();
3505 f9d37699 2022-06-28 stsp if (fd1 == -1) {
3506 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3507 b72706c3 2022-06-01 stsp goto done;
3510 f9d37699 2022-06-28 stsp fd2 = got_opentempfd();
3511 f9d37699 2022-06-28 stsp if (fd2 == -1) {
3512 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3513 f9d37699 2022-06-28 stsp goto done;
3516 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3517 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3518 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3519 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3520 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3522 69de9dd4 2021-09-03 stsp goto done;
3524 234035bc 2019-06-01 stsp arg.worktree = worktree;
3525 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3526 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3527 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3528 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3529 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3530 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3531 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3532 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3533 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3534 b72706c3 2022-06-01 stsp merge_file_cb, &arg, 1);
3535 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3536 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3537 af12c6b9 2019-06-04 stsp err = sync_err;
3539 a44927cc 2022-04-07 stsp if (commit1)
3540 a44927cc 2022-04-07 stsp got_object_commit_close(commit1);
3541 a44927cc 2022-04-07 stsp if (commit2)
3542 a44927cc 2022-04-07 stsp got_object_commit_close(commit2);
3543 234035bc 2019-06-01 stsp if (tree1)
3544 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3545 234035bc 2019-06-01 stsp if (tree2)
3546 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3547 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3548 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3549 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3550 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3551 f9d37699 2022-06-28 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3552 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3553 f9d37699 2022-06-28 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3554 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3555 f69721c3 2019-10-21 stsp free(label_orig);
3556 818c7501 2019-07-11 stsp return err;
3559 818c7501 2019-07-11 stsp const struct got_error *
3560 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3561 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3562 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3563 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3565 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3566 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3567 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3568 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args cma;
3570 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3572 818c7501 2019-07-11 stsp return err;
3574 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3576 818c7501 2019-07-11 stsp goto done;
3578 cdbfe5d2 2023-07-24 stsp cma.worktree = worktree;
3579 cdbfe5d2 2023-07-24 stsp cma.cancel_cb = cancel_cb;
3580 cdbfe5d2 2023-07-24 stsp cma.cancel_arg = cancel_arg;
3582 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3585 818c7501 2019-07-11 stsp goto done;
3587 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3588 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3589 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3591 818c7501 2019-07-11 stsp if (fileindex)
3592 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3593 818c7501 2019-07-11 stsp free(fileindex_path);
3594 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3595 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3596 234035bc 2019-06-01 stsp err = unlockerr;
3597 234035bc 2019-06-01 stsp return err;
3600 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3601 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3602 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3603 927df6b7 2019-02-10 stsp const char *status_path;
3604 927df6b7 2019-02-10 stsp size_t status_path_len;
3605 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3606 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3607 f8d1f275 2019-02-04 stsp void *status_arg;
3608 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3609 f8d1f275 2019-02-04 stsp void *cancel_arg;
3610 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3611 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3612 f2a9dc41 2019-12-13 tracey int report_unchanged;
3613 3143d852 2020-06-25 stsp int no_ignores;
3616 f8d1f275 2019-02-04 stsp static const struct got_error *
3617 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3618 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3619 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3620 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3622 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3623 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3624 2c4740ad 2023-02-12 mark unsigned char staged_status;
3625 927df6b7 2019-02-10 stsp struct stat sb;
3626 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3627 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3628 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3630 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
3631 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3633 98eaaa12 2019-08-03 stsp return err;
3635 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3636 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3637 98eaaa12 2019-08-03 stsp return NULL;
3639 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
3640 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3641 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
3642 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3643 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3644 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3645 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3646 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
3649 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3650 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3653 927df6b7 2019-02-10 stsp static const struct got_error *
3654 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3655 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3657 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3658 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3659 f8d1f275 2019-02-04 stsp char *abspath;
3661 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
3662 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
3664 f6b8c3c2 2023-07-24 stsp return err;
3667 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3668 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3669 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3670 927df6b7 2019-02-10 stsp return NULL;
3672 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3673 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3674 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3675 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3677 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3678 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3679 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3682 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3683 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3684 f8d1f275 2019-02-04 stsp free(abspath);
3685 9d31a1d8 2018-03-11 stsp return err;
3688 f8d1f275 2019-02-04 stsp static const struct got_error *
3689 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3691 f6b8c3c2 2023-07-24 stsp const struct got_error *err;
3692 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3693 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3694 2ec1f75b 2019-03-26 stsp unsigned char status;
3696 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
3697 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
3699 f6b8c3c2 2023-07-24 stsp return err;
3702 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3703 927df6b7 2019-02-10 stsp return NULL;
3705 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&blob_id, ie);
3706 b4b2adf5 2023-02-09 op got_fileindex_entry_get_commit_id(&commit_id, ie);
3707 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3708 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3710 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3711 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3712 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3716 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3718 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3720 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3721 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3723 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3725 d8bacb93 2023-01-10 mark got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3728 6841da00 2019-08-08 stsp static const struct got_error *
3729 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3731 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3732 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3733 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3734 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3735 6841da00 2019-08-08 stsp size_t linesize = 0;
3736 6841da00 2019-08-08 stsp ssize_t linelen;
3738 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3739 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3740 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3741 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3743 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3744 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3745 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3747 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3748 bd8de430 2019-10-04 stsp if (line[0] == '#')
3751 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3752 bd8de430 2019-10-04 stsp if (line[0] == '!')
3755 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3756 6841da00 2019-08-08 stsp line) == -1) {
3757 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3758 6841da00 2019-08-08 stsp goto done;
3760 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3762 6841da00 2019-08-08 stsp goto done;
3764 6841da00 2019-08-08 stsp if (ferror(f)) {
3765 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3766 6841da00 2019-08-08 stsp goto done;
3769 6841da00 2019-08-08 stsp dirpath = strdup(path);
3770 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3771 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3772 6841da00 2019-08-08 stsp goto done;
3774 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3776 6841da00 2019-08-08 stsp free(line);
3777 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3778 6841da00 2019-08-08 stsp free(dirpath);
3779 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3781 6841da00 2019-08-08 stsp return err;
3784 249b637c 2023-02-20 stsp static int
3785 249b637c 2023-02-20 stsp match_path(const char *pattern, size_t pattern_len, const char *path,
3786 249b637c 2023-02-20 stsp int flags)
3788 249b637c 2023-02-20 stsp char buf[PATH_MAX];
3791 249b637c 2023-02-20 stsp * Trailing slashes signify directories.
3792 249b637c 2023-02-20 stsp * Append a * to make such patterns conform to fnmatch rules.
3794 249b637c 2023-02-20 stsp if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3795 249b637c 2023-02-20 stsp if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3796 249b637c 2023-02-20 stsp return FNM_NOMATCH; /* XXX */
3798 249b637c 2023-02-20 stsp return fnmatch(buf, path, flags);
3801 249b637c 2023-02-20 stsp return fnmatch(pattern, path, flags);
3805 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3807 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3809 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3810 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3811 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3812 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3814 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3815 249b637c 2023-02-20 stsp const char *p;
3817 249b637c 2023-02-20 stsp if (pi->path_len < 3 ||
3818 249b637c 2023-02-20 stsp strncmp(pi->path, "**/", 3) != 0)
3821 bd8de430 2019-10-04 stsp while (*p) {
3822 249b637c 2023-02-20 stsp if (match_path(pi->path + 3,
3823 249b637c 2023-02-20 stsp pi->path_len - 3, p,
3824 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3825 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3826 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3828 bd8de430 2019-10-04 stsp while (*p == '/')
3838 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3839 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3840 6841da00 2019-08-08 stsp * ignores backwards.
3842 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3843 6841da00 2019-08-08 stsp while (pe) {
3844 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3845 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3846 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3847 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3848 249b637c 2023-02-20 stsp int flags = FNM_LEADING_DIR;
3849 249b637c 2023-02-20 stsp if (strstr(pi->path, "/**/") == NULL)
3850 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3851 249b637c 2023-02-20 stsp if (match_path(pi->path, pi->path_len,
3852 249b637c 2023-02-20 stsp path, flags))
3857 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3863 6841da00 2019-08-08 stsp static const struct got_error *
3864 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3865 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3867 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3868 6841da00 2019-08-08 stsp char *ignorespath;
3869 886cec17 2019-12-15 stsp int fd = -1;
3870 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3872 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3873 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3874 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3876 886cec17 2019-12-15 stsp if (dirfd != -1) {
3877 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3878 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3879 886cec17 2019-12-15 stsp if (fd == -1) {
3880 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3881 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3882 886cec17 2019-12-15 stsp ignorespath);
3884 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3885 5e91dae4 2022-08-30 stsp if (ignoresfile == NULL)
3886 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3887 886cec17 2019-12-15 stsp ignorespath);
3890 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3894 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3895 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3896 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3897 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3898 886cec17 2019-12-15 stsp ignorespath);
3900 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3903 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3904 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3905 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3906 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3907 6841da00 2019-08-08 stsp free(ignorespath);
3908 6841da00 2019-08-08 stsp return err;
3911 f8d1f275 2019-02-04 stsp static const struct got_error *
3912 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3913 62da3196 2021-10-01 stsp int dirfd)
3915 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3916 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3917 f8d1f275 2019-02-04 stsp char *path = NULL;
3919 62da3196 2021-10-01 stsp if (ignore != NULL)
3920 62da3196 2021-10-01 stsp *ignore = 0;
3922 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
3923 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
3925 f6b8c3c2 2023-07-24 stsp return err;
3928 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3929 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3930 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3932 f8d1f275 2019-02-04 stsp path = de->d_name;
3935 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3936 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3937 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3938 62da3196 2021-10-01 stsp *ignore = 1;
3939 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3940 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3941 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3942 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3943 f8d1f275 2019-02-04 stsp if (parent_path[0])
3944 f8d1f275 2019-02-04 stsp free(path);
3945 b72f483a 2019-02-05 stsp return err;
3948 347d1d3e 2019-07-12 stsp static const struct got_error *
3949 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3951 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3952 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3954 3143d852 2020-06-25 stsp if (a->no_ignores)
3955 3143d852 2020-06-25 stsp return NULL;
3957 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3958 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3960 3143d852 2020-06-25 stsp return err;
3962 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3963 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3965 3143d852 2020-06-25 stsp return err;
3968 3143d852 2020-06-25 stsp static const struct got_error *
3969 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3970 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3971 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3972 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3974 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3975 abb4604f 2019-07-27 stsp struct stat sb;
3977 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3979 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3980 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3982 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3983 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3984 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3985 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3986 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3989 916237f3 2022-02-11 stsp if (!no_ignores && match_ignores(ignores, path))
3990 916237f3 2022-02-11 stsp return NULL;
3992 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3993 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3994 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3996 abb4604f 2019-07-27 stsp return NULL;
3999 3143d852 2020-06-25 stsp static const struct got_error *
4000 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
4001 3143d852 2020-06-25 stsp const char *root_path, const char *path)
4003 3143d852 2020-06-25 stsp const struct got_error *err;
4004 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
4006 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
4007 3143d852 2020-06-25 stsp ".cvsignore");
4009 3143d852 2020-06-25 stsp return err;
4011 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
4012 3143d852 2020-06-25 stsp ".gitignore");
4014 3143d852 2020-06-25 stsp return err;
4016 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
4017 3143d852 2020-06-25 stsp if (err) {
4018 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
4019 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
4020 3143d852 2020-06-25 stsp return err;
4022 3143d852 2020-06-25 stsp for (;;) {
4023 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
4024 3143d852 2020-06-25 stsp ".cvsignore");
4027 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
4028 3143d852 2020-06-25 stsp ".gitignore");
4031 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
4032 3143d852 2020-06-25 stsp if (err) {
4033 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
4034 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
4037 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
4039 b737c85a 2020-06-26 stsp free(parent_path);
4040 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
4041 b737c85a 2020-06-26 stsp next_parent_path = NULL;
4044 b737c85a 2020-06-26 stsp free(parent_path);
4045 b737c85a 2020-06-26 stsp free(next_parent_path);
4046 3143d852 2020-06-25 stsp return err;
4049 31cf15ec 2023-04-12 stsp struct find_missing_children_args {
4050 31cf15ec 2023-04-12 stsp const char *parent_path;
4051 31cf15ec 2023-04-12 stsp size_t parent_len;
4052 31cf15ec 2023-04-12 stsp struct got_pathlist_head *children;
4053 31cf15ec 2023-04-12 stsp got_cancel_cb cancel_cb;
4054 31cf15ec 2023-04-12 stsp void *cancel_arg;
4057 abb4604f 2019-07-27 stsp static const struct got_error *
4058 31cf15ec 2023-04-12 stsp find_missing_children(void *arg, struct got_fileindex_entry *ie)
4060 31cf15ec 2023-04-12 stsp const struct got_error *err = NULL;
4061 31cf15ec 2023-04-12 stsp struct find_missing_children_args *a = arg;
4063 31cf15ec 2023-04-12 stsp if (a->cancel_cb) {
4064 31cf15ec 2023-04-12 stsp err = a->cancel_cb(a->cancel_arg);
4066 31cf15ec 2023-04-12 stsp return err;
4069 31cf15ec 2023-04-12 stsp if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4070 31cf15ec 2023-04-12 stsp err = got_pathlist_append(a->children, ie->path, NULL);
4072 31cf15ec 2023-04-12 stsp return err;
4075 31cf15ec 2023-04-12 stsp static const struct got_error *
4076 31cf15ec 2023-04-12 stsp report_children(struct got_pathlist_head *children,
4077 31cf15ec 2023-04-12 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4078 31cf15ec 2023-04-12 stsp struct got_repository *repo, int is_root_dir, int report_unchanged,
4079 31cf15ec 2023-04-12 stsp struct got_pathlist_head *ignores, int no_ignores,
4080 31cf15ec 2023-04-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4081 31cf15ec 2023-04-12 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4083 31cf15ec 2023-04-12 stsp const struct got_error *err = NULL;
4084 31cf15ec 2023-04-12 stsp struct got_pathlist_entry *pe;
4085 31cf15ec 2023-04-12 stsp char *ondisk_path = NULL;
4087 31cf15ec 2023-04-12 stsp TAILQ_FOREACH(pe, children, entry) {
4088 31cf15ec 2023-04-12 stsp if (cancel_cb) {
4089 31cf15ec 2023-04-12 stsp err = cancel_cb(cancel_arg);
4094 31cf15ec 2023-04-12 stsp if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4095 31cf15ec 2023-04-12 stsp !is_root_dir ? "/" : "", pe->path) == -1) {
4096 31cf15ec 2023-04-12 stsp err = got_error_from_errno("asprintf");
4097 31cf15ec 2023-04-12 stsp ondisk_path = NULL;
4101 31cf15ec 2023-04-12 stsp err = report_single_file_status(pe->path, ondisk_path,
4102 31cf15ec 2023-04-12 stsp fileindex, status_cb, status_arg, repo, report_unchanged,
4103 31cf15ec 2023-04-12 stsp ignores, no_ignores);
4107 31cf15ec 2023-04-12 stsp free(ondisk_path);
4108 31cf15ec 2023-04-12 stsp ondisk_path = NULL;
4111 31cf15ec 2023-04-12 stsp free(ondisk_path);
4112 31cf15ec 2023-04-12 stsp return err;
4115 31cf15ec 2023-04-12 stsp static const struct got_error *
4116 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
4117 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4118 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4119 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4120 f2a9dc41 2019-12-13 tracey int report_unchanged)
4122 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
4123 6fc93f37 2019-12-13 stsp int fd = -1;
4124 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
4125 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
4126 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
4127 31cf15ec 2023-04-12 stsp struct got_pathlist_head ignores, missing_children;
4128 a84c0d30 2022-03-12 stsp struct got_fileindex_entry *ie;
4130 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
4131 31cf15ec 2023-04-12 stsp TAILQ_INIT(&missing_children);
4133 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
4134 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
4135 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
4137 a84c0d30 2022-03-12 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4139 a84c0d30 2022-03-12 stsp err = report_single_file_status(path, ondisk_path,
4140 a84c0d30 2022-03-12 stsp fileindex, status_cb, status_arg, repo,
4141 a84c0d30 2022-03-12 stsp report_unchanged, &ignores, no_ignores);
4142 a84c0d30 2022-03-12 stsp goto done;
4144 31cf15ec 2023-04-12 stsp struct find_missing_children_args fmca;
4145 31cf15ec 2023-04-12 stsp fmca.parent_path = path;
4146 31cf15ec 2023-04-12 stsp fmca.parent_len = strlen(path);
4147 31cf15ec 2023-04-12 stsp fmca.children = &missing_children;
4148 31cf15ec 2023-04-12 stsp fmca.cancel_cb = cancel_cb;
4149 31cf15ec 2023-04-12 stsp fmca.cancel_arg = cancel_arg;
4150 31cf15ec 2023-04-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
4151 31cf15ec 2023-04-12 stsp find_missing_children, &fmca);
4153 31cf15ec 2023-04-12 stsp goto done;
4156 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4157 6fc93f37 2019-12-13 stsp if (fd == -1) {
4158 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4159 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
4160 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
4162 ff56836b 2021-07-08 stsp if (!no_ignores) {
4163 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4164 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
4166 ff56836b 2021-07-08 stsp goto done;
4168 31cf15ec 2023-04-12 stsp if (TAILQ_EMPTY(&missing_children)) {
4169 31cf15ec 2023-04-12 stsp err = report_single_file_status(path,
4170 31cf15ec 2023-04-12 stsp ondisk_path, fileindex,
4171 31cf15ec 2023-04-12 stsp status_cb, status_arg, repo,
4172 31cf15ec 2023-04-12 stsp report_unchanged, &ignores, no_ignores);
4174 31cf15ec 2023-04-12 stsp goto done;
4176 31cf15ec 2023-04-12 stsp err = report_children(&missing_children,
4177 31cf15ec 2023-04-12 stsp worktree, fileindex, repo,
4178 31cf15ec 2023-04-12 stsp (path[0] == '\0'), report_unchanged,
4179 31cf15ec 2023-04-12 stsp &ignores, no_ignores,
4180 31cf15ec 2023-04-12 stsp status_cb, status_arg,
4181 31cf15ec 2023-04-12 stsp cancel_cb, cancel_arg);
4183 46108e23 2023-04-12 stsp goto done;
4187 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4188 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4189 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4190 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4191 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4192 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4193 abb4604f 2019-07-27 stsp arg.status_path = path;
4194 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4195 abb4604f 2019-07-27 stsp arg.repo = repo;
4196 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4197 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4198 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4199 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4200 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4201 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4202 022fae89 2019-12-06 tracey if (!no_ignores) {
4203 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4204 3143d852 2020-06-25 stsp worktree->root_path, path);
4206 3143d852 2020-06-25 stsp goto done;
4208 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4209 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4210 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4213 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4214 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4215 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4216 927df6b7 2019-02-10 stsp free(ondisk_path);
4217 347d1d3e 2019-07-12 stsp return err;
4220 347d1d3e 2019-07-12 stsp const struct got_error *
4221 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4222 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4223 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4224 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4226 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4227 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4228 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4229 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4231 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4233 347d1d3e 2019-07-12 stsp return err;
4235 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4236 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4237 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4238 f6343036 2021-06-22 stsp no_ignores, 0);
4242 f8d1f275 2019-02-04 stsp free(fileindex_path);
4243 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4244 f8d1f275 2019-02-04 stsp return err;
4247 6c7ab921 2019-03-18 stsp const struct got_error *
4248 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4249 6c7ab921 2019-03-18 stsp const char *arg)
4251 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4252 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4253 6c7ab921 2019-03-18 stsp size_t len;
4254 00bb5ea0 2020-07-23 stsp struct stat sb;
4255 01740607 2020-11-04 stsp char *abspath = NULL;
4256 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4258 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4260 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4261 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4262 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4264 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4265 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4266 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4267 00bb5ea0 2020-07-23 stsp goto done;
4269 727173c3 2020-11-06 stsp sb.st_mode = 0;
4271 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4273 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4274 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4275 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4276 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4278 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4279 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4280 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4281 00bb5ea0 2020-07-23 stsp goto done;
4285 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4286 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4288 d0710d08 2019-07-22 stsp goto done;
4289 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4290 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4291 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4292 00bb5ea0 2020-07-23 stsp goto done;
4295 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4296 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4297 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4298 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4299 00bb5ea0 2020-07-23 stsp goto done;
4301 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4302 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4303 01740607 2020-11-04 stsp goto done;
4305 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4306 01740607 2020-11-04 stsp sizeof(canonpath));
4308 00bb5ea0 2020-07-23 stsp goto done;
4309 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4310 01740607 2020-11-04 stsp if (resolved == NULL) {
4311 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4312 01740607 2020-11-04 stsp goto done;
4317 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4318 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4319 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4320 6c7ab921 2019-03-18 stsp goto done;
4323 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4324 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4325 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4327 f6d88e1a 2019-05-29 stsp goto done;
4329 f6d88e1a 2019-05-29 stsp path = strdup("");
4330 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4331 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4332 f6d88e1a 2019-05-29 stsp goto done;
4336 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4337 6c7ab921 2019-03-18 stsp len = strlen(path);
4338 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4339 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4343 01740607 2020-11-04 stsp free(abspath);
4344 6c7ab921 2019-03-18 stsp free(resolved);
4345 d0710d08 2019-07-22 stsp free(cwd);
4346 6c7ab921 2019-03-18 stsp if (err == NULL)
4347 6c7ab921 2019-03-18 stsp *wt_path = path;
4349 6c7ab921 2019-03-18 stsp free(path);
4350 d00136be 2019-03-26 stsp return err;
4353 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4354 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4355 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4356 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4357 4e68cba3 2019-11-23 stsp void *progress_arg;
4358 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4361 b88936d3 2023-06-21 stsp static int
4362 b88936d3 2023-06-21 stsp add_noop_status(unsigned char status)
4364 b88936d3 2023-06-21 stsp return (status == GOT_STATUS_ADD ||
4365 b88936d3 2023-06-21 stsp status == GOT_STATUS_MODIFY ||
4366 b88936d3 2023-06-21 stsp status == GOT_STATUS_CONFLICT ||
4367 b88936d3 2023-06-21 stsp status == GOT_STATUS_MODE_CHANGE ||
4368 b88936d3 2023-06-21 stsp status == GOT_STATUS_NO_CHANGE);
4371 4e68cba3 2019-11-23 stsp static const struct got_error *
4372 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4373 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4374 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4375 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4377 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4378 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4379 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4380 6d022e97 2019-08-04 stsp struct stat sb;
4381 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4383 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4384 dbb83fbd 2019-12-12 stsp relpath) == -1)
4385 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4387 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4389 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4390 12463d8b 2019-12-13 stsp de_name, a->repo);
4392 dbb83fbd 2019-12-12 stsp goto done;
4393 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4394 b88936d3 2023-06-21 stsp if (staged_status == GOT_STATUS_NO_CHANGE &&
4395 b88936d3 2023-06-21 stsp add_noop_status(status))
4396 dbb83fbd 2019-12-12 stsp goto done;
4397 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4399 dbb83fbd 2019-12-12 stsp goto done;
4402 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4403 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
4404 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
4406 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4407 dbb83fbd 2019-12-12 stsp goto done;
4410 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4412 4e68cba3 2019-11-23 stsp goto done;
4413 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4414 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4415 3969253a 2020-03-07 stsp if (err) {
4416 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4417 3969253a 2020-03-07 stsp goto done;
4419 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4420 07f5b47a 2019-06-02 stsp if (err) {
4421 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4422 4e68cba3 2019-11-23 stsp goto done;
4425 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4427 dbb83fbd 2019-12-12 stsp return err;
4428 b88936d3 2023-06-21 stsp if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4429 dbb83fbd 2019-12-12 stsp return NULL;
4430 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4433 d00136be 2019-03-26 stsp const struct got_error *
4434 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4435 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4436 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4437 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4439 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4440 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4441 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4442 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4443 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4445 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4447 d00136be 2019-03-26 stsp return err;
4449 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4451 d00136be 2019-03-26 stsp goto done;
4453 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4454 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4455 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4456 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4457 4e68cba3 2019-11-23 stsp saa.repo = repo;
4459 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4460 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4461 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4465 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4466 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4467 af12c6b9 2019-06-04 stsp err = sync_err;
4469 fb399478 2019-07-12 stsp free(fileindex_path);
4470 d00136be 2019-03-26 stsp if (fileindex)
4471 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4472 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4473 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4474 d00136be 2019-03-26 stsp err = unlockerr;
4475 6c7ab921 2019-03-18 stsp return err;
4478 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4479 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4480 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4481 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4482 f2a9dc41 2019-12-13 tracey void *progress_arg;
4483 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4484 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4485 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4486 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4487 7f4e5320 2023-05-29 stsp const char *status_path;
4488 7f4e5320 2023-05-29 stsp size_t status_path_len;
4489 766841c2 2020-08-13 stsp const char *status_codes;
4492 f2a9dc41 2019-12-13 tracey static const struct got_error *
4493 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4494 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4495 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4496 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4498 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4499 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4500 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4501 17ed4618 2019-06-02 stsp struct stat sb;
4502 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4504 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4505 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4506 4e12cd97 2022-01-25 stsp return NULL;
4507 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4510 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4511 17ed4618 2019-06-02 stsp if (ie == NULL)
4512 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4514 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4515 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4516 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4517 9acbc4fa 2019-08-03 stsp return NULL;
4518 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4521 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4522 f2a9dc41 2019-12-13 tracey relpath) == -1)
4523 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4525 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4528 f2a9dc41 2019-12-13 tracey goto done;
4530 766841c2 2020-08-13 stsp if (a->status_codes) {
4531 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4533 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4534 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4537 5e91dae4 2022-08-30 stsp if (i == ncodes) {
4538 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4539 766841c2 2020-08-13 stsp free(ondisk_path);
4540 766841c2 2020-08-13 stsp return NULL;
4542 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4543 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4544 766841c2 2020-08-13 stsp static char msg[64];
4545 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4546 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4547 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4548 766841c2 2020-08-13 stsp goto done;
4552 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4553 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4554 f2a9dc41 2019-12-13 tracey goto done;
4555 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4556 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4557 f2a9dc41 2019-12-13 tracey goto done;
4559 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4560 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4561 4e12cd97 2022-01-25 stsp goto done;
4563 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4564 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4565 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4566 f2a9dc41 2019-12-13 tracey goto done;
4570 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4571 20a2ad1c 2020-10-20 stsp size_t root_len;
4573 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4574 a06ca3f7 2022-10-15 stsp if (unlinkat(dirfd, de_name, 0) == -1) {
4575 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4576 12463d8b 2019-12-13 stsp ondisk_path);
4577 12463d8b 2019-12-13 stsp goto done;
4579 a06ca3f7 2022-10-15 stsp } else if (unlink(ondisk_path) == -1) {
4580 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4581 12463d8b 2019-12-13 stsp goto done;
4584 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4586 20a2ad1c 2020-10-20 stsp char *parent;
4588 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4590 2513f20a 2020-10-20 stsp goto done;
4591 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4592 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4593 7f4e5320 2023-05-29 stsp if (got_path_cmp(ondisk_path, a->status_path,
4594 7f4e5320 2023-05-29 stsp strlen(ondisk_path), a->status_path_len) != 0 &&
4595 7f4e5320 2023-05-29 stsp !got_path_is_child(ondisk_path, a->status_path,
4596 7f4e5320 2023-05-29 stsp a->status_path_len))
4598 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4599 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4600 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4601 20a2ad1c 2020-10-20 stsp ondisk_path);
4604 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4605 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4608 f6635657 2023-08-25 stsp if (got_fileindex_entry_has_blob(ie))
4609 f6635657 2023-08-25 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4611 f6635657 2023-08-25 stsp got_fileindex_entry_remove(a->fileindex, ie);
4613 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4614 f2a9dc41 2019-12-13 tracey if (err)
4615 f2a9dc41 2019-12-13 tracey return err;
4616 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4617 f2a9dc41 2019-12-13 tracey return NULL;
4618 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4619 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4622 2ec1f75b 2019-03-26 stsp const struct got_error *
4623 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4624 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4625 766841c2 2020-08-13 stsp const char *status_codes,
4626 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4627 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4629 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4630 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4631 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4632 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4633 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4635 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4637 2ec1f75b 2019-03-26 stsp return err;
4639 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4641 2ec1f75b 2019-03-26 stsp goto done;
4643 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4644 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4645 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4646 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4647 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4648 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4649 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4650 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4651 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4653 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4654 7f4e5320 2023-05-29 stsp char *ondisk_status_path;
4656 7f4e5320 2023-05-29 stsp if (asprintf(&ondisk_status_path, "%s%s%s",
4657 7f4e5320 2023-05-29 stsp got_worktree_get_root_path(worktree),
4658 7f4e5320 2023-05-29 stsp pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4659 7f4e5320 2023-05-29 stsp err = got_error_from_errno("asprintf");
4660 7f4e5320 2023-05-29 stsp goto done;
4662 7f4e5320 2023-05-29 stsp sda.status_path = ondisk_status_path;
4663 7f4e5320 2023-05-29 stsp sda.status_path_len = strlen(ondisk_status_path);
4664 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4665 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4666 7f4e5320 2023-05-29 stsp free(ondisk_status_path);
4670 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4671 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4672 af12c6b9 2019-06-04 stsp err = sync_err;
4674 fb399478 2019-07-12 stsp free(fileindex_path);
4675 2ec1f75b 2019-03-26 stsp if (fileindex)
4676 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4677 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4678 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4679 2ec1f75b 2019-03-26 stsp err = unlockerr;
4680 33aa809d 2019-08-08 stsp return err;
4683 33aa809d 2019-08-08 stsp static const struct got_error *
4684 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4686 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4687 33aa809d 2019-08-08 stsp char *line = NULL;
4688 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4689 33aa809d 2019-08-08 stsp ssize_t linelen;
4691 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4692 33aa809d 2019-08-08 stsp if (linelen == -1) {
4693 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4694 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4695 33aa809d 2019-08-08 stsp goto done;
4697 33aa809d 2019-08-08 stsp return NULL;
4699 33aa809d 2019-08-08 stsp if (outfile) {
4700 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4701 33aa809d 2019-08-08 stsp if (n != linelen) {
4702 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4703 33aa809d 2019-08-08 stsp goto done;
4706 33aa809d 2019-08-08 stsp if (rejectfile) {
4707 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4708 33aa809d 2019-08-08 stsp if (n != linelen)
4709 910d235d 2023-01-10 mark err = got_ferror(rejectfile, GOT_ERR_IO);
4712 33aa809d 2019-08-08 stsp free(line);
4713 2ec1f75b 2019-03-26 stsp return err;
4716 33aa809d 2019-08-08 stsp static const struct got_error *
4717 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4719 33aa809d 2019-08-08 stsp char *line = NULL;
4720 33aa809d 2019-08-08 stsp size_t linesize = 0;
4721 33aa809d 2019-08-08 stsp ssize_t linelen;
4723 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4724 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4725 500467ff 2019-09-25 hiltjo if (ferror(f))
4726 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4727 500467ff 2019-09-25 hiltjo return NULL;
4729 33aa809d 2019-08-08 stsp free(line);
4730 33aa809d 2019-08-08 stsp return NULL;
4733 33aa809d 2019-08-08 stsp static const struct got_error *
4734 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4735 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4736 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4738 33aa809d 2019-08-08 stsp const struct got_error *err;
4740 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4741 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4742 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4744 33aa809d 2019-08-08 stsp return err;
4745 33aa809d 2019-08-08 stsp (*line_cur1)++;
4747 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4748 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4749 33aa809d 2019-08-08 stsp if (rejectfile)
4750 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4752 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4754 33aa809d 2019-08-08 stsp return err;
4755 33aa809d 2019-08-08 stsp (*line_cur2)++;
4757 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4758 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4759 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4761 33aa809d 2019-08-08 stsp return err;
4762 33aa809d 2019-08-08 stsp (*line_cur2)++;
4764 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4765 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4766 33aa809d 2019-08-08 stsp if (rejectfile)
4767 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4769 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4771 33aa809d 2019-08-08 stsp return err;
4772 33aa809d 2019-08-08 stsp (*line_cur1)++;
4775 f1e81a05 2019-08-10 stsp return NULL;
4778 f1e81a05 2019-08-10 stsp static const struct got_error *
4779 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4780 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4782 f1e81a05 2019-08-10 stsp const struct got_error *err;
4784 f1e81a05 2019-08-10 stsp if (outfile) {
4785 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4786 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4787 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4789 f1e81a05 2019-08-10 stsp return err;
4790 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4793 f1e81a05 2019-08-10 stsp if (rejectfile) {
4794 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4795 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4796 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4798 f1e81a05 2019-08-10 stsp return err;
4799 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4803 33aa809d 2019-08-08 stsp return NULL;
4806 33aa809d 2019-08-08 stsp static const struct got_error *
4807 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4808 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4809 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4810 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4811 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4813 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4814 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4815 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4816 33aa809d 2019-08-08 stsp FILE *hunkfile;
4817 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4818 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4821 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4823 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4824 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4825 fe621944 2020-11-10 stsp start_old = cc.left.start;
4826 fe621944 2020-11-10 stsp end_old = cc.left.end;
4827 fe621944 2020-11-10 stsp start_new = cc.right.start;
4828 fe621944 2020-11-10 stsp end_new = cc.right.end;
4830 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4831 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4832 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4834 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4835 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4836 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4838 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4839 fe621944 2020-11-10 stsp if (diff_state == NULL)
4840 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4841 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4843 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4844 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4845 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4846 33aa809d 2019-08-08 stsp goto done;
4849 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4850 fe621944 2020-11-10 stsp diff_result, &cc);
4851 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4852 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4853 33aa809d 2019-08-08 stsp goto done;
4856 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4857 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4858 33aa809d 2019-08-08 stsp goto done;
4861 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4862 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4864 33aa809d 2019-08-08 stsp goto done;
4866 33aa809d 2019-08-08 stsp switch (*choice) {
4867 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4868 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4869 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4871 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4872 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4873 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4875 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4878 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4882 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4883 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4884 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4885 33aa809d 2019-08-08 stsp return err;
4888 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4889 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4890 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4891 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4892 1f1abb7e 2019-08-08 stsp void *progress_arg;
4893 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4894 33aa809d 2019-08-08 stsp void *patch_arg;
4895 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4896 f259c4c1 2021-09-24 stsp int unlink_added_files;
4897 af179be7 2023-04-14 stsp struct got_pathlist_head *added_files_to_unlink;
4900 e20a8b6f 2019-06-04 stsp static const struct got_error *
4901 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4902 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4903 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4904 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4905 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4907 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4908 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4909 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4910 eb81bc23 2022-06-28 tracey int fd = -1, fd2 = -1;
4911 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4912 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4913 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4914 fe621944 2020-11-10 stsp struct stat sb2;
4915 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4916 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4917 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4919 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4921 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4923 33aa809d 2019-08-08 stsp return err;
4925 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4926 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4927 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4928 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4929 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4930 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4931 fa3cef63 2020-07-23 stsp goto done;
4933 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4934 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4935 c0df5966 2021-12-31 stsp if (link_len == -1) {
4936 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4939 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4940 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4943 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4944 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4945 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4946 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4947 fa3cef63 2020-07-23 stsp goto done;
4949 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4950 fa3cef63 2020-07-23 stsp sizeof(link_target));
4951 fa3cef63 2020-07-23 stsp if (link_len == -1)
4952 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4953 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4954 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4957 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4958 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4959 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4960 fa3cef63 2020-07-23 stsp goto done;
4963 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4964 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4965 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4966 fa3cef63 2020-07-23 stsp goto done;
4971 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4972 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4973 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4974 fa3cef63 2020-07-23 stsp goto done;
4976 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4977 fa3cef63 2020-07-23 stsp if (n != link_len) {
4978 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4979 fa3cef63 2020-07-23 stsp goto done;
4981 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4982 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4983 fa3cef63 2020-07-23 stsp goto done;
4985 fa3cef63 2020-07-23 stsp rewind(f2);
4988 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4989 eb81bc23 2022-06-28 tracey if (fd == -1) {
4990 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4991 eb81bc23 2022-06-28 tracey goto done;
4994 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4996 33aa809d 2019-08-08 stsp goto done;
4998 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
5000 33aa809d 2019-08-08 stsp goto done;
5002 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5004 33aa809d 2019-08-08 stsp goto done;
5006 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5007 4b752015 2022-06-30 stsp 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5009 33aa809d 2019-08-08 stsp goto done;
5011 b90054ed 2022-11-01 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5014 33aa809d 2019-08-08 stsp goto done;
5016 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
5017 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
5018 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
5019 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
5021 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
5022 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5023 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
5024 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
5025 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
5026 fe621944 2020-11-10 stsp nchanges++;
5028 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5029 33aa809d 2019-08-08 stsp int choice;
5030 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
5031 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
5032 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
5033 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
5034 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
5035 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
5037 33aa809d 2019-08-08 stsp goto done;
5038 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
5039 33aa809d 2019-08-08 stsp have_content = 1;
5040 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
5043 1ebedb77 2019-10-19 stsp if (have_content) {
5044 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5045 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
5046 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
5048 1ebedb77 2019-10-19 stsp goto done;
5050 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
5051 b2b3fce1 2022-10-29 op mode_t mode;
5053 b2b3fce1 2022-10-29 op mode = apply_umask(sb2.st_mode);
5054 b2b3fce1 2022-10-29 op if (fchmod(fileno(outfile), mode) == -1) {
5055 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
5056 fa3cef63 2020-07-23 stsp goto done;
5061 33aa809d 2019-08-08 stsp free(id_str);
5062 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5063 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5065 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
5066 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
5067 fe621944 2020-11-10 stsp if (err == NULL)
5068 fe621944 2020-11-10 stsp err = free_err;
5069 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
5070 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
5071 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
5072 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
5073 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5074 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
5075 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
5076 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
5077 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
5078 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
5079 33aa809d 2019-08-08 stsp if (err || !have_content) {
5080 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5081 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
5082 33aa809d 2019-08-08 stsp free(*path_outfile);
5083 33aa809d 2019-08-08 stsp *path_outfile = NULL;
5085 33aa809d 2019-08-08 stsp free(path1);
5086 33aa809d 2019-08-08 stsp return err;
5089 33aa809d 2019-08-08 stsp static const struct got_error *
5090 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
5091 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
5092 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5093 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5095 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
5096 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
5097 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
5098 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
5099 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
5100 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
5101 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
5102 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
5103 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
5104 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
5105 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
5106 eb81bc23 2022-06-28 tracey int fd = -1;
5108 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
5109 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
5110 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
5111 d3bcc3d1 2019-08-08 stsp return NULL;
5113 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
5114 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
5115 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
5117 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5118 65084dad 2019-08-08 stsp if (ie == NULL)
5119 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
5121 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
5122 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
5123 a129376b 2019-03-28 stsp if (err) {
5124 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
5125 a129376b 2019-03-28 stsp goto done;
5126 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
5127 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
5128 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
5129 e20a8b6f 2019-06-04 stsp goto done;
5132 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
5133 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
5134 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5135 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5136 a129376b 2019-03-28 stsp goto done;
5139 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
5140 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
5141 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5142 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5143 a129376b 2019-03-28 stsp goto done;
5146 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
5147 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
5148 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5149 a129376b 2019-03-28 stsp goto done;
5154 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
5155 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
5157 a44927cc 2022-04-07 stsp goto done;
5159 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5160 a9fa2909 2019-07-27 stsp if (err) {
5161 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5162 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
5163 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
5164 a9fa2909 2019-07-27 stsp goto done;
5166 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
5168 a9fa2909 2019-07-27 stsp goto done;
5170 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
5172 a9fa2909 2019-07-27 stsp goto done;
5174 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
5175 1233e6b6 2020-10-19 stsp free(te_name);
5176 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
5177 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
5178 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5179 a9fa2909 2019-07-27 stsp goto done;
5183 a129376b 2019-03-28 stsp switch (status) {
5184 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
5185 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5186 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5187 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5188 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5190 33aa809d 2019-08-08 stsp goto done;
5191 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5194 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5195 1f1abb7e 2019-08-08 stsp ie->path);
5197 1ee397ad 2019-07-12 stsp goto done;
5198 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5199 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
5200 af179be7 2023-04-14 stsp int do_unlink = a->added_files_to_unlink ? 0 : 1;
5202 af179be7 2023-04-14 stsp if (a->added_files_to_unlink) {
5203 af179be7 2023-04-14 stsp struct got_pathlist_entry *pe;
5205 af179be7 2023-04-14 stsp TAILQ_FOREACH(pe, a->added_files_to_unlink,
5207 af179be7 2023-04-14 stsp if (got_path_cmp(pe->path, relpath,
5208 af179be7 2023-04-14 stsp pe->path_len, strlen(relpath)))
5210 af179be7 2023-04-14 stsp do_unlink = 1;
5215 af179be7 2023-04-14 stsp if (do_unlink) {
5216 af179be7 2023-04-14 stsp if (asprintf(&ondisk_path, "%s/%s",
5217 af179be7 2023-04-14 stsp got_worktree_get_root_path(a->worktree),
5218 af179be7 2023-04-14 stsp relpath) == -1) {
5219 af179be7 2023-04-14 stsp err = got_error_from_errno("asprintf");
5220 af179be7 2023-04-14 stsp goto done;
5222 af179be7 2023-04-14 stsp if (unlink(ondisk_path) == -1) {
5223 af179be7 2023-04-14 stsp err = got_error_from_errno2("unlink",
5224 af179be7 2023-04-14 stsp ondisk_path);
5230 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5231 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5232 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5233 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5234 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5236 33aa809d 2019-08-08 stsp goto done;
5237 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5240 33aa809d 2019-08-08 stsp /* fall through */
5241 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5242 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5243 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5244 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5245 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5246 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5247 b4b2adf5 2023-02-09 op staged_status == GOT_STATUS_MODIFY)
5248 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
5250 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
5251 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5252 eb81bc23 2022-06-28 tracey if (fd == -1) {
5253 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5254 eb81bc23 2022-06-28 tracey goto done;
5257 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5259 65084dad 2019-08-08 stsp goto done;
5261 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5262 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5263 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5264 a129376b 2019-03-28 stsp goto done;
5267 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5268 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5269 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5270 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5271 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5272 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5273 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5275 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5276 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5277 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5278 369fd7e5 2020-07-23 stsp path_content);
5281 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5282 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5283 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
5284 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5286 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5287 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5288 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5289 369fd7e5 2020-07-23 stsp goto done;
5293 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5294 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5295 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5296 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5297 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
5298 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5300 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5302 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5303 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5304 ef623445 2023-09-16 op 0, 1, 0, 0, NULL, a->repo,
5305 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5308 a129376b 2019-03-28 stsp goto done;
5309 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5310 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5311 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5312 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5313 437adc9d 2020-12-10 yzhong blob->id.sha1,
5314 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5316 2e1fa222 2020-07-23 stsp goto done;
5318 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5319 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5320 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5329 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5330 33aa809d 2019-08-08 stsp free(path_content);
5331 e20a8b6f 2019-06-04 stsp free(parent_path);
5332 a129376b 2019-03-28 stsp free(tree_path);
5333 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5334 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5336 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5338 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5339 a129376b 2019-03-28 stsp free(tree_id);
5340 a44927cc 2022-04-07 stsp if (base_commit)
5341 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
5342 e20a8b6f 2019-06-04 stsp return err;
5345 e20a8b6f 2019-06-04 stsp const struct got_error *
5346 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5347 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5348 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5349 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5350 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5352 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5353 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5354 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5355 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5356 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5357 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5359 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5361 e20a8b6f 2019-06-04 stsp return err;
5363 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5365 e20a8b6f 2019-06-04 stsp goto done;
5367 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5368 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5369 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5370 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5371 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5372 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5373 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5374 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
5375 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5376 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5377 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
5381 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5382 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5383 e20a8b6f 2019-06-04 stsp err = sync_err;
5385 fb399478 2019-07-12 stsp free(fileindex_path);
5386 a129376b 2019-03-28 stsp if (fileindex)
5387 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5388 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5389 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5390 a129376b 2019-03-28 stsp err = unlockerr;
5391 c4296144 2019-05-09 stsp return err;
5394 cf066bf8 2019-05-09 stsp static void
5395 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5397 24519714 2019-05-09 stsp free(ct->path);
5398 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5399 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5400 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5401 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5402 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5403 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5407 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5408 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5409 24519714 2019-05-09 stsp struct got_repository *repo;
5410 24519714 2019-05-09 stsp struct got_worktree *worktree;
5411 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5412 5f8a88c6 2019-08-03 stsp int have_staged_files;
5413 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5414 2a47b1e5 2022-11-01 stsp int diff_header_shown;
5415 12383673 2023-02-18 mark int commit_conflicts;
5416 2a47b1e5 2022-11-01 stsp FILE *diff_outfile;
5422 2a47b1e5 2022-11-01 stsp * Create a file which contains the target path of a symlink so we can feed
5423 2a47b1e5 2022-11-01 stsp * it as content to the diff engine.
5425 2a47b1e5 2022-11-01 stsp static const struct got_error *
5426 2a47b1e5 2022-11-01 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5427 2a47b1e5 2022-11-01 stsp const char *abspath)
5429 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5430 2a47b1e5 2022-11-01 stsp char target_path[PATH_MAX];
5431 2a47b1e5 2022-11-01 stsp ssize_t target_len, outlen;
5435 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5436 2a47b1e5 2022-11-01 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5437 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5438 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlinkat", abspath);
5440 2a47b1e5 2022-11-01 stsp target_len = readlink(abspath, target_path, PATH_MAX);
5441 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5442 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlink", abspath);
5445 2a47b1e5 2022-11-01 stsp *fd = got_opentempfd();
5446 2a47b1e5 2022-11-01 stsp if (*fd == -1)
5447 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentempfd");
5449 2a47b1e5 2022-11-01 stsp outlen = write(*fd, target_path, target_len);
5450 2a47b1e5 2022-11-01 stsp if (outlen == -1) {
5451 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5452 2a47b1e5 2022-11-01 stsp goto done;
5455 2a47b1e5 2022-11-01 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
5456 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("lseek", abspath);
5457 2a47b1e5 2022-11-01 stsp goto done;
5460 2a47b1e5 2022-11-01 stsp if (err) {
5461 2a47b1e5 2022-11-01 stsp close(*fd);
5464 2a47b1e5 2022-11-01 stsp return err;
5467 2a47b1e5 2022-11-01 stsp static const struct got_error *
5468 2a47b1e5 2022-11-01 stsp append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5469 2a47b1e5 2022-11-01 stsp FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5470 6d15dc69 2022-11-01 stsp int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5472 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5473 2a47b1e5 2022-11-01 stsp struct got_blob_object *blob1 = NULL;
5474 2a47b1e5 2022-11-01 stsp int fd = -1, fd1 = -1, fd2 = -1;
5475 2a47b1e5 2022-11-01 stsp FILE *ondisk_file = NULL;
5476 2a47b1e5 2022-11-01 stsp char *label1 = NULL;
5477 2a47b1e5 2022-11-01 stsp struct stat sb;
5478 2a47b1e5 2022-11-01 stsp off_t size1 = 0;
5479 2a47b1e5 2022-11-01 stsp int f2_exists = 0;
5480 2a47b1e5 2022-11-01 stsp char *id_str = NULL;
5482 2a47b1e5 2022-11-01 stsp memset(&sb, 0, sizeof(sb));
5484 4ba5cca9 2022-11-01 stsp if (diff_staged) {
5485 4ba5cca9 2022-11-01 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5486 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_ADD &&
5487 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_DELETE)
5488 4ba5cca9 2022-11-01 stsp return NULL;
5490 4ba5cca9 2022-11-01 stsp if (ct->status != GOT_STATUS_MODIFY &&
5491 4ba5cca9 2022-11-01 stsp ct->status != GOT_STATUS_ADD &&
5492 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5493 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5494 4ba5cca9 2022-11-01 stsp return NULL;
5497 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f1);
5499 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5500 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f2);
5502 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5504 2a47b1e5 2022-11-01 stsp if (!*diff_header_shown) {
5505 2a47b1e5 2022-11-01 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
5507 2a47b1e5 2022-11-01 stsp return err;
5508 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5509 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree));
5510 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "commit - %s\n", id_str);
5511 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "path + %s%s\n",
5512 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree),
5513 2a47b1e5 2022-11-01 stsp diff_staged ? " (staged changes)" : "");
5514 2a47b1e5 2022-11-01 stsp *diff_header_shown = 1;
5517 2a47b1e5 2022-11-01 stsp if (diff_staged) {
5518 2a47b1e5 2022-11-01 stsp const char *label1 = NULL, *label2 = NULL;
5519 2a47b1e5 2022-11-01 stsp switch (ct->staged_status) {
5520 2a47b1e5 2022-11-01 stsp case GOT_STATUS_MODIFY:
5521 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5522 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5524 2a47b1e5 2022-11-01 stsp case GOT_STATUS_ADD:
5525 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5527 2a47b1e5 2022-11-01 stsp case GOT_STATUS_DELETE:
5528 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5531 2a47b1e5 2022-11-01 stsp return got_error(GOT_ERR_FILE_STATUS);
5533 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5534 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5535 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5536 2a47b1e5 2022-11-01 stsp goto done;
5538 2a47b1e5 2022-11-01 stsp fd2 = got_opentempfd();
5539 2a47b1e5 2022-11-01 stsp if (fd2 == -1) {
5540 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5541 2a47b1e5 2022-11-01 stsp goto done;
5543 2a47b1e5 2022-11-01 stsp err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5544 2a47b1e5 2022-11-01 stsp fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5545 1f3405c9 2023-01-17 mark label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5546 a76e88e5 2023-01-10 mark NULL, repo, diff_outfile);
5547 2a47b1e5 2022-11-01 stsp goto done;
5550 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5551 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5552 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5553 2a47b1e5 2022-11-01 stsp goto done;
5556 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_ADD) {
5557 2a47b1e5 2022-11-01 stsp err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5558 2a47b1e5 2022-11-01 stsp 8192, fd1);
5560 2a47b1e5 2022-11-01 stsp goto done;
5563 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_DELETE) {
5564 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5565 2a47b1e5 2022-11-01 stsp fd = openat(dirfd, de_name,
5566 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5567 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5568 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5569 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("openat",
5570 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5571 2a47b1e5 2022-11-01 stsp goto done;
5573 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5574 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5576 2a47b1e5 2022-11-01 stsp goto done;
5579 2a47b1e5 2022-11-01 stsp fd = open(ct->ondisk_path,
5580 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5581 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5582 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5583 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("open",
5584 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5585 2a47b1e5 2022-11-01 stsp goto done;
5587 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5588 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5590 2a47b1e5 2022-11-01 stsp goto done;
5593 2a47b1e5 2022-11-01 stsp if (fstatat(fd, ct->ondisk_path, &sb,
5594 2a47b1e5 2022-11-01 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5595 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fstatat", ct->ondisk_path);
5596 2a47b1e5 2022-11-01 stsp goto done;
5598 2a47b1e5 2022-11-01 stsp ondisk_file = fdopen(fd, "r");
5599 2a47b1e5 2022-11-01 stsp if (ondisk_file == NULL) {
5600 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fdopen", ct->ondisk_path);
5601 2a47b1e5 2022-11-01 stsp goto done;
5604 2a47b1e5 2022-11-01 stsp f2_exists = 1;
5607 2a47b1e5 2022-11-01 stsp if (blob1) {
5608 2a47b1e5 2022-11-01 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5609 2a47b1e5 2022-11-01 stsp f1, blob1);
5611 2a47b1e5 2022-11-01 stsp goto done;
5614 2a47b1e5 2022-11-01 stsp err = got_diff_blob_file(blob1, f1, size1, label1,
5615 2a47b1e5 2022-11-01 stsp ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5616 1f3405c9 2023-01-17 mark GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5618 2a47b1e5 2022-11-01 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5619 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5620 2a47b1e5 2022-11-01 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5621 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5622 2a47b1e5 2022-11-01 stsp if (blob1)
5623 2a47b1e5 2022-11-01 stsp got_object_blob_close(blob1);
5624 2a47b1e5 2022-11-01 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5625 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5626 2a47b1e5 2022-11-01 stsp if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5627 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
5628 2a47b1e5 2022-11-01 stsp return err;
5631 c4296144 2019-05-09 stsp static const struct got_error *
5632 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5633 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5634 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5635 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5637 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5638 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5639 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5640 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5641 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5642 768aea60 2019-05-09 stsp struct stat sb;
5644 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5645 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5646 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5647 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5648 5f8a88c6 2019-08-03 stsp return NULL;
5650 12383673 2023-02-18 mark if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5651 12383673 2023-02-18 mark printf("C %s\n", relpath);
5652 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5655 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5656 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5657 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5658 12383673 2023-02-18 mark status != GOT_STATUS_DELETE &&
5659 12383673 2023-02-18 mark status != GOT_STATUS_CONFLICT)
5660 5f8a88c6 2019-08-03 stsp return NULL;
5663 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5664 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5665 036813ee 2019-05-09 stsp goto done;
5667 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5668 036813ee 2019-05-09 stsp parent_path = strdup("");
5669 69960a46 2019-05-09 stsp if (parent_path == NULL)
5670 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5672 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5674 69960a46 2019-05-09 stsp return err;
5677 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5678 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5679 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5680 c4296144 2019-05-09 stsp goto done;
5683 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5684 768aea60 2019-05-09 stsp relpath) == -1) {
5685 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5686 768aea60 2019-05-09 stsp goto done;
5689 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5690 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5691 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5692 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5693 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5694 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5695 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5696 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5698 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5699 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5702 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5703 0aeb8099 2020-07-23 stsp goto done;
5705 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5706 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5707 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5708 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5709 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5710 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5711 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5712 12463d8b 2019-12-13 stsp ct->ondisk_path);
5713 12463d8b 2019-12-13 stsp goto done;
5715 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5716 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5717 768aea60 2019-05-09 stsp goto done;
5719 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5722 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5723 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5724 44d03001 2019-05-09 stsp relpath) == -1) {
5725 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5726 44d03001 2019-05-09 stsp goto done;
5729 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5730 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5731 35213c7c 2020-07-23 stsp int is_bad_symlink;
5732 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5733 35213c7c 2020-07-23 stsp ssize_t target_len;
5734 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5735 35213c7c 2020-07-23 stsp sizeof(target_path));
5736 35213c7c 2020-07-23 stsp if (target_len == -1) {
5737 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5738 35213c7c 2020-07-23 stsp ct->ondisk_path);
5739 35213c7c 2020-07-23 stsp goto done;
5741 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5742 df6221c7 2023-07-19 stsp target_len, ct->ondisk_path, a->worktree->root_path,
5743 df6221c7 2023-07-19 stsp a->worktree->meta_dir);
5745 35213c7c 2020-07-23 stsp goto done;
5746 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5747 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5748 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5749 35213c7c 2020-07-23 stsp goto done;
5754 cf066bf8 2019-05-09 stsp ct->status = status;
5755 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5756 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5757 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5758 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5759 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5760 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5761 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5762 b416585c 2019-05-13 stsp goto done;
5764 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5765 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5766 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5767 f0b75401 2019-08-03 stsp goto done;
5770 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5771 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5772 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5773 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5774 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5775 036813ee 2019-05-09 stsp goto done;
5778 24519714 2019-05-09 stsp ct->path = strdup(path);
5779 24519714 2019-05-09 stsp if (ct->path == NULL) {
5780 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5781 24519714 2019-05-09 stsp goto done;
5784 c78dbc03 2023-08-25 stsp if (a->diff_outfile) {
5785 2a47b1e5 2022-11-01 stsp err = append_ct_diff(ct, &a->diff_header_shown,
5786 2a47b1e5 2022-11-01 stsp a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5787 6d15dc69 2022-11-01 stsp a->have_staged_files, a->repo, a->worktree);
5789 2a47b1e5 2022-11-01 stsp goto done;
5792 c78dbc03 2023-08-25 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5794 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5795 ed175427 2019-05-09 stsp free_commitable(ct);
5796 24519714 2019-05-09 stsp free(parent_path);
5797 036813ee 2019-05-09 stsp free(path);
5798 a129376b 2019-03-28 stsp return err;
5801 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5802 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5803 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5804 036813ee 2019-05-09 stsp struct got_repository *);
5806 ed175427 2019-05-09 stsp static const struct got_error *
5807 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5808 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5809 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5810 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5811 afa376bf 2019-05-09 stsp struct got_repository *repo)
5813 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5814 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5815 036813ee 2019-05-09 stsp char *subpath;
5817 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5818 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5819 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5821 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5823 ed175427 2019-05-09 stsp return err;
5825 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5826 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5827 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5828 036813ee 2019-05-09 stsp free(subpath);
5829 036813ee 2019-05-09 stsp return err;
5832 036813ee 2019-05-09 stsp static const struct got_error *
5833 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5835 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5836 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5838 036813ee 2019-05-09 stsp *match = 0;
5840 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5841 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5842 0f63689d 2019-05-10 stsp return NULL;
5845 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5847 0f63689d 2019-05-10 stsp return err;
5848 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5849 036813ee 2019-05-09 stsp free(ct_parent_path);
5850 036813ee 2019-05-09 stsp return err;
5853 768aea60 2019-05-09 stsp static mode_t
5854 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5856 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5857 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5859 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5862 036813ee 2019-05-09 stsp static const struct got_error *
5863 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5864 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5866 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5868 036813ee 2019-05-09 stsp *new_te = NULL;
5870 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5872 036813ee 2019-05-09 stsp goto done;
5874 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5876 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5877 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5878 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5880 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5882 036813ee 2019-05-09 stsp if (err && *new_te) {
5883 56e0773d 2019-11-28 stsp free(*new_te);
5884 036813ee 2019-05-09 stsp *new_te = NULL;
5886 036813ee 2019-05-09 stsp return err;
5889 036813ee 2019-05-09 stsp static const struct got_error *
5890 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5891 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5893 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5894 102b254e 2020-10-19 stsp char *ct_name = NULL;
5896 036813ee 2019-05-09 stsp *new_te = NULL;
5898 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5899 036813ee 2019-05-09 stsp if (*new_te == NULL)
5900 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5902 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5904 036813ee 2019-05-09 stsp goto done;
5905 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5906 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5907 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5908 036813ee 2019-05-09 stsp goto done;
5911 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5913 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5914 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5915 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5917 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5919 102b254e 2020-10-19 stsp free(ct_name);
5920 036813ee 2019-05-09 stsp if (err && *new_te) {
5921 56e0773d 2019-11-28 stsp free(*new_te);
5922 036813ee 2019-05-09 stsp *new_te = NULL;
5924 036813ee 2019-05-09 stsp return err;
5927 036813ee 2019-05-09 stsp static const struct got_error *
5928 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5929 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5931 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5932 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5934 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5936 036813ee 2019-05-09 stsp return err;
5937 036813ee 2019-05-09 stsp if (new_pe == NULL)
5938 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5939 036813ee 2019-05-09 stsp return NULL;
5942 afa376bf 2019-05-09 stsp static const struct got_error *
5943 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5944 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5946 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5947 5f8a88c6 2019-08-03 stsp unsigned char status;
5949 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5950 0ff8d236 2021-09-28 stsp return NULL;
5952 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5953 afa376bf 2019-05-09 stsp ct_path++;
5955 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5956 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5958 5f8a88c6 2019-08-03 stsp status = ct->status;
5960 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5961 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5964 036813ee 2019-05-09 stsp static const struct got_error *
5965 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5966 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5968 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5969 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5970 44d03001 2019-05-09 stsp char *te_path;
5972 44d03001 2019-05-09 stsp *modified = 0;
5974 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5975 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5976 44d03001 2019-05-09 stsp te->name) == -1)
5977 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5979 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5980 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5981 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5982 44d03001 2019-05-09 stsp strlen(te_path));
5983 62d463ca 2020-10-20 naddy if (*modified)
5987 44d03001 2019-05-09 stsp free(te_path);
5988 44d03001 2019-05-09 stsp return err;
5991 44d03001 2019-05-09 stsp static const struct got_error *
5992 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5993 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5994 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5996 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5997 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5999 036813ee 2019-05-09 stsp *ctp = NULL;
6001 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6002 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6003 036813ee 2019-05-09 stsp char *ct_name = NULL;
6004 036813ee 2019-05-09 stsp int path_matches;
6006 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6007 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
6008 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
6009 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
6010 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
6013 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
6014 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
6018 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6021 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6023 036813ee 2019-05-09 stsp return err;
6024 036813ee 2019-05-09 stsp if (!path_matches)
6027 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
6029 d34b633e 2020-10-19 stsp return err;
6031 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
6032 d34b633e 2020-10-19 stsp free(ct_name);
6035 d34b633e 2020-10-19 stsp free(ct_name);
6037 036813ee 2019-05-09 stsp *ctp = ct;
6041 2b496619 2019-07-10 stsp return err;
6044 2b496619 2019-07-10 stsp static const struct got_error *
6045 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6046 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
6047 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
6048 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
6049 2b496619 2019-07-10 stsp struct got_repository *repo)
6051 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
6052 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
6053 2b496619 2019-07-10 stsp char *subtree_path;
6054 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
6055 ba580f68 2020-03-22 stsp int nentries;
6057 2b496619 2019-07-10 stsp *new_tep = NULL;
6059 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6060 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
6061 2b496619 2019-07-10 stsp child_path) == -1)
6062 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
6064 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
6065 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
6066 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
6067 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
6069 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6070 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
6071 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
6072 2b496619 2019-07-10 stsp goto done;
6074 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
6075 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
6076 2b496619 2019-07-10 stsp if (err) {
6077 56e0773d 2019-11-28 stsp free(new_te);
6078 2b496619 2019-07-10 stsp goto done;
6080 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
6083 2b496619 2019-07-10 stsp free(subtree_path);
6084 2b496619 2019-07-10 stsp if (err == NULL)
6085 2b496619 2019-07-10 stsp *new_tep = new_te;
6086 036813ee 2019-05-09 stsp return err;
6089 036813ee 2019-05-09 stsp static const struct got_error *
6090 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
6091 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
6092 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
6093 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6094 036813ee 2019-05-09 stsp struct got_repository *repo)
6096 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
6097 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
6098 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
6099 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
6101 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
6102 ba580f68 2020-03-22 stsp *nentries = 0;
6104 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
6105 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6106 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6107 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
6109 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
6110 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
6111 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
6114 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6115 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
6118 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6119 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
6121 036813ee 2019-05-09 stsp goto done;
6123 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
6124 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
6125 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
6127 036813ee 2019-05-09 stsp goto done;
6128 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
6130 afa376bf 2019-05-09 stsp goto done;
6131 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
6132 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6134 2f51b5b3 2019-05-09 stsp goto done;
6135 ba580f68 2020-03-22 stsp (*nentries)++;
6137 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
6138 2b496619 2019-07-10 stsp if (base_tree == NULL ||
6139 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
6140 2b496619 2019-07-10 stsp == NULL) {
6141 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
6142 2b496619 2019-07-10 stsp child_path, path_base_tree,
6143 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
6146 2b496619 2019-07-10 stsp goto done;
6147 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6149 2b496619 2019-07-10 stsp goto done;
6150 ba580f68 2020-03-22 stsp (*nentries)++;
6155 2f51b5b3 2019-05-09 stsp if (base_tree) {
6156 56e0773d 2019-11-28 stsp int i, nbase_entries;
6157 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
6158 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
6159 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
6160 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
6162 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
6163 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
6164 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
6165 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
6167 63c5ca5d 2019-08-24 stsp goto done;
6168 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
6170 63c5ca5d 2019-08-24 stsp goto done;
6171 ba580f68 2020-03-22 stsp (*nentries)++;
6175 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
6176 44d03001 2019-05-09 stsp int modified;
6177 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6179 036813ee 2019-05-09 stsp goto done;
6180 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
6181 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
6183 2f51b5b3 2019-05-09 stsp goto done;
6184 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
6185 44d03001 2019-05-09 stsp if (modified) {
6186 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
6187 ba580f68 2020-03-22 stsp int nsubentries;
6188 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
6189 ba580f68 2020-03-22 stsp &nsubentries, te,
6190 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
6191 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
6193 44d03001 2019-05-09 stsp goto done;
6194 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6195 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6196 ba580f68 2020-03-22 stsp free(new_id);
6199 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6200 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6201 56e0773d 2019-11-28 stsp free(new_id);
6203 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6205 036813ee 2019-05-09 stsp goto done;
6206 ba580f68 2020-03-22 stsp (*nentries)++;
6210 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6211 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6213 f66c734c 2019-09-22 stsp goto done;
6215 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6216 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6217 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6218 12383673 2023-02-18 mark ct->status == GOT_STATUS_CONFLICT ||
6219 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6220 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6221 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6223 2f51b5b3 2019-05-09 stsp goto done;
6224 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6226 2f51b5b3 2019-05-09 stsp goto done;
6227 ba580f68 2020-03-22 stsp (*nentries)++;
6229 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6230 b416585c 2019-05-13 stsp status_arg);
6232 afa376bf 2019-05-09 stsp goto done;
6234 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6235 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6237 2f51b5b3 2019-05-09 stsp goto done;
6238 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6240 2f51b5b3 2019-05-09 stsp goto done;
6241 ba580f68 2020-03-22 stsp (*nentries)++;
6246 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6247 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6249 d8bacb93 2023-01-10 mark got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6250 2e1fa222 2020-07-23 stsp return err;
6253 2e1fa222 2020-07-23 stsp static const struct got_error *
6254 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6255 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6256 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6257 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6259 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6260 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6261 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6263 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6264 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6265 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6267 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6269 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6270 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6271 437adc9d 2020-12-10 yzhong if (err)
6272 437adc9d 2020-12-10 yzhong goto done;
6275 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6276 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6277 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6278 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6279 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6280 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6281 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6282 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6284 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6285 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6286 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6287 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6288 72fd46fa 2019-09-06 stsp !have_staged_files);
6290 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6291 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6292 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6293 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6294 72fd46fa 2019-09-06 stsp !have_staged_files);
6296 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6298 437adc9d 2020-12-10 yzhong goto done;
6299 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6300 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6301 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6302 3969253a 2020-03-07 stsp if (err) {
6303 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6304 437adc9d 2020-12-10 yzhong goto done;
6306 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6307 3969253a 2020-03-07 stsp if (err) {
6308 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6309 437adc9d 2020-12-10 yzhong goto done;
6312 437adc9d 2020-12-10 yzhong free(relpath);
6313 437adc9d 2020-12-10 yzhong relpath = NULL;
6316 437adc9d 2020-12-10 yzhong free(relpath);
6317 d56d26ce 2019-05-10 stsp return err;
6321 d56d26ce 2019-05-10 stsp static const struct got_error *
6322 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6323 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6324 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6325 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6326 735ef5ac 2019-08-03 stsp int ood_errcode)
6328 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6329 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6330 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6332 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6333 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6334 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6335 1a36436d 2019-06-10 stsp return NULL;
6337 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6338 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6340 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
6342 a44927cc 2022-04-07 stsp goto done;
6343 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6344 9bead371 2019-07-28 stsp if (err) {
6345 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6346 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6347 1a36436d 2019-06-10 stsp goto done;
6348 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6349 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6351 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6352 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
6354 a44927cc 2022-04-07 stsp goto done;
6355 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6356 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6357 1a36436d 2019-06-10 stsp goto done;
6358 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6362 a44927cc 2022-04-07 stsp if (commit)
6363 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6364 1a36436d 2019-06-10 stsp return err;
6367 336075a4 2022-06-25 op static const struct got_error *
6368 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6369 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6370 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
6371 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
6372 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
6373 2a47b1e5 2022-11-01 stsp const char *author, const char *committer, char *diff_path,
6374 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6375 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6376 afa376bf 2019-05-09 stsp struct got_repository *repo)
6378 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6379 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6380 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6381 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6382 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6383 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6384 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6385 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6386 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
6387 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6388 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6389 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6390 f8399b8f 2022-07-22 stsp time_t timestamp;
6392 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6394 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6396 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6398 588edf97 2019-05-10 stsp goto done;
6400 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6402 588edf97 2019-05-10 stsp goto done;
6404 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6405 2a47b1e5 2022-11-01 stsp err = commit_msg_cb(commitable_paths, diff_path,
6406 2a47b1e5 2022-11-01 stsp &logmsg, commit_arg);
6411 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6412 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6416 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6417 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6418 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6419 cf066bf8 2019-05-09 stsp char *ondisk_path;
6421 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6422 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6423 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6426 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6427 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6428 12383673 2023-02-18 mark ct->status != GOT_STATUS_MODE_CHANGE &&
6429 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
6432 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6433 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6434 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6435 cf066bf8 2019-05-09 stsp goto done;
6437 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6438 2e1fa222 2020-07-23 stsp free(ondisk_path);
6440 cf066bf8 2019-05-09 stsp goto done;
6443 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6444 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6445 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6447 036813ee 2019-05-09 stsp goto done;
6449 2b706956 2023-04-17 stsp err = got_object_qid_alloc(&pid, head_commit_id);
6451 de18fc63 2019-05-09 stsp goto done;
6452 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6453 f259c4c1 2021-09-24 stsp nparents++;
6454 f259c4c1 2021-09-24 stsp if (parent_id2) {
6455 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
6457 f259c4c1 2021-09-24 stsp goto done;
6458 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6459 f259c4c1 2021-09-24 stsp nparents++;
6461 f8399b8f 2022-07-22 stsp timestamp = time(NULL);
6462 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6463 f8399b8f 2022-07-22 stsp nparents, author, timestamp, committer, timestamp, logmsg, repo);
6464 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6465 33ad4cbe 2019-05-12 jcs free(logmsg);
6467 9d40349a 2019-05-09 stsp goto done;
6469 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6470 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6471 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6472 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6473 09f5bd90 2019-05-09 stsp goto done;
6475 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6476 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6478 09f5bd90 2019-05-09 stsp goto done;
6479 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6481 ebf99748 2019-05-09 stsp goto done;
6482 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6483 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6484 09f5bd90 2019-05-09 stsp goto done;
6486 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6487 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6489 f2c16586 2019-05-09 stsp goto done;
6490 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6492 f2c16586 2019-05-09 stsp goto done;
6494 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6496 09f5bd90 2019-05-09 stsp goto done;
6498 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6500 ebf99748 2019-05-09 stsp goto done;
6502 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
6503 588edf97 2019-05-10 stsp if (head_tree)
6504 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6505 588edf97 2019-05-10 stsp if (head_commit)
6506 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6507 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6508 2f17228e 2019-05-12 stsp if (head_ref2) {
6509 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6510 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6511 2f17228e 2019-05-12 stsp err = unlockerr;
6512 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6514 39cd0ff6 2019-07-12 stsp return err;
6517 5c1e53bc 2019-07-28 stsp static const struct got_error *
6518 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6519 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6521 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6522 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6524 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6525 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6526 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6528 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6529 5c1e53bc 2019-07-28 stsp ct_path++;
6531 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6532 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6536 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6537 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6539 5c1e53bc 2019-07-28 stsp return NULL;
6542 f0b75401 2019-08-03 stsp static const struct got_error *
6543 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6545 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6547 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6548 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6549 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6552 f0b75401 2019-08-03 stsp return NULL;
6555 5f8a88c6 2019-08-03 stsp static const struct got_error *
6556 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6557 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6559 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6560 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6562 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6563 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6565 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6566 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6567 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6568 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6569 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6570 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6573 5f8a88c6 2019-08-03 stsp return NULL;
6576 39cd0ff6 2019-07-12 stsp const struct got_error *
6577 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6578 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6579 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6580 12383673 2023-02-18 mark int show_diff, int commit_conflicts,
6581 12383673 2023-02-18 mark got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6582 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6583 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6585 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6586 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6587 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6588 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6589 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6590 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6591 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6592 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6593 2a47b1e5 2022-11-01 stsp char *diff_path = NULL;
6594 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6596 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6598 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6599 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6601 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6603 39cd0ff6 2019-07-12 stsp goto done;
6605 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6607 39cd0ff6 2019-07-12 stsp goto done;
6609 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6611 39cd0ff6 2019-07-12 stsp goto done;
6613 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6615 39cd0ff6 2019-07-12 stsp goto done;
6617 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6618 5f8a88c6 2019-08-03 stsp &have_staged_files);
6619 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6620 5f8a88c6 2019-08-03 stsp goto done;
6621 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6622 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6624 5f8a88c6 2019-08-03 stsp goto done;
6627 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6628 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6629 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6630 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6631 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6632 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6633 2a47b1e5 2022-11-01 stsp cc_arg.diff_header_shown = 0;
6634 12383673 2023-02-18 mark cc_arg.commit_conflicts = commit_conflicts;
6635 2a47b1e5 2022-11-01 stsp if (show_diff) {
6636 2a47b1e5 2022-11-01 stsp err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6637 b90054ed 2022-11-01 stsp GOT_TMPDIR_STR "/got", ".diff");
6639 2a47b1e5 2022-11-01 stsp goto done;
6640 2a47b1e5 2022-11-01 stsp cc_arg.f1 = got_opentemp();
6641 2a47b1e5 2022-11-01 stsp if (cc_arg.f1 == NULL) {
6642 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6643 2a47b1e5 2022-11-01 stsp goto done;
6645 2a47b1e5 2022-11-01 stsp cc_arg.f2 = got_opentemp();
6646 2a47b1e5 2022-11-01 stsp if (cc_arg.f2 == NULL) {
6647 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6648 2a47b1e5 2022-11-01 stsp goto done;
6652 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6653 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6654 4ba2e955 2022-09-02 sh+got collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6656 5c1e53bc 2019-07-28 stsp goto done;
6659 2a47b1e5 2022-11-01 stsp if (show_diff) {
6660 2a47b1e5 2022-11-01 stsp if (fflush(cc_arg.diff_outfile) == EOF) {
6661 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fflush");
6662 2a47b1e5 2022-11-01 stsp goto done;
6666 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6667 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6668 39cd0ff6 2019-07-12 stsp goto done;
6671 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6672 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6674 5c1e53bc 2019-07-28 stsp goto done;
6677 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6678 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6679 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6681 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6682 f0b75401 2019-08-03 stsp ct_path++;
6683 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6684 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6685 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6687 b50cabdf 2019-07-12 stsp goto done;
6691 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6692 210c2321 2022-11-01 stsp head_commit_id, NULL, worktree, author, committer,
6693 210c2321 2022-11-01 stsp (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6694 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6696 39cd0ff6 2019-07-12 stsp goto done;
6698 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6699 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6700 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6701 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6702 39cd0ff6 2019-07-12 stsp err = sync_err;
6704 39cd0ff6 2019-07-12 stsp if (fileindex)
6705 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6706 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6707 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6708 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6709 39cd0ff6 2019-07-12 stsp err = unlockerr;
6710 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6711 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6713 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6715 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6716 2a47b1e5 2022-11-01 stsp if (diff_path && unlink(diff_path) == -1 && err == NULL)
6717 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("unlink", diff_path);
6718 2a47b1e5 2022-11-01 stsp free(diff_path);
6719 2a47b1e5 2022-11-01 stsp if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6720 2a47b1e5 2022-11-01 stsp err == NULL)
6721 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
6722 c4296144 2019-05-09 stsp return err;
6725 8656d6c4 2019-05-20 stsp const char *
6726 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6728 8656d6c4 2019-05-20 stsp return ct->path;
6731 8656d6c4 2019-05-20 stsp unsigned int
6732 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6734 8656d6c4 2019-05-20 stsp return ct->status;
6737 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6738 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6739 818c7501 2019-07-11 stsp struct got_repository *repo;
6742 818c7501 2019-07-11 stsp static const struct got_error *
6743 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6745 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6746 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6747 818c7501 2019-07-11 stsp unsigned char status;
6748 818c7501 2019-07-11 stsp struct stat sb;
6749 818c7501 2019-07-11 stsp char *ondisk_path;
6751 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6752 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6753 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6754 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6756 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6758 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6760 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6761 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6762 818c7501 2019-07-11 stsp free(ondisk_path);
6764 818c7501 2019-07-11 stsp return err;
6766 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6767 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6768 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6769 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6771 818c7501 2019-07-11 stsp return NULL;
6774 818c7501 2019-07-11 stsp const struct got_error *
6775 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6776 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6777 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6778 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6780 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6781 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6782 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6783 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6784 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6785 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6786 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6788 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6789 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6790 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6792 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6794 818c7501 2019-07-11 stsp return err;
6796 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6798 818c7501 2019-07-11 stsp goto done;
6800 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6801 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6802 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6805 818c7501 2019-07-11 stsp goto done;
6807 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6809 818c7501 2019-07-11 stsp goto done;
6811 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6813 818c7501 2019-07-11 stsp goto done;
6815 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6817 818c7501 2019-07-11 stsp goto done;
6819 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6822 e51d7b55 2020-01-04 stsp goto done;
6824 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6826 818c7501 2019-07-11 stsp goto done;
6827 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6828 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6829 e51d7b55 2020-01-04 stsp goto done;
6832 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6833 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6835 818c7501 2019-07-11 stsp goto done;
6836 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6838 818c7501 2019-07-11 stsp goto done;
6840 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6842 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6844 818c7501 2019-07-11 stsp goto done;
6846 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6848 818c7501 2019-07-11 stsp goto done;
6850 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6851 818c7501 2019-07-11 stsp worktree->base_commit_id);
6853 818c7501 2019-07-11 stsp goto done;
6854 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6856 818c7501 2019-07-11 stsp goto done;
6858 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6860 818c7501 2019-07-11 stsp goto done;
6862 818c7501 2019-07-11 stsp free(fileindex_path);
6863 818c7501 2019-07-11 stsp free(tmp_branch_name);
6864 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6865 818c7501 2019-07-11 stsp free(branch_ref_name);
6866 818c7501 2019-07-11 stsp if (branch_ref)
6867 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6868 818c7501 2019-07-11 stsp if (wt_branch)
6869 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6870 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6871 818c7501 2019-07-11 stsp if (err) {
6872 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6873 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6874 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6876 818c7501 2019-07-11 stsp if (*tmp_branch) {
6877 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6878 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6880 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6881 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6882 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6884 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6886 818c7501 2019-07-11 stsp return err;
6889 818c7501 2019-07-11 stsp const struct got_error *
6890 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6891 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6892 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6893 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6895 818c7501 2019-07-11 stsp const struct got_error *err;
6896 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6897 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6898 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6899 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6900 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6902 818c7501 2019-07-11 stsp *commit_id = NULL;
6903 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6904 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6905 3e3a69f1 2019-07-25 stsp *branch = NULL;
6906 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6908 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6910 3e3a69f1 2019-07-25 stsp return err;
6912 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6914 f032f1f7 2019-08-04 stsp goto done;
6916 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6917 f032f1f7 2019-08-04 stsp &have_staged_files);
6918 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6919 f032f1f7 2019-08-04 stsp goto done;
6920 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6921 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6922 3e3a69f1 2019-07-25 stsp goto done;
6925 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6927 f032f1f7 2019-08-04 stsp goto done;
6929 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6931 818c7501 2019-07-11 stsp goto done;
6933 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6935 818c7501 2019-07-11 stsp goto done;
6937 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6939 818c7501 2019-07-11 stsp goto done;
6941 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6943 818c7501 2019-07-11 stsp goto done;
6945 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6946 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6948 818c7501 2019-07-11 stsp goto done;
6950 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6952 818c7501 2019-07-11 stsp goto done;
6954 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6956 818c7501 2019-07-11 stsp goto done;
6958 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6959 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6961 818c7501 2019-07-11 stsp goto done;
6963 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6965 818c7501 2019-07-11 stsp goto done;
6967 818c7501 2019-07-11 stsp free(commit_ref_name);
6968 818c7501 2019-07-11 stsp free(branch_ref_name);
6969 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6970 818c7501 2019-07-11 stsp if (commit_ref)
6971 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6972 818c7501 2019-07-11 stsp if (branch_ref)
6973 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6974 818c7501 2019-07-11 stsp if (err) {
6975 818c7501 2019-07-11 stsp free(*commit_id);
6976 818c7501 2019-07-11 stsp *commit_id = NULL;
6977 818c7501 2019-07-11 stsp if (*tmp_branch) {
6978 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6979 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6981 818c7501 2019-07-11 stsp if (*new_base_branch) {
6982 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6983 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6985 818c7501 2019-07-11 stsp if (*branch) {
6986 818c7501 2019-07-11 stsp got_ref_close(*branch);
6987 818c7501 2019-07-11 stsp *branch = NULL;
6989 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6990 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6991 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6993 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6995 818c7501 2019-07-11 stsp return err;
6998 818c7501 2019-07-11 stsp const struct got_error *
6999 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
7001 818c7501 2019-07-11 stsp const struct got_error *err;
7002 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
7004 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7006 818c7501 2019-07-11 stsp return err;
7008 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7009 818c7501 2019-07-11 stsp free(tmp_branch_name);
7010 818c7501 2019-07-11 stsp return NULL;
7013 818c7501 2019-07-11 stsp static const struct got_error *
7014 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7015 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
7017 0ebf8283 2019-07-24 stsp *logmsg = arg;
7018 818c7501 2019-07-11 stsp return NULL;
7021 818c7501 2019-07-11 stsp static const struct got_error *
7022 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7023 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7024 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7025 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7027 818c7501 2019-07-11 stsp return NULL;
7030 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
7031 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
7032 01757395 2019-07-12 stsp void *progress_arg;
7033 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
7036 01757395 2019-07-12 stsp static const struct got_error *
7037 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
7039 01757395 2019-07-12 stsp const struct got_error *err;
7040 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
7042 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
7044 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
7046 01757395 2019-07-12 stsp return err;
7048 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
7049 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
7050 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
7051 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
7052 01757395 2019-07-12 stsp return NULL;
7054 01757395 2019-07-12 stsp p = strdup(path);
7055 01757395 2019-07-12 stsp if (p == NULL)
7056 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
7058 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7059 01757395 2019-07-12 stsp if (err || new == NULL)
7061 01757395 2019-07-12 stsp return err;
7064 0ebf8283 2019-07-24 stsp static const struct got_error *
7065 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7066 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
7068 818c7501 2019-07-11 stsp const struct got_error *err;
7069 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
7071 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7072 818c7501 2019-07-11 stsp if (err) {
7073 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
7074 818c7501 2019-07-11 stsp goto done;
7075 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7077 818c7501 2019-07-11 stsp goto done;
7078 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
7080 818c7501 2019-07-11 stsp goto done;
7081 de05890f 2020-03-05 stsp } else if (is_rebase) {
7082 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
7085 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
7087 818c7501 2019-07-11 stsp goto done;
7088 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
7089 818c7501 2019-07-11 stsp free(stored_id);
7090 818c7501 2019-07-11 stsp if (cmp != 0) {
7091 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7092 818c7501 2019-07-11 stsp goto done;
7096 0ebf8283 2019-07-24 stsp if (commit_ref)
7097 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7098 0ebf8283 2019-07-24 stsp return err;
7101 0ebf8283 2019-07-24 stsp static const struct got_error *
7102 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
7103 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
7104 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7105 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
7106 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7107 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7109 0ebf8283 2019-07-24 stsp const struct got_error *err;
7110 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7111 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
7112 3e3a69f1 2019-07-25 stsp char *fileindex_path;
7114 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7116 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7118 0ebf8283 2019-07-24 stsp return err;
7120 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
7121 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
7122 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
7123 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
7124 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
7125 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
7126 818c7501 2019-07-11 stsp if (commit_ref)
7127 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
7128 818c7501 2019-07-11 stsp return err;
7131 818c7501 2019-07-11 stsp const struct got_error *
7132 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7133 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7134 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7135 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7136 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7137 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7139 0ebf8283 2019-07-24 stsp const struct got_error *err;
7140 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7142 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7144 0ebf8283 2019-07-24 stsp return err;
7146 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7148 0ebf8283 2019-07-24 stsp goto done;
7150 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7151 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7152 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7154 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7155 0ebf8283 2019-07-24 stsp return err;
7158 0ebf8283 2019-07-24 stsp const struct got_error *
7159 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7160 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7161 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7162 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7163 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7164 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7166 0ebf8283 2019-07-24 stsp const struct got_error *err;
7167 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7169 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7171 0ebf8283 2019-07-24 stsp return err;
7173 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7175 0ebf8283 2019-07-24 stsp goto done;
7177 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7178 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7179 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7181 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7182 0ebf8283 2019-07-24 stsp return err;
7185 0ebf8283 2019-07-24 stsp static const struct got_error *
7186 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
7187 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7188 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7189 598eac43 2022-07-22 stsp struct got_reference *tmp_branch, const char *committer,
7190 598eac43 2022-07-22 stsp struct got_commit_object *orig_commit, const char *new_logmsg,
7191 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
7193 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7194 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7195 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7196 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7197 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7198 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7199 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7201 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
7202 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7203 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7205 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7207 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7209 3e3a69f1 2019-07-25 stsp return err;
7211 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7212 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7213 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7214 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7215 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
7217 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7218 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7220 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7221 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7222 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7223 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7224 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7225 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7227 01757395 2019-07-12 stsp if (merged_paths) {
7228 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7229 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7230 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7231 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7234 01757395 2019-07-12 stsp goto done;
7237 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7238 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7240 01757395 2019-07-12 stsp goto done;
7243 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7244 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7245 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7247 a0e95631 2019-07-12 stsp goto done;
7248 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7249 a0e95631 2019-07-12 stsp goto done;
7252 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7254 edd02c5e 2019-07-11 stsp goto done;
7256 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7258 818c7501 2019-07-11 stsp goto done;
7260 5943eee2 2019-08-13 stsp if (new_logmsg) {
7261 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7262 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7263 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7264 5943eee2 2019-08-13 stsp goto done;
7267 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7269 5943eee2 2019-08-13 stsp goto done;
7272 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7273 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7274 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
7275 50e7a649 2022-07-22 stsp committer ? committer :
7276 2a47b1e5 2022-11-01 stsp got_object_commit_get_committer(orig_commit), NULL,
7277 50e7a649 2022-07-22 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7279 a0e95631 2019-07-12 stsp goto done;
7281 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7283 a0e95631 2019-07-12 stsp goto done;
7285 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7287 a0e95631 2019-07-12 stsp goto done;
7289 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7290 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7291 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7292 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7293 a0e95631 2019-07-12 stsp err = sync_err;
7295 a0e95631 2019-07-12 stsp free(fileindex_path);
7296 a0e95631 2019-07-12 stsp free(head_commit_id);
7297 a0e95631 2019-07-12 stsp if (head_ref)
7298 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7299 818c7501 2019-07-11 stsp if (err) {
7300 818c7501 2019-07-11 stsp free(*new_commit_id);
7301 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7303 818c7501 2019-07-11 stsp return err;
7306 818c7501 2019-07-11 stsp const struct got_error *
7307 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7308 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7309 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7310 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
7311 12383673 2023-02-18 mark struct got_object_id *orig_commit_id, int allow_conflict,
7312 12383673 2023-02-18 mark struct got_repository *repo)
7314 0ebf8283 2019-07-24 stsp const struct got_error *err;
7315 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7316 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7317 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7319 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7321 0ebf8283 2019-07-24 stsp return err;
7323 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7325 0ebf8283 2019-07-24 stsp goto done;
7326 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7328 0ebf8283 2019-07-24 stsp goto done;
7329 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7330 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7331 0ebf8283 2019-07-24 stsp goto done;
7334 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7335 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
7336 12383673 2023-02-18 mark NULL, allow_conflict, repo);
7338 0ebf8283 2019-07-24 stsp if (commit_ref)
7339 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7340 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7341 0ebf8283 2019-07-24 stsp free(commit_id);
7342 0ebf8283 2019-07-24 stsp return err;
7345 0ebf8283 2019-07-24 stsp const struct got_error *
7346 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7347 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7348 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7349 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
7350 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7351 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
7353 0ebf8283 2019-07-24 stsp const struct got_error *err;
7354 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7355 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7357 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7359 0ebf8283 2019-07-24 stsp return err;
7361 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7363 0ebf8283 2019-07-24 stsp goto done;
7365 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7366 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
7367 12383673 2023-02-18 mark new_logmsg, allow_conflict, repo);
7369 0ebf8283 2019-07-24 stsp if (commit_ref)
7370 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7371 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7372 0ebf8283 2019-07-24 stsp return err;
7375 0ebf8283 2019-07-24 stsp const struct got_error *
7376 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7377 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7379 3e3a69f1 2019-07-25 stsp if (fileindex)
7380 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7381 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7384 69844fba 2019-07-11 stsp static const struct got_error *
7385 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7387 69844fba 2019-07-11 stsp const struct got_error *err;
7388 69844fba 2019-07-11 stsp struct got_reference *ref;
7390 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7391 69844fba 2019-07-11 stsp if (err) {
7392 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7393 69844fba 2019-07-11 stsp return NULL;
7394 69844fba 2019-07-11 stsp return err;
7397 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7398 69844fba 2019-07-11 stsp got_ref_close(ref);
7399 69844fba 2019-07-11 stsp return err;
7402 69844fba 2019-07-11 stsp static const struct got_error *
7403 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7405 69844fba 2019-07-11 stsp const struct got_error *err;
7406 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7407 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7409 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7411 69844fba 2019-07-11 stsp goto done;
7412 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7414 69844fba 2019-07-11 stsp goto done;
7416 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7418 69844fba 2019-07-11 stsp goto done;
7419 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7421 69844fba 2019-07-11 stsp goto done;
7423 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7425 69844fba 2019-07-11 stsp goto done;
7426 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7428 69844fba 2019-07-11 stsp goto done;
7430 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7432 69844fba 2019-07-11 stsp goto done;
7433 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7435 69844fba 2019-07-11 stsp goto done;
7438 69844fba 2019-07-11 stsp free(tmp_branch_name);
7439 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7440 69844fba 2019-07-11 stsp free(branch_ref_name);
7441 69844fba 2019-07-11 stsp free(commit_ref_name);
7442 e600f124 2021-03-21 stsp return err;
7445 336075a4 2022-06-25 op static const struct got_error *
7446 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7447 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7449 e600f124 2021-03-21 stsp const struct got_error *err;
7450 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7451 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7452 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7453 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7454 e600f124 2021-03-21 stsp char *refname = NULL;
7456 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7457 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7458 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7459 e600f124 2021-03-21 stsp branch_name += 11;
7461 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7463 e600f124 2021-03-21 stsp return err;
7465 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7466 e600f124 2021-03-21 stsp new_id_str) == -1) {
7467 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7468 e600f124 2021-03-21 stsp goto done;
7471 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7473 e600f124 2021-03-21 stsp goto done;
7475 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7477 e600f124 2021-03-21 stsp goto done;
7479 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7481 e600f124 2021-03-21 stsp free(new_id_str);
7482 e600f124 2021-03-21 stsp free(refname);
7483 e600f124 2021-03-21 stsp free(old_commit_id);
7485 e600f124 2021-03-21 stsp got_ref_close(ref);
7486 69844fba 2019-07-11 stsp return err;
7489 818c7501 2019-07-11 stsp const struct got_error *
7490 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7491 ef85a376 2023-02-01 mark struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7492 ef85a376 2023-02-01 mark struct got_reference *rebased_branch, struct got_repository *repo,
7493 ef85a376 2023-02-01 mark int create_backup)
7495 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7496 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7497 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7499 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7501 818c7501 2019-07-11 stsp return err;
7503 e600f124 2021-03-21 stsp if (create_backup) {
7504 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7505 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7507 e600f124 2021-03-21 stsp goto done;
7510 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7512 818c7501 2019-07-11 stsp goto done;
7514 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7516 818c7501 2019-07-11 stsp goto done;
7518 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7520 818c7501 2019-07-11 stsp goto done;
7522 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7524 a615e0e7 2020-12-16 stsp goto done;
7526 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7528 a615e0e7 2020-12-16 stsp goto done;
7529 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7530 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7531 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7532 a615e0e7 2020-12-16 stsp err = sync_err;
7534 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7535 a615e0e7 2020-12-16 stsp free(fileindex_path);
7536 818c7501 2019-07-11 stsp free(new_head_commit_id);
7537 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7538 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7539 818c7501 2019-07-11 stsp err = unlockerr;
7540 818c7501 2019-07-11 stsp return err;
7543 af179be7 2023-04-14 stsp static const struct got_error *
7544 af179be7 2023-04-14 stsp get_paths_changed_between_commits(struct got_pathlist_head *paths,
7545 af179be7 2023-04-14 stsp struct got_object_id *id1, struct got_object_id *id2,
7546 af179be7 2023-04-14 stsp struct got_repository *repo)
7548 af179be7 2023-04-14 stsp const struct got_error *err;
7549 af179be7 2023-04-14 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7550 af179be7 2023-04-14 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7552 af179be7 2023-04-14 stsp if (id1) {
7553 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit1, repo, id1);
7555 af179be7 2023-04-14 stsp goto done;
7557 af179be7 2023-04-14 stsp err = got_object_open_as_tree(&tree1, repo,
7558 af179be7 2023-04-14 stsp got_object_commit_get_tree_id(commit1));
7560 af179be7 2023-04-14 stsp goto done;
7563 af179be7 2023-04-14 stsp if (id2) {
7564 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit2, repo, id2);
7566 af179be7 2023-04-14 stsp goto done;
7568 af179be7 2023-04-14 stsp err = got_object_open_as_tree(&tree2, repo,
7569 af179be7 2023-04-14 stsp got_object_commit_get_tree_id(commit2));
7571 af179be7 2023-04-14 stsp goto done;
7574 af179be7 2023-04-14 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7575 af179be7 2023-04-14 stsp got_diff_tree_collect_changed_paths, paths, 0);
7577 af179be7 2023-04-14 stsp goto done;
7579 af179be7 2023-04-14 stsp if (commit1)
7580 af179be7 2023-04-14 stsp got_object_commit_close(commit1);
7581 af179be7 2023-04-14 stsp if (commit2)
7582 af179be7 2023-04-14 stsp got_object_commit_close(commit2);
7583 af179be7 2023-04-14 stsp if (tree1)
7584 af179be7 2023-04-14 stsp got_object_tree_close(tree1);
7585 af179be7 2023-04-14 stsp if (tree2)
7586 af179be7 2023-04-14 stsp got_object_tree_close(tree2);
7587 af179be7 2023-04-14 stsp return err;
7590 af179be7 2023-04-14 stsp static const struct got_error *
7591 af179be7 2023-04-14 stsp get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7592 af179be7 2023-04-14 stsp struct got_object_id *id1, struct got_object_id *id2,
7593 af179be7 2023-04-14 stsp const char *path_prefix, struct got_repository *repo)
7595 af179be7 2023-04-14 stsp const struct got_error *err;
7596 af179be7 2023-04-14 stsp struct got_pathlist_head merged_paths;
7597 af179be7 2023-04-14 stsp struct got_pathlist_entry *pe;
7598 af179be7 2023-04-14 stsp char *abspath = NULL, *wt_path = NULL;
7600 af179be7 2023-04-14 stsp TAILQ_INIT(&merged_paths);
7602 af179be7 2023-04-14 stsp err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7604 af179be7 2023-04-14 stsp goto done;
7606 af179be7 2023-04-14 stsp TAILQ_FOREACH(pe, &merged_paths, entry) {
7607 af179be7 2023-04-14 stsp struct got_diff_changed_path *change = pe->data;
7609 af179be7 2023-04-14 stsp if (change->status != GOT_STATUS_ADD)
7612 af179be7 2023-04-14 stsp if (got_path_is_root_dir(path_prefix)) {
7613 af179be7 2023-04-14 stsp wt_path = strdup(pe->path);
7614 af179be7 2023-04-14 stsp if (wt_path == NULL) {
7615 af179be7 2023-04-14 stsp err = got_error_from_errno("strdup");
7616 af179be7 2023-04-14 stsp goto done;
7619 af179be7 2023-04-14 stsp if (asprintf(&abspath, "/%s", pe->path) == -1) {
7620 af179be7 2023-04-14 stsp err = got_error_from_errno("asprintf");
7621 af179be7 2023-04-14 stsp goto done;
7624 af179be7 2023-04-14 stsp err = got_path_skip_common_ancestor(&wt_path,
7625 af179be7 2023-04-14 stsp path_prefix, abspath);
7627 af179be7 2023-04-14 stsp goto done;
7628 af179be7 2023-04-14 stsp free(abspath);
7629 af179be7 2023-04-14 stsp abspath = NULL;
7632 af179be7 2023-04-14 stsp err = got_pathlist_append(added_paths, wt_path, NULL);
7634 af179be7 2023-04-14 stsp goto done;
7635 af179be7 2023-04-14 stsp wt_path = NULL;
7639 af179be7 2023-04-14 stsp got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7640 af179be7 2023-04-14 stsp free(abspath);
7641 af179be7 2023-04-14 stsp free(wt_path);
7642 af179be7 2023-04-14 stsp return err;
7645 af179be7 2023-04-14 stsp static const struct got_error *
7646 af179be7 2023-04-14 stsp get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7647 af179be7 2023-04-14 stsp struct got_object_id *id, const char *path_prefix,
7648 af179be7 2023-04-14 stsp struct got_repository *repo)
7650 af179be7 2023-04-14 stsp const struct got_error *err;
7651 af179be7 2023-04-14 stsp struct got_commit_object *commit = NULL;
7652 af179be7 2023-04-14 stsp struct got_object_qid *pid;
7654 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit, repo, id);
7656 af179be7 2023-04-14 stsp goto done;
7658 af179be7 2023-04-14 stsp pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7660 af179be7 2023-04-14 stsp err = get_paths_added_between_commits(added_paths,
7661 af179be7 2023-04-14 stsp pid ? &pid->id : NULL, id, path_prefix, repo);
7663 af179be7 2023-04-14 stsp goto done;
7665 af179be7 2023-04-14 stsp if (commit)
7666 af179be7 2023-04-14 stsp got_object_commit_close(commit);
7667 af179be7 2023-04-14 stsp return err;
7670 818c7501 2019-07-11 stsp const struct got_error *
7671 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7672 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7673 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7674 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7676 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7677 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7678 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7679 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
7680 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7681 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7682 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
7683 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
7684 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7685 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7686 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
7688 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
7690 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7692 818c7501 2019-07-11 stsp return err;
7694 af179be7 2023-04-14 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7696 af179be7 2023-04-14 stsp goto done;
7698 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7700 af179be7 2023-04-14 stsp goto done;
7702 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7704 af179be7 2023-04-14 stsp goto done;
7707 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
7708 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
7709 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files which were added by
7710 af179be7 2023-04-14 stsp * the user for conflict resolution purposes.
7712 af179be7 2023-04-14 stsp err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7713 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
7715 af179be7 2023-04-14 stsp goto done;
7717 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7718 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7720 818c7501 2019-07-11 stsp goto done;
7722 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7724 818c7501 2019-07-11 stsp goto done;
7727 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7728 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7729 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7731 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7733 818c7501 2019-07-11 stsp goto done;
7735 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7737 1b093d84 2023-04-12 stsp goto done;
7739 1b093d84 2023-04-12 stsp err = got_object_open_as_commit(&commit, repo,
7740 1b093d84 2023-04-12 stsp worktree->base_commit_id);
7742 818c7501 2019-07-11 stsp goto done;
7744 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7745 a44927cc 2022-04-07 stsp worktree->path_prefix);
7747 ca355955 2019-07-12 stsp goto done;
7749 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7751 ca355955 2019-07-12 stsp goto done;
7753 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7755 818c7501 2019-07-11 stsp goto done;
7757 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7758 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7759 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7760 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7761 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7762 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7763 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7764 af179be7 2023-04-14 stsp rfa.unlink_added_files = 1;
7765 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
7766 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7767 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7769 1f1abb7e 2019-08-08 stsp goto sync;
7771 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7772 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7774 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7775 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7776 ca355955 2019-07-12 stsp err = sync_err;
7778 af179be7 2023-04-14 stsp got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7779 818c7501 2019-07-11 stsp got_ref_close(resolved);
7780 a3a2faf2 2019-07-12 stsp free(tree_id);
7781 818c7501 2019-07-11 stsp free(commit_id);
7782 af179be7 2023-04-14 stsp free(merged_commit_id);
7783 a44927cc 2022-04-07 stsp if (commit)
7784 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7785 0ebf8283 2019-07-24 stsp if (fileindex)
7786 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7787 0ebf8283 2019-07-24 stsp free(fileindex_path);
7788 af179be7 2023-04-14 stsp free(commit_ref_name);
7789 af179be7 2023-04-14 stsp if (commit_ref)
7790 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
7792 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7793 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7794 0ebf8283 2019-07-24 stsp err = unlockerr;
7795 0ebf8283 2019-07-24 stsp return err;
7798 0ebf8283 2019-07-24 stsp const struct got_error *
7799 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7800 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7801 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7802 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7804 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7805 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7806 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7807 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7808 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7809 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7810 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7811 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7813 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7814 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7815 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7816 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7818 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7820 0ebf8283 2019-07-24 stsp return err;
7822 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7824 0ebf8283 2019-07-24 stsp goto done;
7826 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7827 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7828 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7831 0ebf8283 2019-07-24 stsp goto done;
7833 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7835 0ebf8283 2019-07-24 stsp goto done;
7837 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7839 0ebf8283 2019-07-24 stsp goto done;
7841 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7842 0ebf8283 2019-07-24 stsp worktree);
7844 0ebf8283 2019-07-24 stsp goto done;
7846 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7849 0ebf8283 2019-07-24 stsp goto done;
7851 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7853 0ebf8283 2019-07-24 stsp goto done;
7855 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7857 0ebf8283 2019-07-24 stsp goto done;
7859 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7860 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7862 0ebf8283 2019-07-24 stsp goto done;
7863 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7865 0ebf8283 2019-07-24 stsp goto done;
7866 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7867 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7868 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7869 0ebf8283 2019-07-24 stsp goto done;
7872 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7873 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7875 0ebf8283 2019-07-24 stsp goto done;
7876 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7878 0ebf8283 2019-07-24 stsp goto done;
7880 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7882 0ebf8283 2019-07-24 stsp goto done;
7884 0ebf8283 2019-07-24 stsp free(fileindex_path);
7885 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7886 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7887 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7888 0ebf8283 2019-07-24 stsp if (wt_branch)
7889 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7890 0ebf8283 2019-07-24 stsp if (err) {
7891 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7892 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7893 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7895 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7896 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7897 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7899 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7900 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7901 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7902 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7904 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7906 0ebf8283 2019-07-24 stsp return err;
7909 0ebf8283 2019-07-24 stsp const struct got_error *
7910 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7911 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7913 3e3a69f1 2019-07-25 stsp if (fileindex)
7914 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7915 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7918 0ebf8283 2019-07-24 stsp const struct got_error *
7919 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7920 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7922 0ebf8283 2019-07-24 stsp const struct got_error *err;
7923 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7925 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7927 0ebf8283 2019-07-24 stsp return err;
7929 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7930 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7931 0ebf8283 2019-07-24 stsp return NULL;
7934 0ebf8283 2019-07-24 stsp const struct got_error *
7935 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7936 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7937 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7938 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7940 0ebf8283 2019-07-24 stsp const struct got_error *err;
7941 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7942 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7943 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7944 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7945 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7946 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7948 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7949 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7950 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7951 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7953 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7955 3e3a69f1 2019-07-25 stsp return err;
7957 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7959 f032f1f7 2019-08-04 stsp goto done;
7961 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7962 f032f1f7 2019-08-04 stsp &have_staged_files);
7963 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7964 f032f1f7 2019-08-04 stsp goto done;
7965 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7966 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7967 3e3a69f1 2019-07-25 stsp goto done;
7970 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7972 f032f1f7 2019-08-04 stsp goto done;
7974 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7976 0ebf8283 2019-07-24 stsp goto done;
7978 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7980 0ebf8283 2019-07-24 stsp goto done;
7982 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7983 0ebf8283 2019-07-24 stsp worktree);
7985 0ebf8283 2019-07-24 stsp goto done;
7987 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7989 0ebf8283 2019-07-24 stsp goto done;
7991 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7993 0ebf8283 2019-07-24 stsp goto done;
7994 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7996 0ebf8283 2019-07-24 stsp goto done;
7998 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
8000 0ebf8283 2019-07-24 stsp goto done;
8001 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
8003 0ebf8283 2019-07-24 stsp goto done;
8005 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
8007 0ebf8283 2019-07-24 stsp goto done;
8009 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8010 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8011 3e3a69f1 2019-07-25 stsp free(fileindex_path);
8012 0ebf8283 2019-07-24 stsp if (commit_ref)
8013 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
8014 0ebf8283 2019-07-24 stsp if (base_commit_ref)
8015 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
8016 0ebf8283 2019-07-24 stsp if (err) {
8017 0ebf8283 2019-07-24 stsp free(*commit_id);
8018 0ebf8283 2019-07-24 stsp *commit_id = NULL;
8019 0ebf8283 2019-07-24 stsp free(*base_commit_id);
8020 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
8021 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
8022 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
8023 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
8025 3e3a69f1 2019-07-25 stsp if (*fileindex) {
8026 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
8027 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
8029 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
8031 0ebf8283 2019-07-24 stsp return err;
8034 0ebf8283 2019-07-24 stsp static const struct got_error *
8035 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8037 0ebf8283 2019-07-24 stsp const struct got_error *err;
8038 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8039 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
8041 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8043 0ebf8283 2019-07-24 stsp goto done;
8044 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
8046 0ebf8283 2019-07-24 stsp goto done;
8048 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8049 0ebf8283 2019-07-24 stsp worktree);
8051 0ebf8283 2019-07-24 stsp goto done;
8052 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
8054 0ebf8283 2019-07-24 stsp goto done;
8056 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8058 0ebf8283 2019-07-24 stsp goto done;
8059 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
8061 0ebf8283 2019-07-24 stsp goto done;
8063 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8065 0ebf8283 2019-07-24 stsp goto done;
8066 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8068 0ebf8283 2019-07-24 stsp goto done;
8070 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
8071 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
8072 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8073 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8074 0ebf8283 2019-07-24 stsp return err;
8077 0ebf8283 2019-07-24 stsp const struct got_error *
8078 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
8079 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8080 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
8081 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8083 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8084 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8085 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
8086 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
8087 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8088 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
8089 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
8090 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
8091 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
8092 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
8094 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
8096 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
8098 0ebf8283 2019-07-24 stsp return err;
8100 af179be7 2023-04-14 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8102 af179be7 2023-04-14 stsp goto done;
8104 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8105 af179be7 2023-04-14 stsp if (err) {
8106 af179be7 2023-04-14 stsp if (err->code != GOT_ERR_NOT_REF)
8107 af179be7 2023-04-14 stsp goto done;
8108 af179be7 2023-04-14 stsp /* Can happen on early abort due to invalid histedit script. */
8109 af179be7 2023-04-14 stsp commit_ref = NULL;
8112 af179be7 2023-04-14 stsp if (commit_ref) {
8113 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8115 af179be7 2023-04-14 stsp goto done;
8118 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
8119 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
8120 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files added by the
8121 af179be7 2023-04-14 stsp * user during conflict resolution or during histedit -e.
8123 af179be7 2023-04-14 stsp err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8124 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
8126 af179be7 2023-04-14 stsp goto done;
8129 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8130 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
8132 0ebf8283 2019-07-24 stsp goto done;
8134 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8136 0ebf8283 2019-07-24 stsp goto done;
8138 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8140 0ebf8283 2019-07-24 stsp goto done;
8142 1b093d84 2023-04-12 stsp err = got_object_open_as_commit(&commit, repo,
8143 1b093d84 2023-04-12 stsp worktree->base_commit_id);
8145 1b093d84 2023-04-12 stsp goto done;
8147 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8148 0ebf8283 2019-07-24 stsp worktree->path_prefix);
8150 0ebf8283 2019-07-24 stsp goto done;
8152 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8154 0ebf8283 2019-07-24 stsp goto done;
8156 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
8158 0ebf8283 2019-07-24 stsp goto done;
8160 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
8161 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
8162 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
8163 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
8164 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
8165 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
8166 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
8167 af179be7 2023-04-14 stsp rfa.unlink_added_files = 1;
8168 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
8169 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8170 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8172 1f1abb7e 2019-08-08 stsp goto sync;
8174 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8175 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8177 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8178 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
8179 0ebf8283 2019-07-24 stsp err = sync_err;
8181 af179be7 2023-04-14 stsp if (resolved)
8182 af179be7 2023-04-14 stsp got_ref_close(resolved);
8183 af179be7 2023-04-14 stsp if (commit_ref)
8184 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
8185 af179be7 2023-04-14 stsp free(merged_commit_id);
8186 0ebf8283 2019-07-24 stsp free(tree_id);
8187 818c7501 2019-07-11 stsp free(fileindex_path);
8188 af179be7 2023-04-14 stsp free(commit_ref_name);
8190 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8191 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
8192 818c7501 2019-07-11 stsp err = unlockerr;
8193 818c7501 2019-07-11 stsp return err;
8196 0ebf8283 2019-07-24 stsp const struct got_error *
8197 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8198 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8199 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8201 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8202 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8203 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8204 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8206 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8208 0ebf8283 2019-07-24 stsp return err;
8210 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8211 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8213 e600f124 2021-03-21 stsp goto done;
8215 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8216 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8218 0ebf8283 2019-07-24 stsp goto done;
8220 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8222 0ebf8283 2019-07-24 stsp goto done;
8224 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
8226 0ebf8283 2019-07-24 stsp goto done;
8228 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8230 0ebf8283 2019-07-24 stsp goto done;
8232 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8234 a615e0e7 2020-12-16 stsp goto done;
8236 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8238 a615e0e7 2020-12-16 stsp goto done;
8239 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8240 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8241 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8242 a615e0e7 2020-12-16 stsp err = sync_err;
8244 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8245 a615e0e7 2020-12-16 stsp free(fileindex_path);
8246 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8247 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8248 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8249 0ebf8283 2019-07-24 stsp err = unlockerr;
8250 0ebf8283 2019-07-24 stsp return err;
8253 0ebf8283 2019-07-24 stsp const struct got_error *
8254 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8255 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8257 0ebf8283 2019-07-24 stsp const struct got_error *err;
8258 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8260 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8262 0ebf8283 2019-07-24 stsp return err;
8264 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8266 0ebf8283 2019-07-24 stsp goto done;
8268 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8270 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8271 2822a352 2019-10-15 stsp return err;
8274 2822a352 2019-10-15 stsp const struct got_error *
8275 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8276 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8277 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8278 2822a352 2019-10-15 stsp struct got_repository *repo)
8280 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8281 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8282 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8284 2822a352 2019-10-15 stsp *fileindex = NULL;
8285 2822a352 2019-10-15 stsp *branch_ref = NULL;
8286 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8288 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8290 2822a352 2019-10-15 stsp return err;
8292 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8293 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8294 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8295 2822a352 2019-10-15 stsp "update -b or different branch name required");
8296 2822a352 2019-10-15 stsp goto done;
8299 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8301 2822a352 2019-10-15 stsp goto done;
8303 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8304 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8305 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8306 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8309 2822a352 2019-10-15 stsp goto done;
8311 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8313 2822a352 2019-10-15 stsp goto done;
8315 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8316 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8318 2822a352 2019-10-15 stsp if (err) {
8319 2822a352 2019-10-15 stsp if (*branch_ref) {
8320 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8321 2822a352 2019-10-15 stsp *branch_ref = NULL;
8323 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8324 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8325 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8327 2822a352 2019-10-15 stsp if (*fileindex) {
8328 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8329 2822a352 2019-10-15 stsp *fileindex = NULL;
8331 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8333 0cb83759 2019-08-03 stsp return err;
8336 2822a352 2019-10-15 stsp const struct got_error *
8337 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8338 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8339 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8340 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8341 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8343 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8344 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8345 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8346 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8348 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8350 2822a352 2019-10-15 stsp goto done;
8352 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8354 2822a352 2019-10-15 stsp goto done;
8356 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8358 a44927cc 2022-04-07 stsp goto done;
8360 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8361 2822a352 2019-10-15 stsp worktree->path_prefix);
8363 2822a352 2019-10-15 stsp goto done;
8365 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8367 2822a352 2019-10-15 stsp goto done;
8369 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8370 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8372 2822a352 2019-10-15 stsp goto sync;
8374 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8376 2822a352 2019-10-15 stsp goto sync;
8378 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8380 6e210706 2021-01-22 stsp goto sync;
8382 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8384 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8385 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8386 2822a352 2019-10-15 stsp err = sync_err;
8389 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8390 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8391 2822a352 2019-10-15 stsp err = unlockerr;
8392 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8394 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8395 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8396 2822a352 2019-10-15 stsp err = unlockerr;
8397 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8399 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8400 2822a352 2019-10-15 stsp free(fileindex_path);
8401 2822a352 2019-10-15 stsp free(tree_id);
8402 a44927cc 2022-04-07 stsp if (commit)
8403 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8405 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8406 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8407 2822a352 2019-10-15 stsp err = unlockerr;
8408 2822a352 2019-10-15 stsp return err;
8411 2822a352 2019-10-15 stsp const struct got_error *
8412 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8413 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8414 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8416 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8418 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8420 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8422 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8423 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8424 8b692cd0 2019-10-21 stsp err = unlockerr;
8425 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8427 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8428 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8429 8b692cd0 2019-10-21 stsp err = unlockerr;
8430 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8432 f259c4c1 2021-09-24 stsp return err;
8435 f259c4c1 2021-09-24 stsp const struct got_error *
8436 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
8437 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
8439 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
8440 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8442 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8444 f259c4c1 2021-09-24 stsp goto done;
8446 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8448 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
8449 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8450 f259c4c1 2021-09-24 stsp err = sync_err;
8452 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8453 f259c4c1 2021-09-24 stsp free(fileindex_path);
8454 8b692cd0 2019-10-21 stsp return err;
8457 f259c4c1 2021-09-24 stsp static const struct got_error *
8458 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8460 f259c4c1 2021-09-24 stsp const struct got_error *err;
8461 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
8463 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8465 f259c4c1 2021-09-24 stsp goto done;
8466 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
8468 f259c4c1 2021-09-24 stsp goto done;
8470 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8472 f259c4c1 2021-09-24 stsp goto done;
8473 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
8475 f259c4c1 2021-09-24 stsp goto done;
8478 f259c4c1 2021-09-24 stsp free(branch_refname);
8479 f259c4c1 2021-09-24 stsp free(commit_refname);
8480 f259c4c1 2021-09-24 stsp return err;
8483 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
8484 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
8485 f259c4c1 2021-09-24 stsp const char *branch_name;
8488 f259c4c1 2021-09-24 stsp static const struct got_error *
8489 2a47b1e5 2022-11-01 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8490 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
8492 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
8494 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8495 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
8496 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
8498 f259c4c1 2021-09-24 stsp return NULL;
8502 f259c4c1 2021-09-24 stsp const struct got_error *
8503 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
8504 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
8505 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
8506 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
8507 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8508 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8510 f259c4c1 2021-09-24 stsp const struct got_error *err;
8511 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8512 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args cma;
8514 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8516 f259c4c1 2021-09-24 stsp goto done;
8518 cdbfe5d2 2023-07-24 stsp cma.worktree = worktree;
8519 cdbfe5d2 2023-07-24 stsp cma.cancel_cb = cancel_cb;
8520 cdbfe5d2 2023-07-24 stsp cma.cancel_arg = cancel_arg;
8522 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8525 f259c4c1 2021-09-24 stsp goto done;
8527 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8528 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
8529 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
8531 f259c4c1 2021-09-24 stsp free(fileindex_path);
8532 f259c4c1 2021-09-24 stsp return err;
8535 f259c4c1 2021-09-24 stsp const struct got_error *
8536 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
8537 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8538 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
8539 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
8540 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo,
8541 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
8544 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
8545 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
8546 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
8547 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
8548 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
8549 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
8550 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8551 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
8552 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8554 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
8555 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
8557 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
8559 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8561 f259c4c1 2021-09-24 stsp goto done;
8563 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8565 f259c4c1 2021-09-24 stsp goto done;
8567 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8569 f259c4c1 2021-09-24 stsp goto done;
8571 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8572 f259c4c1 2021-09-24 stsp &have_staged_files);
8573 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8574 f259c4c1 2021-09-24 stsp goto done;
8575 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8576 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8577 f259c4c1 2021-09-24 stsp goto done;
8580 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
8581 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
8582 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
8583 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
8584 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
8585 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8586 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
8587 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8588 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8590 f259c4c1 2021-09-24 stsp goto done;
8592 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
8593 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
8594 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
8595 2a47b1e5 2022-11-01 stsp head_commit_id, branch_tip, worktree, author, committer, NULL,
8596 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8598 f259c4c1 2021-09-24 stsp goto done;
8600 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
8601 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
8602 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8603 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8604 f259c4c1 2021-09-24 stsp err = sync_err;
8606 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
8607 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
8609 f259c4c1 2021-09-24 stsp free_commitable(ct);
8611 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8612 f259c4c1 2021-09-24 stsp free(fileindex_path);
8613 f259c4c1 2021-09-24 stsp return err;
8616 f259c4c1 2021-09-24 stsp const struct got_error *
8617 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
8618 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
8620 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8621 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8623 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8625 f259c4c1 2021-09-24 stsp goto done;
8627 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8629 f259c4c1 2021-09-24 stsp goto done;
8630 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8631 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8632 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8633 f259c4c1 2021-09-24 stsp err = sync_err;
8635 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8636 f259c4c1 2021-09-24 stsp free(fileindex_path);
8637 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8638 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8639 f259c4c1 2021-09-24 stsp err = unlockerr;
8640 f259c4c1 2021-09-24 stsp return err;
8643 f259c4c1 2021-09-24 stsp const struct got_error *
8644 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8645 f259c4c1 2021-09-24 stsp struct got_repository *repo)
8647 f259c4c1 2021-09-24 stsp const struct got_error *err;
8648 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
8649 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
8651 f259c4c1 2021-09-24 stsp *in_progress = 0;
8653 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8655 f259c4c1 2021-09-24 stsp return err;
8656 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8657 793fcac3 2021-09-24 stsp free(branch_refname);
8658 f259c4c1 2021-09-24 stsp if (err) {
8659 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
8660 f259c4c1 2021-09-24 stsp return err;
8662 f259c4c1 2021-09-24 stsp *in_progress = 1;
8664 f259c4c1 2021-09-24 stsp return NULL;
8667 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
8668 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
8669 179f9db0 2023-06-20 falsifian struct got_repository *repo)
8671 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
8672 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8673 179f9db0 2023-06-20 falsifian struct got_reference *wt_branch = NULL;
8674 179f9db0 2023-06-20 falsifian struct got_object_id *wt_branch_tip = NULL;
8675 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
8677 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8679 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8681 f259c4c1 2021-09-24 stsp return err;
8683 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8685 f259c4c1 2021-09-24 stsp goto done;
8687 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
8688 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
8689 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
8690 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8693 f259c4c1 2021-09-24 stsp goto done;
8695 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8698 f259c4c1 2021-09-24 stsp goto done;
8700 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8702 f259c4c1 2021-09-24 stsp goto done;
8704 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8705 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8706 f259c4c1 2021-09-24 stsp goto done;
8709 179f9db0 2023-06-20 falsifian done:
8710 179f9db0 2023-06-20 falsifian free(fileindex_path);
8711 179f9db0 2023-06-20 falsifian if (wt_branch)
8712 179f9db0 2023-06-20 falsifian got_ref_close(wt_branch);
8713 179f9db0 2023-06-20 falsifian free(wt_branch_tip);
8714 179f9db0 2023-06-20 falsifian if (err) {
8715 179f9db0 2023-06-20 falsifian if (*fileindex) {
8716 179f9db0 2023-06-20 falsifian got_fileindex_free(*fileindex);
8717 179f9db0 2023-06-20 falsifian *fileindex = NULL;
8719 179f9db0 2023-06-20 falsifian lock_worktree(worktree, LOCK_SH);
8721 179f9db0 2023-06-20 falsifian return err;
8724 179f9db0 2023-06-20 falsifian const struct got_error *got_worktree_merge_write_refs(
8725 179f9db0 2023-06-20 falsifian struct got_worktree *worktree, struct got_reference *branch,
8726 179f9db0 2023-06-20 falsifian struct got_repository *repo)
8728 179f9db0 2023-06-20 falsifian const struct got_error *err = NULL;
8729 179f9db0 2023-06-20 falsifian char *branch_refname = NULL, *commit_refname = NULL;
8730 179f9db0 2023-06-20 falsifian struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8731 179f9db0 2023-06-20 falsifian struct got_object_id *branch_tip = NULL;
8733 179f9db0 2023-06-20 falsifian err = get_merge_branch_ref_name(&branch_refname, worktree);
8734 179f9db0 2023-06-20 falsifian if (err)
8735 179f9db0 2023-06-20 falsifian return err;
8737 179f9db0 2023-06-20 falsifian err = get_merge_commit_ref_name(&commit_refname, worktree);
8738 179f9db0 2023-06-20 falsifian if (err)
8739 179f9db0 2023-06-20 falsifian return err;
8741 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
8743 f259c4c1 2021-09-24 stsp goto done;
8745 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8747 f259c4c1 2021-09-24 stsp goto done;
8748 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
8750 f259c4c1 2021-09-24 stsp goto done;
8752 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8754 f259c4c1 2021-09-24 stsp goto done;
8755 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
8757 f259c4c1 2021-09-24 stsp goto done;
8760 f259c4c1 2021-09-24 stsp free(branch_refname);
8761 f259c4c1 2021-09-24 stsp free(commit_refname);
8762 f259c4c1 2021-09-24 stsp if (branch_ref)
8763 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8764 f259c4c1 2021-09-24 stsp if (commit_ref)
8765 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8766 179f9db0 2023-06-20 falsifian free(branch_tip);
8767 f259c4c1 2021-09-24 stsp return err;
8770 f259c4c1 2021-09-24 stsp const struct got_error *
8771 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
8772 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8773 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8775 f259c4c1 2021-09-24 stsp const struct got_error *err;
8776 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
8777 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8778 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8779 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8781 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8782 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8783 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8785 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8787 f259c4c1 2021-09-24 stsp return err;
8789 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8791 f259c4c1 2021-09-24 stsp goto done;
8793 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8794 f259c4c1 2021-09-24 stsp &have_staged_files);
8795 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8796 f259c4c1 2021-09-24 stsp goto done;
8797 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8798 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
8799 f259c4c1 2021-09-24 stsp goto done;
8802 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8804 f259c4c1 2021-09-24 stsp goto done;
8806 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8808 f259c4c1 2021-09-24 stsp goto done;
8810 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8812 f259c4c1 2021-09-24 stsp goto done;
8814 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
8815 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8816 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
8817 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
8818 f259c4c1 2021-09-24 stsp goto done;
8820 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8821 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
8822 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
8823 f259c4c1 2021-09-24 stsp goto done;
8826 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8828 f259c4c1 2021-09-24 stsp goto done;
8830 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
8832 f259c4c1 2021-09-24 stsp goto done;
8834 f259c4c1 2021-09-24 stsp free(commit_refname);
8835 f259c4c1 2021-09-24 stsp free(branch_refname);
8836 f259c4c1 2021-09-24 stsp free(fileindex_path);
8837 f259c4c1 2021-09-24 stsp if (commit_ref)
8838 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8839 f259c4c1 2021-09-24 stsp if (branch_ref)
8840 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8841 f259c4c1 2021-09-24 stsp if (err) {
8842 f259c4c1 2021-09-24 stsp if (*branch_name) {
8843 f259c4c1 2021-09-24 stsp free(*branch_name);
8844 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8846 f259c4c1 2021-09-24 stsp free(*branch_tip);
8847 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8848 f259c4c1 2021-09-24 stsp if (*fileindex) {
8849 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8850 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8852 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8854 f259c4c1 2021-09-24 stsp return err;
8857 f259c4c1 2021-09-24 stsp const struct got_error *
8858 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
8859 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8860 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8862 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8863 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8864 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8865 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
8866 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
8867 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
8868 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
8869 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
8870 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
8872 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
8874 af179be7 2023-04-14 stsp err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8876 af179be7 2023-04-14 stsp goto done;
8878 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8880 af179be7 2023-04-14 stsp goto done;
8882 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8884 af179be7 2023-04-14 stsp goto done;
8887 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
8888 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
8889 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files which were added by
8890 af179be7 2023-04-14 stsp * the user for conflict resolution purposes.
8892 af179be7 2023-04-14 stsp err = get_paths_added_between_commits(&added_paths,
8893 af179be7 2023-04-14 stsp got_worktree_get_base_commit_id(worktree), merged_commit_id,
8894 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
8896 af179be7 2023-04-14 stsp goto done;
8899 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
8900 a44927cc 2022-04-07 stsp worktree->base_commit_id);
8902 a44927cc 2022-04-07 stsp goto done;
8904 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8905 a44927cc 2022-04-07 stsp worktree->path_prefix);
8907 f259c4c1 2021-09-24 stsp goto done;
8909 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8911 f259c4c1 2021-09-24 stsp goto done;
8913 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8915 f259c4c1 2021-09-24 stsp goto done;
8917 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
8918 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
8919 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
8920 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
8921 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
8922 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
8923 f259c4c1 2021-09-24 stsp rfa.repo = repo;
8924 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
8925 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
8926 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8927 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8929 f259c4c1 2021-09-24 stsp goto sync;
8931 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8932 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8934 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8935 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8936 f259c4c1 2021-09-24 stsp err = sync_err;
8938 f259c4c1 2021-09-24 stsp free(tree_id);
8939 af179be7 2023-04-14 stsp free(merged_commit_id);
8940 a44927cc 2022-04-07 stsp if (commit)
8941 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8942 f259c4c1 2021-09-24 stsp if (fileindex)
8943 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8944 f259c4c1 2021-09-24 stsp free(fileindex_path);
8945 af179be7 2023-04-14 stsp if (commit_ref)
8946 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
8947 af179be7 2023-04-14 stsp free(commit_ref_name);
8949 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8950 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8951 f259c4c1 2021-09-24 stsp err = unlockerr;
8952 f259c4c1 2021-09-24 stsp return err;
8955 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8956 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8957 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8958 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8959 2db2652d 2019-08-07 stsp struct got_repository *repo;
8960 2db2652d 2019-08-07 stsp int have_changes;
8963 336075a4 2022-06-25 op static const struct got_error *
8964 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8965 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8966 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8967 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8969 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8970 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8971 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8972 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8973 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8974 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8976 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8977 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8978 8b13ce36 2019-08-08 stsp return NULL;
8979 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8980 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8982 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8983 735ef5ac 2019-08-03 stsp if (ie == NULL)
8984 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8986 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8987 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8988 735ef5ac 2019-08-03 stsp relpath) == -1)
8989 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8991 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8992 b4b2adf5 2023-02-09 op base_commit_idp = got_fileindex_entry_get_commit_id(
8993 b4b2adf5 2023-02-09 op &base_commit_id, ie);
8996 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8997 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8998 3aa5969e 2019-08-06 stsp goto done;
8999 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
9000 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
9001 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
9002 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9003 735ef5ac 2019-08-03 stsp goto done;
9006 2db2652d 2019-08-07 stsp a->have_changes = 1;
9008 735ef5ac 2019-08-03 stsp p = in_repo_path;
9009 735ef5ac 2019-08-03 stsp while (p[0] == '/')
9011 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
9012 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
9013 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
9015 735ef5ac 2019-08-03 stsp free(in_repo_path);
9016 dc424a06 2019-08-07 stsp return err;
9019 2db2652d 2019-08-07 stsp struct stage_path_arg {
9020 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
9021 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
9022 2db2652d 2019-08-07 stsp struct got_repository *repo;
9023 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
9024 2db2652d 2019-08-07 stsp void *status_arg;
9025 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
9026 2db2652d 2019-08-07 stsp void *patch_arg;
9027 7b5dc508 2019-10-28 stsp int staged_something;
9028 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
9031 2db2652d 2019-08-07 stsp static const struct got_error *
9032 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
9033 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
9034 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9035 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9037 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
9038 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
9039 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
9040 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
9041 0cb83759 2019-08-03 stsp uint32_t stage;
9042 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
9043 0aeb8099 2020-07-23 stsp struct stat sb;
9045 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
9046 8b13ce36 2019-08-08 stsp return NULL;
9048 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9049 d3e7c587 2019-08-03 stsp if (ie == NULL)
9050 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9052 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9053 2db2652d 2019-08-07 stsp relpath)== -1)
9054 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
9056 0cb83759 2019-08-03 stsp switch (status) {
9057 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
9058 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
9059 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
9060 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
9061 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
9064 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9065 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
9066 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9067 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9068 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
9071 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
9074 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
9075 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
9076 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
9077 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
9078 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
9082 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
9083 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
9086 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9087 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
9088 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9089 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
9091 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
9092 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9093 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
9094 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
9095 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
9096 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
9097 35213c7c 2020-07-23 stsp ssize_t target_len;
9098 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
9099 35213c7c 2020-07-23 stsp sizeof(target_path));
9100 35213c7c 2020-07-23 stsp if (target_len == -1) {
9101 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
9102 35213c7c 2020-07-23 stsp ondisk_path);
9105 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
9106 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
9107 df6221c7 2023-07-19 stsp a->worktree->root_path,
9108 df6221c7 2023-07-19 stsp a->worktree->meta_dir);
9111 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
9112 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
9113 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
9117 35213c7c 2020-07-23 stsp if (is_bad_symlink)
9118 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9119 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
9121 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9122 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
9124 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9125 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
9127 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9128 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9130 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9131 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
9132 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
9136 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
9137 9fdde394 2022-06-04 op * implicitly unstage the file.
9139 9fdde394 2022-06-04 op if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9140 9fdde394 2022-06-04 op sizeof(ie->blob_sha1)) == 0) {
9141 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
9142 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
9145 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
9146 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
9148 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9149 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9150 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
9151 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
9154 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9156 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9157 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9161 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
9162 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9163 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9164 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9166 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9167 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9170 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
9172 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
9173 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9175 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
9176 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
9179 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9183 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
9184 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
9185 dc424a06 2019-08-07 stsp free(path_content);
9186 2db2652d 2019-08-07 stsp free(ondisk_path);
9187 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
9188 0ebf8283 2019-07-24 stsp return err;
9191 0cb83759 2019-08-03 stsp const struct got_error *
9192 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
9193 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
9194 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
9195 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9196 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
9198 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9199 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9200 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9201 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9202 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9203 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9204 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9205 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9207 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9209 0cb83759 2019-08-03 stsp return err;
9211 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9212 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9214 735ef5ac 2019-08-03 stsp goto done;
9215 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9217 735ef5ac 2019-08-03 stsp goto done;
9218 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9220 0cb83759 2019-08-03 stsp goto done;
9222 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9223 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9224 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9225 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9226 2db2652d 2019-08-07 stsp oka.repo = repo;
9227 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9228 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9229 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9230 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
9232 735ef5ac 2019-08-03 stsp goto done;
9234 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9235 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9236 2db2652d 2019-08-07 stsp goto done;
9239 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9240 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9241 2db2652d 2019-08-07 stsp spa.repo = repo;
9242 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9243 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9244 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9245 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9246 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9247 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9248 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9249 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9250 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
9252 2db2652d 2019-08-07 stsp goto done;
9254 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9255 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9256 7b5dc508 2019-10-28 stsp goto done;
9259 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9260 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9261 0cb83759 2019-08-03 stsp err = sync_err;
9263 735ef5ac 2019-08-03 stsp if (head_ref)
9264 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9265 735ef5ac 2019-08-03 stsp free(head_commit_id);
9266 0cb83759 2019-08-03 stsp free(fileindex_path);
9267 0cb83759 2019-08-03 stsp if (fileindex)
9268 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9269 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9270 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9271 0cb83759 2019-08-03 stsp err = unlockerr;
9272 0cb83759 2019-08-03 stsp return err;
9275 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9276 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9277 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9278 ad493afc 2019-08-03 stsp struct got_repository *repo;
9279 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9280 ad493afc 2019-08-03 stsp void *progress_arg;
9281 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9282 2e1f37b0 2019-08-08 stsp void *patch_arg;
9285 2e1f37b0 2019-08-08 stsp static const struct got_error *
9286 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9287 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9288 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9289 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9290 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9292 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9293 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9294 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9295 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9296 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9297 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9298 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9299 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
9301 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9302 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9304 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9306 2e1f37b0 2019-08-08 stsp return err;
9308 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9309 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9310 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9311 eb81bc23 2022-06-28 tracey goto done;
9313 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9314 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9315 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9316 eb81bc23 2022-06-28 tracey goto done;
9319 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9321 2e1f37b0 2019-08-08 stsp goto done;
9323 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9325 2e1f37b0 2019-08-08 stsp goto done;
9327 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9329 2e1f37b0 2019-08-08 stsp goto done;
9331 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9334 2e1f37b0 2019-08-08 stsp goto done;
9336 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9338 2e1f37b0 2019-08-08 stsp goto done;
9340 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9342 2e1f37b0 2019-08-08 stsp goto done;
9344 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9345 4b752015 2022-06-30 stsp path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9347 2e1f37b0 2019-08-08 stsp goto done;
9349 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9350 b90054ed 2022-11-01 stsp "got-unstaged-content", "");
9352 2e1f37b0 2019-08-08 stsp goto done;
9353 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9354 b90054ed 2022-11-01 stsp "got-new-staged-content", "");
9356 2e1f37b0 2019-08-08 stsp goto done;
9358 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9359 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9360 2e1f37b0 2019-08-08 stsp goto done;
9362 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9363 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9364 2e1f37b0 2019-08-08 stsp goto done;
9366 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9367 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9368 eb81bc23 2022-06-28 tracey struct diff_chunk_context cc = {};
9369 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9370 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9371 fe621944 2020-11-10 stsp nchanges++;
9373 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9374 2e1f37b0 2019-08-08 stsp int choice;
9375 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9376 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9377 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9378 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9380 2e1f37b0 2019-08-08 stsp goto done;
9381 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9382 2e1f37b0 2019-08-08 stsp have_content = 1;
9384 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9385 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9388 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9389 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9390 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9392 2e1f37b0 2019-08-08 stsp free(label1);
9393 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9394 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9396 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9397 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9398 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9399 2e1f37b0 2019-08-08 stsp if (staged_blob)
9400 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9401 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9402 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9403 fe621944 2020-11-10 stsp err = free_err;
9404 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9405 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9406 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9407 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9408 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9409 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9410 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9411 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9412 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9413 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9414 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9415 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9416 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9417 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9418 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9419 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9420 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9421 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9422 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9424 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9425 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9426 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9427 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9428 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9429 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9430 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9432 2e1f37b0 2019-08-08 stsp free(path1);
9433 2e1f37b0 2019-08-08 stsp free(path2);
9434 fda8017d 2020-07-23 stsp return err;
9437 fda8017d 2020-07-23 stsp static const struct got_error *
9438 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9439 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9440 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9441 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9442 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9443 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9444 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9446 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9447 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9448 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9449 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9450 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9451 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9452 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9453 fda8017d 2020-07-23 stsp struct stat sb;
9455 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9456 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9457 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9459 fda8017d 2020-07-23 stsp return err;
9461 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9462 fda8017d 2020-07-23 stsp return NULL;
9464 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9465 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9466 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9468 fda8017d 2020-07-23 stsp goto done;
9471 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
9472 fda8017d 2020-07-23 stsp if (f == NULL) {
9473 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9474 fda8017d 2020-07-23 stsp path_unstaged_content);
9475 fda8017d 2020-07-23 stsp goto done;
9477 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9478 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9479 fda8017d 2020-07-23 stsp goto done;
9481 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9482 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9483 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9485 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9486 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9487 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9488 fda8017d 2020-07-23 stsp goto done;
9490 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9491 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9492 fda8017d 2020-07-23 stsp goto done;
9494 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9495 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9496 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9497 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9498 fda8017d 2020-07-23 stsp progress_arg);
9500 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9502 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9504 67a66647 2021-05-31 stsp return err;
9506 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9507 67a66647 2021-05-31 stsp parent) == -1) {
9508 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9509 67a66647 2021-05-31 stsp base_path = NULL;
9510 67a66647 2021-05-31 stsp goto done;
9513 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_base_path, &f_base,
9514 b90054ed 2022-11-01 stsp base_path, "");
9516 67a66647 2021-05-31 stsp goto done;
9517 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9518 67a66647 2021-05-31 stsp blob_base);
9520 67a66647 2021-05-31 stsp goto done;
9523 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9524 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9526 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9527 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9528 eec2f5a9 2021-06-03 stsp ondisk_path);
9530 eec2f5a9 2021-06-03 stsp goto done;
9533 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
9534 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9535 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9536 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9537 eec2f5a9 2021-06-03 stsp goto done;
9539 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9540 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9541 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9542 eec2f5a9 2021-06-03 stsp close(fd);
9543 eec2f5a9 2021-06-03 stsp goto done;
9547 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9548 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9549 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9550 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9551 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9554 fda8017d 2020-07-23 stsp goto done;
9556 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9557 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9558 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9560 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9561 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9564 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9565 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9566 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9567 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9568 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9569 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9570 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9571 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9572 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9573 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9574 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9575 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9576 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9577 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9578 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9579 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9580 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9581 67a66647 2021-05-31 stsp free(blob_base_path);
9582 67a66647 2021-05-31 stsp free(parent);
9583 67a66647 2021-05-31 stsp free(base_path);
9584 2e1f37b0 2019-08-08 stsp return err;
9587 ad493afc 2019-08-03 stsp static const struct got_error *
9588 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9589 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9590 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9591 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9593 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9594 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9595 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9596 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9597 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9598 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9599 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9600 9bc94a15 2019-08-03 stsp struct stat sb;
9601 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
9603 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9604 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9605 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9606 2e1f37b0 2019-08-08 stsp return NULL;
9608 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9609 ad493afc 2019-08-03 stsp if (ie == NULL)
9610 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9612 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9614 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9616 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9617 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9619 f69721c3 2019-10-21 stsp goto done;
9620 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9621 f69721c3 2019-10-21 stsp id_str) == -1) {
9622 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9623 eb81bc23 2022-06-28 tracey goto done;
9626 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9627 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9628 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9629 eb81bc23 2022-06-28 tracey goto done;
9631 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9632 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9633 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9634 f69721c3 2019-10-21 stsp goto done;
9637 ad493afc 2019-08-03 stsp switch (staged_status) {
9638 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9639 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9640 eb81bc23 2022-06-28 tracey blob_id, 8192, fd1);
9643 ad493afc 2019-08-03 stsp /* fall through */
9644 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9645 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9646 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9647 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9648 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9649 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9652 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9655 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9656 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9657 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9658 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9659 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9660 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9663 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9664 eb81bc23 2022-06-28 tracey staged_blob_id, 8192, fd2);
9667 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9668 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9669 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9670 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9671 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9672 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9673 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9674 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9675 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9677 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9678 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9679 36bf999c 2020-07-23 stsp char *staged_target;
9680 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9681 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9683 36bf999c 2020-07-23 stsp goto done;
9684 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9685 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9686 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9687 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9688 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9689 36bf999c 2020-07-23 stsp free(staged_target);
9691 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9692 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9693 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9694 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9695 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9696 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9697 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9701 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9704 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9705 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9706 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9707 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9710 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9711 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9712 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9713 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9714 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9717 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9719 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9720 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9724 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9725 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9726 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9727 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9730 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9734 ad493afc 2019-08-03 stsp free(ondisk_path);
9735 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9736 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9737 ad493afc 2019-08-03 stsp if (blob_base)
9738 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9739 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9740 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9741 ad493afc 2019-08-03 stsp if (blob_staged)
9742 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9743 f69721c3 2019-10-21 stsp free(id_str);
9744 f69721c3 2019-10-21 stsp free(label_orig);
9745 ad493afc 2019-08-03 stsp return err;
9748 ad493afc 2019-08-03 stsp const struct got_error *
9749 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9750 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9751 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9752 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9753 ad493afc 2019-08-03 stsp struct got_repository *repo)
9755 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9756 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9757 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9758 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9759 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9761 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9763 ad493afc 2019-08-03 stsp return err;
9765 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9767 ad493afc 2019-08-03 stsp goto done;
9769 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9770 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9771 ad493afc 2019-08-03 stsp upa.repo = repo;
9772 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9773 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9774 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9775 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9776 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9777 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9778 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
9780 ad493afc 2019-08-03 stsp goto done;
9783 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9784 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9785 ad493afc 2019-08-03 stsp err = sync_err;
9787 ad493afc 2019-08-03 stsp free(fileindex_path);
9788 ad493afc 2019-08-03 stsp if (fileindex)
9789 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9790 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9791 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9792 ad493afc 2019-08-03 stsp err = unlockerr;
9793 ad493afc 2019-08-03 stsp return err;
9796 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9797 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9798 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9799 b2118c49 2020-07-28 stsp void *info_arg;
9800 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9801 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9802 b2118c49 2020-07-28 stsp void *cancel_arg;
9805 b2118c49 2020-07-28 stsp static const struct got_error *
9806 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9808 f6b8c3c2 2023-07-24 stsp const struct got_error *err;
9809 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9810 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9811 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9812 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9813 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9814 b2118c49 2020-07-28 stsp int stage;
9816 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
9817 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
9819 f6b8c3c2 2023-07-24 stsp return err;
9822 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9823 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9824 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9827 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9828 b2118c49 2020-07-28 stsp return NULL;
9830 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
9831 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9832 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9833 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9834 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9835 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9836 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
9839 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
9840 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9842 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9843 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9846 b2118c49 2020-07-28 stsp const struct got_error *
9847 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9848 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9849 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9850 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9853 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9854 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9855 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9856 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9858 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9860 b2118c49 2020-07-28 stsp return err;
9862 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9864 b2118c49 2020-07-28 stsp goto done;
9866 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9867 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9868 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9869 b2118c49 2020-07-28 stsp arg.paths = paths;
9870 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9871 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9872 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9875 b2118c49 2020-07-28 stsp free(fileindex_path);
9876 b2118c49 2020-07-28 stsp if (fileindex)
9877 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9878 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9879 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9880 b2118c49 2020-07-28 stsp err = unlockerr;
9881 b2118c49 2020-07-28 stsp return err;
9884 78f5ac24 2022-03-19 op static const struct got_error *
9885 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
9886 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
9887 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
9889 78f5ac24 2022-03-19 op const struct got_error *err;
9890 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
9891 78f5ac24 2022-03-19 op struct stat sb;
9892 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
9894 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
9898 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9899 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
9900 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
9902 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9904 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
9905 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9910 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
9911 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
9912 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
9913 78f5ac24 2022-03-19 op if (errno != ENOENT) {
9914 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
9915 78f5ac24 2022-03-19 op ondisk_path);
9918 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
9923 78f5ac24 2022-03-19 op free(ondisk_path);
9927 78f5ac24 2022-03-19 op static const struct got_error *
9928 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
9929 78f5ac24 2022-03-19 op unsigned char staged_status)
9931 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9932 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9933 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9934 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9935 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
9936 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
9937 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9938 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9939 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9940 78f5ac24 2022-03-19 op return NULL;
9943 78f5ac24 2022-03-19 op static const struct got_error *
9944 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
9946 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
9947 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9948 78f5ac24 2022-03-19 op return NULL;
9951 78f5ac24 2022-03-19 op static const struct got_error *
9952 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
9953 78f5ac24 2022-03-19 op unsigned char staged_status)
9955 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9956 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9957 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9958 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9959 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
9960 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9961 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9962 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9963 78f5ac24 2022-03-19 op return NULL;
9966 78f5ac24 2022-03-19 op const struct got_error *
9967 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
9968 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
9970 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
9973 78f5ac24 2022-03-19 op const struct got_error *
9974 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
9975 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
9976 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
9978 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
9979 78f5ac24 2022-03-19 op int file_renamed = 0;
9980 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
9981 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
9983 78f5ac24 2022-03-19 op *oldpath = NULL;
9984 78f5ac24 2022-03-19 op *newpath = NULL;
9986 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
9987 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
9991 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
9992 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
9996 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
9997 78f5ac24 2022-03-19 op file_renamed = 1;
9999 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
10000 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
10001 78f5ac24 2022-03-19 op else if (file_renamed) {
10002 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
10003 78f5ac24 2022-03-19 op if (err == NULL)
10004 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
10005 78f5ac24 2022-03-19 op } else if (old == NULL)
10006 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
10008 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
10012 78f5ac24 2022-03-19 op free(*oldpath);
10013 78f5ac24 2022-03-19 op *oldpath = NULL;
10014 78f5ac24 2022-03-19 op free(*newpath);
10015 78f5ac24 2022-03-19 op *newpath = NULL;
10017 78f5ac24 2022-03-19 op return err;
10020 78f5ac24 2022-03-19 op const struct got_error *
10021 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10022 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
10023 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
10025 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
10027 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
10028 f2dd7807 2022-05-17 op saa.worktree = worktree;
10029 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
10030 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
10031 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
10032 f2dd7807 2022-05-17 op saa.repo = repo;
10034 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
10035 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
10038 f2dd7807 2022-05-17 op const struct got_error *
10039 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10040 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
10041 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
10043 7f4e5320 2023-05-29 stsp const struct got_error *err;
10044 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
10045 7f4e5320 2023-05-29 stsp char *ondisk_status_path;
10047 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
10048 f2dd7807 2022-05-17 op sda.worktree = worktree;
10049 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
10050 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
10051 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
10052 f2dd7807 2022-05-17 op sda.repo = repo;
10053 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
10054 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
10055 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
10056 f2dd7807 2022-05-17 op sda.status_codes = NULL;
10057 7f4e5320 2023-05-29 stsp if (asprintf(&ondisk_status_path, "%s/%s",
10058 7f4e5320 2023-05-29 stsp got_worktree_get_root_path(worktree), path) == -1)
10059 7f4e5320 2023-05-29 stsp return got_error_from_errno("asprintf");
10060 7f4e5320 2023-05-29 stsp sda.status_path = ondisk_status_path;
10061 7f4e5320 2023-05-29 stsp sda.status_path_len = strlen(ondisk_status_path);
10063 7f4e5320 2023-05-29 stsp err = worktree_status(worktree, path, fileindex, repo,
10064 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10065 7f4e5320 2023-05-29 stsp free(ondisk_status_path);
10066 7f4e5320 2023-05-29 stsp return err;
10069 f2dd7807 2022-05-17 op const struct got_error *
10070 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
10071 4bcdc895 2022-05-17 op const char *fileindex_path)
10073 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
10075 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
10076 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);
10078 f2dd7807 2022-05-17 op return err;