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 9e83097f 2023-12-05 mark got_pathlist_free(&missing_children, GOT_PATHLIST_FREE_NONE);
4215 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4216 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4217 927df6b7 2019-02-10 stsp free(ondisk_path);
4218 347d1d3e 2019-07-12 stsp return err;
4221 347d1d3e 2019-07-12 stsp const struct got_error *
4222 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4223 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4224 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4225 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4227 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4228 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4229 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4230 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4232 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4234 347d1d3e 2019-07-12 stsp return err;
4236 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4237 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4238 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4239 f6343036 2021-06-22 stsp no_ignores, 0);
4243 f8d1f275 2019-02-04 stsp free(fileindex_path);
4244 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4245 f8d1f275 2019-02-04 stsp return err;
4248 6c7ab921 2019-03-18 stsp const struct got_error *
4249 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4250 6c7ab921 2019-03-18 stsp const char *arg)
4252 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4253 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4254 6c7ab921 2019-03-18 stsp size_t len;
4255 00bb5ea0 2020-07-23 stsp struct stat sb;
4256 01740607 2020-11-04 stsp char *abspath = NULL;
4257 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4259 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4261 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4262 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4263 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4265 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4266 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4267 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4268 00bb5ea0 2020-07-23 stsp goto done;
4270 727173c3 2020-11-06 stsp sb.st_mode = 0;
4272 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4274 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4275 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4276 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4277 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4279 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4280 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4281 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4282 00bb5ea0 2020-07-23 stsp goto done;
4286 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4287 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4289 d0710d08 2019-07-22 stsp goto done;
4290 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4291 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4292 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4293 00bb5ea0 2020-07-23 stsp goto done;
4296 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4297 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4298 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4299 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4300 00bb5ea0 2020-07-23 stsp goto done;
4302 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4303 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4304 01740607 2020-11-04 stsp goto done;
4306 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4307 01740607 2020-11-04 stsp sizeof(canonpath));
4309 00bb5ea0 2020-07-23 stsp goto done;
4310 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4311 01740607 2020-11-04 stsp if (resolved == NULL) {
4312 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4313 01740607 2020-11-04 stsp goto done;
4318 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4319 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4320 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4321 6c7ab921 2019-03-18 stsp goto done;
4324 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4325 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4326 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4328 f6d88e1a 2019-05-29 stsp goto done;
4330 f6d88e1a 2019-05-29 stsp path = strdup("");
4331 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4332 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4333 f6d88e1a 2019-05-29 stsp goto done;
4337 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4338 6c7ab921 2019-03-18 stsp len = strlen(path);
4339 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4340 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4344 01740607 2020-11-04 stsp free(abspath);
4345 6c7ab921 2019-03-18 stsp free(resolved);
4346 d0710d08 2019-07-22 stsp free(cwd);
4347 6c7ab921 2019-03-18 stsp if (err == NULL)
4348 6c7ab921 2019-03-18 stsp *wt_path = path;
4350 6c7ab921 2019-03-18 stsp free(path);
4351 d00136be 2019-03-26 stsp return err;
4354 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4355 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4356 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4357 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4358 4e68cba3 2019-11-23 stsp void *progress_arg;
4359 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4362 b88936d3 2023-06-21 stsp static int
4363 b88936d3 2023-06-21 stsp add_noop_status(unsigned char status)
4365 b88936d3 2023-06-21 stsp return (status == GOT_STATUS_ADD ||
4366 b88936d3 2023-06-21 stsp status == GOT_STATUS_MODIFY ||
4367 b88936d3 2023-06-21 stsp status == GOT_STATUS_CONFLICT ||
4368 b88936d3 2023-06-21 stsp status == GOT_STATUS_MODE_CHANGE ||
4369 b88936d3 2023-06-21 stsp status == GOT_STATUS_NO_CHANGE);
4372 4e68cba3 2019-11-23 stsp static const struct got_error *
4373 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4374 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4375 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4376 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4378 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4379 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4380 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4381 6d022e97 2019-08-04 stsp struct stat sb;
4382 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4384 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4385 dbb83fbd 2019-12-12 stsp relpath) == -1)
4386 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4388 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4390 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4391 12463d8b 2019-12-13 stsp de_name, a->repo);
4393 dbb83fbd 2019-12-12 stsp goto done;
4394 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4395 b88936d3 2023-06-21 stsp if (staged_status == GOT_STATUS_NO_CHANGE &&
4396 b88936d3 2023-06-21 stsp add_noop_status(status))
4397 dbb83fbd 2019-12-12 stsp goto done;
4398 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4400 dbb83fbd 2019-12-12 stsp goto done;
4403 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4404 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
4405 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
4407 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4408 dbb83fbd 2019-12-12 stsp goto done;
4411 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4413 4e68cba3 2019-11-23 stsp goto done;
4414 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4415 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4416 3969253a 2020-03-07 stsp if (err) {
4417 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4418 3969253a 2020-03-07 stsp goto done;
4420 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4421 07f5b47a 2019-06-02 stsp if (err) {
4422 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4423 4e68cba3 2019-11-23 stsp goto done;
4426 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4428 dbb83fbd 2019-12-12 stsp return err;
4429 b88936d3 2023-06-21 stsp if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4430 dbb83fbd 2019-12-12 stsp return NULL;
4431 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4434 d00136be 2019-03-26 stsp const struct got_error *
4435 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4436 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4437 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4438 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4440 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4441 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4442 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4443 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4444 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4446 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4448 d00136be 2019-03-26 stsp return err;
4450 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4452 d00136be 2019-03-26 stsp goto done;
4454 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4455 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4456 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4457 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4458 4e68cba3 2019-11-23 stsp saa.repo = repo;
4460 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4461 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4462 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4466 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4467 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4468 af12c6b9 2019-06-04 stsp err = sync_err;
4470 fb399478 2019-07-12 stsp free(fileindex_path);
4471 d00136be 2019-03-26 stsp if (fileindex)
4472 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4473 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4474 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4475 d00136be 2019-03-26 stsp err = unlockerr;
4476 6c7ab921 2019-03-18 stsp return err;
4479 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4480 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4481 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4482 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4483 f2a9dc41 2019-12-13 tracey void *progress_arg;
4484 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4485 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4486 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4487 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4488 7f4e5320 2023-05-29 stsp const char *status_path;
4489 7f4e5320 2023-05-29 stsp size_t status_path_len;
4490 766841c2 2020-08-13 stsp const char *status_codes;
4493 f2a9dc41 2019-12-13 tracey static const struct got_error *
4494 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4495 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4496 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4497 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4499 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4500 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4501 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4502 17ed4618 2019-06-02 stsp struct stat sb;
4503 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4505 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4506 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4507 4e12cd97 2022-01-25 stsp return NULL;
4508 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4511 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4512 17ed4618 2019-06-02 stsp if (ie == NULL)
4513 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4515 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4516 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4517 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4518 9acbc4fa 2019-08-03 stsp return NULL;
4519 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4522 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4523 f2a9dc41 2019-12-13 tracey relpath) == -1)
4524 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4526 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4529 f2a9dc41 2019-12-13 tracey goto done;
4531 766841c2 2020-08-13 stsp if (a->status_codes) {
4532 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4534 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4535 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4538 5e91dae4 2022-08-30 stsp if (i == ncodes) {
4539 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4540 766841c2 2020-08-13 stsp free(ondisk_path);
4541 766841c2 2020-08-13 stsp return NULL;
4543 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4544 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4545 766841c2 2020-08-13 stsp static char msg[64];
4546 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4547 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4548 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4549 766841c2 2020-08-13 stsp goto done;
4553 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4554 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4555 f2a9dc41 2019-12-13 tracey goto done;
4556 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4557 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4558 f2a9dc41 2019-12-13 tracey goto done;
4560 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4561 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4562 4e12cd97 2022-01-25 stsp goto done;
4564 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4565 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4566 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4567 f2a9dc41 2019-12-13 tracey goto done;
4571 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4572 20a2ad1c 2020-10-20 stsp size_t root_len;
4574 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4575 a06ca3f7 2022-10-15 stsp if (unlinkat(dirfd, de_name, 0) == -1) {
4576 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4577 12463d8b 2019-12-13 stsp ondisk_path);
4578 12463d8b 2019-12-13 stsp goto done;
4580 a06ca3f7 2022-10-15 stsp } else if (unlink(ondisk_path) == -1) {
4581 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4582 12463d8b 2019-12-13 stsp goto done;
4585 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4587 20a2ad1c 2020-10-20 stsp char *parent;
4589 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4591 2513f20a 2020-10-20 stsp goto done;
4592 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4593 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4594 7f4e5320 2023-05-29 stsp if (got_path_cmp(ondisk_path, a->status_path,
4595 7f4e5320 2023-05-29 stsp strlen(ondisk_path), a->status_path_len) != 0 &&
4596 7f4e5320 2023-05-29 stsp !got_path_is_child(ondisk_path, a->status_path,
4597 7f4e5320 2023-05-29 stsp a->status_path_len))
4599 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4600 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4601 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4602 20a2ad1c 2020-10-20 stsp ondisk_path);
4605 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4606 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4609 f6635657 2023-08-25 stsp if (got_fileindex_entry_has_blob(ie))
4610 f6635657 2023-08-25 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4612 f6635657 2023-08-25 stsp got_fileindex_entry_remove(a->fileindex, ie);
4614 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4615 f2a9dc41 2019-12-13 tracey if (err)
4616 f2a9dc41 2019-12-13 tracey return err;
4617 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4618 f2a9dc41 2019-12-13 tracey return NULL;
4619 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4620 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4623 2ec1f75b 2019-03-26 stsp const struct got_error *
4624 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4625 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4626 766841c2 2020-08-13 stsp const char *status_codes,
4627 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4628 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4630 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4631 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4632 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4633 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4634 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4636 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4638 2ec1f75b 2019-03-26 stsp return err;
4640 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4642 2ec1f75b 2019-03-26 stsp goto done;
4644 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4645 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4646 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4647 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4648 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4649 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4650 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4651 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4652 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4654 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4655 7f4e5320 2023-05-29 stsp char *ondisk_status_path;
4657 7f4e5320 2023-05-29 stsp if (asprintf(&ondisk_status_path, "%s%s%s",
4658 7f4e5320 2023-05-29 stsp got_worktree_get_root_path(worktree),
4659 7f4e5320 2023-05-29 stsp pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4660 7f4e5320 2023-05-29 stsp err = got_error_from_errno("asprintf");
4661 7f4e5320 2023-05-29 stsp goto done;
4663 7f4e5320 2023-05-29 stsp sda.status_path = ondisk_status_path;
4664 7f4e5320 2023-05-29 stsp sda.status_path_len = strlen(ondisk_status_path);
4665 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4666 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4667 7f4e5320 2023-05-29 stsp free(ondisk_status_path);
4671 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4672 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4673 af12c6b9 2019-06-04 stsp err = sync_err;
4675 fb399478 2019-07-12 stsp free(fileindex_path);
4676 2ec1f75b 2019-03-26 stsp if (fileindex)
4677 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4678 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4679 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4680 2ec1f75b 2019-03-26 stsp err = unlockerr;
4681 33aa809d 2019-08-08 stsp return err;
4684 33aa809d 2019-08-08 stsp static const struct got_error *
4685 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4687 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4688 33aa809d 2019-08-08 stsp char *line = NULL;
4689 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4690 33aa809d 2019-08-08 stsp ssize_t linelen;
4692 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4693 33aa809d 2019-08-08 stsp if (linelen == -1) {
4694 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4695 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4696 33aa809d 2019-08-08 stsp goto done;
4698 33aa809d 2019-08-08 stsp return NULL;
4700 33aa809d 2019-08-08 stsp if (outfile) {
4701 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4702 33aa809d 2019-08-08 stsp if (n != linelen) {
4703 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4704 33aa809d 2019-08-08 stsp goto done;
4707 33aa809d 2019-08-08 stsp if (rejectfile) {
4708 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4709 33aa809d 2019-08-08 stsp if (n != linelen)
4710 910d235d 2023-01-10 mark err = got_ferror(rejectfile, GOT_ERR_IO);
4713 33aa809d 2019-08-08 stsp free(line);
4714 2ec1f75b 2019-03-26 stsp return err;
4717 33aa809d 2019-08-08 stsp static const struct got_error *
4718 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4720 33aa809d 2019-08-08 stsp char *line = NULL;
4721 33aa809d 2019-08-08 stsp size_t linesize = 0;
4722 33aa809d 2019-08-08 stsp ssize_t linelen;
4724 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4725 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4726 500467ff 2019-09-25 hiltjo if (ferror(f))
4727 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4728 500467ff 2019-09-25 hiltjo return NULL;
4730 33aa809d 2019-08-08 stsp free(line);
4731 33aa809d 2019-08-08 stsp return NULL;
4734 33aa809d 2019-08-08 stsp static const struct got_error *
4735 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4736 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4737 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4739 33aa809d 2019-08-08 stsp const struct got_error *err;
4741 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4742 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4743 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4745 33aa809d 2019-08-08 stsp return err;
4746 33aa809d 2019-08-08 stsp (*line_cur1)++;
4748 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4749 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4750 33aa809d 2019-08-08 stsp if (rejectfile)
4751 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4753 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4755 33aa809d 2019-08-08 stsp return err;
4756 33aa809d 2019-08-08 stsp (*line_cur2)++;
4758 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4759 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4760 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4762 33aa809d 2019-08-08 stsp return err;
4763 33aa809d 2019-08-08 stsp (*line_cur2)++;
4765 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4766 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4767 33aa809d 2019-08-08 stsp if (rejectfile)
4768 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4770 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4772 33aa809d 2019-08-08 stsp return err;
4773 33aa809d 2019-08-08 stsp (*line_cur1)++;
4776 f1e81a05 2019-08-10 stsp return NULL;
4779 f1e81a05 2019-08-10 stsp static const struct got_error *
4780 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4781 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4783 f1e81a05 2019-08-10 stsp const struct got_error *err;
4785 f1e81a05 2019-08-10 stsp if (outfile) {
4786 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4787 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4788 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4790 f1e81a05 2019-08-10 stsp return err;
4791 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4794 f1e81a05 2019-08-10 stsp if (rejectfile) {
4795 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4796 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4797 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4799 f1e81a05 2019-08-10 stsp return err;
4800 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4804 33aa809d 2019-08-08 stsp return NULL;
4807 33aa809d 2019-08-08 stsp static const struct got_error *
4808 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4809 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4810 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4811 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4812 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4814 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4815 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4816 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4817 33aa809d 2019-08-08 stsp FILE *hunkfile;
4818 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4819 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4822 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4824 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4825 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4826 fe621944 2020-11-10 stsp start_old = cc.left.start;
4827 fe621944 2020-11-10 stsp end_old = cc.left.end;
4828 fe621944 2020-11-10 stsp start_new = cc.right.start;
4829 fe621944 2020-11-10 stsp end_new = cc.right.end;
4831 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4832 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4833 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4835 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4836 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4837 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4839 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4840 fe621944 2020-11-10 stsp if (diff_state == NULL)
4841 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4842 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4844 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4845 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4846 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4847 33aa809d 2019-08-08 stsp goto done;
4850 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4851 fe621944 2020-11-10 stsp diff_result, &cc);
4852 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4853 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4854 33aa809d 2019-08-08 stsp goto done;
4857 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4858 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4859 33aa809d 2019-08-08 stsp goto done;
4862 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4863 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4865 33aa809d 2019-08-08 stsp goto done;
4867 33aa809d 2019-08-08 stsp switch (*choice) {
4868 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4869 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4870 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4872 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4873 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4874 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4876 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4879 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4883 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4884 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4885 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4886 33aa809d 2019-08-08 stsp return err;
4889 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4890 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4891 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4892 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4893 1f1abb7e 2019-08-08 stsp void *progress_arg;
4894 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4895 33aa809d 2019-08-08 stsp void *patch_arg;
4896 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4897 f259c4c1 2021-09-24 stsp int unlink_added_files;
4898 af179be7 2023-04-14 stsp struct got_pathlist_head *added_files_to_unlink;
4901 e20a8b6f 2019-06-04 stsp static const struct got_error *
4902 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4903 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4904 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4905 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4906 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4908 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4909 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4910 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4911 eb81bc23 2022-06-28 tracey int fd = -1, fd2 = -1;
4912 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4913 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4914 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4915 fe621944 2020-11-10 stsp struct stat sb2;
4916 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4917 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4918 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4920 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4922 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4924 33aa809d 2019-08-08 stsp return err;
4926 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4927 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4928 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4929 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4930 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4931 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4932 fa3cef63 2020-07-23 stsp goto done;
4934 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4935 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4936 c0df5966 2021-12-31 stsp if (link_len == -1) {
4937 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4940 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4941 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4944 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4945 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4946 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4947 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4948 fa3cef63 2020-07-23 stsp goto done;
4950 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4951 fa3cef63 2020-07-23 stsp sizeof(link_target));
4952 fa3cef63 2020-07-23 stsp if (link_len == -1)
4953 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4954 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4955 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4958 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4959 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4960 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4961 fa3cef63 2020-07-23 stsp goto done;
4964 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4965 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4966 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4967 fa3cef63 2020-07-23 stsp goto done;
4972 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4973 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4974 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4975 fa3cef63 2020-07-23 stsp goto done;
4977 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4978 fa3cef63 2020-07-23 stsp if (n != link_len) {
4979 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4980 fa3cef63 2020-07-23 stsp goto done;
4982 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4983 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4984 fa3cef63 2020-07-23 stsp goto done;
4986 fa3cef63 2020-07-23 stsp rewind(f2);
4989 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4990 eb81bc23 2022-06-28 tracey if (fd == -1) {
4991 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4992 eb81bc23 2022-06-28 tracey goto done;
4995 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4997 33aa809d 2019-08-08 stsp goto done;
4999 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
5001 33aa809d 2019-08-08 stsp goto done;
5003 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5005 33aa809d 2019-08-08 stsp goto done;
5007 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5008 4b752015 2022-06-30 stsp 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5010 33aa809d 2019-08-08 stsp goto done;
5012 b90054ed 2022-11-01 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5015 33aa809d 2019-08-08 stsp goto done;
5017 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
5018 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
5019 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
5020 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
5022 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
5023 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5024 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
5025 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
5026 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
5027 fe621944 2020-11-10 stsp nchanges++;
5029 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5030 33aa809d 2019-08-08 stsp int choice;
5031 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
5032 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
5033 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
5034 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
5035 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
5036 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
5038 33aa809d 2019-08-08 stsp goto done;
5039 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
5040 33aa809d 2019-08-08 stsp have_content = 1;
5041 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
5044 1ebedb77 2019-10-19 stsp if (have_content) {
5045 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5046 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
5047 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
5049 1ebedb77 2019-10-19 stsp goto done;
5051 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
5052 b2b3fce1 2022-10-29 op mode_t mode;
5054 b2b3fce1 2022-10-29 op mode = apply_umask(sb2.st_mode);
5055 b2b3fce1 2022-10-29 op if (fchmod(fileno(outfile), mode) == -1) {
5056 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
5057 fa3cef63 2020-07-23 stsp goto done;
5062 33aa809d 2019-08-08 stsp free(id_str);
5063 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5064 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5066 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
5067 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
5068 fe621944 2020-11-10 stsp if (err == NULL)
5069 fe621944 2020-11-10 stsp err = free_err;
5070 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
5071 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
5072 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
5073 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
5074 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5075 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
5076 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
5077 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
5078 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
5079 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
5080 33aa809d 2019-08-08 stsp if (err || !have_content) {
5081 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5082 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
5083 33aa809d 2019-08-08 stsp free(*path_outfile);
5084 33aa809d 2019-08-08 stsp *path_outfile = NULL;
5086 33aa809d 2019-08-08 stsp free(path1);
5087 33aa809d 2019-08-08 stsp return err;
5090 33aa809d 2019-08-08 stsp static const struct got_error *
5091 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
5092 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
5093 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5094 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5096 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
5097 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
5098 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
5099 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
5100 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
5101 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
5102 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
5103 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
5104 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
5105 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
5106 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
5107 eb81bc23 2022-06-28 tracey int fd = -1;
5109 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
5110 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
5111 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
5112 d3bcc3d1 2019-08-08 stsp return NULL;
5114 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
5115 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
5116 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
5118 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5119 65084dad 2019-08-08 stsp if (ie == NULL)
5120 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
5122 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
5123 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
5124 a129376b 2019-03-28 stsp if (err) {
5125 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
5126 a129376b 2019-03-28 stsp goto done;
5127 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
5128 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
5129 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
5130 e20a8b6f 2019-06-04 stsp goto done;
5133 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
5134 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
5135 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5136 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5137 a129376b 2019-03-28 stsp goto done;
5140 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
5141 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
5142 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5143 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5144 a129376b 2019-03-28 stsp goto done;
5147 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
5148 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
5149 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5150 a129376b 2019-03-28 stsp goto done;
5155 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
5156 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
5158 a44927cc 2022-04-07 stsp goto done;
5160 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5161 a9fa2909 2019-07-27 stsp if (err) {
5162 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5163 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
5164 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
5165 a9fa2909 2019-07-27 stsp goto done;
5167 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
5169 a9fa2909 2019-07-27 stsp goto done;
5171 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
5173 a9fa2909 2019-07-27 stsp goto done;
5175 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
5176 1233e6b6 2020-10-19 stsp free(te_name);
5177 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
5178 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
5179 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5180 a9fa2909 2019-07-27 stsp goto done;
5184 a129376b 2019-03-28 stsp switch (status) {
5185 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
5186 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5187 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5188 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5189 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5191 33aa809d 2019-08-08 stsp goto done;
5192 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5195 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5196 1f1abb7e 2019-08-08 stsp ie->path);
5198 1ee397ad 2019-07-12 stsp goto done;
5199 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5200 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
5201 af179be7 2023-04-14 stsp int do_unlink = a->added_files_to_unlink ? 0 : 1;
5203 af179be7 2023-04-14 stsp if (a->added_files_to_unlink) {
5204 af179be7 2023-04-14 stsp struct got_pathlist_entry *pe;
5206 af179be7 2023-04-14 stsp TAILQ_FOREACH(pe, a->added_files_to_unlink,
5208 af179be7 2023-04-14 stsp if (got_path_cmp(pe->path, relpath,
5209 af179be7 2023-04-14 stsp pe->path_len, strlen(relpath)))
5211 af179be7 2023-04-14 stsp do_unlink = 1;
5216 af179be7 2023-04-14 stsp if (do_unlink) {
5217 af179be7 2023-04-14 stsp if (asprintf(&ondisk_path, "%s/%s",
5218 af179be7 2023-04-14 stsp got_worktree_get_root_path(a->worktree),
5219 af179be7 2023-04-14 stsp relpath) == -1) {
5220 af179be7 2023-04-14 stsp err = got_error_from_errno("asprintf");
5221 af179be7 2023-04-14 stsp goto done;
5223 af179be7 2023-04-14 stsp if (unlink(ondisk_path) == -1) {
5224 af179be7 2023-04-14 stsp err = got_error_from_errno2("unlink",
5225 af179be7 2023-04-14 stsp ondisk_path);
5231 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5232 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5233 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5234 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5235 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5237 33aa809d 2019-08-08 stsp goto done;
5238 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5241 33aa809d 2019-08-08 stsp /* fall through */
5242 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5243 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5244 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5245 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5246 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5247 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5248 b4b2adf5 2023-02-09 op staged_status == GOT_STATUS_MODIFY)
5249 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
5251 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
5252 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5253 eb81bc23 2022-06-28 tracey if (fd == -1) {
5254 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5255 eb81bc23 2022-06-28 tracey goto done;
5258 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5260 65084dad 2019-08-08 stsp goto done;
5262 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5263 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5264 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5265 a129376b 2019-03-28 stsp goto done;
5268 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5269 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5270 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5271 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5272 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5273 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5274 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5276 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5277 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5278 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5279 369fd7e5 2020-07-23 stsp path_content);
5282 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5283 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5284 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
5285 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5287 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5288 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5289 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5290 369fd7e5 2020-07-23 stsp goto done;
5294 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5295 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5296 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5297 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5298 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
5299 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5301 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5303 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5304 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5305 ef623445 2023-09-16 op 0, 1, 0, 0, NULL, a->repo,
5306 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5309 a129376b 2019-03-28 stsp goto done;
5310 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5311 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5312 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5313 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5314 437adc9d 2020-12-10 yzhong blob->id.sha1,
5315 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5317 2e1fa222 2020-07-23 stsp goto done;
5319 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5320 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5321 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5330 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5331 33aa809d 2019-08-08 stsp free(path_content);
5332 e20a8b6f 2019-06-04 stsp free(parent_path);
5333 a129376b 2019-03-28 stsp free(tree_path);
5334 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5335 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5337 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5339 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5340 a129376b 2019-03-28 stsp free(tree_id);
5341 a44927cc 2022-04-07 stsp if (base_commit)
5342 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
5343 e20a8b6f 2019-06-04 stsp return err;
5346 e20a8b6f 2019-06-04 stsp const struct got_error *
5347 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5348 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5349 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5350 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5351 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5353 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5354 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5355 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5356 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5357 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5358 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5360 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5362 e20a8b6f 2019-06-04 stsp return err;
5364 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5366 e20a8b6f 2019-06-04 stsp goto done;
5368 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5369 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5370 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5371 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5372 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5373 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5374 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5375 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
5376 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5377 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5378 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
5382 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5383 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5384 e20a8b6f 2019-06-04 stsp err = sync_err;
5386 fb399478 2019-07-12 stsp free(fileindex_path);
5387 a129376b 2019-03-28 stsp if (fileindex)
5388 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5389 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5390 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5391 a129376b 2019-03-28 stsp err = unlockerr;
5392 c4296144 2019-05-09 stsp return err;
5395 cf066bf8 2019-05-09 stsp static void
5396 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5398 24519714 2019-05-09 stsp free(ct->path);
5399 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5400 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5401 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5402 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5403 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5404 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5408 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5409 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5410 24519714 2019-05-09 stsp struct got_repository *repo;
5411 24519714 2019-05-09 stsp struct got_worktree *worktree;
5412 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5413 5f8a88c6 2019-08-03 stsp int have_staged_files;
5414 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5415 2a47b1e5 2022-11-01 stsp int diff_header_shown;
5416 12383673 2023-02-18 mark int commit_conflicts;
5417 2a47b1e5 2022-11-01 stsp FILE *diff_outfile;
5423 2a47b1e5 2022-11-01 stsp * Create a file which contains the target path of a symlink so we can feed
5424 2a47b1e5 2022-11-01 stsp * it as content to the diff engine.
5426 2a47b1e5 2022-11-01 stsp static const struct got_error *
5427 2a47b1e5 2022-11-01 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5428 2a47b1e5 2022-11-01 stsp const char *abspath)
5430 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5431 2a47b1e5 2022-11-01 stsp char target_path[PATH_MAX];
5432 2a47b1e5 2022-11-01 stsp ssize_t target_len, outlen;
5436 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5437 2a47b1e5 2022-11-01 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5438 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5439 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlinkat", abspath);
5441 2a47b1e5 2022-11-01 stsp target_len = readlink(abspath, target_path, PATH_MAX);
5442 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5443 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlink", abspath);
5446 2a47b1e5 2022-11-01 stsp *fd = got_opentempfd();
5447 2a47b1e5 2022-11-01 stsp if (*fd == -1)
5448 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentempfd");
5450 2a47b1e5 2022-11-01 stsp outlen = write(*fd, target_path, target_len);
5451 2a47b1e5 2022-11-01 stsp if (outlen == -1) {
5452 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5453 2a47b1e5 2022-11-01 stsp goto done;
5456 2a47b1e5 2022-11-01 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
5457 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("lseek", abspath);
5458 2a47b1e5 2022-11-01 stsp goto done;
5461 2a47b1e5 2022-11-01 stsp if (err) {
5462 2a47b1e5 2022-11-01 stsp close(*fd);
5465 2a47b1e5 2022-11-01 stsp return err;
5468 2a47b1e5 2022-11-01 stsp static const struct got_error *
5469 2a47b1e5 2022-11-01 stsp append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5470 2a47b1e5 2022-11-01 stsp FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5471 6d15dc69 2022-11-01 stsp int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5473 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5474 2a47b1e5 2022-11-01 stsp struct got_blob_object *blob1 = NULL;
5475 2a47b1e5 2022-11-01 stsp int fd = -1, fd1 = -1, fd2 = -1;
5476 2a47b1e5 2022-11-01 stsp FILE *ondisk_file = NULL;
5477 2a47b1e5 2022-11-01 stsp char *label1 = NULL;
5478 2a47b1e5 2022-11-01 stsp struct stat sb;
5479 2a47b1e5 2022-11-01 stsp off_t size1 = 0;
5480 2a47b1e5 2022-11-01 stsp int f2_exists = 0;
5481 2a47b1e5 2022-11-01 stsp char *id_str = NULL;
5483 2a47b1e5 2022-11-01 stsp memset(&sb, 0, sizeof(sb));
5485 4ba5cca9 2022-11-01 stsp if (diff_staged) {
5486 4ba5cca9 2022-11-01 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5487 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_ADD &&
5488 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_DELETE)
5489 4ba5cca9 2022-11-01 stsp return NULL;
5491 4ba5cca9 2022-11-01 stsp if (ct->status != GOT_STATUS_MODIFY &&
5492 4ba5cca9 2022-11-01 stsp ct->status != GOT_STATUS_ADD &&
5493 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5494 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5495 4ba5cca9 2022-11-01 stsp return NULL;
5498 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f1);
5500 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5501 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f2);
5503 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5505 2a47b1e5 2022-11-01 stsp if (!*diff_header_shown) {
5506 2a47b1e5 2022-11-01 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
5508 2a47b1e5 2022-11-01 stsp return err;
5509 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5510 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree));
5511 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "commit - %s\n", id_str);
5512 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "path + %s%s\n",
5513 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree),
5514 2a47b1e5 2022-11-01 stsp diff_staged ? " (staged changes)" : "");
5515 2a47b1e5 2022-11-01 stsp *diff_header_shown = 1;
5518 2a47b1e5 2022-11-01 stsp if (diff_staged) {
5519 2a47b1e5 2022-11-01 stsp const char *label1 = NULL, *label2 = NULL;
5520 2a47b1e5 2022-11-01 stsp switch (ct->staged_status) {
5521 2a47b1e5 2022-11-01 stsp case GOT_STATUS_MODIFY:
5522 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5523 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5525 2a47b1e5 2022-11-01 stsp case GOT_STATUS_ADD:
5526 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5528 2a47b1e5 2022-11-01 stsp case GOT_STATUS_DELETE:
5529 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5532 2a47b1e5 2022-11-01 stsp return got_error(GOT_ERR_FILE_STATUS);
5534 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5535 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5536 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5537 2a47b1e5 2022-11-01 stsp goto done;
5539 2a47b1e5 2022-11-01 stsp fd2 = got_opentempfd();
5540 2a47b1e5 2022-11-01 stsp if (fd2 == -1) {
5541 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5542 2a47b1e5 2022-11-01 stsp goto done;
5544 2a47b1e5 2022-11-01 stsp err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5545 2a47b1e5 2022-11-01 stsp fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5546 1f3405c9 2023-01-17 mark label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5547 a76e88e5 2023-01-10 mark NULL, repo, diff_outfile);
5548 2a47b1e5 2022-11-01 stsp goto done;
5551 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5552 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5553 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5554 2a47b1e5 2022-11-01 stsp goto done;
5557 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_ADD) {
5558 2a47b1e5 2022-11-01 stsp err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5559 2a47b1e5 2022-11-01 stsp 8192, fd1);
5561 2a47b1e5 2022-11-01 stsp goto done;
5564 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_DELETE) {
5565 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5566 2a47b1e5 2022-11-01 stsp fd = openat(dirfd, de_name,
5567 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5568 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5569 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5570 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("openat",
5571 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5572 2a47b1e5 2022-11-01 stsp goto done;
5574 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5575 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5577 2a47b1e5 2022-11-01 stsp goto done;
5580 2a47b1e5 2022-11-01 stsp fd = open(ct->ondisk_path,
5581 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5582 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5583 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5584 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("open",
5585 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5586 2a47b1e5 2022-11-01 stsp goto done;
5588 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5589 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5591 2a47b1e5 2022-11-01 stsp goto done;
5594 2a47b1e5 2022-11-01 stsp if (fstatat(fd, ct->ondisk_path, &sb,
5595 2a47b1e5 2022-11-01 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5596 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fstatat", ct->ondisk_path);
5597 2a47b1e5 2022-11-01 stsp goto done;
5599 2a47b1e5 2022-11-01 stsp ondisk_file = fdopen(fd, "r");
5600 2a47b1e5 2022-11-01 stsp if (ondisk_file == NULL) {
5601 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fdopen", ct->ondisk_path);
5602 2a47b1e5 2022-11-01 stsp goto done;
5605 2a47b1e5 2022-11-01 stsp f2_exists = 1;
5608 2a47b1e5 2022-11-01 stsp if (blob1) {
5609 2a47b1e5 2022-11-01 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5610 2a47b1e5 2022-11-01 stsp f1, blob1);
5612 2a47b1e5 2022-11-01 stsp goto done;
5615 2a47b1e5 2022-11-01 stsp err = got_diff_blob_file(blob1, f1, size1, label1,
5616 2a47b1e5 2022-11-01 stsp ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5617 1f3405c9 2023-01-17 mark GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5619 2a47b1e5 2022-11-01 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5620 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5621 2a47b1e5 2022-11-01 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5622 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5623 2a47b1e5 2022-11-01 stsp if (blob1)
5624 2a47b1e5 2022-11-01 stsp got_object_blob_close(blob1);
5625 2a47b1e5 2022-11-01 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5626 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5627 2a47b1e5 2022-11-01 stsp if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5628 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
5629 2a47b1e5 2022-11-01 stsp return err;
5632 c4296144 2019-05-09 stsp static const struct got_error *
5633 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5634 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5635 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5636 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5638 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5639 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5640 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5641 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5642 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5643 768aea60 2019-05-09 stsp struct stat sb;
5645 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5646 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5647 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5648 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5649 5f8a88c6 2019-08-03 stsp return NULL;
5651 12383673 2023-02-18 mark if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5652 12383673 2023-02-18 mark printf("C %s\n", relpath);
5653 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5656 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5657 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5658 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5659 12383673 2023-02-18 mark status != GOT_STATUS_DELETE &&
5660 12383673 2023-02-18 mark status != GOT_STATUS_CONFLICT)
5661 5f8a88c6 2019-08-03 stsp return NULL;
5664 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5665 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5666 036813ee 2019-05-09 stsp goto done;
5668 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5669 036813ee 2019-05-09 stsp parent_path = strdup("");
5670 69960a46 2019-05-09 stsp if (parent_path == NULL)
5671 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5673 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5675 69960a46 2019-05-09 stsp return err;
5678 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5679 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5680 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5681 c4296144 2019-05-09 stsp goto done;
5684 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5685 768aea60 2019-05-09 stsp relpath) == -1) {
5686 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5687 768aea60 2019-05-09 stsp goto done;
5690 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5691 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5692 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5693 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5694 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5695 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5696 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5697 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5699 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5700 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5703 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5704 0aeb8099 2020-07-23 stsp goto done;
5706 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5707 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5708 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5709 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5710 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5711 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5712 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5713 12463d8b 2019-12-13 stsp ct->ondisk_path);
5714 12463d8b 2019-12-13 stsp goto done;
5716 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5717 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5718 768aea60 2019-05-09 stsp goto done;
5720 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5723 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5724 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5725 44d03001 2019-05-09 stsp relpath) == -1) {
5726 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5727 44d03001 2019-05-09 stsp goto done;
5730 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5731 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5732 35213c7c 2020-07-23 stsp int is_bad_symlink;
5733 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5734 35213c7c 2020-07-23 stsp ssize_t target_len;
5735 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5736 35213c7c 2020-07-23 stsp sizeof(target_path));
5737 35213c7c 2020-07-23 stsp if (target_len == -1) {
5738 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5739 35213c7c 2020-07-23 stsp ct->ondisk_path);
5740 35213c7c 2020-07-23 stsp goto done;
5742 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5743 df6221c7 2023-07-19 stsp target_len, ct->ondisk_path, a->worktree->root_path,
5744 df6221c7 2023-07-19 stsp a->worktree->meta_dir);
5746 35213c7c 2020-07-23 stsp goto done;
5747 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5748 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5749 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5750 35213c7c 2020-07-23 stsp goto done;
5755 cf066bf8 2019-05-09 stsp ct->status = status;
5756 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5757 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5758 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5759 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5760 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5761 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5762 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5763 b416585c 2019-05-13 stsp goto done;
5765 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5766 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5767 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5768 f0b75401 2019-08-03 stsp goto done;
5771 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5772 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5773 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5774 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5775 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5776 036813ee 2019-05-09 stsp goto done;
5779 24519714 2019-05-09 stsp ct->path = strdup(path);
5780 24519714 2019-05-09 stsp if (ct->path == NULL) {
5781 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5782 24519714 2019-05-09 stsp goto done;
5785 c78dbc03 2023-08-25 stsp if (a->diff_outfile) {
5786 2a47b1e5 2022-11-01 stsp err = append_ct_diff(ct, &a->diff_header_shown,
5787 2a47b1e5 2022-11-01 stsp a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5788 6d15dc69 2022-11-01 stsp a->have_staged_files, a->repo, a->worktree);
5790 2a47b1e5 2022-11-01 stsp goto done;
5793 c78dbc03 2023-08-25 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5795 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5796 ed175427 2019-05-09 stsp free_commitable(ct);
5797 24519714 2019-05-09 stsp free(parent_path);
5798 036813ee 2019-05-09 stsp free(path);
5799 a129376b 2019-03-28 stsp return err;
5802 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5803 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5804 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5805 036813ee 2019-05-09 stsp struct got_repository *);
5807 ed175427 2019-05-09 stsp static const struct got_error *
5808 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5809 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5810 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5811 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5812 afa376bf 2019-05-09 stsp struct got_repository *repo)
5814 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5815 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5816 036813ee 2019-05-09 stsp char *subpath;
5818 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5819 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5820 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5822 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5824 ed175427 2019-05-09 stsp return err;
5826 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5827 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5828 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5829 036813ee 2019-05-09 stsp free(subpath);
5830 036813ee 2019-05-09 stsp return err;
5833 036813ee 2019-05-09 stsp static const struct got_error *
5834 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5836 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5837 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5839 036813ee 2019-05-09 stsp *match = 0;
5841 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5842 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5843 0f63689d 2019-05-10 stsp return NULL;
5846 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5848 0f63689d 2019-05-10 stsp return err;
5849 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5850 036813ee 2019-05-09 stsp free(ct_parent_path);
5851 036813ee 2019-05-09 stsp return err;
5854 768aea60 2019-05-09 stsp static mode_t
5855 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5857 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5858 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5860 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5863 036813ee 2019-05-09 stsp static const struct got_error *
5864 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5865 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5867 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5869 036813ee 2019-05-09 stsp *new_te = NULL;
5871 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5873 036813ee 2019-05-09 stsp goto done;
5875 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5877 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5878 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5879 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5881 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5883 036813ee 2019-05-09 stsp if (err && *new_te) {
5884 56e0773d 2019-11-28 stsp free(*new_te);
5885 036813ee 2019-05-09 stsp *new_te = NULL;
5887 036813ee 2019-05-09 stsp return err;
5890 036813ee 2019-05-09 stsp static const struct got_error *
5891 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5892 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5894 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5895 102b254e 2020-10-19 stsp char *ct_name = NULL;
5897 036813ee 2019-05-09 stsp *new_te = NULL;
5899 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5900 036813ee 2019-05-09 stsp if (*new_te == NULL)
5901 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5903 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5905 036813ee 2019-05-09 stsp goto done;
5906 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5907 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5908 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5909 036813ee 2019-05-09 stsp goto done;
5912 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5914 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5915 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5916 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5918 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5920 102b254e 2020-10-19 stsp free(ct_name);
5921 036813ee 2019-05-09 stsp if (err && *new_te) {
5922 56e0773d 2019-11-28 stsp free(*new_te);
5923 036813ee 2019-05-09 stsp *new_te = NULL;
5925 036813ee 2019-05-09 stsp return err;
5928 036813ee 2019-05-09 stsp static const struct got_error *
5929 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5930 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5932 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5933 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5935 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5937 036813ee 2019-05-09 stsp return err;
5938 036813ee 2019-05-09 stsp if (new_pe == NULL)
5939 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5940 036813ee 2019-05-09 stsp return NULL;
5943 afa376bf 2019-05-09 stsp static const struct got_error *
5944 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5945 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5947 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5948 5f8a88c6 2019-08-03 stsp unsigned char status;
5950 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5951 0ff8d236 2021-09-28 stsp return NULL;
5953 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5954 afa376bf 2019-05-09 stsp ct_path++;
5956 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5957 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5959 5f8a88c6 2019-08-03 stsp status = ct->status;
5961 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5962 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5965 036813ee 2019-05-09 stsp static const struct got_error *
5966 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5967 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5969 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5970 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5971 44d03001 2019-05-09 stsp char *te_path;
5973 44d03001 2019-05-09 stsp *modified = 0;
5975 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5976 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5977 44d03001 2019-05-09 stsp te->name) == -1)
5978 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5980 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5981 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5982 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5983 44d03001 2019-05-09 stsp strlen(te_path));
5984 62d463ca 2020-10-20 naddy if (*modified)
5988 44d03001 2019-05-09 stsp free(te_path);
5989 44d03001 2019-05-09 stsp return err;
5992 44d03001 2019-05-09 stsp static const struct got_error *
5993 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5994 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5995 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5997 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5998 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
6000 036813ee 2019-05-09 stsp *ctp = NULL;
6002 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6003 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6004 036813ee 2019-05-09 stsp char *ct_name = NULL;
6005 036813ee 2019-05-09 stsp int path_matches;
6007 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6008 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
6009 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
6010 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
6011 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
6014 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
6015 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
6019 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6022 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6024 036813ee 2019-05-09 stsp return err;
6025 036813ee 2019-05-09 stsp if (!path_matches)
6028 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
6030 d34b633e 2020-10-19 stsp return err;
6032 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
6033 d34b633e 2020-10-19 stsp free(ct_name);
6036 d34b633e 2020-10-19 stsp free(ct_name);
6038 036813ee 2019-05-09 stsp *ctp = ct;
6042 2b496619 2019-07-10 stsp return err;
6045 2b496619 2019-07-10 stsp static const struct got_error *
6046 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6047 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
6048 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
6049 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
6050 2b496619 2019-07-10 stsp struct got_repository *repo)
6052 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
6053 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
6054 2b496619 2019-07-10 stsp char *subtree_path;
6055 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
6056 ba580f68 2020-03-22 stsp int nentries;
6058 2b496619 2019-07-10 stsp *new_tep = NULL;
6060 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6061 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
6062 2b496619 2019-07-10 stsp child_path) == -1)
6063 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
6065 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
6066 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
6067 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
6068 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
6070 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6071 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
6072 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
6073 2b496619 2019-07-10 stsp goto done;
6075 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
6076 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
6077 2b496619 2019-07-10 stsp if (err) {
6078 56e0773d 2019-11-28 stsp free(new_te);
6079 2b496619 2019-07-10 stsp goto done;
6081 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
6084 2b496619 2019-07-10 stsp free(subtree_path);
6085 2b496619 2019-07-10 stsp if (err == NULL)
6086 2b496619 2019-07-10 stsp *new_tep = new_te;
6087 036813ee 2019-05-09 stsp return err;
6090 036813ee 2019-05-09 stsp static const struct got_error *
6091 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
6092 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
6093 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
6094 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6095 036813ee 2019-05-09 stsp struct got_repository *repo)
6097 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
6098 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
6099 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
6100 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
6102 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
6103 ba580f68 2020-03-22 stsp *nentries = 0;
6105 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
6106 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6107 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6108 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
6110 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
6111 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
6112 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
6115 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6116 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
6119 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6120 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
6122 036813ee 2019-05-09 stsp goto done;
6124 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
6125 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
6126 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
6128 036813ee 2019-05-09 stsp goto done;
6129 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
6131 afa376bf 2019-05-09 stsp goto done;
6132 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
6133 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6135 2f51b5b3 2019-05-09 stsp goto done;
6136 ba580f68 2020-03-22 stsp (*nentries)++;
6138 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
6139 2b496619 2019-07-10 stsp if (base_tree == NULL ||
6140 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
6141 2b496619 2019-07-10 stsp == NULL) {
6142 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
6143 2b496619 2019-07-10 stsp child_path, path_base_tree,
6144 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
6147 2b496619 2019-07-10 stsp goto done;
6148 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6150 2b496619 2019-07-10 stsp goto done;
6151 ba580f68 2020-03-22 stsp (*nentries)++;
6156 2f51b5b3 2019-05-09 stsp if (base_tree) {
6157 56e0773d 2019-11-28 stsp int i, nbase_entries;
6158 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
6159 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
6160 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
6161 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
6163 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
6164 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
6165 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
6166 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
6168 63c5ca5d 2019-08-24 stsp goto done;
6169 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
6171 63c5ca5d 2019-08-24 stsp goto done;
6172 ba580f68 2020-03-22 stsp (*nentries)++;
6176 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
6177 44d03001 2019-05-09 stsp int modified;
6178 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6180 036813ee 2019-05-09 stsp goto done;
6181 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
6182 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
6184 2f51b5b3 2019-05-09 stsp goto done;
6185 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
6186 44d03001 2019-05-09 stsp if (modified) {
6187 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
6188 ba580f68 2020-03-22 stsp int nsubentries;
6189 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
6190 ba580f68 2020-03-22 stsp &nsubentries, te,
6191 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
6192 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
6194 44d03001 2019-05-09 stsp goto done;
6195 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6196 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6197 ba580f68 2020-03-22 stsp free(new_id);
6200 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6201 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6202 56e0773d 2019-11-28 stsp free(new_id);
6204 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6206 036813ee 2019-05-09 stsp goto done;
6207 ba580f68 2020-03-22 stsp (*nentries)++;
6211 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6212 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6214 f66c734c 2019-09-22 stsp goto done;
6216 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6217 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6218 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6219 12383673 2023-02-18 mark ct->status == GOT_STATUS_CONFLICT ||
6220 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6221 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6222 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6224 2f51b5b3 2019-05-09 stsp goto done;
6225 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6227 2f51b5b3 2019-05-09 stsp goto done;
6228 ba580f68 2020-03-22 stsp (*nentries)++;
6230 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6231 b416585c 2019-05-13 stsp status_arg);
6233 afa376bf 2019-05-09 stsp goto done;
6235 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6236 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6238 2f51b5b3 2019-05-09 stsp goto done;
6239 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6241 2f51b5b3 2019-05-09 stsp goto done;
6242 ba580f68 2020-03-22 stsp (*nentries)++;
6247 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6248 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6250 d8bacb93 2023-01-10 mark got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6251 2e1fa222 2020-07-23 stsp return err;
6254 2e1fa222 2020-07-23 stsp static const struct got_error *
6255 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6256 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6257 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6258 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6260 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6261 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6262 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6264 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6265 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6266 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6268 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6270 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6271 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6272 437adc9d 2020-12-10 yzhong if (err)
6273 437adc9d 2020-12-10 yzhong goto done;
6276 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6277 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6278 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6279 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6280 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6281 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6282 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6283 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6285 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6286 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6287 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6288 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6289 72fd46fa 2019-09-06 stsp !have_staged_files);
6291 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6292 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6293 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6294 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6295 72fd46fa 2019-09-06 stsp !have_staged_files);
6297 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6299 437adc9d 2020-12-10 yzhong goto done;
6300 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6301 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6302 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6303 3969253a 2020-03-07 stsp if (err) {
6304 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6305 437adc9d 2020-12-10 yzhong goto done;
6307 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6308 3969253a 2020-03-07 stsp if (err) {
6309 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6310 437adc9d 2020-12-10 yzhong goto done;
6313 437adc9d 2020-12-10 yzhong free(relpath);
6314 437adc9d 2020-12-10 yzhong relpath = NULL;
6317 437adc9d 2020-12-10 yzhong free(relpath);
6318 d56d26ce 2019-05-10 stsp return err;
6322 d56d26ce 2019-05-10 stsp static const struct got_error *
6323 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6324 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6325 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6326 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6327 735ef5ac 2019-08-03 stsp int ood_errcode)
6329 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6330 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6331 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6333 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6334 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6335 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6336 1a36436d 2019-06-10 stsp return NULL;
6338 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6339 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6341 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
6343 a44927cc 2022-04-07 stsp goto done;
6344 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6345 9bead371 2019-07-28 stsp if (err) {
6346 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6347 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6348 1a36436d 2019-06-10 stsp goto done;
6349 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6350 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6352 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6353 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
6355 a44927cc 2022-04-07 stsp goto done;
6356 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6357 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6358 1a36436d 2019-06-10 stsp goto done;
6359 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6363 a44927cc 2022-04-07 stsp if (commit)
6364 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6365 1a36436d 2019-06-10 stsp return err;
6368 336075a4 2022-06-25 op static const struct got_error *
6369 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6370 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6371 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
6372 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
6373 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
6374 2a47b1e5 2022-11-01 stsp const char *author, const char *committer, char *diff_path,
6375 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6376 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6377 afa376bf 2019-05-09 stsp struct got_repository *repo)
6379 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6380 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6381 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6382 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6383 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6384 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6385 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6386 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6387 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
6388 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6389 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6390 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6391 f8399b8f 2022-07-22 stsp time_t timestamp;
6393 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6395 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6397 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6399 588edf97 2019-05-10 stsp goto done;
6401 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6403 588edf97 2019-05-10 stsp goto done;
6405 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6406 2a47b1e5 2022-11-01 stsp err = commit_msg_cb(commitable_paths, diff_path,
6407 2a47b1e5 2022-11-01 stsp &logmsg, commit_arg);
6412 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6413 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6417 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6418 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6419 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6420 cf066bf8 2019-05-09 stsp char *ondisk_path;
6422 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6423 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6424 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6427 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6428 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6429 12383673 2023-02-18 mark ct->status != GOT_STATUS_MODE_CHANGE &&
6430 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
6433 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6434 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6435 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6436 cf066bf8 2019-05-09 stsp goto done;
6438 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6439 2e1fa222 2020-07-23 stsp free(ondisk_path);
6441 cf066bf8 2019-05-09 stsp goto done;
6444 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6445 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6446 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6448 036813ee 2019-05-09 stsp goto done;
6450 2b706956 2023-04-17 stsp err = got_object_qid_alloc(&pid, head_commit_id);
6452 de18fc63 2019-05-09 stsp goto done;
6453 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6454 f259c4c1 2021-09-24 stsp nparents++;
6455 f259c4c1 2021-09-24 stsp if (parent_id2) {
6456 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
6458 f259c4c1 2021-09-24 stsp goto done;
6459 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6460 f259c4c1 2021-09-24 stsp nparents++;
6462 f8399b8f 2022-07-22 stsp timestamp = time(NULL);
6463 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6464 f8399b8f 2022-07-22 stsp nparents, author, timestamp, committer, timestamp, logmsg, repo);
6465 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6466 33ad4cbe 2019-05-12 jcs free(logmsg);
6468 9d40349a 2019-05-09 stsp goto done;
6470 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6471 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6472 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6473 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6474 09f5bd90 2019-05-09 stsp goto done;
6476 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6477 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6479 09f5bd90 2019-05-09 stsp goto done;
6480 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6482 ebf99748 2019-05-09 stsp goto done;
6483 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6484 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6485 09f5bd90 2019-05-09 stsp goto done;
6487 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6488 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6490 f2c16586 2019-05-09 stsp goto done;
6491 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6493 f2c16586 2019-05-09 stsp goto done;
6495 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6497 09f5bd90 2019-05-09 stsp goto done;
6499 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6501 ebf99748 2019-05-09 stsp goto done;
6503 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
6504 588edf97 2019-05-10 stsp if (head_tree)
6505 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6506 588edf97 2019-05-10 stsp if (head_commit)
6507 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6508 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6509 2f17228e 2019-05-12 stsp if (head_ref2) {
6510 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6511 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6512 2f17228e 2019-05-12 stsp err = unlockerr;
6513 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6515 39cd0ff6 2019-07-12 stsp return err;
6518 5c1e53bc 2019-07-28 stsp static const struct got_error *
6519 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6520 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6522 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6523 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6525 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6526 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6527 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6529 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6530 5c1e53bc 2019-07-28 stsp ct_path++;
6532 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6533 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6537 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6538 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6540 5c1e53bc 2019-07-28 stsp return NULL;
6543 f0b75401 2019-08-03 stsp static const struct got_error *
6544 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6546 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6548 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6549 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6550 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6553 f0b75401 2019-08-03 stsp return NULL;
6556 5f8a88c6 2019-08-03 stsp static const struct got_error *
6557 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6558 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6560 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6561 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6563 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6564 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6566 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6567 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6568 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6569 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6570 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6571 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6574 5f8a88c6 2019-08-03 stsp return NULL;
6577 39cd0ff6 2019-07-12 stsp const struct got_error *
6578 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6579 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6580 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6581 12383673 2023-02-18 mark int show_diff, int commit_conflicts,
6582 12383673 2023-02-18 mark got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6583 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6584 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6586 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6587 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6588 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6589 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6590 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6591 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6592 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6593 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6594 2a47b1e5 2022-11-01 stsp char *diff_path = NULL;
6595 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6597 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6599 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6600 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6602 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6604 39cd0ff6 2019-07-12 stsp goto done;
6606 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6608 39cd0ff6 2019-07-12 stsp goto done;
6610 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6612 39cd0ff6 2019-07-12 stsp goto done;
6614 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6616 39cd0ff6 2019-07-12 stsp goto done;
6618 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6619 5f8a88c6 2019-08-03 stsp &have_staged_files);
6620 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6621 5f8a88c6 2019-08-03 stsp goto done;
6622 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6623 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6625 5f8a88c6 2019-08-03 stsp goto done;
6628 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6629 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6630 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6631 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6632 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6633 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6634 2a47b1e5 2022-11-01 stsp cc_arg.diff_header_shown = 0;
6635 12383673 2023-02-18 mark cc_arg.commit_conflicts = commit_conflicts;
6636 2a47b1e5 2022-11-01 stsp if (show_diff) {
6637 2a47b1e5 2022-11-01 stsp err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6638 b90054ed 2022-11-01 stsp GOT_TMPDIR_STR "/got", ".diff");
6640 2a47b1e5 2022-11-01 stsp goto done;
6641 2a47b1e5 2022-11-01 stsp cc_arg.f1 = got_opentemp();
6642 2a47b1e5 2022-11-01 stsp if (cc_arg.f1 == NULL) {
6643 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6644 2a47b1e5 2022-11-01 stsp goto done;
6646 2a47b1e5 2022-11-01 stsp cc_arg.f2 = got_opentemp();
6647 2a47b1e5 2022-11-01 stsp if (cc_arg.f2 == NULL) {
6648 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6649 2a47b1e5 2022-11-01 stsp goto done;
6653 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6654 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6655 4ba2e955 2022-09-02 sh+got collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6657 5c1e53bc 2019-07-28 stsp goto done;
6660 2a47b1e5 2022-11-01 stsp if (show_diff) {
6661 2a47b1e5 2022-11-01 stsp if (fflush(cc_arg.diff_outfile) == EOF) {
6662 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fflush");
6663 2a47b1e5 2022-11-01 stsp goto done;
6667 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6668 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6669 39cd0ff6 2019-07-12 stsp goto done;
6672 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6673 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6675 5c1e53bc 2019-07-28 stsp goto done;
6678 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6679 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6680 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6682 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6683 f0b75401 2019-08-03 stsp ct_path++;
6684 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6685 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6686 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6688 b50cabdf 2019-07-12 stsp goto done;
6692 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6693 210c2321 2022-11-01 stsp head_commit_id, NULL, worktree, author, committer,
6694 210c2321 2022-11-01 stsp (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6695 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6697 39cd0ff6 2019-07-12 stsp goto done;
6699 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6700 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6701 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6702 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6703 39cd0ff6 2019-07-12 stsp err = sync_err;
6705 39cd0ff6 2019-07-12 stsp if (fileindex)
6706 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6707 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6708 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6709 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6710 39cd0ff6 2019-07-12 stsp err = unlockerr;
6711 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6712 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6714 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6716 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6717 2a47b1e5 2022-11-01 stsp if (diff_path && unlink(diff_path) == -1 && err == NULL)
6718 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("unlink", diff_path);
6719 2a47b1e5 2022-11-01 stsp free(diff_path);
6720 2a47b1e5 2022-11-01 stsp if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6721 2a47b1e5 2022-11-01 stsp err == NULL)
6722 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
6723 c4296144 2019-05-09 stsp return err;
6726 8656d6c4 2019-05-20 stsp const char *
6727 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6729 8656d6c4 2019-05-20 stsp return ct->path;
6732 8656d6c4 2019-05-20 stsp unsigned int
6733 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6735 8656d6c4 2019-05-20 stsp return ct->status;
6738 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6739 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6740 818c7501 2019-07-11 stsp struct got_repository *repo;
6743 818c7501 2019-07-11 stsp static const struct got_error *
6744 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6746 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6747 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6748 818c7501 2019-07-11 stsp unsigned char status;
6749 818c7501 2019-07-11 stsp struct stat sb;
6750 818c7501 2019-07-11 stsp char *ondisk_path;
6752 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6753 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6754 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6755 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6757 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6759 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6761 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6762 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6763 818c7501 2019-07-11 stsp free(ondisk_path);
6765 818c7501 2019-07-11 stsp return err;
6767 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6768 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6769 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6770 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6772 818c7501 2019-07-11 stsp return NULL;
6775 818c7501 2019-07-11 stsp const struct got_error *
6776 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6777 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6778 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6779 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6781 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6782 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6783 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6784 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6785 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6786 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6787 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6789 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6790 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6791 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6793 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6795 818c7501 2019-07-11 stsp return err;
6797 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6799 818c7501 2019-07-11 stsp goto done;
6801 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6802 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6803 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6806 818c7501 2019-07-11 stsp goto done;
6808 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6810 818c7501 2019-07-11 stsp goto done;
6812 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6814 818c7501 2019-07-11 stsp goto done;
6816 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6818 818c7501 2019-07-11 stsp goto done;
6820 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6823 e51d7b55 2020-01-04 stsp goto done;
6825 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6827 818c7501 2019-07-11 stsp goto done;
6828 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6829 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6830 e51d7b55 2020-01-04 stsp goto done;
6833 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6834 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6836 818c7501 2019-07-11 stsp goto done;
6837 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6839 818c7501 2019-07-11 stsp goto done;
6841 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6843 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6845 818c7501 2019-07-11 stsp goto done;
6847 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6849 818c7501 2019-07-11 stsp goto done;
6851 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6852 818c7501 2019-07-11 stsp worktree->base_commit_id);
6854 818c7501 2019-07-11 stsp goto done;
6855 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6857 818c7501 2019-07-11 stsp goto done;
6859 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6861 818c7501 2019-07-11 stsp goto done;
6863 818c7501 2019-07-11 stsp free(fileindex_path);
6864 818c7501 2019-07-11 stsp free(tmp_branch_name);
6865 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6866 818c7501 2019-07-11 stsp free(branch_ref_name);
6867 818c7501 2019-07-11 stsp if (branch_ref)
6868 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6869 818c7501 2019-07-11 stsp if (wt_branch)
6870 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6871 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6872 818c7501 2019-07-11 stsp if (err) {
6873 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6874 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6875 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6877 818c7501 2019-07-11 stsp if (*tmp_branch) {
6878 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6879 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6881 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6882 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6883 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6885 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6887 818c7501 2019-07-11 stsp return err;
6890 818c7501 2019-07-11 stsp const struct got_error *
6891 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6892 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6893 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6894 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6896 818c7501 2019-07-11 stsp const struct got_error *err;
6897 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6898 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6899 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6900 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6901 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6903 818c7501 2019-07-11 stsp *commit_id = NULL;
6904 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6905 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6906 3e3a69f1 2019-07-25 stsp *branch = NULL;
6907 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6909 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6911 3e3a69f1 2019-07-25 stsp return err;
6913 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6915 f032f1f7 2019-08-04 stsp goto done;
6917 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6918 f032f1f7 2019-08-04 stsp &have_staged_files);
6919 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6920 f032f1f7 2019-08-04 stsp goto done;
6921 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6922 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6923 3e3a69f1 2019-07-25 stsp goto done;
6926 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6928 f032f1f7 2019-08-04 stsp goto done;
6930 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6932 818c7501 2019-07-11 stsp goto done;
6934 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6936 818c7501 2019-07-11 stsp goto done;
6938 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6940 818c7501 2019-07-11 stsp goto done;
6942 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6944 818c7501 2019-07-11 stsp goto done;
6946 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6947 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6949 818c7501 2019-07-11 stsp goto done;
6951 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6953 818c7501 2019-07-11 stsp goto done;
6955 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6957 818c7501 2019-07-11 stsp goto done;
6959 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6960 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6962 818c7501 2019-07-11 stsp goto done;
6964 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6966 818c7501 2019-07-11 stsp goto done;
6968 818c7501 2019-07-11 stsp free(commit_ref_name);
6969 818c7501 2019-07-11 stsp free(branch_ref_name);
6970 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6971 818c7501 2019-07-11 stsp if (commit_ref)
6972 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6973 818c7501 2019-07-11 stsp if (branch_ref)
6974 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6975 818c7501 2019-07-11 stsp if (err) {
6976 818c7501 2019-07-11 stsp free(*commit_id);
6977 818c7501 2019-07-11 stsp *commit_id = NULL;
6978 818c7501 2019-07-11 stsp if (*tmp_branch) {
6979 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6980 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6982 818c7501 2019-07-11 stsp if (*new_base_branch) {
6983 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6984 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6986 818c7501 2019-07-11 stsp if (*branch) {
6987 818c7501 2019-07-11 stsp got_ref_close(*branch);
6988 818c7501 2019-07-11 stsp *branch = NULL;
6990 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6991 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6992 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6994 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6996 818c7501 2019-07-11 stsp return err;
6999 818c7501 2019-07-11 stsp const struct got_error *
7000 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
7002 818c7501 2019-07-11 stsp const struct got_error *err;
7003 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
7005 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7007 818c7501 2019-07-11 stsp return err;
7009 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7010 818c7501 2019-07-11 stsp free(tmp_branch_name);
7011 818c7501 2019-07-11 stsp return NULL;
7014 818c7501 2019-07-11 stsp static const struct got_error *
7015 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7016 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
7018 0ebf8283 2019-07-24 stsp *logmsg = arg;
7019 818c7501 2019-07-11 stsp return NULL;
7022 818c7501 2019-07-11 stsp static const struct got_error *
7023 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7024 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7025 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7026 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7028 818c7501 2019-07-11 stsp return NULL;
7031 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
7032 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
7033 01757395 2019-07-12 stsp void *progress_arg;
7034 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
7037 01757395 2019-07-12 stsp static const struct got_error *
7038 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
7040 01757395 2019-07-12 stsp const struct got_error *err;
7041 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
7043 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
7045 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
7047 01757395 2019-07-12 stsp return err;
7049 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
7050 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
7051 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
7052 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
7053 01757395 2019-07-12 stsp return NULL;
7055 01757395 2019-07-12 stsp p = strdup(path);
7056 01757395 2019-07-12 stsp if (p == NULL)
7057 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
7059 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7060 01757395 2019-07-12 stsp if (err || new == NULL)
7062 01757395 2019-07-12 stsp return err;
7065 0ebf8283 2019-07-24 stsp static const struct got_error *
7066 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7067 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
7069 818c7501 2019-07-11 stsp const struct got_error *err;
7070 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
7072 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7073 818c7501 2019-07-11 stsp if (err) {
7074 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
7075 818c7501 2019-07-11 stsp goto done;
7076 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7078 818c7501 2019-07-11 stsp goto done;
7079 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
7081 818c7501 2019-07-11 stsp goto done;
7082 de05890f 2020-03-05 stsp } else if (is_rebase) {
7083 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
7086 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
7088 818c7501 2019-07-11 stsp goto done;
7089 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
7090 818c7501 2019-07-11 stsp free(stored_id);
7091 818c7501 2019-07-11 stsp if (cmp != 0) {
7092 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7093 818c7501 2019-07-11 stsp goto done;
7097 0ebf8283 2019-07-24 stsp if (commit_ref)
7098 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7099 0ebf8283 2019-07-24 stsp return err;
7102 0ebf8283 2019-07-24 stsp static const struct got_error *
7103 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
7104 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
7105 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7106 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
7107 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7108 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7110 0ebf8283 2019-07-24 stsp const struct got_error *err;
7111 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7112 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
7113 3e3a69f1 2019-07-25 stsp char *fileindex_path;
7115 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7117 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7119 0ebf8283 2019-07-24 stsp return err;
7121 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
7122 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
7123 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
7124 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
7125 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
7126 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
7127 818c7501 2019-07-11 stsp if (commit_ref)
7128 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
7129 818c7501 2019-07-11 stsp return err;
7132 818c7501 2019-07-11 stsp const struct got_error *
7133 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7134 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7135 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7136 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7137 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7138 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7140 0ebf8283 2019-07-24 stsp const struct got_error *err;
7141 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7143 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7145 0ebf8283 2019-07-24 stsp return err;
7147 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7149 0ebf8283 2019-07-24 stsp goto done;
7151 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7152 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7153 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7155 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7156 0ebf8283 2019-07-24 stsp return err;
7159 0ebf8283 2019-07-24 stsp const struct got_error *
7160 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7161 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7162 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7163 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7164 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7165 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7167 0ebf8283 2019-07-24 stsp const struct got_error *err;
7168 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7170 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7172 0ebf8283 2019-07-24 stsp return err;
7174 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7176 0ebf8283 2019-07-24 stsp goto done;
7178 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7179 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7180 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7182 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7183 0ebf8283 2019-07-24 stsp return err;
7186 0ebf8283 2019-07-24 stsp static const struct got_error *
7187 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
7188 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7189 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7190 598eac43 2022-07-22 stsp struct got_reference *tmp_branch, const char *committer,
7191 598eac43 2022-07-22 stsp struct got_commit_object *orig_commit, const char *new_logmsg,
7192 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
7194 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7195 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7196 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7197 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7198 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7199 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7200 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7202 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
7203 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7204 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7206 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7208 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7210 3e3a69f1 2019-07-25 stsp return err;
7212 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7213 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7214 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7215 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7216 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
7218 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7219 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7221 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7222 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7223 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7224 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7225 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7226 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7228 01757395 2019-07-12 stsp if (merged_paths) {
7229 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7230 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7231 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7232 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7235 01757395 2019-07-12 stsp goto done;
7238 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7239 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7241 01757395 2019-07-12 stsp goto done;
7244 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7245 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7246 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7248 a0e95631 2019-07-12 stsp goto done;
7249 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7250 a0e95631 2019-07-12 stsp goto done;
7253 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7255 edd02c5e 2019-07-11 stsp goto done;
7257 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7259 818c7501 2019-07-11 stsp goto done;
7261 5943eee2 2019-08-13 stsp if (new_logmsg) {
7262 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7263 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7264 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7265 5943eee2 2019-08-13 stsp goto done;
7268 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7270 5943eee2 2019-08-13 stsp goto done;
7273 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7274 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7275 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
7276 50e7a649 2022-07-22 stsp committer ? committer :
7277 2a47b1e5 2022-11-01 stsp got_object_commit_get_committer(orig_commit), NULL,
7278 50e7a649 2022-07-22 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7280 a0e95631 2019-07-12 stsp goto done;
7282 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7284 a0e95631 2019-07-12 stsp goto done;
7286 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7288 a0e95631 2019-07-12 stsp goto done;
7290 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7291 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7292 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7293 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7294 a0e95631 2019-07-12 stsp err = sync_err;
7296 a0e95631 2019-07-12 stsp free(fileindex_path);
7297 a0e95631 2019-07-12 stsp free(head_commit_id);
7298 a0e95631 2019-07-12 stsp if (head_ref)
7299 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7300 818c7501 2019-07-11 stsp if (err) {
7301 818c7501 2019-07-11 stsp free(*new_commit_id);
7302 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7304 818c7501 2019-07-11 stsp return err;
7307 818c7501 2019-07-11 stsp const struct got_error *
7308 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7309 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7310 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7311 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
7312 12383673 2023-02-18 mark struct got_object_id *orig_commit_id, int allow_conflict,
7313 12383673 2023-02-18 mark struct got_repository *repo)
7315 0ebf8283 2019-07-24 stsp const struct got_error *err;
7316 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7317 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7318 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7320 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7322 0ebf8283 2019-07-24 stsp return err;
7324 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7326 0ebf8283 2019-07-24 stsp goto done;
7327 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7329 0ebf8283 2019-07-24 stsp goto done;
7330 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7331 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7332 0ebf8283 2019-07-24 stsp goto done;
7335 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7336 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
7337 12383673 2023-02-18 mark NULL, allow_conflict, repo);
7339 0ebf8283 2019-07-24 stsp if (commit_ref)
7340 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7341 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7342 0ebf8283 2019-07-24 stsp free(commit_id);
7343 0ebf8283 2019-07-24 stsp return err;
7346 0ebf8283 2019-07-24 stsp const struct got_error *
7347 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7348 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7349 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7350 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
7351 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7352 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
7354 0ebf8283 2019-07-24 stsp const struct got_error *err;
7355 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7356 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7358 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7360 0ebf8283 2019-07-24 stsp return err;
7362 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7364 0ebf8283 2019-07-24 stsp goto done;
7366 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7367 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
7368 12383673 2023-02-18 mark new_logmsg, allow_conflict, repo);
7370 0ebf8283 2019-07-24 stsp if (commit_ref)
7371 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7372 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7373 0ebf8283 2019-07-24 stsp return err;
7376 0ebf8283 2019-07-24 stsp const struct got_error *
7377 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7378 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7380 3e3a69f1 2019-07-25 stsp if (fileindex)
7381 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7382 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7385 69844fba 2019-07-11 stsp static const struct got_error *
7386 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7388 69844fba 2019-07-11 stsp const struct got_error *err;
7389 69844fba 2019-07-11 stsp struct got_reference *ref;
7391 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7392 69844fba 2019-07-11 stsp if (err) {
7393 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7394 69844fba 2019-07-11 stsp return NULL;
7395 69844fba 2019-07-11 stsp return err;
7398 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7399 69844fba 2019-07-11 stsp got_ref_close(ref);
7400 69844fba 2019-07-11 stsp return err;
7403 69844fba 2019-07-11 stsp static const struct got_error *
7404 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7406 69844fba 2019-07-11 stsp const struct got_error *err;
7407 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7408 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7410 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7412 69844fba 2019-07-11 stsp goto done;
7413 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7415 69844fba 2019-07-11 stsp goto done;
7417 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7419 69844fba 2019-07-11 stsp goto done;
7420 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7422 69844fba 2019-07-11 stsp goto done;
7424 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7426 69844fba 2019-07-11 stsp goto done;
7427 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7429 69844fba 2019-07-11 stsp goto done;
7431 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7433 69844fba 2019-07-11 stsp goto done;
7434 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7436 69844fba 2019-07-11 stsp goto done;
7439 69844fba 2019-07-11 stsp free(tmp_branch_name);
7440 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7441 69844fba 2019-07-11 stsp free(branch_ref_name);
7442 69844fba 2019-07-11 stsp free(commit_ref_name);
7443 e600f124 2021-03-21 stsp return err;
7446 336075a4 2022-06-25 op static const struct got_error *
7447 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7448 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7450 e600f124 2021-03-21 stsp const struct got_error *err;
7451 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7452 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7453 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7454 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7455 e600f124 2021-03-21 stsp char *refname = NULL;
7457 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7458 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7459 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7460 e600f124 2021-03-21 stsp branch_name += 11;
7462 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7464 e600f124 2021-03-21 stsp return err;
7466 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7467 e600f124 2021-03-21 stsp new_id_str) == -1) {
7468 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7469 e600f124 2021-03-21 stsp goto done;
7472 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7474 e600f124 2021-03-21 stsp goto done;
7476 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7478 e600f124 2021-03-21 stsp goto done;
7480 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7482 e600f124 2021-03-21 stsp free(new_id_str);
7483 e600f124 2021-03-21 stsp free(refname);
7484 e600f124 2021-03-21 stsp free(old_commit_id);
7486 e600f124 2021-03-21 stsp got_ref_close(ref);
7487 69844fba 2019-07-11 stsp return err;
7490 818c7501 2019-07-11 stsp const struct got_error *
7491 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7492 ef85a376 2023-02-01 mark struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7493 ef85a376 2023-02-01 mark struct got_reference *rebased_branch, struct got_repository *repo,
7494 ef85a376 2023-02-01 mark int create_backup)
7496 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7497 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7498 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7500 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7502 818c7501 2019-07-11 stsp return err;
7504 e600f124 2021-03-21 stsp if (create_backup) {
7505 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7506 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7508 e600f124 2021-03-21 stsp goto done;
7511 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7513 818c7501 2019-07-11 stsp goto done;
7515 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7517 818c7501 2019-07-11 stsp goto done;
7519 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7521 818c7501 2019-07-11 stsp goto done;
7523 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7525 a615e0e7 2020-12-16 stsp goto done;
7527 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7529 a615e0e7 2020-12-16 stsp goto done;
7530 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7531 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7532 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7533 a615e0e7 2020-12-16 stsp err = sync_err;
7535 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7536 a615e0e7 2020-12-16 stsp free(fileindex_path);
7537 818c7501 2019-07-11 stsp free(new_head_commit_id);
7538 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7539 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7540 818c7501 2019-07-11 stsp err = unlockerr;
7541 818c7501 2019-07-11 stsp return err;
7544 af179be7 2023-04-14 stsp static const struct got_error *
7545 af179be7 2023-04-14 stsp get_paths_changed_between_commits(struct got_pathlist_head *paths,
7546 af179be7 2023-04-14 stsp struct got_object_id *id1, struct got_object_id *id2,
7547 af179be7 2023-04-14 stsp struct got_repository *repo)
7549 af179be7 2023-04-14 stsp const struct got_error *err;
7550 af179be7 2023-04-14 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7551 af179be7 2023-04-14 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7553 af179be7 2023-04-14 stsp if (id1) {
7554 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit1, repo, id1);
7556 af179be7 2023-04-14 stsp goto done;
7558 af179be7 2023-04-14 stsp err = got_object_open_as_tree(&tree1, repo,
7559 af179be7 2023-04-14 stsp got_object_commit_get_tree_id(commit1));
7561 af179be7 2023-04-14 stsp goto done;
7564 af179be7 2023-04-14 stsp if (id2) {
7565 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit2, repo, id2);
7567 af179be7 2023-04-14 stsp goto done;
7569 af179be7 2023-04-14 stsp err = got_object_open_as_tree(&tree2, repo,
7570 af179be7 2023-04-14 stsp got_object_commit_get_tree_id(commit2));
7572 af179be7 2023-04-14 stsp goto done;
7575 af179be7 2023-04-14 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7576 af179be7 2023-04-14 stsp got_diff_tree_collect_changed_paths, paths, 0);
7578 af179be7 2023-04-14 stsp goto done;
7580 af179be7 2023-04-14 stsp if (commit1)
7581 af179be7 2023-04-14 stsp got_object_commit_close(commit1);
7582 af179be7 2023-04-14 stsp if (commit2)
7583 af179be7 2023-04-14 stsp got_object_commit_close(commit2);
7584 af179be7 2023-04-14 stsp if (tree1)
7585 af179be7 2023-04-14 stsp got_object_tree_close(tree1);
7586 af179be7 2023-04-14 stsp if (tree2)
7587 af179be7 2023-04-14 stsp got_object_tree_close(tree2);
7588 af179be7 2023-04-14 stsp return err;
7591 af179be7 2023-04-14 stsp static const struct got_error *
7592 af179be7 2023-04-14 stsp get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7593 af179be7 2023-04-14 stsp struct got_object_id *id1, struct got_object_id *id2,
7594 af179be7 2023-04-14 stsp const char *path_prefix, struct got_repository *repo)
7596 af179be7 2023-04-14 stsp const struct got_error *err;
7597 af179be7 2023-04-14 stsp struct got_pathlist_head merged_paths;
7598 af179be7 2023-04-14 stsp struct got_pathlist_entry *pe;
7599 af179be7 2023-04-14 stsp char *abspath = NULL, *wt_path = NULL;
7601 af179be7 2023-04-14 stsp TAILQ_INIT(&merged_paths);
7603 af179be7 2023-04-14 stsp err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7605 af179be7 2023-04-14 stsp goto done;
7607 af179be7 2023-04-14 stsp TAILQ_FOREACH(pe, &merged_paths, entry) {
7608 af179be7 2023-04-14 stsp struct got_diff_changed_path *change = pe->data;
7610 af179be7 2023-04-14 stsp if (change->status != GOT_STATUS_ADD)
7613 af179be7 2023-04-14 stsp if (got_path_is_root_dir(path_prefix)) {
7614 af179be7 2023-04-14 stsp wt_path = strdup(pe->path);
7615 af179be7 2023-04-14 stsp if (wt_path == NULL) {
7616 af179be7 2023-04-14 stsp err = got_error_from_errno("strdup");
7617 af179be7 2023-04-14 stsp goto done;
7620 af179be7 2023-04-14 stsp if (asprintf(&abspath, "/%s", pe->path) == -1) {
7621 af179be7 2023-04-14 stsp err = got_error_from_errno("asprintf");
7622 af179be7 2023-04-14 stsp goto done;
7625 af179be7 2023-04-14 stsp err = got_path_skip_common_ancestor(&wt_path,
7626 af179be7 2023-04-14 stsp path_prefix, abspath);
7628 af179be7 2023-04-14 stsp goto done;
7629 af179be7 2023-04-14 stsp free(abspath);
7630 af179be7 2023-04-14 stsp abspath = NULL;
7633 af179be7 2023-04-14 stsp err = got_pathlist_append(added_paths, wt_path, NULL);
7635 af179be7 2023-04-14 stsp goto done;
7636 af179be7 2023-04-14 stsp wt_path = NULL;
7640 af179be7 2023-04-14 stsp got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7641 af179be7 2023-04-14 stsp free(abspath);
7642 af179be7 2023-04-14 stsp free(wt_path);
7643 af179be7 2023-04-14 stsp return err;
7646 af179be7 2023-04-14 stsp static const struct got_error *
7647 af179be7 2023-04-14 stsp get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7648 af179be7 2023-04-14 stsp struct got_object_id *id, const char *path_prefix,
7649 af179be7 2023-04-14 stsp struct got_repository *repo)
7651 af179be7 2023-04-14 stsp const struct got_error *err;
7652 af179be7 2023-04-14 stsp struct got_commit_object *commit = NULL;
7653 af179be7 2023-04-14 stsp struct got_object_qid *pid;
7655 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit, repo, id);
7657 af179be7 2023-04-14 stsp goto done;
7659 af179be7 2023-04-14 stsp pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7661 af179be7 2023-04-14 stsp err = get_paths_added_between_commits(added_paths,
7662 af179be7 2023-04-14 stsp pid ? &pid->id : NULL, id, path_prefix, repo);
7664 af179be7 2023-04-14 stsp goto done;
7666 af179be7 2023-04-14 stsp if (commit)
7667 af179be7 2023-04-14 stsp got_object_commit_close(commit);
7668 af179be7 2023-04-14 stsp return err;
7671 818c7501 2019-07-11 stsp const struct got_error *
7672 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7673 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7674 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7675 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7677 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7678 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7679 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7680 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
7681 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7682 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7683 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
7684 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
7685 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7686 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7687 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
7689 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
7691 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7693 818c7501 2019-07-11 stsp return err;
7695 af179be7 2023-04-14 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7697 af179be7 2023-04-14 stsp goto done;
7699 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7701 af179be7 2023-04-14 stsp goto done;
7703 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7705 af179be7 2023-04-14 stsp goto done;
7708 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
7709 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
7710 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files which were added by
7711 af179be7 2023-04-14 stsp * the user for conflict resolution purposes.
7713 af179be7 2023-04-14 stsp err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7714 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
7716 af179be7 2023-04-14 stsp goto done;
7718 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7719 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7721 818c7501 2019-07-11 stsp goto done;
7723 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7725 818c7501 2019-07-11 stsp goto done;
7728 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7729 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7730 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7732 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7734 818c7501 2019-07-11 stsp goto done;
7736 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7738 1b093d84 2023-04-12 stsp goto done;
7740 1b093d84 2023-04-12 stsp err = got_object_open_as_commit(&commit, repo,
7741 1b093d84 2023-04-12 stsp worktree->base_commit_id);
7743 818c7501 2019-07-11 stsp goto done;
7745 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7746 a44927cc 2022-04-07 stsp worktree->path_prefix);
7748 ca355955 2019-07-12 stsp goto done;
7750 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7752 ca355955 2019-07-12 stsp goto done;
7754 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7756 818c7501 2019-07-11 stsp goto done;
7758 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7759 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7760 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7761 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7762 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7763 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7764 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7765 af179be7 2023-04-14 stsp rfa.unlink_added_files = 1;
7766 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
7767 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7768 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7770 1f1abb7e 2019-08-08 stsp goto sync;
7772 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7773 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7775 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7776 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7777 ca355955 2019-07-12 stsp err = sync_err;
7779 af179be7 2023-04-14 stsp got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7780 818c7501 2019-07-11 stsp got_ref_close(resolved);
7781 a3a2faf2 2019-07-12 stsp free(tree_id);
7782 818c7501 2019-07-11 stsp free(commit_id);
7783 af179be7 2023-04-14 stsp free(merged_commit_id);
7784 a44927cc 2022-04-07 stsp if (commit)
7785 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7786 0ebf8283 2019-07-24 stsp if (fileindex)
7787 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7788 0ebf8283 2019-07-24 stsp free(fileindex_path);
7789 af179be7 2023-04-14 stsp free(commit_ref_name);
7790 af179be7 2023-04-14 stsp if (commit_ref)
7791 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
7793 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7794 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7795 0ebf8283 2019-07-24 stsp err = unlockerr;
7796 0ebf8283 2019-07-24 stsp return err;
7799 0ebf8283 2019-07-24 stsp const struct got_error *
7800 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7801 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7802 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7803 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7805 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7806 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7807 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7808 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7809 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7810 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7811 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7812 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7814 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7815 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7816 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7817 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7819 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7821 0ebf8283 2019-07-24 stsp return err;
7823 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7825 0ebf8283 2019-07-24 stsp goto done;
7827 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7828 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7829 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7832 0ebf8283 2019-07-24 stsp goto done;
7834 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7836 0ebf8283 2019-07-24 stsp goto done;
7838 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7840 0ebf8283 2019-07-24 stsp goto done;
7842 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7843 0ebf8283 2019-07-24 stsp worktree);
7845 0ebf8283 2019-07-24 stsp goto done;
7847 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7850 0ebf8283 2019-07-24 stsp goto done;
7852 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7854 0ebf8283 2019-07-24 stsp goto done;
7856 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7858 0ebf8283 2019-07-24 stsp goto done;
7860 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7861 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7863 0ebf8283 2019-07-24 stsp goto done;
7864 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7866 0ebf8283 2019-07-24 stsp goto done;
7867 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7868 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7869 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7870 0ebf8283 2019-07-24 stsp goto done;
7873 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7874 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7876 0ebf8283 2019-07-24 stsp goto done;
7877 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7879 0ebf8283 2019-07-24 stsp goto done;
7881 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7883 0ebf8283 2019-07-24 stsp goto done;
7885 0ebf8283 2019-07-24 stsp free(fileindex_path);
7886 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7887 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7888 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7889 0ebf8283 2019-07-24 stsp if (wt_branch)
7890 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7891 0ebf8283 2019-07-24 stsp if (err) {
7892 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7893 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7894 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7896 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7897 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7898 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7900 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7901 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7902 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7903 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7905 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7907 0ebf8283 2019-07-24 stsp return err;
7910 0ebf8283 2019-07-24 stsp const struct got_error *
7911 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7912 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7914 3e3a69f1 2019-07-25 stsp if (fileindex)
7915 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7916 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7919 0ebf8283 2019-07-24 stsp const struct got_error *
7920 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7921 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7923 0ebf8283 2019-07-24 stsp const struct got_error *err;
7924 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7926 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7928 0ebf8283 2019-07-24 stsp return err;
7930 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7931 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7932 0ebf8283 2019-07-24 stsp return NULL;
7935 0ebf8283 2019-07-24 stsp const struct got_error *
7936 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7937 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7938 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7939 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7941 0ebf8283 2019-07-24 stsp const struct got_error *err;
7942 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7943 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7944 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7945 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7946 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7947 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7949 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7950 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7951 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7952 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7954 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7956 3e3a69f1 2019-07-25 stsp return err;
7958 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7960 f032f1f7 2019-08-04 stsp goto done;
7962 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7963 f032f1f7 2019-08-04 stsp &have_staged_files);
7964 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7965 f032f1f7 2019-08-04 stsp goto done;
7966 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7967 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7968 3e3a69f1 2019-07-25 stsp goto done;
7971 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7973 f032f1f7 2019-08-04 stsp goto done;
7975 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7977 0ebf8283 2019-07-24 stsp goto done;
7979 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7981 0ebf8283 2019-07-24 stsp goto done;
7983 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7984 0ebf8283 2019-07-24 stsp worktree);
7986 0ebf8283 2019-07-24 stsp goto done;
7988 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7990 0ebf8283 2019-07-24 stsp goto done;
7992 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7994 0ebf8283 2019-07-24 stsp goto done;
7995 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7997 0ebf8283 2019-07-24 stsp goto done;
7999 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
8001 0ebf8283 2019-07-24 stsp goto done;
8002 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
8004 0ebf8283 2019-07-24 stsp goto done;
8006 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
8008 0ebf8283 2019-07-24 stsp goto done;
8010 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8011 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8012 3e3a69f1 2019-07-25 stsp free(fileindex_path);
8013 0ebf8283 2019-07-24 stsp if (commit_ref)
8014 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
8015 0ebf8283 2019-07-24 stsp if (base_commit_ref)
8016 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
8017 0ebf8283 2019-07-24 stsp if (err) {
8018 0ebf8283 2019-07-24 stsp free(*commit_id);
8019 0ebf8283 2019-07-24 stsp *commit_id = NULL;
8020 0ebf8283 2019-07-24 stsp free(*base_commit_id);
8021 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
8022 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
8023 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
8024 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
8026 3e3a69f1 2019-07-25 stsp if (*fileindex) {
8027 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
8028 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
8030 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
8032 0ebf8283 2019-07-24 stsp return err;
8035 0ebf8283 2019-07-24 stsp static const struct got_error *
8036 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8038 0ebf8283 2019-07-24 stsp const struct got_error *err;
8039 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8040 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
8042 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8044 0ebf8283 2019-07-24 stsp goto done;
8045 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
8047 0ebf8283 2019-07-24 stsp goto done;
8049 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8050 0ebf8283 2019-07-24 stsp worktree);
8052 0ebf8283 2019-07-24 stsp goto done;
8053 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
8055 0ebf8283 2019-07-24 stsp goto done;
8057 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8059 0ebf8283 2019-07-24 stsp goto done;
8060 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
8062 0ebf8283 2019-07-24 stsp goto done;
8064 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8066 0ebf8283 2019-07-24 stsp goto done;
8067 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8069 0ebf8283 2019-07-24 stsp goto done;
8071 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
8072 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
8073 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8074 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8075 0ebf8283 2019-07-24 stsp return err;
8078 0ebf8283 2019-07-24 stsp const struct got_error *
8079 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
8080 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8081 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
8082 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8084 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8085 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8086 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
8087 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
8088 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8089 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
8090 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
8091 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
8092 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
8093 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
8095 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
8097 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
8099 0ebf8283 2019-07-24 stsp return err;
8101 af179be7 2023-04-14 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8103 af179be7 2023-04-14 stsp goto done;
8105 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8106 af179be7 2023-04-14 stsp if (err) {
8107 af179be7 2023-04-14 stsp if (err->code != GOT_ERR_NOT_REF)
8108 af179be7 2023-04-14 stsp goto done;
8109 af179be7 2023-04-14 stsp /* Can happen on early abort due to invalid histedit script. */
8110 af179be7 2023-04-14 stsp commit_ref = NULL;
8113 af179be7 2023-04-14 stsp if (commit_ref) {
8114 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8116 af179be7 2023-04-14 stsp goto done;
8119 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
8120 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
8121 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files added by the
8122 af179be7 2023-04-14 stsp * user during conflict resolution or during histedit -e.
8124 af179be7 2023-04-14 stsp err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8125 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
8127 af179be7 2023-04-14 stsp goto done;
8130 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8131 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
8133 0ebf8283 2019-07-24 stsp goto done;
8135 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8137 0ebf8283 2019-07-24 stsp goto done;
8139 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8141 0ebf8283 2019-07-24 stsp goto done;
8143 1b093d84 2023-04-12 stsp err = got_object_open_as_commit(&commit, repo,
8144 1b093d84 2023-04-12 stsp worktree->base_commit_id);
8146 1b093d84 2023-04-12 stsp goto done;
8148 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8149 0ebf8283 2019-07-24 stsp worktree->path_prefix);
8151 0ebf8283 2019-07-24 stsp goto done;
8153 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8155 0ebf8283 2019-07-24 stsp goto done;
8157 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
8159 0ebf8283 2019-07-24 stsp goto done;
8161 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
8162 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
8163 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
8164 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
8165 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
8166 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
8167 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
8168 af179be7 2023-04-14 stsp rfa.unlink_added_files = 1;
8169 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
8170 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8171 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8173 1f1abb7e 2019-08-08 stsp goto sync;
8175 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8176 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8178 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8179 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
8180 0ebf8283 2019-07-24 stsp err = sync_err;
8182 af179be7 2023-04-14 stsp if (resolved)
8183 af179be7 2023-04-14 stsp got_ref_close(resolved);
8184 af179be7 2023-04-14 stsp if (commit_ref)
8185 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
8186 af179be7 2023-04-14 stsp free(merged_commit_id);
8187 0ebf8283 2019-07-24 stsp free(tree_id);
8188 818c7501 2019-07-11 stsp free(fileindex_path);
8189 af179be7 2023-04-14 stsp free(commit_ref_name);
8191 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8192 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
8193 818c7501 2019-07-11 stsp err = unlockerr;
8194 818c7501 2019-07-11 stsp return err;
8197 0ebf8283 2019-07-24 stsp const struct got_error *
8198 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8199 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8200 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8202 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8203 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8204 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8205 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8207 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8209 0ebf8283 2019-07-24 stsp return err;
8211 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8212 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8214 e600f124 2021-03-21 stsp goto done;
8216 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8217 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8219 0ebf8283 2019-07-24 stsp goto done;
8221 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8223 0ebf8283 2019-07-24 stsp goto done;
8225 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
8227 0ebf8283 2019-07-24 stsp goto done;
8229 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8231 0ebf8283 2019-07-24 stsp goto done;
8233 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8235 a615e0e7 2020-12-16 stsp goto done;
8237 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8239 a615e0e7 2020-12-16 stsp goto done;
8240 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8241 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8242 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8243 a615e0e7 2020-12-16 stsp err = sync_err;
8245 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8246 a615e0e7 2020-12-16 stsp free(fileindex_path);
8247 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8248 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8249 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8250 0ebf8283 2019-07-24 stsp err = unlockerr;
8251 0ebf8283 2019-07-24 stsp return err;
8254 0ebf8283 2019-07-24 stsp const struct got_error *
8255 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8256 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8258 0ebf8283 2019-07-24 stsp const struct got_error *err;
8259 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8261 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8263 0ebf8283 2019-07-24 stsp return err;
8265 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8267 0ebf8283 2019-07-24 stsp goto done;
8269 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8271 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8272 2822a352 2019-10-15 stsp return err;
8275 2822a352 2019-10-15 stsp const struct got_error *
8276 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8277 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8278 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8279 2822a352 2019-10-15 stsp struct got_repository *repo)
8281 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8282 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8283 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8285 2822a352 2019-10-15 stsp *fileindex = NULL;
8286 2822a352 2019-10-15 stsp *branch_ref = NULL;
8287 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8289 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8291 2822a352 2019-10-15 stsp return err;
8293 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8294 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8295 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8296 2822a352 2019-10-15 stsp "update -b or different branch name required");
8297 2822a352 2019-10-15 stsp goto done;
8300 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8302 2822a352 2019-10-15 stsp goto done;
8304 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8305 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8306 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8307 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8310 2822a352 2019-10-15 stsp goto done;
8312 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8314 2822a352 2019-10-15 stsp goto done;
8316 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8317 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8319 2822a352 2019-10-15 stsp if (err) {
8320 2822a352 2019-10-15 stsp if (*branch_ref) {
8321 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8322 2822a352 2019-10-15 stsp *branch_ref = NULL;
8324 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8325 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8326 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8328 2822a352 2019-10-15 stsp if (*fileindex) {
8329 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8330 2822a352 2019-10-15 stsp *fileindex = NULL;
8332 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8334 0cb83759 2019-08-03 stsp return err;
8337 2822a352 2019-10-15 stsp const struct got_error *
8338 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8339 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8340 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8341 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8342 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8344 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8345 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8346 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8347 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8349 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8351 2822a352 2019-10-15 stsp goto done;
8353 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8355 2822a352 2019-10-15 stsp goto done;
8357 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8359 a44927cc 2022-04-07 stsp goto done;
8361 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8362 2822a352 2019-10-15 stsp worktree->path_prefix);
8364 2822a352 2019-10-15 stsp goto done;
8366 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8368 2822a352 2019-10-15 stsp goto done;
8370 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8371 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8373 2822a352 2019-10-15 stsp goto sync;
8375 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8377 2822a352 2019-10-15 stsp goto sync;
8379 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8381 6e210706 2021-01-22 stsp goto sync;
8383 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8385 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8386 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8387 2822a352 2019-10-15 stsp err = sync_err;
8390 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8391 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8392 2822a352 2019-10-15 stsp err = unlockerr;
8393 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8395 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8396 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8397 2822a352 2019-10-15 stsp err = unlockerr;
8398 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8400 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8401 2822a352 2019-10-15 stsp free(fileindex_path);
8402 2822a352 2019-10-15 stsp free(tree_id);
8403 a44927cc 2022-04-07 stsp if (commit)
8404 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8406 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8407 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8408 2822a352 2019-10-15 stsp err = unlockerr;
8409 2822a352 2019-10-15 stsp return err;
8412 2822a352 2019-10-15 stsp const struct got_error *
8413 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8414 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8415 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8417 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8419 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8421 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8423 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8424 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8425 8b692cd0 2019-10-21 stsp err = unlockerr;
8426 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8428 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8429 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8430 8b692cd0 2019-10-21 stsp err = unlockerr;
8431 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8433 f259c4c1 2021-09-24 stsp return err;
8436 f259c4c1 2021-09-24 stsp const struct got_error *
8437 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
8438 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
8440 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
8441 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8443 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8445 f259c4c1 2021-09-24 stsp goto done;
8447 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8449 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
8450 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8451 f259c4c1 2021-09-24 stsp err = sync_err;
8453 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8454 f259c4c1 2021-09-24 stsp free(fileindex_path);
8455 8b692cd0 2019-10-21 stsp return err;
8458 f259c4c1 2021-09-24 stsp static const struct got_error *
8459 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8461 f259c4c1 2021-09-24 stsp const struct got_error *err;
8462 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
8464 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8466 f259c4c1 2021-09-24 stsp goto done;
8467 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
8469 f259c4c1 2021-09-24 stsp goto done;
8471 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8473 f259c4c1 2021-09-24 stsp goto done;
8474 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
8476 f259c4c1 2021-09-24 stsp goto done;
8479 f259c4c1 2021-09-24 stsp free(branch_refname);
8480 f259c4c1 2021-09-24 stsp free(commit_refname);
8481 f259c4c1 2021-09-24 stsp return err;
8484 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
8485 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
8486 f259c4c1 2021-09-24 stsp const char *branch_name;
8489 f259c4c1 2021-09-24 stsp static const struct got_error *
8490 2a47b1e5 2022-11-01 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8491 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
8493 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
8495 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8496 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
8497 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
8499 f259c4c1 2021-09-24 stsp return NULL;
8503 f259c4c1 2021-09-24 stsp const struct got_error *
8504 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
8505 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
8506 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
8507 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
8508 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8509 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8511 f259c4c1 2021-09-24 stsp const struct got_error *err;
8512 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8513 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args cma;
8515 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8517 f259c4c1 2021-09-24 stsp goto done;
8519 cdbfe5d2 2023-07-24 stsp cma.worktree = worktree;
8520 cdbfe5d2 2023-07-24 stsp cma.cancel_cb = cancel_cb;
8521 cdbfe5d2 2023-07-24 stsp cma.cancel_arg = cancel_arg;
8523 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8526 f259c4c1 2021-09-24 stsp goto done;
8528 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8529 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
8530 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
8532 f259c4c1 2021-09-24 stsp free(fileindex_path);
8533 f259c4c1 2021-09-24 stsp return err;
8536 f259c4c1 2021-09-24 stsp const struct got_error *
8537 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
8538 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8539 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
8540 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
8541 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo,
8542 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
8545 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
8546 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
8547 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
8548 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
8549 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
8550 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
8551 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8552 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
8553 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8555 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
8556 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
8558 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
8560 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8562 f259c4c1 2021-09-24 stsp goto done;
8564 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8566 f259c4c1 2021-09-24 stsp goto done;
8568 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8570 f259c4c1 2021-09-24 stsp goto done;
8572 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8573 f259c4c1 2021-09-24 stsp &have_staged_files);
8574 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8575 f259c4c1 2021-09-24 stsp goto done;
8576 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8577 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8578 f259c4c1 2021-09-24 stsp goto done;
8581 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
8582 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
8583 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
8584 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
8585 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
8586 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8587 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
8588 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8589 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8591 f259c4c1 2021-09-24 stsp goto done;
8593 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
8594 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
8595 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
8596 2a47b1e5 2022-11-01 stsp head_commit_id, branch_tip, worktree, author, committer, NULL,
8597 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8599 f259c4c1 2021-09-24 stsp goto done;
8601 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
8602 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
8603 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8604 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8605 f259c4c1 2021-09-24 stsp err = sync_err;
8607 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
8608 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
8610 f259c4c1 2021-09-24 stsp free_commitable(ct);
8612 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8613 f259c4c1 2021-09-24 stsp free(fileindex_path);
8614 f259c4c1 2021-09-24 stsp return err;
8617 f259c4c1 2021-09-24 stsp const struct got_error *
8618 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
8619 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
8621 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8622 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8624 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8626 f259c4c1 2021-09-24 stsp goto done;
8628 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8630 f259c4c1 2021-09-24 stsp goto done;
8631 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8632 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8633 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8634 f259c4c1 2021-09-24 stsp err = sync_err;
8636 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8637 f259c4c1 2021-09-24 stsp free(fileindex_path);
8638 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8639 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8640 f259c4c1 2021-09-24 stsp err = unlockerr;
8641 f259c4c1 2021-09-24 stsp return err;
8644 f259c4c1 2021-09-24 stsp const struct got_error *
8645 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8646 f259c4c1 2021-09-24 stsp struct got_repository *repo)
8648 f259c4c1 2021-09-24 stsp const struct got_error *err;
8649 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
8650 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
8652 f259c4c1 2021-09-24 stsp *in_progress = 0;
8654 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8656 f259c4c1 2021-09-24 stsp return err;
8657 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8658 793fcac3 2021-09-24 stsp free(branch_refname);
8659 f259c4c1 2021-09-24 stsp if (err) {
8660 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
8661 f259c4c1 2021-09-24 stsp return err;
8663 f259c4c1 2021-09-24 stsp *in_progress = 1;
8665 f259c4c1 2021-09-24 stsp return NULL;
8668 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
8669 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
8670 179f9db0 2023-06-20 falsifian struct got_repository *repo)
8672 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
8673 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8674 179f9db0 2023-06-20 falsifian struct got_reference *wt_branch = NULL;
8675 179f9db0 2023-06-20 falsifian struct got_object_id *wt_branch_tip = NULL;
8676 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
8678 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8680 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8682 f259c4c1 2021-09-24 stsp return err;
8684 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8686 f259c4c1 2021-09-24 stsp goto done;
8688 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
8689 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
8690 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
8691 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8694 f259c4c1 2021-09-24 stsp goto done;
8696 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8699 f259c4c1 2021-09-24 stsp goto done;
8701 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8703 f259c4c1 2021-09-24 stsp goto done;
8705 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8706 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8707 f259c4c1 2021-09-24 stsp goto done;
8710 179f9db0 2023-06-20 falsifian done:
8711 179f9db0 2023-06-20 falsifian free(fileindex_path);
8712 179f9db0 2023-06-20 falsifian if (wt_branch)
8713 179f9db0 2023-06-20 falsifian got_ref_close(wt_branch);
8714 179f9db0 2023-06-20 falsifian free(wt_branch_tip);
8715 179f9db0 2023-06-20 falsifian if (err) {
8716 179f9db0 2023-06-20 falsifian if (*fileindex) {
8717 179f9db0 2023-06-20 falsifian got_fileindex_free(*fileindex);
8718 179f9db0 2023-06-20 falsifian *fileindex = NULL;
8720 179f9db0 2023-06-20 falsifian lock_worktree(worktree, LOCK_SH);
8722 179f9db0 2023-06-20 falsifian return err;
8725 179f9db0 2023-06-20 falsifian const struct got_error *got_worktree_merge_write_refs(
8726 179f9db0 2023-06-20 falsifian struct got_worktree *worktree, struct got_reference *branch,
8727 179f9db0 2023-06-20 falsifian struct got_repository *repo)
8729 179f9db0 2023-06-20 falsifian const struct got_error *err = NULL;
8730 179f9db0 2023-06-20 falsifian char *branch_refname = NULL, *commit_refname = NULL;
8731 179f9db0 2023-06-20 falsifian struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8732 179f9db0 2023-06-20 falsifian struct got_object_id *branch_tip = NULL;
8734 179f9db0 2023-06-20 falsifian err = get_merge_branch_ref_name(&branch_refname, worktree);
8735 179f9db0 2023-06-20 falsifian if (err)
8736 179f9db0 2023-06-20 falsifian return err;
8738 179f9db0 2023-06-20 falsifian err = get_merge_commit_ref_name(&commit_refname, worktree);
8739 179f9db0 2023-06-20 falsifian if (err)
8740 179f9db0 2023-06-20 falsifian return err;
8742 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
8744 f259c4c1 2021-09-24 stsp goto done;
8746 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8748 f259c4c1 2021-09-24 stsp goto done;
8749 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
8751 f259c4c1 2021-09-24 stsp goto done;
8753 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8755 f259c4c1 2021-09-24 stsp goto done;
8756 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
8758 f259c4c1 2021-09-24 stsp goto done;
8761 f259c4c1 2021-09-24 stsp free(branch_refname);
8762 f259c4c1 2021-09-24 stsp free(commit_refname);
8763 f259c4c1 2021-09-24 stsp if (branch_ref)
8764 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8765 f259c4c1 2021-09-24 stsp if (commit_ref)
8766 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8767 179f9db0 2023-06-20 falsifian free(branch_tip);
8768 f259c4c1 2021-09-24 stsp return err;
8771 f259c4c1 2021-09-24 stsp const struct got_error *
8772 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
8773 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8774 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8776 f259c4c1 2021-09-24 stsp const struct got_error *err;
8777 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
8778 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8779 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8780 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8782 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8783 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8784 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8786 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8788 f259c4c1 2021-09-24 stsp return err;
8790 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8792 f259c4c1 2021-09-24 stsp goto done;
8794 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8795 f259c4c1 2021-09-24 stsp &have_staged_files);
8796 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8797 f259c4c1 2021-09-24 stsp goto done;
8798 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8799 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
8800 f259c4c1 2021-09-24 stsp goto done;
8803 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8805 f259c4c1 2021-09-24 stsp goto done;
8807 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8809 f259c4c1 2021-09-24 stsp goto done;
8811 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8813 f259c4c1 2021-09-24 stsp goto done;
8815 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
8816 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8817 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
8818 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
8819 f259c4c1 2021-09-24 stsp goto done;
8821 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8822 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
8823 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
8824 f259c4c1 2021-09-24 stsp goto done;
8827 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8829 f259c4c1 2021-09-24 stsp goto done;
8831 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
8833 f259c4c1 2021-09-24 stsp goto done;
8835 f259c4c1 2021-09-24 stsp free(commit_refname);
8836 f259c4c1 2021-09-24 stsp free(branch_refname);
8837 f259c4c1 2021-09-24 stsp free(fileindex_path);
8838 f259c4c1 2021-09-24 stsp if (commit_ref)
8839 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8840 f259c4c1 2021-09-24 stsp if (branch_ref)
8841 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8842 f259c4c1 2021-09-24 stsp if (err) {
8843 f259c4c1 2021-09-24 stsp if (*branch_name) {
8844 f259c4c1 2021-09-24 stsp free(*branch_name);
8845 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8847 f259c4c1 2021-09-24 stsp free(*branch_tip);
8848 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8849 f259c4c1 2021-09-24 stsp if (*fileindex) {
8850 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8851 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8853 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8855 f259c4c1 2021-09-24 stsp return err;
8858 f259c4c1 2021-09-24 stsp const struct got_error *
8859 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
8860 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8861 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8863 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8864 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8865 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8866 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
8867 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
8868 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
8869 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
8870 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
8871 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
8873 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
8875 af179be7 2023-04-14 stsp err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8877 af179be7 2023-04-14 stsp goto done;
8879 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8881 af179be7 2023-04-14 stsp goto done;
8883 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8885 af179be7 2023-04-14 stsp goto done;
8888 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
8889 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
8890 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files which were added by
8891 af179be7 2023-04-14 stsp * the user for conflict resolution purposes.
8893 af179be7 2023-04-14 stsp err = get_paths_added_between_commits(&added_paths,
8894 af179be7 2023-04-14 stsp got_worktree_get_base_commit_id(worktree), merged_commit_id,
8895 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
8897 af179be7 2023-04-14 stsp goto done;
8900 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
8901 a44927cc 2022-04-07 stsp worktree->base_commit_id);
8903 a44927cc 2022-04-07 stsp goto done;
8905 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8906 a44927cc 2022-04-07 stsp worktree->path_prefix);
8908 f259c4c1 2021-09-24 stsp goto done;
8910 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8912 f259c4c1 2021-09-24 stsp goto done;
8914 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8916 f259c4c1 2021-09-24 stsp goto done;
8918 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
8919 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
8920 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
8921 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
8922 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
8923 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
8924 f259c4c1 2021-09-24 stsp rfa.repo = repo;
8925 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
8926 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
8927 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8928 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8930 f259c4c1 2021-09-24 stsp goto sync;
8932 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8933 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8935 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8936 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8937 f259c4c1 2021-09-24 stsp err = sync_err;
8939 f259c4c1 2021-09-24 stsp free(tree_id);
8940 af179be7 2023-04-14 stsp free(merged_commit_id);
8941 a44927cc 2022-04-07 stsp if (commit)
8942 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8943 f259c4c1 2021-09-24 stsp if (fileindex)
8944 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8945 f259c4c1 2021-09-24 stsp free(fileindex_path);
8946 af179be7 2023-04-14 stsp if (commit_ref)
8947 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
8948 af179be7 2023-04-14 stsp free(commit_ref_name);
8950 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8951 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8952 f259c4c1 2021-09-24 stsp err = unlockerr;
8953 f259c4c1 2021-09-24 stsp return err;
8956 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8957 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8958 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8959 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8960 2db2652d 2019-08-07 stsp struct got_repository *repo;
8961 2db2652d 2019-08-07 stsp int have_changes;
8964 336075a4 2022-06-25 op static const struct got_error *
8965 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8966 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8967 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8968 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8970 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8971 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8972 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8973 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8974 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8975 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8977 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8978 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8979 8b13ce36 2019-08-08 stsp return NULL;
8980 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8981 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8983 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8984 735ef5ac 2019-08-03 stsp if (ie == NULL)
8985 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8987 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8988 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8989 735ef5ac 2019-08-03 stsp relpath) == -1)
8990 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8992 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8993 b4b2adf5 2023-02-09 op base_commit_idp = got_fileindex_entry_get_commit_id(
8994 b4b2adf5 2023-02-09 op &base_commit_id, ie);
8997 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8998 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8999 3aa5969e 2019-08-06 stsp goto done;
9000 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
9001 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
9002 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
9003 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9004 735ef5ac 2019-08-03 stsp goto done;
9007 2db2652d 2019-08-07 stsp a->have_changes = 1;
9009 735ef5ac 2019-08-03 stsp p = in_repo_path;
9010 735ef5ac 2019-08-03 stsp while (p[0] == '/')
9012 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
9013 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
9014 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
9016 735ef5ac 2019-08-03 stsp free(in_repo_path);
9017 dc424a06 2019-08-07 stsp return err;
9020 2db2652d 2019-08-07 stsp struct stage_path_arg {
9021 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
9022 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
9023 2db2652d 2019-08-07 stsp struct got_repository *repo;
9024 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
9025 2db2652d 2019-08-07 stsp void *status_arg;
9026 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
9027 2db2652d 2019-08-07 stsp void *patch_arg;
9028 7b5dc508 2019-10-28 stsp int staged_something;
9029 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
9032 2db2652d 2019-08-07 stsp static const struct got_error *
9033 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
9034 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
9035 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9036 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9038 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
9039 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
9040 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
9041 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
9042 0cb83759 2019-08-03 stsp uint32_t stage;
9043 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
9044 0aeb8099 2020-07-23 stsp struct stat sb;
9046 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
9047 8b13ce36 2019-08-08 stsp return NULL;
9049 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9050 d3e7c587 2019-08-03 stsp if (ie == NULL)
9051 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9053 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9054 2db2652d 2019-08-07 stsp relpath)== -1)
9055 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
9057 0cb83759 2019-08-03 stsp switch (status) {
9058 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
9059 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
9060 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
9061 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
9062 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
9065 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9066 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
9067 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9068 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9069 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
9072 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
9075 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
9076 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
9077 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
9078 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
9079 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
9083 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
9084 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
9087 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9088 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
9089 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9090 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
9092 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
9093 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9094 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
9095 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
9096 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
9097 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
9098 35213c7c 2020-07-23 stsp ssize_t target_len;
9099 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
9100 35213c7c 2020-07-23 stsp sizeof(target_path));
9101 35213c7c 2020-07-23 stsp if (target_len == -1) {
9102 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
9103 35213c7c 2020-07-23 stsp ondisk_path);
9106 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
9107 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
9108 df6221c7 2023-07-19 stsp a->worktree->root_path,
9109 df6221c7 2023-07-19 stsp a->worktree->meta_dir);
9112 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
9113 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
9114 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
9118 35213c7c 2020-07-23 stsp if (is_bad_symlink)
9119 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9120 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
9122 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9123 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
9125 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9126 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
9128 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9129 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9131 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9132 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
9133 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
9137 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
9138 9fdde394 2022-06-04 op * implicitly unstage the file.
9140 9fdde394 2022-06-04 op if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9141 9fdde394 2022-06-04 op sizeof(ie->blob_sha1)) == 0) {
9142 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
9143 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
9146 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
9147 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
9149 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9150 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9151 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
9152 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
9155 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9157 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9158 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9162 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
9163 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9164 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9165 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9167 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9168 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9171 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
9173 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
9174 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9176 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
9177 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
9180 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9184 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
9185 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
9186 dc424a06 2019-08-07 stsp free(path_content);
9187 2db2652d 2019-08-07 stsp free(ondisk_path);
9188 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
9189 0ebf8283 2019-07-24 stsp return err;
9192 0cb83759 2019-08-03 stsp const struct got_error *
9193 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
9194 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
9195 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
9196 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9197 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
9199 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9200 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9201 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9202 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9203 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9204 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9205 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9206 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9208 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9210 0cb83759 2019-08-03 stsp return err;
9212 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9213 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9215 735ef5ac 2019-08-03 stsp goto done;
9216 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9218 735ef5ac 2019-08-03 stsp goto done;
9219 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9221 0cb83759 2019-08-03 stsp goto done;
9223 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9224 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9225 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9226 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9227 2db2652d 2019-08-07 stsp oka.repo = repo;
9228 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9229 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9230 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9231 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
9233 735ef5ac 2019-08-03 stsp goto done;
9235 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9236 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9237 2db2652d 2019-08-07 stsp goto done;
9240 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9241 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9242 2db2652d 2019-08-07 stsp spa.repo = repo;
9243 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9244 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9245 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9246 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9247 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9248 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9249 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9250 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9251 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
9253 2db2652d 2019-08-07 stsp goto done;
9255 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9256 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9257 7b5dc508 2019-10-28 stsp goto done;
9260 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9261 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9262 0cb83759 2019-08-03 stsp err = sync_err;
9264 735ef5ac 2019-08-03 stsp if (head_ref)
9265 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9266 735ef5ac 2019-08-03 stsp free(head_commit_id);
9267 0cb83759 2019-08-03 stsp free(fileindex_path);
9268 0cb83759 2019-08-03 stsp if (fileindex)
9269 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9270 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9271 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9272 0cb83759 2019-08-03 stsp err = unlockerr;
9273 0cb83759 2019-08-03 stsp return err;
9276 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9277 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9278 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9279 ad493afc 2019-08-03 stsp struct got_repository *repo;
9280 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9281 ad493afc 2019-08-03 stsp void *progress_arg;
9282 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9283 2e1f37b0 2019-08-08 stsp void *patch_arg;
9286 2e1f37b0 2019-08-08 stsp static const struct got_error *
9287 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9288 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9289 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9290 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9291 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9293 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9294 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9295 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9296 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9297 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9298 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9299 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9300 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
9302 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9303 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9305 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9307 2e1f37b0 2019-08-08 stsp return err;
9309 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9310 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9311 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9312 eb81bc23 2022-06-28 tracey goto done;
9314 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9315 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9316 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9317 eb81bc23 2022-06-28 tracey goto done;
9320 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9322 2e1f37b0 2019-08-08 stsp goto done;
9324 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9326 2e1f37b0 2019-08-08 stsp goto done;
9328 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9330 2e1f37b0 2019-08-08 stsp goto done;
9332 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9335 2e1f37b0 2019-08-08 stsp goto done;
9337 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9339 2e1f37b0 2019-08-08 stsp goto done;
9341 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9343 2e1f37b0 2019-08-08 stsp goto done;
9345 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9346 4b752015 2022-06-30 stsp path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9348 2e1f37b0 2019-08-08 stsp goto done;
9350 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9351 b90054ed 2022-11-01 stsp "got-unstaged-content", "");
9353 2e1f37b0 2019-08-08 stsp goto done;
9354 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9355 b90054ed 2022-11-01 stsp "got-new-staged-content", "");
9357 2e1f37b0 2019-08-08 stsp goto done;
9359 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9360 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9361 2e1f37b0 2019-08-08 stsp goto done;
9363 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9364 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9365 2e1f37b0 2019-08-08 stsp goto done;
9367 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9368 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9369 eb81bc23 2022-06-28 tracey struct diff_chunk_context cc = {};
9370 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9371 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9372 fe621944 2020-11-10 stsp nchanges++;
9374 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9375 2e1f37b0 2019-08-08 stsp int choice;
9376 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9377 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9378 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9379 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9381 2e1f37b0 2019-08-08 stsp goto done;
9382 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9383 2e1f37b0 2019-08-08 stsp have_content = 1;
9385 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9386 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9389 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9390 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9391 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9393 2e1f37b0 2019-08-08 stsp free(label1);
9394 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9395 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9397 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9398 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9399 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9400 2e1f37b0 2019-08-08 stsp if (staged_blob)
9401 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9402 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9403 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9404 fe621944 2020-11-10 stsp err = free_err;
9405 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9406 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9407 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9408 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9409 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9410 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9411 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9412 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9413 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9414 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9415 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9416 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9417 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9418 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9419 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9420 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9421 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9422 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9423 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9425 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9426 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9427 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9428 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9429 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9430 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9431 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9433 2e1f37b0 2019-08-08 stsp free(path1);
9434 2e1f37b0 2019-08-08 stsp free(path2);
9435 fda8017d 2020-07-23 stsp return err;
9438 fda8017d 2020-07-23 stsp static const struct got_error *
9439 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9440 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9441 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9442 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9443 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9444 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9445 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9447 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9448 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9449 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9450 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9451 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9452 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9453 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9454 fda8017d 2020-07-23 stsp struct stat sb;
9456 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9457 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9458 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9460 fda8017d 2020-07-23 stsp return err;
9462 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9463 fda8017d 2020-07-23 stsp return NULL;
9465 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9466 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9467 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9469 fda8017d 2020-07-23 stsp goto done;
9472 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
9473 fda8017d 2020-07-23 stsp if (f == NULL) {
9474 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9475 fda8017d 2020-07-23 stsp path_unstaged_content);
9476 fda8017d 2020-07-23 stsp goto done;
9478 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9479 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9480 fda8017d 2020-07-23 stsp goto done;
9482 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9483 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9484 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9486 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9487 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9488 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9489 fda8017d 2020-07-23 stsp goto done;
9491 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9492 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9493 fda8017d 2020-07-23 stsp goto done;
9495 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9496 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9497 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9498 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9499 fda8017d 2020-07-23 stsp progress_arg);
9501 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9503 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9505 67a66647 2021-05-31 stsp return err;
9507 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9508 67a66647 2021-05-31 stsp parent) == -1) {
9509 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9510 67a66647 2021-05-31 stsp base_path = NULL;
9511 67a66647 2021-05-31 stsp goto done;
9514 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_base_path, &f_base,
9515 b90054ed 2022-11-01 stsp base_path, "");
9517 67a66647 2021-05-31 stsp goto done;
9518 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9519 67a66647 2021-05-31 stsp blob_base);
9521 67a66647 2021-05-31 stsp goto done;
9524 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9525 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9527 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9528 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9529 eec2f5a9 2021-06-03 stsp ondisk_path);
9531 eec2f5a9 2021-06-03 stsp goto done;
9534 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
9535 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9536 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9537 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9538 eec2f5a9 2021-06-03 stsp goto done;
9540 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9541 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9542 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9543 eec2f5a9 2021-06-03 stsp close(fd);
9544 eec2f5a9 2021-06-03 stsp goto done;
9548 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9549 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9550 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9551 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9552 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9555 fda8017d 2020-07-23 stsp goto done;
9557 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9558 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9559 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9561 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9562 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9565 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9566 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9567 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9568 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9569 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9570 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9571 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9572 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9573 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9574 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9575 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9576 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9577 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9578 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9579 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9580 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9581 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9582 67a66647 2021-05-31 stsp free(blob_base_path);
9583 67a66647 2021-05-31 stsp free(parent);
9584 67a66647 2021-05-31 stsp free(base_path);
9585 2e1f37b0 2019-08-08 stsp return err;
9588 ad493afc 2019-08-03 stsp static const struct got_error *
9589 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9590 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9591 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9592 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9594 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9595 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9596 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9597 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9598 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9599 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9600 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9601 9bc94a15 2019-08-03 stsp struct stat sb;
9602 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
9604 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9605 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9606 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9607 2e1f37b0 2019-08-08 stsp return NULL;
9609 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9610 ad493afc 2019-08-03 stsp if (ie == NULL)
9611 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9613 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9615 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9617 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9618 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9620 f69721c3 2019-10-21 stsp goto done;
9621 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9622 f69721c3 2019-10-21 stsp id_str) == -1) {
9623 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9624 eb81bc23 2022-06-28 tracey goto done;
9627 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9628 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9629 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9630 eb81bc23 2022-06-28 tracey goto done;
9632 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9633 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9634 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9635 f69721c3 2019-10-21 stsp goto done;
9638 ad493afc 2019-08-03 stsp switch (staged_status) {
9639 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9640 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9641 eb81bc23 2022-06-28 tracey blob_id, 8192, fd1);
9644 ad493afc 2019-08-03 stsp /* fall through */
9645 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9646 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9647 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9648 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9649 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9650 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9653 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9656 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9657 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9658 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9659 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9660 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9661 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9664 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9665 eb81bc23 2022-06-28 tracey staged_blob_id, 8192, fd2);
9668 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9669 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9670 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9671 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9672 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9673 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9674 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9675 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9676 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9678 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9679 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9680 36bf999c 2020-07-23 stsp char *staged_target;
9681 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9682 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9684 36bf999c 2020-07-23 stsp goto done;
9685 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9686 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9687 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9688 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9689 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9690 36bf999c 2020-07-23 stsp free(staged_target);
9692 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9693 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9694 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9695 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9696 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9697 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9698 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9702 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9705 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9706 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9707 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9708 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9711 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9712 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9713 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9714 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9715 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9718 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9720 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9721 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9725 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9726 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9727 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9728 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9731 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9735 ad493afc 2019-08-03 stsp free(ondisk_path);
9736 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9737 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9738 ad493afc 2019-08-03 stsp if (blob_base)
9739 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9740 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9741 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9742 ad493afc 2019-08-03 stsp if (blob_staged)
9743 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9744 f69721c3 2019-10-21 stsp free(id_str);
9745 f69721c3 2019-10-21 stsp free(label_orig);
9746 ad493afc 2019-08-03 stsp return err;
9749 ad493afc 2019-08-03 stsp const struct got_error *
9750 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9751 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9752 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9753 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9754 ad493afc 2019-08-03 stsp struct got_repository *repo)
9756 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9757 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9758 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9759 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9760 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9762 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9764 ad493afc 2019-08-03 stsp return err;
9766 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9768 ad493afc 2019-08-03 stsp goto done;
9770 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9771 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9772 ad493afc 2019-08-03 stsp upa.repo = repo;
9773 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9774 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9775 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9776 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9777 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9778 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9779 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
9781 ad493afc 2019-08-03 stsp goto done;
9784 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9785 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9786 ad493afc 2019-08-03 stsp err = sync_err;
9788 ad493afc 2019-08-03 stsp free(fileindex_path);
9789 ad493afc 2019-08-03 stsp if (fileindex)
9790 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9791 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9792 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9793 ad493afc 2019-08-03 stsp err = unlockerr;
9794 ad493afc 2019-08-03 stsp return err;
9797 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9798 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9799 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9800 b2118c49 2020-07-28 stsp void *info_arg;
9801 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9802 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9803 b2118c49 2020-07-28 stsp void *cancel_arg;
9806 b2118c49 2020-07-28 stsp static const struct got_error *
9807 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9809 f6b8c3c2 2023-07-24 stsp const struct got_error *err;
9810 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9811 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9812 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9813 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9814 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9815 b2118c49 2020-07-28 stsp int stage;
9817 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
9818 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
9820 f6b8c3c2 2023-07-24 stsp return err;
9823 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9824 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9825 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9828 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9829 b2118c49 2020-07-28 stsp return NULL;
9831 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
9832 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9833 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9834 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9835 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9836 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9837 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
9840 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
9841 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9843 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9844 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9847 b2118c49 2020-07-28 stsp const struct got_error *
9848 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9849 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9850 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9851 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9854 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9855 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9856 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9857 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9859 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9861 b2118c49 2020-07-28 stsp return err;
9863 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9865 b2118c49 2020-07-28 stsp goto done;
9867 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9868 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9869 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9870 b2118c49 2020-07-28 stsp arg.paths = paths;
9871 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9872 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9873 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9876 b2118c49 2020-07-28 stsp free(fileindex_path);
9877 b2118c49 2020-07-28 stsp if (fileindex)
9878 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9879 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9880 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9881 b2118c49 2020-07-28 stsp err = unlockerr;
9882 b2118c49 2020-07-28 stsp return err;
9885 78f5ac24 2022-03-19 op static const struct got_error *
9886 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
9887 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
9888 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
9890 78f5ac24 2022-03-19 op const struct got_error *err;
9891 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
9892 78f5ac24 2022-03-19 op struct stat sb;
9893 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
9895 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
9899 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9900 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
9901 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
9903 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9905 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
9906 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9911 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
9912 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
9913 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
9914 78f5ac24 2022-03-19 op if (errno != ENOENT) {
9915 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
9916 78f5ac24 2022-03-19 op ondisk_path);
9919 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
9924 78f5ac24 2022-03-19 op free(ondisk_path);
9928 78f5ac24 2022-03-19 op static const struct got_error *
9929 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
9930 78f5ac24 2022-03-19 op unsigned char staged_status)
9932 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9933 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9934 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9935 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9936 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
9937 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
9938 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9939 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9940 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9941 78f5ac24 2022-03-19 op return NULL;
9944 78f5ac24 2022-03-19 op static const struct got_error *
9945 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
9947 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
9948 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9949 78f5ac24 2022-03-19 op return NULL;
9952 78f5ac24 2022-03-19 op static const struct got_error *
9953 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
9954 78f5ac24 2022-03-19 op unsigned char staged_status)
9956 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9957 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9958 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9959 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9960 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
9961 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9962 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9963 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9964 78f5ac24 2022-03-19 op return NULL;
9967 78f5ac24 2022-03-19 op const struct got_error *
9968 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
9969 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
9971 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
9974 78f5ac24 2022-03-19 op const struct got_error *
9975 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
9976 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
9977 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
9979 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
9980 78f5ac24 2022-03-19 op int file_renamed = 0;
9981 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
9982 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
9984 78f5ac24 2022-03-19 op *oldpath = NULL;
9985 78f5ac24 2022-03-19 op *newpath = NULL;
9987 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
9988 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
9992 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
9993 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
9997 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
9998 78f5ac24 2022-03-19 op file_renamed = 1;
10000 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
10001 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
10002 78f5ac24 2022-03-19 op else if (file_renamed) {
10003 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
10004 78f5ac24 2022-03-19 op if (err == NULL)
10005 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
10006 78f5ac24 2022-03-19 op } else if (old == NULL)
10007 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
10009 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
10013 78f5ac24 2022-03-19 op free(*oldpath);
10014 78f5ac24 2022-03-19 op *oldpath = NULL;
10015 78f5ac24 2022-03-19 op free(*newpath);
10016 78f5ac24 2022-03-19 op *newpath = NULL;
10018 78f5ac24 2022-03-19 op return err;
10021 78f5ac24 2022-03-19 op const struct got_error *
10022 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10023 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
10024 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
10026 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
10028 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
10029 f2dd7807 2022-05-17 op saa.worktree = worktree;
10030 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
10031 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
10032 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
10033 f2dd7807 2022-05-17 op saa.repo = repo;
10035 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
10036 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
10039 f2dd7807 2022-05-17 op const struct got_error *
10040 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10041 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
10042 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
10044 7f4e5320 2023-05-29 stsp const struct got_error *err;
10045 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
10046 7f4e5320 2023-05-29 stsp char *ondisk_status_path;
10048 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
10049 f2dd7807 2022-05-17 op sda.worktree = worktree;
10050 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
10051 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
10052 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
10053 f2dd7807 2022-05-17 op sda.repo = repo;
10054 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
10055 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
10056 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
10057 f2dd7807 2022-05-17 op sda.status_codes = NULL;
10058 7f4e5320 2023-05-29 stsp if (asprintf(&ondisk_status_path, "%s/%s",
10059 7f4e5320 2023-05-29 stsp got_worktree_get_root_path(worktree), path) == -1)
10060 7f4e5320 2023-05-29 stsp return got_error_from_errno("asprintf");
10061 7f4e5320 2023-05-29 stsp sda.status_path = ondisk_status_path;
10062 7f4e5320 2023-05-29 stsp sda.status_path_len = strlen(ondisk_status_path);
10064 7f4e5320 2023-05-29 stsp err = worktree_status(worktree, path, fileindex, repo,
10065 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10066 7f4e5320 2023-05-29 stsp free(ondisk_status_path);
10067 7f4e5320 2023-05-29 stsp return err;
10070 f2dd7807 2022-05-17 op const struct got_error *
10071 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
10072 4bcdc895 2022-05-17 op const char *fileindex_path)
10074 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
10076 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
10077 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);
10079 f2dd7807 2022-05-17 op return err;