2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
19 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
20 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
22 d1f6d47b 2019-02-04 stsp #include <dirent.h>
23 12ce7a6c 2019-08-12 stsp #include <limits.h>
24 f5c49f82 2019-01-06 stsp #include <stddef.h>
25 86c3caaf 2018-03-09 stsp #include <string.h>
26 86c3caaf 2018-03-09 stsp #include <stdio.h>
27 86c3caaf 2018-03-09 stsp #include <stdlib.h>
28 303e14b5 2019-09-23 stsp #include <time.h>
29 86c3caaf 2018-03-09 stsp #include <fcntl.h>
30 86c3caaf 2018-03-09 stsp #include <errno.h>
31 86c3caaf 2018-03-09 stsp #include <unistd.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
36 86c3caaf 2018-03-09 stsp #include "got_error.h"
37 86c3caaf 2018-03-09 stsp #include "got_repository.h"
38 5261c201 2018-04-01 stsp #include "got_reference.h"
39 9d31a1d8 2018-03-11 stsp #include "got_object.h"
40 1dd54920 2019-05-11 stsp #include "got_path.h"
41 e6209546 2019-08-22 stsp #include "got_cancel.h"
42 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 234035bc 2019-06-01 stsp #include "got_diff.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
47 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
53 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
54 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
55 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
56 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
59 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 a807c50b 2023-07-05 thomas static mode_t
66 a807c50b 2023-07-05 thomas apply_umask(mode_t mode)
68 a807c50b 2023-07-05 thomas mode_t um;
70 a807c50b 2023-07-05 thomas um = umask(000);
71 a807c50b 2023-07-05 thomas umask(um);
72 a807c50b 2023-07-05 thomas return mode & ~um;
75 99724ed4 2018-03-10 stsp static const struct got_error *
76 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
78 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
81 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
82 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
84 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
89 507dc3bb 2018-12-29 stsp static const struct got_error *
90 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
92 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
93 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
94 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
95 507dc3bb 2018-12-29 stsp char *path = NULL;
97 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
98 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
99 507dc3bb 2018-12-29 stsp path = NULL;
103 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &tmpfile, path, "");
107 507dc3bb 2018-12-29 stsp if (content) {
108 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
109 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
110 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
115 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
116 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
117 2a57020b 2019-02-20 stsp unlink(tmppath);
122 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
123 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
124 230a42bd 2019-05-11 jcs free(tmppath);
125 09fe317a 2018-03-11 stsp return err;
128 024e9686 2019-05-14 stsp static const struct got_error *
129 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
131 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
132 024e9686 2019-05-14 stsp char *refstr = NULL;
134 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
135 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
136 024e9686 2019-05-14 stsp if (refstr == NULL)
137 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
139 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
140 024e9686 2019-05-14 stsp if (refstr == NULL)
141 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
143 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
144 024e9686 2019-05-14 stsp free(refstr);
145 024e9686 2019-05-14 stsp return err;
148 86c3caaf 2018-03-09 stsp const struct got_error *
149 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
150 ad10f64e 2023-07-19 thomas const char *prefix, const char *meta_dir, struct got_repository *repo)
152 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
153 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
154 ec22038e 2019-03-10 stsp uuid_t uuid;
155 ec22038e 2019-03-10 stsp uint32_t uuid_status;
156 65596e15 2018-12-24 stsp int obj_type;
157 7ac97322 2018-03-11 stsp char *path_got = NULL;
158 1451e70d 2018-03-10 stsp char *formatstr = NULL;
159 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
160 65596e15 2018-12-24 stsp char *basestr = NULL;
161 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
163 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
164 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
168 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
170 65596e15 2018-12-24 stsp return err;
171 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
173 65596e15 2018-12-24 stsp return err;
174 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
175 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
177 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
178 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
179 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
182 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
183 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
184 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
188 ad10f64e 2023-07-19 thomas /* Create .got/.cvg directory (may already exist). */
189 ad10f64e 2023-07-19 thomas if (asprintf(&path_got, "%s/%s", path, meta_dir) == -1) {
190 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
193 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
194 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
198 056e7441 2018-03-11 stsp /* Create an empty lock file. */
199 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
203 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
204 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
208 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
209 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
213 65596e15 2018-12-24 stsp /* Record our base commit. */
214 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
217 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
221 1451e70d 2018-03-10 stsp /* Store path to repository. */
222 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
223 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
227 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
228 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
229 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
233 ec22038e 2019-03-10 stsp /* Generate UUID. */
234 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
235 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
236 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
239 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
240 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
241 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
244 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
248 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
249 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
250 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
253 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
258 65596e15 2018-12-24 stsp free(commit_id);
259 7ac97322 2018-03-11 stsp free(path_got);
260 1451e70d 2018-03-10 stsp free(formatstr);
261 0bb8a95e 2018-03-12 stsp free(absprefix);
262 65596e15 2018-12-24 stsp free(basestr);
263 ec22038e 2019-03-10 stsp free(uuidstr);
264 86c3caaf 2018-03-09 stsp return err;
267 247140b2 2019-02-05 stsp const struct got_error *
268 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
269 e5dc7198 2018-12-29 stsp const char *path_prefix)
271 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
273 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
274 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
275 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
277 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
278 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
279 e5dc7198 2018-12-29 stsp free(absprefix);
280 e5dc7198 2018-12-29 stsp return NULL;
283 bc70eb79 2019-05-09 stsp const char *
284 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
286 36a38700 2019-05-10 stsp return worktree->head_ref_name;
289 024e9686 2019-05-14 stsp const struct got_error *
290 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
291 024e9686 2019-05-14 stsp struct got_reference *head_ref)
293 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
294 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
296 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
297 ad10f64e 2023-07-19 thomas worktree->meta_dir) == -1) {
298 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
299 024e9686 2019-05-14 stsp path_got = NULL;
303 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
304 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
305 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
309 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
313 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
314 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
316 024e9686 2019-05-14 stsp free(path_got);
318 024e9686 2019-05-14 stsp free(head_ref_name);
319 024e9686 2019-05-14 stsp return err;
322 b72f483a 2019-02-05 stsp struct got_object_id *
323 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
325 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
328 507dc3bb 2018-12-29 stsp const struct got_error *
329 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
330 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
332 507dc3bb 2018-12-29 stsp const struct got_error *err;
333 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
334 507dc3bb 2018-12-29 stsp char *id_str = NULL;
335 507dc3bb 2018-12-29 stsp char *path_got = NULL;
337 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
338 ad10f64e 2023-07-19 thomas worktree->meta_dir) == -1) {
339 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
340 507dc3bb 2018-12-29 stsp path_got = NULL;
344 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
346 507dc3bb 2018-12-29 stsp return err;
348 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
349 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
353 507dc3bb 2018-12-29 stsp /* Record our base commit. */
354 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
357 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
361 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
362 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
363 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
364 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
369 507dc3bb 2018-12-29 stsp got_object_close(obj);
370 507dc3bb 2018-12-29 stsp free(id_str);
371 507dc3bb 2018-12-29 stsp free(path_got);
372 507dc3bb 2018-12-29 stsp return err;
375 50b0790e 2020-09-11 stsp const struct got_gotconfig *
376 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
378 50b0790e 2020-09-11 stsp return worktree->gotconfig;
381 9d31a1d8 2018-03-11 stsp static const struct got_error *
382 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
384 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
385 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
386 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
387 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
388 86c3caaf 2018-03-09 stsp return NULL;
391 21908da4 2019-01-13 stsp static const struct got_error *
392 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
394 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
395 4a1ddfc2 2019-01-12 stsp char *abspath;
397 59530ea7 2023-07-17 thomas /* We only accept worktree-relative paths here. */
398 59530ea7 2023-07-17 thomas if (got_path_is_absolute(path)) {
399 59530ea7 2023-07-17 thomas return got_error_fmt(GOT_ERR_BAD_PATH,
400 59530ea7 2023-07-17 thomas "%s does not accept absolute paths: %s",
401 59530ea7 2023-07-17 thomas __func__, path);
404 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
405 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
407 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
408 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
409 ddcd8544 2019-03-11 stsp struct stat sb;
410 ddcd8544 2019-03-11 stsp err = NULL;
411 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
412 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
413 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
414 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
415 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
418 4a1ddfc2 2019-01-12 stsp free(abspath);
419 68c76935 2019-02-19 stsp return err;
422 68c76935 2019-02-19 stsp static const struct got_error *
423 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
425 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
426 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
427 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
428 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
433 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
434 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
435 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
438 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
439 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
440 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
443 68c76935 2019-02-19 stsp if (flen1 == 0) {
444 68c76935 2019-02-19 stsp if (flen2 != 0)
447 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
448 68c76935 2019-02-19 stsp if (flen1 != 0)
451 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
452 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
462 68c76935 2019-02-19 stsp return err;
465 68c76935 2019-02-19 stsp static const struct got_error *
466 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
468 68c76935 2019-02-19 stsp struct stat sb;
469 68c76935 2019-02-19 stsp size_t size1, size2;
473 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
474 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
475 68c76935 2019-02-19 stsp size1 = sb.st_size;
477 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
478 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
479 68c76935 2019-02-19 stsp size2 = sb.st_size;
481 68c76935 2019-02-19 stsp if (size1 != size2) {
483 68c76935 2019-02-19 stsp return NULL;
486 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
487 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
488 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
489 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
491 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
494 24f136e0 2021-11-20 thomas static const struct got_error *
495 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
497 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
498 24f136e0 2021-11-20 thomas size_t flen;
499 24f136e0 2021-11-20 thomas ssize_t outlen;
501 24f136e0 2021-11-20 thomas *outsize = 0;
503 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
504 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
506 24f136e0 2021-11-20 thomas for (;;) {
507 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
508 24f136e0 2021-11-20 thomas if (flen == 0) {
509 24f136e0 2021-11-20 thomas if (ferror(f))
510 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
511 24f136e0 2021-11-20 thomas if (feof(f))
514 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
515 24f136e0 2021-11-20 thomas if (outlen == -1)
516 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
517 24f136e0 2021-11-20 thomas if (outlen != flen)
518 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
519 24f136e0 2021-11-20 thomas *outsize += outlen;
522 24f136e0 2021-11-20 thomas return NULL;
525 24f136e0 2021-11-20 thomas static const struct got_error *
526 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
527 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
528 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
529 24f136e0 2021-11-20 thomas const char *ondisk_path)
531 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
532 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
533 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
534 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
535 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
536 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
537 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
539 24f136e0 2021-11-20 thomas *overlapcnt = 0;
541 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
543 24f136e0 2021-11-20 thomas return err;
545 24f136e0 2021-11-20 thomas if (same_content)
546 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
548 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
550 24f136e0 2021-11-20 thomas return err;
551 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
552 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
554 24f136e0 2021-11-20 thomas return err;
555 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
557 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
558 24f136e0 2021-11-20 thomas *overlapcnt = 1;
559 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
560 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
561 24f136e0 2021-11-20 thomas goto done;
563 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
564 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
565 24f136e0 2021-11-20 thomas goto done;
567 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
568 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
569 24f136e0 2021-11-20 thomas goto done;
571 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
572 fc2a50f2 2022-11-01 thomas base_path_orig, "");
574 24f136e0 2021-11-20 thomas goto done;
575 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
576 fc2a50f2 2022-11-01 thomas base_path_deriv, "");
578 24f136e0 2021-11-20 thomas goto done;
579 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
580 fc2a50f2 2022-11-01 thomas base_path_deriv2, "");
582 24f136e0 2021-11-20 thomas goto done;
583 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
585 24f136e0 2021-11-20 thomas goto done;
586 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
588 24f136e0 2021-11-20 thomas goto done;
589 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
591 24f136e0 2021-11-20 thomas goto done;
592 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
593 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
594 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
595 24f136e0 2021-11-20 thomas goto done;
597 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
598 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
599 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
600 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
601 24f136e0 2021-11-20 thomas path_deriv) < 0) {
602 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
603 24f136e0 2021-11-20 thomas goto done;
605 24f136e0 2021-11-20 thomas if (size_orig > 0) {
606 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
607 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
608 24f136e0 2021-11-20 thomas label_orig ? " " : "",
609 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
610 24f136e0 2021-11-20 thomas path_orig) < 0) {
611 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
612 24f136e0 2021-11-20 thomas goto done;
615 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
616 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
617 24f136e0 2021-11-20 thomas path_deriv2,
618 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
619 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
620 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
621 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
622 24f136e0 2021-11-20 thomas goto done;
624 24f136e0 2021-11-20 thomas } else if (changed_deriv)
625 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
626 24f136e0 2021-11-20 thomas else if (changed_deriv2)
627 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
629 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
630 24f136e0 2021-11-20 thomas err == NULL)
631 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
632 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
633 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
634 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
635 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
636 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
637 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
638 24f136e0 2021-11-20 thomas free(path_orig);
639 24f136e0 2021-11-20 thomas free(path_deriv);
640 24f136e0 2021-11-20 thomas free(path_deriv2);
641 24f136e0 2021-11-20 thomas free(base_path_orig);
642 24f136e0 2021-11-20 thomas free(base_path_deriv);
643 24f136e0 2021-11-20 thomas free(base_path_deriv2);
644 24f136e0 2021-11-20 thomas return err;
648 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
649 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
650 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
651 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
652 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
654 6353ad76 2019-02-08 stsp static const struct got_error *
655 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
656 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
657 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
658 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
659 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
660 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
662 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
663 6353ad76 2019-02-08 stsp int merged_fd = -1;
664 db590691 2021-06-02 stsp FILE *f_merged = NULL;
665 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
666 234035bc 2019-06-01 stsp int overlapcnt = 0;
667 3524bbf9 2020-10-20 stsp char *parent = NULL;
669 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
671 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
673 3524bbf9 2020-10-20 stsp return err;
675 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
676 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
680 fc2a50f2 2022-11-01 thomas err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
684 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
685 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
686 24f136e0 2021-11-20 thomas if (err) {
687 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
688 24f136e0 2021-11-20 thomas goto done;
689 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
690 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
691 24f136e0 2021-11-20 thomas ondisk_path);
693 24f136e0 2021-11-20 thomas goto done;
696 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
697 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
701 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
702 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
706 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
707 db590691 2021-06-02 stsp if (f_merged == NULL) {
708 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
711 db590691 2021-06-02 stsp merged_fd = -1;
713 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
714 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
715 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
721 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
722 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
726 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
727 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
728 230a42bd 2019-05-11 jcs ondisk_path);
733 fdcb7daf 2019-12-15 stsp if (merged_path)
734 fdcb7daf 2019-12-15 stsp unlink(merged_path);
736 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
737 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
738 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
739 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
740 6353ad76 2019-02-08 stsp free(merged_path);
741 af54ae4a 2019-02-19 stsp free(base_path);
742 3524bbf9 2020-10-20 stsp free(parent);
743 af57b12a 2020-07-23 stsp return err;
746 af57b12a 2020-07-23 stsp static const struct got_error *
747 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
748 af57b12a 2020-07-23 stsp size_t target_len)
750 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
751 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
752 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
753 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
754 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
755 af57b12a 2020-07-23 stsp ondisk_path);
756 af57b12a 2020-07-23 stsp return NULL;
760 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
761 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
762 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
763 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
765 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
766 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
768 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
769 993e2a1b 2020-07-23 stsp * has deleted this symlink.
771 11cc08c1 2020-07-23 stsp static const struct got_error *
772 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
773 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
774 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
776 11cc08c1 2020-07-23 stsp const struct got_error *err;
777 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
778 11cc08c1 2020-07-23 stsp FILE *f = NULL;
780 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
782 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
784 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
785 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
786 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
790 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
792 3818e3c4 2020-11-01 naddy goto done;
794 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
795 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
799 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
800 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
801 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
802 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
803 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
804 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
805 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
806 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
807 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
808 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
812 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
813 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
816 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
817 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
821 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
822 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
823 11cc08c1 2020-07-23 stsp free(path);
824 11cc08c1 2020-07-23 stsp free(id_str);
825 11cc08c1 2020-07-23 stsp free(label_deriv);
826 11cc08c1 2020-07-23 stsp return err;
829 d219f183 2020-07-23 stsp /* forward declaration */
830 d219f183 2020-07-23 stsp static const struct got_error *
831 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
832 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
833 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
834 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
837 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
838 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
839 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
840 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
842 af57b12a 2020-07-23 stsp static const struct got_error *
843 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
844 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
845 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
846 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
847 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
849 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
850 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
851 af57b12a 2020-07-23 stsp struct stat sb;
852 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
853 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
854 11cc08c1 2020-07-23 stsp int have_local_change = 0;
855 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
857 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
858 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
860 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
861 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
862 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
863 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
864 af57b12a 2020-07-23 stsp ondisk_path);
867 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
869 526a746f 2020-07-23 stsp if (blob_orig) {
870 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
875 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
876 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
877 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
878 11cc08c1 2020-07-23 stsp have_local_change = 1;
880 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
881 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
882 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
883 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
884 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
886 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
887 11cc08c1 2020-07-23 stsp if (ancestor_target) {
888 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
889 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
891 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
892 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
893 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
894 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
897 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
898 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
899 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
900 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
903 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
906 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
907 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
908 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
909 af57b12a 2020-07-23 stsp strlen(deriv_target));
912 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
913 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
914 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
915 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
916 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
917 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
920 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
921 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
922 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
925 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
931 af57b12a 2020-07-23 stsp free(ancestor_target);
932 eec2f5a9 2021-06-03 stsp return err;
935 eec2f5a9 2021-06-03 stsp static const struct got_error *
936 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
938 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
939 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
940 eec2f5a9 2021-06-03 stsp ssize_t target_len;
944 eec2f5a9 2021-06-03 stsp *outfile = NULL;
946 eec2f5a9 2021-06-03 stsp f = got_opentemp();
947 eec2f5a9 2021-06-03 stsp if (f == NULL)
948 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
949 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
950 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
951 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
954 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
955 eec2f5a9 2021-06-03 stsp if (n != target_len) {
956 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
959 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
960 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
963 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
964 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
971 eec2f5a9 2021-06-03 stsp *outfile = f;
972 14c901f1 2019-08-08 stsp return err;
976 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
977 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
978 14c901f1 2019-08-08 stsp * acts as the second derived version.
980 14c901f1 2019-08-08 stsp static const struct got_error *
981 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
982 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
983 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
984 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
985 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
986 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
988 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
989 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
990 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
991 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
992 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
994 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
996 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
998 ed6b5030 2020-10-20 stsp return err;
1000 67a66647 2021-05-31 stsp if (blob_orig) {
1001 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
1002 67a66647 2021-05-31 stsp parent) == -1) {
1003 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
1004 67a66647 2021-05-31 stsp base_path = NULL;
1005 67a66647 2021-05-31 stsp goto done;
1008 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_orig_path, &f_orig,
1009 fc2a50f2 2022-11-01 thomas base_path, "");
1011 67a66647 2021-05-31 stsp goto done;
1012 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1013 67a66647 2021-05-31 stsp blob_orig);
1015 67a66647 2021-05-31 stsp goto done;
1016 67a66647 2021-05-31 stsp free(base_path);
1019 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1020 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1021 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1023 db590691 2021-06-02 stsp f_orig = got_opentemp();
1024 db590691 2021-06-02 stsp if (f_orig == NULL) {
1025 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1026 db590691 2021-06-02 stsp goto done;
1030 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1031 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1032 14c901f1 2019-08-08 stsp base_path = NULL;
1033 14c901f1 2019-08-08 stsp goto done;
1036 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1038 14c901f1 2019-08-08 stsp goto done;
1039 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1040 14c901f1 2019-08-08 stsp blob_deriv);
1042 14c901f1 2019-08-08 stsp goto done;
1044 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1046 14c901f1 2019-08-08 stsp goto done;
1047 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1048 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1049 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1050 14c901f1 2019-08-08 stsp goto done;
1054 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1055 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1057 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1058 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1060 eec2f5a9 2021-06-03 stsp goto done;
1063 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1064 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1065 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1066 eec2f5a9 2021-06-03 stsp goto done;
1068 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1069 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1070 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1071 eec2f5a9 2021-06-03 stsp close(fd);
1072 eec2f5a9 2021-06-03 stsp goto done;
1076 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1077 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1078 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1080 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1081 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1082 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1083 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1084 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1085 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1086 14c901f1 2019-08-08 stsp free(base_path);
1087 67a66647 2021-05-31 stsp if (blob_orig_path) {
1088 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1089 67a66647 2021-05-31 stsp free(blob_orig_path);
1091 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1092 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1093 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1095 6353ad76 2019-02-08 stsp free(id_str);
1096 818c7501 2019-07-11 stsp free(label_deriv);
1097 ed6b5030 2020-10-20 stsp free(parent);
1098 4a1ddfc2 2019-01-12 stsp return err;
1101 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1102 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1103 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1104 aaf972e2 2023-09-18 thomas int wt_fd, const char *path, struct got_object_id *blob_id,
1105 aaf972e2 2023-09-18 thomas int update_timestamps)
1107 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1108 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1110 65b05cec 2020-07-23 stsp *new_iep = NULL;
1112 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1114 054041d0 2020-06-24 stsp return err;
1116 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1117 aaf972e2 2023-09-18 thomas blob_id->sha1, base_commit_id->sha1, update_timestamps);
1119 054041d0 2020-06-24 stsp goto done;
1121 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1124 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1126 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1127 13d9040b 2019-03-27 stsp return err;
1130 1ebedb77 2019-10-19 stsp static mode_t
1131 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1133 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1135 1ebedb77 2019-10-19 stsp if (executable) {
1136 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1137 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1138 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1139 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1140 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1141 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1144 a2c162eb 2022-10-30 thomas return st_mode;
1147 8ba819a3 2020-07-23 stsp /* forward declaration */
1148 13d9040b 2019-03-27 stsp static const struct got_error *
1149 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1150 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1151 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1152 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1153 aaf972e2 2023-09-18 thomas int path_is_unversioned, int *update_timestamps,
1154 aaf972e2 2023-09-18 thomas struct got_repository *repo,
1155 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1158 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1159 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1161 8ba819a3 2020-07-23 stsp static const struct got_error *
1162 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1163 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1165 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1166 5a1fbc73 2020-07-23 stsp ssize_t elen;
1167 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1170 c6e8a826 2021-04-05 stsp *did_something = 0;
1173 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1174 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1175 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1176 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1177 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1178 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1180 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1181 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1182 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1183 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1185 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1186 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1187 5a1fbc73 2020-07-23 stsp if (elen == -1)
1188 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1190 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1191 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1192 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1195 c6e8a826 2021-04-05 stsp *did_something = 1;
1196 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1197 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1198 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1199 5a1fbc73 2020-07-23 stsp return err;
1202 3c1ec782 2020-07-23 stsp static const struct got_error *
1203 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1204 ad10f64e 2023-07-19 thomas size_t target_len, const char *ondisk_path, const char *wtroot_path,
1205 ad10f64e 2023-07-19 thomas const char *meta_dir)
1207 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1208 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1209 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1211 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1213 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1214 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1215 3c1ec782 2020-07-23 stsp return NULL;
1219 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1220 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1221 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1222 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1223 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1225 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1226 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1227 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1229 ce031e9e 2020-10-20 stsp return err;
1230 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1231 ce031e9e 2020-10-20 stsp free(parent);
1232 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1234 ce031e9e 2020-10-20 stsp free(parent);
1235 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1236 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1237 3c1ec782 2020-07-23 stsp free(abspath);
1238 3c1ec782 2020-07-23 stsp return err;
1240 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1241 3c1ec782 2020-07-23 stsp free(abspath);
1243 3c1ec782 2020-07-23 stsp return err;
1245 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1247 3c1ec782 2020-07-23 stsp return err;
1250 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1251 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1252 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1253 3c1ec782 2020-07-23 stsp return NULL;
1256 ad10f64e 2023-07-19 thomas /* Do not allow symlinks pointing into the meta directory. */
1257 ad10f64e 2023-07-19 thomas if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1258 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1259 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1260 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1262 3c1ec782 2020-07-23 stsp free(path_got);
1263 3c1ec782 2020-07-23 stsp return NULL;
1266 5a1fbc73 2020-07-23 stsp static const struct got_error *
1267 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1268 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1269 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1270 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1271 ace90326 2021-09-27 thomas struct got_repository *repo,
1272 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1274 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1275 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1276 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1277 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1278 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1280 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1283 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1284 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1285 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1286 8ba819a3 2020-07-23 stsp * in the blob object.
1289 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1290 3521b466 2022-07-21 thomas if (err)
1291 3521b466 2022-07-21 thomas return err;
1293 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1294 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1295 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1296 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1297 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1298 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1299 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1300 aaf972e2 2023-09-18 thomas 1, path_is_unversioned, NULL, repo, progress_cb,
1301 3b9f0f87 2020-07-23 stsp progress_arg);
1303 8ba819a3 2020-07-23 stsp if (len > 0) {
1304 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1305 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1306 8ba819a3 2020-07-23 stsp len - hdrlen);
1307 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1308 8ba819a3 2020-07-23 stsp hdrlen = 0;
1310 8ba819a3 2020-07-23 stsp } while (len != 0);
1311 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1313 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1314 ad10f64e 2023-07-19 thomas ondisk_path, worktree->root_path, worktree->meta_dir);
1316 3c1ec782 2020-07-23 stsp return err;
1318 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1319 906c123b 2020-07-23 stsp /* install as a regular file */
1320 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1321 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1322 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1323 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1324 aaf972e2 2023-09-18 thomas path_is_unversioned, NULL, repo,
1325 aaf972e2 2023-09-18 thomas progress_cb, progress_arg);
1326 9e39ca77 2022-07-21 thomas return err;
1329 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1330 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1331 c6e8a826 2021-04-05 stsp int symlink_replaced;
1332 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1333 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1334 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1335 9e39ca77 2022-07-21 thomas return err;
1337 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1338 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1340 9e39ca77 2022-07-21 thomas return err;
1341 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1342 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1343 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1344 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1345 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1346 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1348 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1349 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1352 9e39ca77 2022-07-21 thomas return err; /* Nothing else to do. */
1355 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1356 f4994adc 2020-10-20 stsp char *parent;
1357 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1359 9e39ca77 2022-07-21 thomas return err;
1360 59530ea7 2023-07-17 thomas err = got_path_mkdir(parent);
1361 f4994adc 2020-10-20 stsp free(parent);
1363 9e39ca77 2022-07-21 thomas return err;
1365 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1366 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1368 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1369 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1370 59530ea7 2023-07-17 thomas if (progress_cb) {
1371 59530ea7 2023-07-17 thomas err = (*progress_cb)(progress_arg,
1372 59530ea7 2023-07-17 thomas reverting_versioned_file ?
1373 59530ea7 2023-07-17 thomas GOT_STATUS_REVERT : GOT_STATUS_ADD,
1376 9e39ca77 2022-07-21 thomas return err;
1380 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1381 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1382 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1383 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1384 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1385 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1386 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1387 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1388 aaf972e2 2023-09-18 thomas path_is_unversioned, NULL, repo,
1389 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1390 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1391 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1392 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1394 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1395 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1397 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1398 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1399 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1400 8ba819a3 2020-07-23 stsp return err;
1403 8ba819a3 2020-07-23 stsp static const struct got_error *
1404 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1405 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1406 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1407 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1408 aaf972e2 2023-09-18 thomas int path_is_unversioned, int *update_timestamps,
1409 aaf972e2 2023-09-18 thomas struct got_repository *repo,
1410 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1412 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1413 507dc3bb 2018-12-29 stsp int fd = -1;
1414 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1415 507dc3bb 2018-12-29 stsp int update = 0;
1416 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1417 a2c162eb 2022-10-30 thomas mode_t mode;
1419 aaf972e2 2023-09-18 thomas if (update_timestamps)
1420 aaf972e2 2023-09-18 thomas *update_timestamps = 1;
1422 a2c162eb 2022-10-30 thomas mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1423 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1424 a2c162eb 2022-10-30 thomas O_CLOEXEC, mode);
1425 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1426 9f323212 2023-03-10 thomas if (errno == ENOENT || errno == ENOTDIR) {
1427 f5375317 2020-10-20 stsp char *parent;
1428 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1430 f5375317 2020-10-20 stsp return err;
1431 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1432 9f323212 2023-03-10 thomas if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1433 9f323212 2023-03-10 thomas err = got_error_path(path, err->code);
1434 f5375317 2020-10-20 stsp free(parent);
1436 21908da4 2019-01-13 stsp return err;
1437 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1438 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1440 21908da4 2019-01-13 stsp if (fd == -1)
1441 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1442 230a42bd 2019-05-11 jcs ondisk_path);
1443 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1444 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1445 aaf972e2 2023-09-18 thomas if (update_timestamps)
1446 aaf972e2 2023-09-18 thomas *update_timestamps = 0;
1447 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1448 aaf972e2 2023-09-18 thomas GOT_STATUS_EXISTS, path);
1449 3b9f0f87 2020-07-23 stsp goto done;
1451 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1452 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1453 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1454 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1455 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1456 507dc3bb 2018-12-29 stsp goto done;
1458 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1459 fc2a50f2 2022-11-01 thomas ondisk_path, "");
1461 507dc3bb 2018-12-29 stsp goto done;
1462 507dc3bb 2018-12-29 stsp update = 1;
1464 a2c162eb 2022-10-30 thomas if (fchmod(fd, apply_umask(mode)) == -1) {
1465 a2c162eb 2022-10-30 thomas err = got_error_from_errno2("fchmod",
1466 a2c162eb 2022-10-30 thomas tmppath);
1467 a2c162eb 2022-10-30 thomas goto done;
1471 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1474 bd6aa359 2020-07-23 stsp if (progress_cb) {
1475 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1476 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1478 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1479 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1482 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1483 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1485 bd6aa359 2020-07-23 stsp goto done;
1488 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1490 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1491 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1494 9d31a1d8 2018-03-11 stsp if (len > 0) {
1495 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1496 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1497 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1498 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1499 b87c6f83 2018-12-24 stsp goto done;
1500 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1501 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1502 b87c6f83 2018-12-24 stsp goto done;
1504 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1506 9d31a1d8 2018-03-11 stsp } while (len != 0);
1508 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1509 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1510 816dc654 2019-02-16 stsp goto done;
1513 507dc3bb 2018-12-29 stsp if (update) {
1514 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1515 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1516 f6d8c0ac 2020-11-09 stsp goto done;
1518 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1519 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1520 230a42bd 2019-05-11 jcs ondisk_path);
1521 68ed9ba5 2019-02-10 stsp goto done;
1523 63df146d 2020-11-09 stsp free(tmppath);
1524 63df146d 2020-11-09 stsp tmppath = NULL;
1528 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1529 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1530 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1531 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1532 507dc3bb 2018-12-29 stsp free(tmppath);
1533 6353ad76 2019-02-08 stsp return err;
1537 be94c032 2023-02-20 thomas * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1538 be94c032 2023-02-20 thomas * conflict marker is found in newly added lines only.
1540 6353ad76 2019-02-08 stsp static const struct got_error *
1541 be94c032 2023-02-20 thomas get_modified_file_content_status(unsigned char *status,
1542 be94c032 2023-02-20 thomas struct got_blob_object *blob, const char *path, struct stat *sb,
1543 be94c032 2023-02-20 thomas FILE *ondisk_file)
1545 8de9d8ad 2023-02-20 thomas const struct got_error *err, *free_err;
1546 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1547 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1548 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1549 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1551 8de9d8ad 2023-02-20 thomas FILE *f1 = NULL;
1552 8de9d8ad 2023-02-20 thomas struct got_diffreg_result *diffreg_result = NULL;
1553 8de9d8ad 2023-02-20 thomas struct diff_result *r;
1554 8de9d8ad 2023-02-20 thomas int nchunks_parsed, n, i = 0, ln = 0;
1555 9bdd68dd 2020-01-02 naddy char *line = NULL;
1556 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1557 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1559 8de9d8ad 2023-02-20 thomas if (*status != GOT_STATUS_MODIFY)
1560 8de9d8ad 2023-02-20 thomas return NULL;
1562 8de9d8ad 2023-02-20 thomas f1 = got_opentemp();
1563 8de9d8ad 2023-02-20 thomas if (f1 == NULL)
1564 8de9d8ad 2023-02-20 thomas return got_error_from_errno("got_opentemp");
1566 8de9d8ad 2023-02-20 thomas if (blob) {
1567 8de9d8ad 2023-02-20 thomas got_object_blob_rewind(blob);
1568 8de9d8ad 2023-02-20 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1569 be94c032 2023-02-20 thomas if (err)
1570 be94c032 2023-02-20 thomas goto done;
1573 8de9d8ad 2023-02-20 thomas err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1574 8de9d8ad 2023-02-20 thomas 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1575 8de9d8ad 2023-02-20 thomas if (err)
1576 8de9d8ad 2023-02-20 thomas goto done;
1578 8de9d8ad 2023-02-20 thomas r = diffreg_result->result;
1580 8de9d8ad 2023-02-20 thomas for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1581 8de9d8ad 2023-02-20 thomas struct diff_chunk *c;
1582 8de9d8ad 2023-02-20 thomas struct diff_chunk_context cc = {};
1583 450d9f6b 2023-02-20 thomas off_t pos;
1586 8de9d8ad 2023-02-20 thomas * We can optimise a little by advancing straight
1587 8de9d8ad 2023-02-20 thomas * to the next chunk if this one has no added lines.
1589 8de9d8ad 2023-02-20 thomas c = diff_chunk_get(r, n);
1591 47c7ee21 2023-02-20 thomas if (diff_chunk_type(c) != CHUNK_PLUS) {
1592 8de9d8ad 2023-02-20 thomas nchunks_parsed = 1;
1593 450d9f6b 2023-02-20 thomas continue; /* removed or unchanged lines */
1596 450d9f6b 2023-02-20 thomas pos = diff_chunk_get_right_start_pos(c);
1597 450d9f6b 2023-02-20 thomas if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1598 450d9f6b 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1599 450d9f6b 2023-02-20 thomas goto done;
1602 450d9f6b 2023-02-20 thomas diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1603 450d9f6b 2023-02-20 thomas ln = cc.right.start;
1605 8de9d8ad 2023-02-20 thomas while (ln < cc.right.end) {
1606 8de9d8ad 2023-02-20 thomas linelen = getline(&line, &linesize, ondisk_file);
1607 8de9d8ad 2023-02-20 thomas if (linelen == -1) {
1608 8de9d8ad 2023-02-20 thomas if (feof(ondisk_file))
1610 8de9d8ad 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1614 8de9d8ad 2023-02-20 thomas if (line && strncmp(line, markers[i],
1615 8de9d8ad 2023-02-20 thomas strlen(markers[i])) == 0) {
1616 8de9d8ad 2023-02-20 thomas if (strcmp(markers[i],
1617 8de9d8ad 2023-02-20 thomas GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1618 8de9d8ad 2023-02-20 thomas *status = GOT_STATUS_CONFLICT;
1619 8de9d8ad 2023-02-20 thomas goto done;
1628 9bdd68dd 2020-01-02 naddy free(line);
1629 be94c032 2023-02-20 thomas if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1630 be94c032 2023-02-20 thomas err = got_error_from_errno("fclose");
1631 8de9d8ad 2023-02-20 thomas free_err = got_diffreg_result_free(diffreg_result);
1632 8de9d8ad 2023-02-20 thomas if (err == NULL)
1633 8de9d8ad 2023-02-20 thomas err = free_err;
1635 7154f6ce 2019-03-27 stsp return err;
1638 e2b1e152 2019-07-13 stsp static int
1639 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1641 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1642 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1645 1ebedb77 2019-10-19 stsp static int
1646 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1648 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1649 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1650 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1651 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1652 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1653 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1656 c363b2c1 2019-08-03 stsp static unsigned char
1657 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1659 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1660 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1661 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1662 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1663 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1664 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1665 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1667 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1671 a919d5c4 2020-07-23 stsp static const struct got_error *
1672 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1673 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1674 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1676 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1677 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1678 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1679 a919d5c4 2020-07-23 stsp ssize_t elen;
1680 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1681 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1682 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1684 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1686 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1688 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1690 a919d5c4 2020-07-23 stsp return err;
1691 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1693 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1694 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1696 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1698 a919d5c4 2020-07-23 stsp if (len > 0) {
1699 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1700 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1701 a919d5c4 2020-07-23 stsp len - hdrlen);
1702 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1703 a919d5c4 2020-07-23 stsp hdrlen = 0;
1705 a919d5c4 2020-07-23 stsp } while (len != 0);
1706 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1708 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1709 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1710 a919d5c4 2020-07-23 stsp if (elen == -1)
1711 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1713 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1714 a919d5c4 2020-07-23 stsp if (elen == -1)
1715 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1718 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1719 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1721 a919d5c4 2020-07-23 stsp return NULL;
1724 7154f6ce 2019-03-27 stsp static const struct got_error *
1725 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1726 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1727 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1729 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1730 6353ad76 2019-02-08 stsp struct got_object_id id;
1731 6353ad76 2019-02-08 stsp size_t hdrlen;
1732 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1733 6353ad76 2019-02-08 stsp FILE *f = NULL;
1734 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1735 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1736 6353ad76 2019-02-08 stsp size_t flen, blen;
1737 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
1739 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
1740 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1741 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1744 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1745 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1746 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1748 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1749 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1750 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1751 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1752 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1754 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1755 882ef1b9 2019-12-13 stsp goto done;
1757 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1758 3d35a492 2019-12-13 stsp goto done;
1761 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1762 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1763 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1764 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1765 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1766 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1767 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1768 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1769 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1770 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1771 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1773 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1774 3d35a492 2019-12-13 stsp goto done;
1776 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1777 1338848f 2019-12-13 stsp goto done;
1781 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1782 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1783 1338848f 2019-12-13 stsp goto done;
1786 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1787 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1788 1338848f 2019-12-13 stsp goto done;
1789 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1790 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1791 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1792 1338848f 2019-12-13 stsp goto done;
1795 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1796 f179e66d 2020-07-23 stsp goto done;
1798 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1799 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1800 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1801 1338848f 2019-12-13 stsp goto done;
1804 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1805 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1806 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
1808 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
1810 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1811 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1812 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1813 f4ae6ddb 2022-07-01 thomas goto done;
1815 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1817 1338848f 2019-12-13 stsp goto done;
1819 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1820 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1821 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1822 a919d5c4 2020-07-23 stsp goto done;
1825 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1826 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1827 ab0d4361 2019-12-13 stsp if (fd == -1) {
1828 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1829 ab0d4361 2019-12-13 stsp goto done;
1833 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1834 6353ad76 2019-02-08 stsp if (f == NULL) {
1835 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1836 6353ad76 2019-02-08 stsp goto done;
1839 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1841 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1842 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1844 7154f6ce 2019-03-27 stsp goto done;
1845 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1846 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1847 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1848 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1849 7154f6ce 2019-03-27 stsp goto done;
1851 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1852 6353ad76 2019-02-08 stsp if (flen != 0)
1853 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1855 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1856 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1857 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1859 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1860 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1861 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1862 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1866 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1869 6353ad76 2019-02-08 stsp hdrlen = 0;
1872 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1873 7154f6ce 2019-03-27 stsp rewind(f);
1874 be94c032 2023-02-20 thomas err = get_modified_file_content_status(status, blob, ie->path,
1876 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1877 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1879 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1880 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1882 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1883 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1884 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1885 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1886 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1887 9d31a1d8 2018-03-11 stsp return err;
1891 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1892 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1894 e2b1e152 2019-07-13 stsp static const struct got_error *
1895 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1896 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1898 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1899 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1900 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1902 e2b1e152 2019-07-13 stsp return NULL;
1905 9f323212 2023-03-10 thomas static const struct got_error *remove_ondisk_file(const char *, const char *);
1907 9d31a1d8 2018-03-11 stsp static const struct got_error *
1908 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1909 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1910 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1911 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1912 0584f854 2019-04-06 stsp void *progress_arg)
1914 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1915 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1916 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1917 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1918 b8f41171 2019-02-10 stsp struct stat sb;
1919 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1920 4f46fcf8 2024-02-13 thomas int update_timestamps = 0;
1922 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1923 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1926 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1927 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1928 a76c42e6 2019-08-03 stsp goto done;
1930 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1933 abb4604f 2019-07-27 stsp goto done;
1934 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1935 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1937 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1938 9f323212 2023-03-10 thomas if (errno != ENOENT && errno != ENOTDIR) {
1939 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1940 e6f4ba31 2021-09-24 thomas ondisk_path);
1941 e6f4ba31 2021-09-24 thomas goto done;
1943 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1944 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1945 e6f4ba31 2021-09-24 thomas } else {
1946 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1947 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1949 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1953 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1955 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1956 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1957 b8f41171 2019-02-10 stsp goto done;
1959 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1961 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1962 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1964 5036ab18 2020-04-18 stsp goto done;
1967 9f323212 2023-03-10 thomas if (S_ISDIR(te->mode)) { /* file changing into a directory */
1968 9f323212 2023-03-10 thomas if (status == GOT_STATUS_UNVERSIONED) {
1969 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, status, path);
1970 9f323212 2023-03-10 thomas } else if (status != GOT_STATUS_NO_CHANGE &&
1971 9f323212 2023-03-10 thomas status != GOT_STATUS_DELETE &&
1972 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1973 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1974 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg,
1975 9f323212 2023-03-10 thomas GOT_STATUS_CANNOT_DELETE, path);
1976 9f323212 2023-03-10 thomas } else if (ie) {
1977 9f323212 2023-03-10 thomas if (status != GOT_STATUS_DELETE &&
1978 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1979 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1980 9f323212 2023-03-10 thomas err = remove_ondisk_file(worktree->root_path,
1981 9f323212 2023-03-10 thomas ie->path);
1982 9f323212 2023-03-10 thomas if (err && !(err->code == GOT_ERR_ERRNO &&
1983 9f323212 2023-03-10 thomas errno == ENOENT))
1984 9f323212 2023-03-10 thomas goto done;
1986 9f323212 2023-03-10 thomas got_fileindex_entry_remove(fileindex, ie);
1987 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1988 9f323212 2023-03-10 thomas ie->path);
1990 9f323212 2023-03-10 thomas goto done; /* nothing else to do */
1993 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1994 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1995 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1997 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1998 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1999 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
2000 c6e8a826 2021-04-05 stsp * updating contents of this file.
2002 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
2003 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
2004 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
2005 c6e8a826 2021-04-05 stsp /* Same commit. */
2006 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2007 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2009 e2b1e152 2019-07-13 stsp goto done;
2010 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2012 6353ad76 2019-02-08 stsp goto done;
2014 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
2015 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
2016 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
2017 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
2018 b36a9774 2023-04-14 thomas if (got_fileindex_entry_has_commit(ie)) {
2019 b36a9774 2023-04-14 thomas /* Update the base commit ID of this file. */
2020 b36a9774 2023-04-14 thomas memcpy(ie->commit_sha1,
2021 b36a9774 2023-04-14 thomas worktree->base_commit_id->sha1,
2022 b36a9774 2023-04-14 thomas sizeof(ie->commit_sha1));
2024 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2025 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2027 0f58026f 2021-04-05 stsp goto done;
2028 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2030 b8f41171 2019-02-10 stsp goto done;
2034 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
2035 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
2036 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2037 f4ae6ddb 2022-07-01 thomas goto done;
2039 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2041 6353ad76 2019-02-08 stsp goto done;
2043 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2044 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2045 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2046 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2047 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
2048 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
2049 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2050 f4ae6ddb 2022-07-01 thomas goto done;
2052 234035bc 2019-06-01 stsp struct got_object_id id2;
2053 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id2, ie);
2054 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2057 f69721c3 2019-10-21 stsp goto done;
2059 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2060 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2061 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2062 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2063 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2064 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2065 f69721c3 2019-10-21 stsp goto done;
2067 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2068 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2069 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2070 234035bc 2019-06-01 stsp goto done;
2073 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2074 36bf999c 2020-07-23 stsp char *link_target;
2075 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2077 36bf999c 2020-07-23 stsp goto done;
2078 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2079 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2080 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2081 36bf999c 2020-07-23 stsp free(link_target);
2083 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2084 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2085 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2086 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2088 f69721c3 2019-10-21 stsp free(label_orig);
2089 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2090 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2091 f4ae6ddb 2022-07-01 thomas goto done;
2093 234035bc 2019-06-01 stsp if (blob2)
2094 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2096 909d120e 2019-09-22 stsp goto done;
2098 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2099 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2100 234035bc 2019-06-01 stsp * unmodified files again.
2102 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2103 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2104 234035bc 2019-06-01 stsp update_timestamps);
2105 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2106 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2107 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2108 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2109 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2111 1ee397ad 2019-07-12 stsp goto done;
2112 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2113 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2115 13d9040b 2019-03-27 stsp goto done;
2117 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2118 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2119 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2120 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2121 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2122 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2123 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2125 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2126 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2127 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2128 aaf972e2 2023-09-18 thomas status == GOT_STATUS_UNVERSIONED,
2129 aaf972e2 2023-09-18 thomas &update_timestamps,
2130 aaf972e2 2023-09-18 thomas repo, progress_cb, progress_arg);
2133 13d9040b 2019-03-27 stsp goto done;
2136 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2137 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2138 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2140 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2141 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2142 aaf972e2 2023-09-18 thomas &blob->id, update_timestamps);
2145 13d9040b 2019-03-27 stsp goto done;
2147 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2148 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2149 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2153 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2154 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2155 f4ae6ddb 2022-07-01 thomas goto done;
2157 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2159 6353ad76 2019-02-08 stsp free(ondisk_path);
2160 8da9e5f4 2019-01-12 stsp return err;
2163 90285c3b 2019-01-08 stsp static const struct got_error *
2164 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2166 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2167 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2169 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2170 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2172 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2173 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2174 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2176 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2177 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2179 1c4cdd89 2021-06-20 stsp goto done;
2180 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2181 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2182 fddefe3b 2020-10-20 stsp free(ondisk_path);
2183 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2184 1c4cdd89 2021-06-20 stsp parent = NULL;
2185 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2186 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2187 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2188 fddefe3b 2020-10-20 stsp ondisk_path);
2191 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2197 90285c3b 2019-01-08 stsp free(ondisk_path);
2198 1c4cdd89 2021-06-20 stsp free(parent);
2199 90285c3b 2019-01-08 stsp return err;
2202 708d8e67 2019-03-27 stsp static const struct got_error *
2203 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2204 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2205 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2207 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2208 708d8e67 2019-03-27 stsp unsigned char status;
2209 708d8e67 2019-03-27 stsp struct stat sb;
2210 708d8e67 2019-03-27 stsp char *ondisk_path;
2212 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2213 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2215 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2217 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2219 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2221 5a58a424 2020-06-23 stsp goto done;
2223 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2224 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2225 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2226 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2227 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2228 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2229 993e2a1b 2020-07-23 stsp goto done;
2231 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2232 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2233 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2234 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2236 993e2a1b 2020-07-23 stsp goto done;
2237 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2238 993e2a1b 2020-07-23 stsp ie->path);
2239 993e2a1b 2020-07-23 stsp goto done;
2242 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2243 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2244 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2246 5a58a424 2020-06-23 stsp goto done;
2248 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2249 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2251 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2252 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2254 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2256 5a58a424 2020-06-23 stsp goto done;
2257 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2258 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2260 5a58a424 2020-06-23 stsp goto done;
2262 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2265 5a58a424 2020-06-23 stsp free(ondisk_path);
2266 fc6346c4 2019-03-27 stsp return err;
2269 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2270 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2271 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2272 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2273 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2274 8da9e5f4 2019-01-12 stsp void *progress_arg;
2275 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2276 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2279 512f0d0e 2019-01-02 stsp static const struct got_error *
2280 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2281 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2283 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
2284 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2286 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
2287 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
2288 eb6fb9cd 2023-07-26 thomas if (err)
2289 eb6fb9cd 2023-07-26 thomas return err;
2292 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2293 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2296 8da9e5f4 2019-01-12 stsp static const struct got_error *
2297 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2299 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
2300 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2302 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
2303 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
2304 eb6fb9cd 2023-07-26 thomas if (err)
2305 eb6fb9cd 2023-07-26 thomas return err;
2308 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2309 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2312 9d31a1d8 2018-03-11 stsp static const struct got_error *
2313 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2315 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
2316 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2317 8da9e5f4 2019-01-12 stsp char *path;
2319 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
2320 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
2321 eb6fb9cd 2023-07-26 thomas if (err)
2322 eb6fb9cd 2023-07-26 thomas return err;
2325 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2326 59530ea7 2023-07-17 thomas return NULL;
2328 59530ea7 2023-07-17 thomas if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2329 63c5ca5d 2019-08-24 stsp return NULL;
2331 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2332 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2334 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2336 59530ea7 2023-07-17 thomas err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2337 59530ea7 2023-07-17 thomas a->repo, a->progress_cb, a->progress_arg);
2339 8da9e5f4 2019-01-12 stsp free(path);
2340 0cd1c46a 2019-03-11 stsp return err;
2343 b2118c49 2020-07-28 stsp const struct got_error *
2344 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2346 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2348 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2349 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2350 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2351 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2354 b2118c49 2020-07-28 stsp return NULL;
2357 818c7501 2019-07-11 stsp static const struct got_error *
2358 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2360 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2361 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2363 517bab73 2019-03-11 stsp *refname = NULL;
2365 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2367 b2118c49 2020-07-28 stsp return err;
2369 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2370 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2371 0647c563 2019-03-11 stsp *refname = NULL;
2373 517bab73 2019-03-11 stsp free(uuidstr);
2374 517bab73 2019-03-11 stsp return err;
2377 818c7501 2019-07-11 stsp const struct got_error *
2378 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2379 f6cd0243 2023-01-28 thomas const char *prefix)
2381 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2384 fcfaa6d5 2023-07-05 thomas static const struct got_error *
2385 fcfaa6d5 2023-07-05 thomas get_base_ref_name(char **refname, struct got_worktree *worktree)
2387 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2390 818c7501 2019-07-11 stsp static const struct got_error *
2391 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2393 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2394 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2397 818c7501 2019-07-11 stsp static const struct got_error *
2398 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2400 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2403 818c7501 2019-07-11 stsp static const struct got_error *
2404 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2406 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2407 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2410 818c7501 2019-07-11 stsp static const struct got_error *
2411 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2413 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2414 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2417 0ebf8283 2019-07-24 stsp static const struct got_error *
2418 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2420 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2421 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2424 0ebf8283 2019-07-24 stsp static const struct got_error *
2425 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2427 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2428 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2431 0ebf8283 2019-07-24 stsp static const struct got_error *
2432 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2434 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2435 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2438 0ebf8283 2019-07-24 stsp static const struct got_error *
2439 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2441 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2442 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2445 0ebf8283 2019-07-24 stsp const struct got_error *
2446 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2447 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2449 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2450 ad10f64e 2023-07-19 thomas worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2451 0ebf8283 2019-07-24 stsp *path = NULL;
2452 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2454 0ebf8283 2019-07-24 stsp return NULL;
2457 10604dce 2021-09-24 thomas static const struct got_error *
2458 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2460 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2461 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2464 10604dce 2021-09-24 thomas static const struct got_error *
2465 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2467 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2468 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2472 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2473 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2475 517bab73 2019-03-11 stsp static const struct got_error *
2476 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2478 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2479 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2480 517bab73 2019-03-11 stsp char *refname;
2482 fcfaa6d5 2023-07-05 thomas err = get_base_ref_name(&refname, worktree);
2484 517bab73 2019-03-11 stsp return err;
2486 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2488 0cd1c46a 2019-03-11 stsp goto done;
2490 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2492 0cd1c46a 2019-03-11 stsp free(refname);
2494 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2495 3e3a69f1 2019-07-25 stsp return err;
2498 3e3a69f1 2019-07-25 stsp static const struct got_error *
2499 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2501 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2503 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2504 ad10f64e 2023-07-19 thomas worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2505 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2506 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2508 9d31a1d8 2018-03-11 stsp return err;
2512 ebf99748 2019-05-09 stsp static const struct got_error *
2513 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2514 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2516 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2517 ebf99748 2019-05-09 stsp FILE *index = NULL;
2519 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2520 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2521 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2522 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2524 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2526 ebf99748 2019-05-09 stsp goto done;
2528 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2529 ebf99748 2019-05-09 stsp if (index == NULL) {
2530 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2531 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2533 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2534 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2535 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2538 ebf99748 2019-05-09 stsp if (err) {
2539 ebf99748 2019-05-09 stsp free(*fileindex_path);
2540 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2541 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2542 ebf99748 2019-05-09 stsp *fileindex = NULL;
2544 ebf99748 2019-05-09 stsp return err;
2547 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2548 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2549 c932eeeb 2019-05-22 stsp const char *path;
2550 c932eeeb 2019-05-22 stsp size_t path_len;
2551 c932eeeb 2019-05-22 stsp const char *entry_name;
2552 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2553 a484d721 2019-06-10 stsp void *progress_arg;
2556 c932eeeb 2019-05-22 stsp static const struct got_error *
2557 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2559 1ee397ad 2019-07-12 stsp const struct got_error *err;
2560 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2562 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2563 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2564 c932eeeb 2019-05-22 stsp return NULL;
2565 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2566 102ff934 2019-06-11 stsp return NULL;
2568 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2569 a769b60b 2021-06-27 stsp return NULL;
2571 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2572 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2573 c932eeeb 2019-05-22 stsp return NULL;
2575 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2576 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2577 edd02c5e 2019-07-11 stsp ie->path);
2579 1ee397ad 2019-07-12 stsp return err;
2581 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2582 c932eeeb 2019-05-22 stsp return NULL;
2585 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2586 a615e0e7 2020-12-16 stsp static const struct got_error *
2587 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2588 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2589 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2591 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2593 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2594 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2595 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2596 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2597 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2598 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2600 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2601 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2604 9c6338c4 2019-06-02 stsp static const struct got_error *
2605 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2607 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2608 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2609 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2610 867630bb 2020-01-17 stsp struct timespec timeout;
2612 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2613 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2615 9c6338c4 2019-06-02 stsp goto done;
2617 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2619 9c6338c4 2019-06-02 stsp goto done;
2621 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2622 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2623 9c6338c4 2019-06-02 stsp fileindex_path);
2624 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2628 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2629 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2630 867630bb 2020-01-17 stsp * was recorded in the file index.
2632 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2633 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2634 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2636 9c6338c4 2019-06-02 stsp if (new_index)
2637 9c6338c4 2019-06-02 stsp fclose(new_index);
2638 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2639 9c6338c4 2019-06-02 stsp return err;
2642 6ced4176 2019-07-12 stsp static const struct got_error *
2643 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2644 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2645 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2646 945f9229 2022-04-16 thomas struct got_repository *repo)
2648 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2649 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2650 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2651 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2653 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2654 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2655 6ced4176 2019-07-12 stsp *tree_id = NULL;
2657 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2658 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2659 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2660 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2661 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2662 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2663 6ced4176 2019-07-12 stsp goto done;
2665 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2666 945f9229 2022-04-16 thomas worktree->path_prefix);
2668 6ced4176 2019-07-12 stsp goto done;
2669 6ced4176 2019-07-12 stsp return NULL;
2672 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2674 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2675 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2676 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2677 6ced4176 2019-07-12 stsp goto done;
2680 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2682 6ced4176 2019-07-12 stsp goto done;
2684 6ced4176 2019-07-12 stsp free(in_repo_path);
2685 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2687 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2689 6ced4176 2019-07-12 stsp goto done;
2691 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2692 6ced4176 2019-07-12 stsp /* Check out a single file. */
2693 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2694 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2695 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2696 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2697 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2698 6ced4176 2019-07-12 stsp goto done;
2700 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2701 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2702 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2703 6ced4176 2019-07-12 stsp goto done;
2706 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2707 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2709 6ced4176 2019-07-12 stsp return err;
2710 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2711 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2712 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2713 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2714 6ced4176 2019-07-12 stsp goto done;
2717 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2718 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2720 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2721 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2722 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2723 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2724 6ced4176 2019-07-12 stsp goto done;
2726 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2727 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2728 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2729 6ced4176 2019-07-12 stsp goto done;
2734 6ced4176 2019-07-12 stsp free(in_repo_path);
2735 6ced4176 2019-07-12 stsp if (err) {
2736 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2737 6ced4176 2019-07-12 stsp free(*tree_relpath);
2738 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2739 6ced4176 2019-07-12 stsp free(*tree_id);
2740 6ced4176 2019-07-12 stsp *tree_id = NULL;
2742 07ed472d 2019-07-12 stsp return err;
2745 07ed472d 2019-07-12 stsp static const struct got_error *
2746 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2747 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2748 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2749 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2751 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2752 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2753 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2754 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2755 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2757 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2758 7f47418f 2019-12-20 stsp if (err) {
2759 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2760 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2761 7f47418f 2019-12-20 stsp goto done;
2762 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2763 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2765 7f47418f 2019-12-20 stsp return err;
2768 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2769 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2771 07ed472d 2019-07-12 stsp goto done;
2773 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2775 07ed472d 2019-07-12 stsp goto done;
2777 07ed472d 2019-07-12 stsp if (entry_name &&
2778 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2779 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2780 07ed472d 2019-07-12 stsp goto done;
2783 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2784 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2785 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2786 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2787 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2788 07ed472d 2019-07-12 stsp arg.repo = repo;
2789 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2790 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2791 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2792 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2793 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2794 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2797 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2798 07ed472d 2019-07-12 stsp if (commit)
2799 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2800 6ced4176 2019-07-12 stsp return err;
2803 9d31a1d8 2018-03-11 stsp const struct got_error *
2804 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2805 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2806 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2807 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2809 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2810 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2811 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2812 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2813 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2814 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2815 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2816 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2817 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2818 f2ea84fa 2019-07-27 stsp int entry_type;
2819 f2ea84fa 2019-07-27 stsp char *relpath;
2820 f2ea84fa 2019-07-27 stsp char *entry_name;
2821 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2822 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2824 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2826 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2828 9d31a1d8 2018-03-11 stsp return err;
2830 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2831 945f9229 2022-04-16 thomas worktree->base_commit_id);
2832 945f9229 2022-04-16 thomas if (err)
2833 945f9229 2022-04-16 thomas goto done;
2835 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2836 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2837 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2838 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2839 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2840 f2ea84fa 2019-07-27 stsp goto done;
2843 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2844 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2845 945f9229 2022-04-16 thomas worktree, repo);
2846 f2ea84fa 2019-07-27 stsp if (err) {
2847 f2ea84fa 2019-07-27 stsp free(tpd);
2848 6ced4176 2019-07-12 stsp goto done;
2851 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2852 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2853 f2ea84fa 2019-07-27 stsp if (err) {
2854 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2855 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2856 f2ea84fa 2019-07-27 stsp free(tpd);
2857 f2ea84fa 2019-07-27 stsp goto done;
2860 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2862 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2866 51514078 2018-12-25 stsp * Read the file index.
2867 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2868 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2870 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2872 c4cdcb68 2019-04-03 stsp goto done;
2874 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2875 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2876 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2878 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2879 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2880 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2884 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2885 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2886 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2887 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2888 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2889 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2890 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2891 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2895 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2897 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2898 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2899 af12c6b9 2019-06-04 stsp err = sync_err;
2901 9c6338c4 2019-06-02 stsp free(fileindex_path);
2903 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2904 9d31a1d8 2018-03-11 stsp if (commit)
2905 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2906 5ade8233 2019-07-12 stsp if (fileindex)
2907 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2908 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2909 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2910 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2911 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2912 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2913 f2ea84fa 2019-07-27 stsp free(tpd);
2915 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2916 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2917 9d31a1d8 2018-03-11 stsp err = unlockerr;
2918 f8d1f275 2019-02-04 stsp return err;
2921 0529f8df 2023-03-10 thomas static const struct got_error *
2922 0529f8df 2023-03-10 thomas add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2923 0529f8df 2023-03-10 thomas struct got_fileindex_entry *ie, const char *ondisk_path,
2924 0529f8df 2023-03-10 thomas const char *path2, struct got_blob_object *blob2, mode_t mode2,
2925 0529f8df 2023-03-10 thomas int restoring_missing_file, int reverting_versioned_file,
2926 0529f8df 2023-03-10 thomas int path_is_unversioned, int allow_bad_symlinks,
2927 0529f8df 2023-03-10 thomas struct got_repository *repo,
2928 0529f8df 2023-03-10 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
2930 0529f8df 2023-03-10 thomas const struct got_error *err = NULL;
2931 0529f8df 2023-03-10 thomas int is_bad_symlink = 0;
2933 0529f8df 2023-03-10 thomas if (S_ISLNK(mode2)) {
2934 0529f8df 2023-03-10 thomas err = install_symlink(&is_bad_symlink,
2935 0529f8df 2023-03-10 thomas worktree, ondisk_path, path2, blob2,
2936 0529f8df 2023-03-10 thomas restoring_missing_file,
2937 0529f8df 2023-03-10 thomas reverting_versioned_file,
2938 0529f8df 2023-03-10 thomas path_is_unversioned, allow_bad_symlinks,
2939 0529f8df 2023-03-10 thomas repo, progress_cb, progress_arg);
2940 0529f8df 2023-03-10 thomas } else {
2941 0529f8df 2023-03-10 thomas err = install_blob(worktree, ondisk_path, path2,
2942 0529f8df 2023-03-10 thomas mode2, GOT_DEFAULT_FILE_MODE, blob2,
2943 0529f8df 2023-03-10 thomas restoring_missing_file, reverting_versioned_file, 0,
2944 aaf972e2 2023-09-18 thomas path_is_unversioned, NULL, repo,
2945 aaf972e2 2023-09-18 thomas progress_cb, progress_arg);
2947 0529f8df 2023-03-10 thomas if (err)
2948 0529f8df 2023-03-10 thomas return err;
2949 0529f8df 2023-03-10 thomas if (ie == NULL) {
2950 0529f8df 2023-03-10 thomas /* Adding an unversioned file. */
2951 0529f8df 2023-03-10 thomas err = got_fileindex_entry_alloc(&ie, path2);
2952 0529f8df 2023-03-10 thomas if (err)
2953 0529f8df 2023-03-10 thomas return err;
2954 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2955 0529f8df 2023-03-10 thomas worktree->root_fd, path2, NULL, NULL, 1);
2956 0529f8df 2023-03-10 thomas if (err) {
2957 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2958 0529f8df 2023-03-10 thomas return err;
2960 0529f8df 2023-03-10 thomas err = got_fileindex_entry_add(fileindex, ie);
2961 0529f8df 2023-03-10 thomas if (err) {
2962 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2963 0529f8df 2023-03-10 thomas return err;
2965 0529f8df 2023-03-10 thomas } else {
2966 0529f8df 2023-03-10 thomas /* Re-adding a locally deleted file. */
2967 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2968 0529f8df 2023-03-10 thomas worktree->root_fd, path2, ie->blob_sha1,
2969 0529f8df 2023-03-10 thomas worktree->base_commit_id->sha1, 0);
2970 0529f8df 2023-03-10 thomas if (err)
2971 0529f8df 2023-03-10 thomas return err;
2974 0529f8df 2023-03-10 thomas if (is_bad_symlink) {
2975 0529f8df 2023-03-10 thomas got_fileindex_entry_filetype_set(ie,
2976 0529f8df 2023-03-10 thomas GOT_FILEIDX_MODE_BAD_SYMLINK);
2979 0529f8df 2023-03-10 thomas return NULL;
2982 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2983 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2984 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2985 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2986 234035bc 2019-06-01 stsp void *progress_arg;
2987 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2988 234035bc 2019-06-01 stsp void *cancel_arg;
2989 f69721c3 2019-10-21 stsp const char *label_orig;
2990 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2991 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2994 234035bc 2019-06-01 stsp static const struct got_error *
2995 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2996 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2997 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2998 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2999 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3001 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
3002 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
3003 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
3004 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
3005 234035bc 2019-06-01 stsp struct stat sb;
3006 234035bc 2019-06-01 stsp unsigned char status;
3007 234035bc 2019-06-01 stsp int local_changes_subsumed;
3008 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3009 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
3010 4c3671a9 2023-07-26 thomas struct got_object_id *id = NULL;
3012 234035bc 2019-06-01 stsp if (blob1 && blob2) {
3013 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3014 d6c87207 2019-08-02 stsp strlen(path2));
3015 1ee397ad 2019-07-12 stsp if (ie == NULL)
3016 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3017 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
3019 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3020 234035bc 2019-06-01 stsp path2) == -1)
3021 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3023 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3026 234035bc 2019-06-01 stsp goto done;
3028 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3029 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3030 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
3031 234035bc 2019-06-01 stsp goto done;
3033 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3034 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3035 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3036 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3037 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3038 234035bc 2019-06-01 stsp goto done;
3041 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3042 36bf999c 2020-07-23 stsp char *link_target2;
3043 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3045 36bf999c 2020-07-23 stsp goto done;
3046 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3047 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3048 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3049 36bf999c 2020-07-23 stsp free(link_target2);
3053 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3054 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3055 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3056 1af628f4 2021-06-03 stsp goto done;
3058 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3059 1af628f4 2021-06-03 stsp f_orig, blob1);
3061 1af628f4 2021-06-03 stsp goto done;
3063 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3064 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3065 1af628f4 2021-06-03 stsp goto done;
3066 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3067 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3069 1af628f4 2021-06-03 stsp goto done;
3071 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
3072 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3073 1af628f4 2021-06-03 stsp if (fd == -1) {
3074 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3075 1af628f4 2021-06-03 stsp ondisk_path);
3076 1af628f4 2021-06-03 stsp goto done;
3078 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3079 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3080 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3081 1af628f4 2021-06-03 stsp ondisk_path);
3082 1af628f4 2021-06-03 stsp close(fd);
3083 1af628f4 2021-06-03 stsp goto done;
3085 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3087 1af628f4 2021-06-03 stsp goto done;
3088 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3089 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3090 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3091 1af628f4 2021-06-03 stsp goto done;
3093 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3094 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3095 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
3096 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3097 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3099 234035bc 2019-06-01 stsp } else if (blob1) {
3100 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3101 d6c87207 2019-08-02 stsp strlen(path1));
3102 1ee397ad 2019-07-12 stsp if (ie == NULL)
3103 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3104 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3106 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3107 2b92fad7 2019-06-02 stsp path1) == -1)
3108 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3110 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3113 2b92fad7 2019-06-02 stsp goto done;
3115 2b92fad7 2019-06-02 stsp switch (status) {
3116 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3117 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3118 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3120 1ee397ad 2019-07-12 stsp goto done;
3121 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3123 2b92fad7 2019-06-02 stsp goto done;
3125 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3127 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3128 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3129 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3130 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3132 1ee397ad 2019-07-12 stsp goto done;
3134 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3136 4c3671a9 2023-07-26 thomas case GOT_STATUS_MODIFY: {
3137 4c3671a9 2023-07-26 thomas FILE *blob1_f;
3138 4c3671a9 2023-07-26 thomas off_t blob1_size;
3139 4c3671a9 2023-07-26 thomas int obj_type;
3141 4c3671a9 2023-07-26 thomas * Delete the file only if its content already
3142 4c3671a9 2023-07-26 thomas * exists in the repository.
3144 4c3671a9 2023-07-26 thomas err = got_object_blob_file_create(&id, &blob1_f,
3145 4c3671a9 2023-07-26 thomas &blob1_size, path1);
3146 4c3671a9 2023-07-26 thomas if (err)
3147 4c3671a9 2023-07-26 thomas goto done;
3148 4c3671a9 2023-07-26 thomas if (fclose(blob1_f) == EOF) {
3149 4c3671a9 2023-07-26 thomas err = got_error_from_errno("fclose");
3150 4c3671a9 2023-07-26 thomas goto done;
3153 4c3671a9 2023-07-26 thomas /* Implied existence check. */
3154 4c3671a9 2023-07-26 thomas err = got_object_get_type(&obj_type, repo, id);
3155 4c3671a9 2023-07-26 thomas if (err) {
3156 4c3671a9 2023-07-26 thomas if (err->code != GOT_ERR_NO_OBJ)
3157 4c3671a9 2023-07-26 thomas goto done;
3158 4c3671a9 2023-07-26 thomas err = (*a->progress_cb)(a->progress_arg,
3159 4c3671a9 2023-07-26 thomas GOT_STATUS_CANNOT_DELETE, path1);
3160 4c3671a9 2023-07-26 thomas goto done;
3161 4c3671a9 2023-07-26 thomas } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3162 4c3671a9 2023-07-26 thomas err = (*a->progress_cb)(a->progress_arg,
3163 4c3671a9 2023-07-26 thomas GOT_STATUS_CANNOT_DELETE, path1);
3164 4c3671a9 2023-07-26 thomas goto done;
3166 4c3671a9 2023-07-26 thomas err = (*a->progress_cb)(a->progress_arg,
3167 4c3671a9 2023-07-26 thomas GOT_STATUS_DELETE, path1);
3168 4c3671a9 2023-07-26 thomas if (err)
3169 4c3671a9 2023-07-26 thomas goto done;
3170 4c3671a9 2023-07-26 thomas err = remove_ondisk_file(a->worktree->root_path,
3172 4c3671a9 2023-07-26 thomas if (err)
3173 4c3671a9 2023-07-26 thomas goto done;
3175 4c3671a9 2023-07-26 thomas got_fileindex_entry_mark_deleted_from_disk(ie);
3178 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3179 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3180 e8f02263 2022-01-23 thomas off_t blob1_size;
3182 4c3671a9 2023-07-26 thomas * Delete the file only if its content already
3183 0a22ca1a 2020-09-23 stsp * exists in the repository.
3185 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
3186 e8f02263 2022-01-23 thomas &blob1_size, path1);
3188 0a22ca1a 2020-09-23 stsp goto done;
3189 4c3671a9 2023-07-26 thomas if (fclose(blob1_f) == EOF) {
3190 4c3671a9 2023-07-26 thomas err = got_error_from_errno("fclose");
3191 4c3671a9 2023-07-26 thomas goto done;
3193 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3194 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3195 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3197 0a22ca1a 2020-09-23 stsp goto done;
3198 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3201 0a22ca1a 2020-09-23 stsp goto done;
3203 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3206 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3207 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3210 0a22ca1a 2020-09-23 stsp goto done;
3213 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3214 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3215 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3217 1ee397ad 2019-07-12 stsp goto done;
3219 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3220 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3222 1ee397ad 2019-07-12 stsp goto done;
3227 234035bc 2019-06-01 stsp } else if (blob2) {
3228 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3229 234035bc 2019-06-01 stsp path2) == -1)
3230 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3231 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3232 d6c87207 2019-08-02 stsp strlen(path2));
3234 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3235 7f91a133 2019-12-13 stsp -1, NULL, repo);
3237 234035bc 2019-06-01 stsp goto done;
3238 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3239 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3240 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3241 0529f8df 2023-03-10 thomas status != GOT_STATUS_ADD &&
3242 0529f8df 2023-03-10 thomas status != GOT_STATUS_DELETE) {
3243 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3244 1ee397ad 2019-07-12 stsp status, path2);
3245 234035bc 2019-06-01 stsp goto done;
3247 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3248 36bf999c 2020-07-23 stsp char *link_target2;
3249 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3252 36bf999c 2020-07-23 stsp goto done;
3253 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3254 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3255 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3256 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3257 36bf999c 2020-07-23 stsp free(link_target2);
3258 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3259 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3260 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3261 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3262 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3263 526a746f 2020-07-23 stsp a->progress_arg);
3264 0529f8df 2023-03-10 thomas } else if (status != GOT_STATUS_DELETE) {
3265 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3266 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3269 dfe9fba0 2020-07-23 stsp goto done;
3270 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3271 0529f8df 2023-03-10 thomas /* Re-add file with content from new blob. */
3272 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, ie,
3273 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3274 0529f8df 2023-03-10 thomas 0, 0, 0, a->allow_bad_symlinks,
3275 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3277 234035bc 2019-06-01 stsp goto done;
3280 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, NULL,
3281 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3282 0529f8df 2023-03-10 thomas 0, 0, 1, a->allow_bad_symlinks,
3283 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3285 2b92fad7 2019-06-02 stsp goto done;
3289 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3290 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3291 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3292 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3293 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3294 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3295 1af628f4 2021-06-03 stsp free(id_str);
3296 4c3671a9 2023-07-26 thomas free(id);
3297 54d5be07 2021-06-03 stsp free(label_deriv2);
3298 234035bc 2019-06-01 stsp free(ondisk_path);
3299 234035bc 2019-06-01 stsp return err;
3302 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args {
3303 6ebb2263 2023-07-26 thomas struct got_worktree *worktree;
3304 6ebb2263 2023-07-26 thomas got_cancel_cb cancel_cb;
3305 6ebb2263 2023-07-26 thomas void *cancel_arg;
3308 69de9dd4 2021-09-03 stsp static const struct got_error *
3309 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3311 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
3312 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args *a = arg;
3314 6ebb2263 2023-07-26 thomas if (a->cancel_cb) {
3315 6ebb2263 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3316 6ebb2263 2023-07-26 thomas if (err)
3317 6ebb2263 2023-07-26 thomas return err;
3320 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3321 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3322 6ebb2263 2023-07-26 thomas memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3323 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3324 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3326 69de9dd4 2021-09-03 stsp return NULL;
3329 349dfd1e 2023-07-23 thomas const struct got_error *
3330 349dfd1e 2023-07-23 thomas got_worktree_get_state(char *state, struct got_repository *repo,
3331 6ebb2263 2023-07-26 thomas struct got_worktree *worktree,
3332 6ebb2263 2023-07-26 thomas got_cancel_cb cancel_cb, void *cancel_arg)
3334 349dfd1e 2023-07-23 thomas const struct got_error *err;
3335 349dfd1e 2023-07-23 thomas struct got_object_id *base_id, *head_id = NULL;
3336 349dfd1e 2023-07-23 thomas struct got_reference *head_ref;
3337 349dfd1e 2023-07-23 thomas struct got_fileindex *fileindex = NULL;
3338 349dfd1e 2023-07-23 thomas char *fileindex_path = NULL;
3339 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args cma;
3341 349dfd1e 2023-07-23 thomas if (worktree == NULL)
3342 349dfd1e 2023-07-23 thomas return got_error(GOT_ERR_NOT_WORKTREE);
3344 349dfd1e 2023-07-23 thomas err = got_ref_open(&head_ref, repo,
3345 349dfd1e 2023-07-23 thomas got_worktree_get_head_ref_name(worktree), 0);
3346 349dfd1e 2023-07-23 thomas if (err)
3347 349dfd1e 2023-07-23 thomas return err;
3349 349dfd1e 2023-07-23 thomas err = got_ref_resolve(&head_id, repo, head_ref);
3350 349dfd1e 2023-07-23 thomas if (err)
3351 349dfd1e 2023-07-23 thomas goto done;
3353 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_UNKNOWN;
3354 349dfd1e 2023-07-23 thomas base_id = got_worktree_get_base_commit_id(worktree);
3356 6ebb2263 2023-07-26 thomas cma.worktree = worktree;
3357 6ebb2263 2023-07-26 thomas cma.cancel_cb = cancel_cb;
3358 6ebb2263 2023-07-26 thomas cma.cancel_arg = cancel_arg;
3360 349dfd1e 2023-07-23 thomas if (got_object_id_cmp(base_id, head_id) == 0) {
3361 349dfd1e 2023-07-23 thomas err = open_fileindex(&fileindex, &fileindex_path, worktree);
3362 349dfd1e 2023-07-23 thomas if (err)
3363 349dfd1e 2023-07-23 thomas goto done;
3365 349dfd1e 2023-07-23 thomas err = got_fileindex_for_each_entry_safe(fileindex,
3366 6ebb2263 2023-07-26 thomas check_mixed_commits, &cma);
3367 349dfd1e 2023-07-23 thomas if (err == NULL)
3368 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_UPTODATE;
3369 92845f09 2023-07-26 thomas else if (err->code == GOT_ERR_MIXED_COMMITS) {
3370 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_OUTOFDATE;
3371 349dfd1e 2023-07-23 thomas err = NULL;
3374 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_OUTOFDATE;
3377 349dfd1e 2023-07-23 thomas free(head_id);
3378 349dfd1e 2023-07-23 thomas free(fileindex_path);
3379 349dfd1e 2023-07-23 thomas got_ref_close(head_ref);
3380 349dfd1e 2023-07-23 thomas if (fileindex != NULL)
3381 349dfd1e 2023-07-23 thomas got_fileindex_free(fileindex);
3382 349dfd1e 2023-07-23 thomas return err;
3385 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3386 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3387 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3388 234035bc 2019-06-01 stsp struct got_repository *repo;
3391 234035bc 2019-06-01 stsp static const struct got_error *
3392 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3393 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3394 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3395 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3396 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3398 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3399 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3400 234035bc 2019-06-01 stsp unsigned char status;
3401 234035bc 2019-06-01 stsp struct stat sb;
3402 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3403 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3404 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3405 234035bc 2019-06-01 stsp char *ondisk_path;
3407 69de9dd4 2021-09-03 stsp if (id == NULL)
3408 69de9dd4 2021-09-03 stsp return NULL;
3410 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3411 69de9dd4 2021-09-03 stsp if (ie == NULL)
3412 69de9dd4 2021-09-03 stsp return NULL;
3414 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3416 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3418 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3419 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3420 5546d466 2021-09-02 stsp free(ondisk_path);
3422 234035bc 2019-06-01 stsp return err;
3423 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3424 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3426 234035bc 2019-06-01 stsp return NULL;
3429 818c7501 2019-07-11 stsp static const struct got_error *
3430 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3431 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3432 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3433 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3434 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3436 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3437 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3438 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3439 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3440 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3441 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3442 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3443 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3444 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3446 03415a1a 2019-06-02 stsp if (commit_id1) {
3447 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3448 945f9229 2022-04-16 thomas if (err)
3449 945f9229 2022-04-16 thomas goto done;
3450 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3451 03415a1a 2019-06-02 stsp worktree->path_prefix);
3452 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3453 03415a1a 2019-06-02 stsp goto done;
3455 69d57f3d 2020-07-31 stsp if (tree_id1) {
3456 69d57f3d 2020-07-31 stsp char *id_str;
3458 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3460 03415a1a 2019-06-02 stsp goto done;
3462 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3464 f69721c3 2019-10-21 stsp goto done;
3466 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3467 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3468 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3469 f69721c3 2019-10-21 stsp free(id_str);
3470 f69721c3 2019-10-21 stsp goto done;
3472 f69721c3 2019-10-21 stsp free(id_str);
3474 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3475 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3476 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3477 a0f32f33 2022-06-13 thomas goto done;
3481 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3482 945f9229 2022-04-16 thomas if (err)
3483 945f9229 2022-04-16 thomas goto done;
3485 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3486 234035bc 2019-06-01 stsp worktree->path_prefix);
3488 234035bc 2019-06-01 stsp goto done;
3490 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3492 234035bc 2019-06-01 stsp goto done;
3494 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3495 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3496 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3497 19a6a6b5 2022-07-01 thomas goto done;
3500 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3501 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3502 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3503 a0f32f33 2022-06-13 thomas goto done;
3506 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3507 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3508 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3509 19a6a6b5 2022-07-01 thomas goto done;
3512 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3513 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3514 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3515 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3516 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3518 69de9dd4 2021-09-03 stsp goto done;
3520 234035bc 2019-06-01 stsp arg.worktree = worktree;
3521 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3522 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3523 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3524 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3525 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3526 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3527 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3528 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3529 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3530 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3531 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3532 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3533 af12c6b9 2019-06-04 stsp err = sync_err;
3535 945f9229 2022-04-16 thomas if (commit1)
3536 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3537 945f9229 2022-04-16 thomas if (commit2)
3538 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3539 234035bc 2019-06-01 stsp if (tree1)
3540 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3541 234035bc 2019-06-01 stsp if (tree2)
3542 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3543 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3544 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3545 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3546 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3547 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3548 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3549 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3550 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3551 f69721c3 2019-10-21 stsp free(label_orig);
3552 818c7501 2019-07-11 stsp return err;
3555 818c7501 2019-07-11 stsp const struct got_error *
3556 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3557 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3558 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3559 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3561 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3562 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3563 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3564 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args cma;
3566 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3568 818c7501 2019-07-11 stsp return err;
3570 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3572 818c7501 2019-07-11 stsp goto done;
3574 6ebb2263 2023-07-26 thomas cma.worktree = worktree;
3575 6ebb2263 2023-07-26 thomas cma.cancel_cb = cancel_cb;
3576 6ebb2263 2023-07-26 thomas cma.cancel_arg = cancel_arg;
3578 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3581 818c7501 2019-07-11 stsp goto done;
3583 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3584 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3585 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3587 818c7501 2019-07-11 stsp if (fileindex)
3588 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3589 818c7501 2019-07-11 stsp free(fileindex_path);
3590 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3591 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3592 234035bc 2019-06-01 stsp err = unlockerr;
3593 234035bc 2019-06-01 stsp return err;
3596 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3597 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3598 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3599 927df6b7 2019-02-10 stsp const char *status_path;
3600 927df6b7 2019-02-10 stsp size_t status_path_len;
3601 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3602 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3603 f8d1f275 2019-02-04 stsp void *status_arg;
3604 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3605 f8d1f275 2019-02-04 stsp void *cancel_arg;
3606 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3607 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3608 f2a9dc41 2019-12-13 tracey int report_unchanged;
3609 3143d852 2020-06-25 stsp int no_ignores;
3612 f8d1f275 2019-02-04 stsp static const struct got_error *
3613 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3614 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3615 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3616 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3618 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3619 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3620 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3621 927df6b7 2019-02-10 stsp struct stat sb;
3622 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3623 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3624 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3626 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3627 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3629 98eaaa12 2019-08-03 stsp return err;
3631 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3632 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3633 98eaaa12 2019-08-03 stsp return NULL;
3635 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3636 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3637 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3638 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3639 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3640 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3641 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3642 43010591 2023-02-17 thomas &staged_blob_id, ie);
3645 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3646 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3649 927df6b7 2019-02-10 stsp static const struct got_error *
3650 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3651 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3653 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3654 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3655 f8d1f275 2019-02-04 stsp char *abspath;
3657 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
3658 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3659 eb6fb9cd 2023-07-26 thomas if (err)
3660 eb6fb9cd 2023-07-26 thomas return err;
3663 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3664 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3665 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3666 927df6b7 2019-02-10 stsp return NULL;
3668 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3669 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3670 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3671 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3673 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3674 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3675 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3678 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3679 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3680 f8d1f275 2019-02-04 stsp free(abspath);
3681 9d31a1d8 2018-03-11 stsp return err;
3684 f8d1f275 2019-02-04 stsp static const struct got_error *
3685 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3687 eb6fb9cd 2023-07-26 thomas const struct got_error *err;
3688 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3689 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3690 2ec1f75b 2019-03-26 stsp unsigned char status;
3692 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
3693 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3694 eb6fb9cd 2023-07-26 thomas if (err)
3695 eb6fb9cd 2023-07-26 thomas return err;
3698 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3699 927df6b7 2019-02-10 stsp return NULL;
3701 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3702 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3703 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3704 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3706 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3707 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3708 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3711 ef20f542 2022-06-26 thomas static void
3712 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3714 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3716 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3717 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3719 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3721 5f42e012 2023-12-09 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_ALL);
3724 6841da00 2019-08-08 stsp static const struct got_error *
3725 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3727 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3728 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3729 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3730 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3731 6841da00 2019-08-08 stsp size_t linesize = 0;
3732 6841da00 2019-08-08 stsp ssize_t linelen;
3734 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3735 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3736 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3737 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3739 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3740 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3741 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3743 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3744 bd8de430 2019-10-04 stsp if (line[0] == '#')
3747 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3748 bd8de430 2019-10-04 stsp if (line[0] == '!')
3751 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3752 6841da00 2019-08-08 stsp line) == -1) {
3753 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3754 6841da00 2019-08-08 stsp goto done;
3756 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3758 6841da00 2019-08-08 stsp goto done;
3760 6841da00 2019-08-08 stsp if (ferror(f)) {
3761 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3762 6841da00 2019-08-08 stsp goto done;
3765 6841da00 2019-08-08 stsp dirpath = strdup(path);
3766 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3767 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3768 6841da00 2019-08-08 stsp goto done;
3770 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3772 6841da00 2019-08-08 stsp free(line);
3773 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3774 6841da00 2019-08-08 stsp free(dirpath);
3775 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3776 5f42e012 2023-12-09 thomas free(ignorelist);
3778 6841da00 2019-08-08 stsp return err;
3781 1b5d300f 2023-02-20 thomas static int
3782 1b5d300f 2023-02-20 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
3783 1b5d300f 2023-02-20 thomas int flags)
3785 1b5d300f 2023-02-20 thomas char buf[PATH_MAX];
3788 1b5d300f 2023-02-20 thomas * Trailing slashes signify directories.
3789 1b5d300f 2023-02-20 thomas * Append a * to make such patterns conform to fnmatch rules.
3791 1b5d300f 2023-02-20 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3792 1b5d300f 2023-02-20 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3793 1b5d300f 2023-02-20 thomas return FNM_NOMATCH; /* XXX */
3795 1b5d300f 2023-02-20 thomas return fnmatch(buf, path, flags);
3798 1b5d300f 2023-02-20 thomas return fnmatch(pattern, path, flags);
3801 ef20f542 2022-06-26 thomas static int
3802 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3804 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3806 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3807 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3808 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3809 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3811 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3812 1b5d300f 2023-02-20 thomas const char *p;
3814 1b5d300f 2023-02-20 thomas if (pi->path_len < 3 ||
3815 1b5d300f 2023-02-20 thomas strncmp(pi->path, "**/", 3) != 0)
3818 bd8de430 2019-10-04 stsp while (*p) {
3819 1b5d300f 2023-02-20 thomas if (match_path(pi->path + 3,
3820 1b5d300f 2023-02-20 thomas pi->path_len - 3, p,
3821 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3822 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3823 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3825 bd8de430 2019-10-04 stsp while (*p == '/')
3835 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3836 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3837 6841da00 2019-08-08 stsp * ignores backwards.
3839 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3840 6841da00 2019-08-08 stsp while (pe) {
3841 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3842 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3843 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3844 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3845 1b5d300f 2023-02-20 thomas int flags = FNM_LEADING_DIR;
3846 1b5d300f 2023-02-20 thomas if (strstr(pi->path, "/**/") == NULL)
3847 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3848 1b5d300f 2023-02-20 thomas if (match_path(pi->path, pi->path_len,
3849 1b5d300f 2023-02-20 thomas path, flags))
3854 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3860 6841da00 2019-08-08 stsp static const struct got_error *
3861 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3862 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3864 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3865 6841da00 2019-08-08 stsp char *ignorespath;
3866 886cec17 2019-12-15 stsp int fd = -1;
3867 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3869 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3870 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3871 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3873 886cec17 2019-12-15 stsp if (dirfd != -1) {
3874 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3875 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3876 886cec17 2019-12-15 stsp if (fd == -1) {
3877 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3878 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3879 886cec17 2019-12-15 stsp ignorespath);
3881 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3882 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3883 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3884 886cec17 2019-12-15 stsp ignorespath);
3887 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3891 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3892 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3893 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3894 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3895 886cec17 2019-12-15 stsp ignorespath);
3897 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3900 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3901 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3902 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3903 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3904 6841da00 2019-08-08 stsp free(ignorespath);
3905 6841da00 2019-08-08 stsp return err;
3908 f8d1f275 2019-02-04 stsp static const struct got_error *
3909 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3910 6092c299 2021-10-04 thomas int dirfd)
3912 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3913 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3914 f8d1f275 2019-02-04 stsp char *path = NULL;
3916 6092c299 2021-10-04 thomas if (ignore != NULL)
3917 6092c299 2021-10-04 thomas *ignore = 0;
3919 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
3920 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3921 eb6fb9cd 2023-07-26 thomas if (err)
3922 eb6fb9cd 2023-07-26 thomas return err;
3925 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3926 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3927 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3929 f8d1f275 2019-02-04 stsp path = de->d_name;
3932 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3933 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3934 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3935 6092c299 2021-10-04 thomas *ignore = 1;
3936 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3937 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3938 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3939 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3940 f8d1f275 2019-02-04 stsp if (parent_path[0])
3941 f8d1f275 2019-02-04 stsp free(path);
3942 b72f483a 2019-02-05 stsp return err;
3945 347d1d3e 2019-07-12 stsp static const struct got_error *
3946 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3948 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3949 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3951 3143d852 2020-06-25 stsp if (a->no_ignores)
3952 3143d852 2020-06-25 stsp return NULL;
3954 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3955 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3957 3143d852 2020-06-25 stsp return err;
3959 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3960 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3962 3143d852 2020-06-25 stsp return err;
3965 3143d852 2020-06-25 stsp static const struct got_error *
3966 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3967 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3968 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3969 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3971 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3972 abb4604f 2019-07-27 stsp struct stat sb;
3974 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3976 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3977 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3979 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3980 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3981 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3982 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3983 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3986 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3987 fe1d8685 2022-02-12 thomas return NULL;
3989 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3990 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3991 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3993 abb4604f 2019-07-27 stsp return NULL;
3996 3143d852 2020-06-25 stsp static const struct got_error *
3997 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3998 3143d852 2020-06-25 stsp const char *root_path, const char *path)
4000 3143d852 2020-06-25 stsp const struct got_error *err;
4001 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
4003 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
4004 3143d852 2020-06-25 stsp ".cvsignore");
4006 3143d852 2020-06-25 stsp return err;
4008 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
4009 3143d852 2020-06-25 stsp ".gitignore");
4011 3143d852 2020-06-25 stsp return err;
4013 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
4014 3143d852 2020-06-25 stsp if (err) {
4015 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
4016 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
4017 3143d852 2020-06-25 stsp return err;
4019 3143d852 2020-06-25 stsp for (;;) {
4020 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
4021 3143d852 2020-06-25 stsp ".cvsignore");
4024 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
4025 3143d852 2020-06-25 stsp ".gitignore");
4028 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
4029 3143d852 2020-06-25 stsp if (err) {
4030 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
4031 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
4034 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
4036 b737c85a 2020-06-26 stsp free(parent_path);
4037 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
4038 b737c85a 2020-06-26 stsp next_parent_path = NULL;
4041 b737c85a 2020-06-26 stsp free(parent_path);
4042 b737c85a 2020-06-26 stsp free(next_parent_path);
4043 3143d852 2020-06-25 stsp return err;
4046 f8836425 2023-04-14 thomas struct find_missing_children_args {
4047 f8836425 2023-04-14 thomas const char *parent_path;
4048 f8836425 2023-04-14 thomas size_t parent_len;
4049 f8836425 2023-04-14 thomas struct got_pathlist_head *children;
4050 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb;
4051 f8836425 2023-04-14 thomas void *cancel_arg;
4054 abb4604f 2019-07-27 stsp static const struct got_error *
4055 f8836425 2023-04-14 thomas find_missing_children(void *arg, struct got_fileindex_entry *ie)
4057 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
4058 f8836425 2023-04-14 thomas struct find_missing_children_args *a = arg;
4060 f8836425 2023-04-14 thomas if (a->cancel_cb) {
4061 f8836425 2023-04-14 thomas err = a->cancel_cb(a->cancel_arg);
4062 f8836425 2023-04-14 thomas if (err)
4063 f8836425 2023-04-14 thomas return err;
4066 f8836425 2023-04-14 thomas if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4067 f8836425 2023-04-14 thomas err = got_pathlist_append(a->children, ie->path, NULL);
4069 f8836425 2023-04-14 thomas return err;
4072 f8836425 2023-04-14 thomas static const struct got_error *
4073 f8836425 2023-04-14 thomas report_children(struct got_pathlist_head *children,
4074 f8836425 2023-04-14 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
4075 f8836425 2023-04-14 thomas struct got_repository *repo, int is_root_dir, int report_unchanged,
4076 f8836425 2023-04-14 thomas struct got_pathlist_head *ignores, int no_ignores,
4077 f8836425 2023-04-14 thomas got_worktree_status_cb status_cb, void *status_arg,
4078 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb, void *cancel_arg)
4080 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
4081 f8836425 2023-04-14 thomas struct got_pathlist_entry *pe;
4082 f8836425 2023-04-14 thomas char *ondisk_path = NULL;
4084 f8836425 2023-04-14 thomas TAILQ_FOREACH(pe, children, entry) {
4085 f8836425 2023-04-14 thomas if (cancel_cb) {
4086 f8836425 2023-04-14 thomas err = cancel_cb(cancel_arg);
4087 f8836425 2023-04-14 thomas if (err)
4091 f8836425 2023-04-14 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4092 f8836425 2023-04-14 thomas !is_root_dir ? "/" : "", pe->path) == -1) {
4093 f8836425 2023-04-14 thomas err = got_error_from_errno("asprintf");
4094 f8836425 2023-04-14 thomas ondisk_path = NULL;
4098 f8836425 2023-04-14 thomas err = report_single_file_status(pe->path, ondisk_path,
4099 f8836425 2023-04-14 thomas fileindex, status_cb, status_arg, repo, report_unchanged,
4100 f8836425 2023-04-14 thomas ignores, no_ignores);
4101 f8836425 2023-04-14 thomas if (err)
4104 f8836425 2023-04-14 thomas free(ondisk_path);
4105 f8836425 2023-04-14 thomas ondisk_path = NULL;
4108 f8836425 2023-04-14 thomas free(ondisk_path);
4109 f8836425 2023-04-14 thomas return err;
4112 f8836425 2023-04-14 thomas static const struct got_error *
4113 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
4114 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4115 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4116 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4117 f2a9dc41 2019-12-13 tracey int report_unchanged)
4119 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
4120 6fc93f37 2019-12-13 stsp int fd = -1;
4121 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
4122 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
4123 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
4124 f8836425 2023-04-14 thomas struct got_pathlist_head ignores, missing_children;
4125 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
4127 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
4128 f8836425 2023-04-14 thomas TAILQ_INIT(&missing_children);
4130 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
4131 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
4132 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
4134 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4135 a78810f8 2022-03-13 thomas if (ie) {
4136 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
4137 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
4138 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
4139 a78810f8 2022-03-13 thomas goto done;
4140 f8836425 2023-04-14 thomas } else {
4141 f8836425 2023-04-14 thomas struct find_missing_children_args fmca;
4142 f8836425 2023-04-14 thomas fmca.parent_path = path;
4143 f8836425 2023-04-14 thomas fmca.parent_len = strlen(path);
4144 f8836425 2023-04-14 thomas fmca.children = &missing_children;
4145 f8836425 2023-04-14 thomas fmca.cancel_cb = cancel_cb;
4146 f8836425 2023-04-14 thomas fmca.cancel_arg = cancel_arg;
4147 f8836425 2023-04-14 thomas err = got_fileindex_for_each_entry_safe(fileindex,
4148 f8836425 2023-04-14 thomas find_missing_children, &fmca);
4149 f8836425 2023-04-14 thomas if (err)
4150 f8836425 2023-04-14 thomas goto done;
4153 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4154 6fc93f37 2019-12-13 stsp if (fd == -1) {
4155 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4156 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
4157 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
4159 ff56836b 2021-07-08 stsp if (!no_ignores) {
4160 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4161 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
4163 ff56836b 2021-07-08 stsp goto done;
4165 f8836425 2023-04-14 thomas if (TAILQ_EMPTY(&missing_children)) {
4166 f8836425 2023-04-14 thomas err = report_single_file_status(path,
4167 f8836425 2023-04-14 thomas ondisk_path, fileindex,
4168 f8836425 2023-04-14 thomas status_cb, status_arg, repo,
4169 f8836425 2023-04-14 thomas report_unchanged, &ignores, no_ignores);
4170 f8836425 2023-04-14 thomas if (err)
4171 f8836425 2023-04-14 thomas goto done;
4172 f8836425 2023-04-14 thomas } else {
4173 f8836425 2023-04-14 thomas err = report_children(&missing_children,
4174 f8836425 2023-04-14 thomas worktree, fileindex, repo,
4175 f8836425 2023-04-14 thomas (path[0] == '\0'), report_unchanged,
4176 f8836425 2023-04-14 thomas &ignores, no_ignores,
4177 f8836425 2023-04-14 thomas status_cb, status_arg,
4178 f8836425 2023-04-14 thomas cancel_cb, cancel_arg);
4179 6f97aa83 2023-04-14 thomas if (err)
4180 6f97aa83 2023-04-14 thomas goto done;
4184 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4185 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4186 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4187 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4188 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4189 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4190 abb4604f 2019-07-27 stsp arg.status_path = path;
4191 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4192 abb4604f 2019-07-27 stsp arg.repo = repo;
4193 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4194 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4195 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4196 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4197 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4198 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4199 022fae89 2019-12-06 tracey if (!no_ignores) {
4200 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4201 3143d852 2020-06-25 stsp worktree->root_path, path);
4203 3143d852 2020-06-25 stsp goto done;
4205 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4206 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4207 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4210 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4211 b2dcbbc7 2023-12-05 thomas got_pathlist_free(&missing_children, GOT_PATHLIST_FREE_NONE);
4212 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4213 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4214 927df6b7 2019-02-10 stsp free(ondisk_path);
4215 347d1d3e 2019-07-12 stsp return err;
4218 347d1d3e 2019-07-12 stsp const struct got_error *
4219 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4220 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4221 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4222 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4224 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4225 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4226 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4227 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4229 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4231 347d1d3e 2019-07-12 stsp return err;
4233 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4234 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4235 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4236 f6343036 2021-06-22 stsp no_ignores, 0);
4240 f8d1f275 2019-02-04 stsp free(fileindex_path);
4241 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4242 f8d1f275 2019-02-04 stsp return err;
4245 6c7ab921 2019-03-18 stsp const struct got_error *
4246 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4247 6c7ab921 2019-03-18 stsp const char *arg)
4249 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4250 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4251 6c7ab921 2019-03-18 stsp size_t len;
4252 00bb5ea0 2020-07-23 stsp struct stat sb;
4253 01740607 2020-11-04 stsp char *abspath = NULL;
4254 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4256 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4258 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4259 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4260 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4262 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4263 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4264 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4265 00bb5ea0 2020-07-23 stsp goto done;
4267 727173c3 2020-11-06 stsp sb.st_mode = 0;
4269 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4271 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4272 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4273 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4274 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4276 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4277 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4278 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4279 00bb5ea0 2020-07-23 stsp goto done;
4283 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4284 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4286 d0710d08 2019-07-22 stsp goto done;
4287 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4288 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4289 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4290 00bb5ea0 2020-07-23 stsp goto done;
4293 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4294 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4295 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4296 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4297 00bb5ea0 2020-07-23 stsp goto done;
4299 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4300 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4301 01740607 2020-11-04 stsp goto done;
4303 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4304 01740607 2020-11-04 stsp sizeof(canonpath));
4306 00bb5ea0 2020-07-23 stsp goto done;
4307 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4308 01740607 2020-11-04 stsp if (resolved == NULL) {
4309 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4310 01740607 2020-11-04 stsp goto done;
4315 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4316 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4317 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4318 6c7ab921 2019-03-18 stsp goto done;
4321 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4322 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4323 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4325 f6d88e1a 2019-05-29 stsp goto done;
4327 f6d88e1a 2019-05-29 stsp path = strdup("");
4328 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4329 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4330 f6d88e1a 2019-05-29 stsp goto done;
4334 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4335 6c7ab921 2019-03-18 stsp len = strlen(path);
4336 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4337 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4341 01740607 2020-11-04 stsp free(abspath);
4342 6c7ab921 2019-03-18 stsp free(resolved);
4343 d0710d08 2019-07-22 stsp free(cwd);
4344 6c7ab921 2019-03-18 stsp if (err == NULL)
4345 6c7ab921 2019-03-18 stsp *wt_path = path;
4347 6c7ab921 2019-03-18 stsp free(path);
4348 d00136be 2019-03-26 stsp return err;
4351 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4352 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4353 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4354 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4355 4e68cba3 2019-11-23 stsp void *progress_arg;
4356 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4359 b21ebdb0 2023-06-22 thomas static int
4360 b21ebdb0 2023-06-22 thomas add_noop_status(unsigned char status)
4362 b21ebdb0 2023-06-22 thomas return (status == GOT_STATUS_ADD ||
4363 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODIFY ||
4364 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_CONFLICT ||
4365 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODE_CHANGE ||
4366 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_NO_CHANGE);
4369 4e68cba3 2019-11-23 stsp static const struct got_error *
4370 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4371 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4372 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4373 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4375 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4376 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4377 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4378 6d022e97 2019-08-04 stsp struct stat sb;
4379 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4381 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4382 dbb83fbd 2019-12-12 stsp relpath) == -1)
4383 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4385 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4387 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4388 12463d8b 2019-12-13 stsp de_name, a->repo);
4390 dbb83fbd 2019-12-12 stsp goto done;
4391 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4392 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE &&
4393 b21ebdb0 2023-06-22 thomas add_noop_status(status))
4394 dbb83fbd 2019-12-12 stsp goto done;
4395 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4397 dbb83fbd 2019-12-12 stsp goto done;
4400 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4401 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4402 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4404 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4405 dbb83fbd 2019-12-12 stsp goto done;
4408 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4410 4e68cba3 2019-11-23 stsp goto done;
4411 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4412 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4413 3969253a 2020-03-07 stsp if (err) {
4414 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4415 3969253a 2020-03-07 stsp goto done;
4417 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4418 07f5b47a 2019-06-02 stsp if (err) {
4419 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4420 4e68cba3 2019-11-23 stsp goto done;
4423 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4425 dbb83fbd 2019-12-12 stsp return err;
4426 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4427 dbb83fbd 2019-12-12 stsp return NULL;
4428 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4431 d00136be 2019-03-26 stsp const struct got_error *
4432 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4433 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4434 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4435 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4437 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4438 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4439 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4440 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4441 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4443 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4445 d00136be 2019-03-26 stsp return err;
4447 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4449 d00136be 2019-03-26 stsp goto done;
4451 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4452 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4453 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4454 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4455 4e68cba3 2019-11-23 stsp saa.repo = repo;
4457 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4458 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4459 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4463 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4464 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4465 af12c6b9 2019-06-04 stsp err = sync_err;
4467 fb399478 2019-07-12 stsp free(fileindex_path);
4468 d00136be 2019-03-26 stsp if (fileindex)
4469 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4470 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4471 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4472 d00136be 2019-03-26 stsp err = unlockerr;
4473 6c7ab921 2019-03-18 stsp return err;
4476 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4477 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4478 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4479 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4480 f2a9dc41 2019-12-13 tracey void *progress_arg;
4481 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4482 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4483 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4484 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4485 8ec7352a 2023-06-01 thomas const char *status_path;
4486 8ec7352a 2023-06-01 thomas size_t status_path_len;
4487 766841c2 2020-08-13 stsp const char *status_codes;
4490 f2a9dc41 2019-12-13 tracey static const struct got_error *
4491 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4492 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4493 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4494 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4496 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4497 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4498 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4499 17ed4618 2019-06-02 stsp struct stat sb;
4500 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4502 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4503 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4504 d64cc78a 2022-01-25 thomas return NULL;
4505 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4508 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4509 17ed4618 2019-06-02 stsp if (ie == NULL)
4510 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4512 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4513 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4514 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4515 9acbc4fa 2019-08-03 stsp return NULL;
4516 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4519 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4520 f2a9dc41 2019-12-13 tracey relpath) == -1)
4521 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4523 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4526 f2a9dc41 2019-12-13 tracey goto done;
4528 766841c2 2020-08-13 stsp if (a->status_codes) {
4529 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4531 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4532 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4535 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4536 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4537 766841c2 2020-08-13 stsp free(ondisk_path);
4538 766841c2 2020-08-13 stsp return NULL;
4540 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4541 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4542 766841c2 2020-08-13 stsp static char msg[64];
4543 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4544 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4545 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4546 766841c2 2020-08-13 stsp goto done;
4550 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4551 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4552 f2a9dc41 2019-12-13 tracey goto done;
4553 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4554 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4555 f2a9dc41 2019-12-13 tracey goto done;
4557 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4558 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4559 d64cc78a 2022-01-25 thomas goto done;
4561 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4562 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4563 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4564 f2a9dc41 2019-12-13 tracey goto done;
4568 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4569 20a2ad1c 2020-10-20 stsp size_t root_len;
4571 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4572 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4573 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4574 12463d8b 2019-12-13 stsp ondisk_path);
4575 12463d8b 2019-12-13 stsp goto done;
4577 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4578 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4579 12463d8b 2019-12-13 stsp goto done;
4582 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4584 20a2ad1c 2020-10-20 stsp char *parent;
4586 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4588 2513f20a 2020-10-20 stsp goto done;
4589 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4590 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4591 8ec7352a 2023-06-01 thomas if (got_path_cmp(ondisk_path, a->status_path,
4592 8ec7352a 2023-06-01 thomas strlen(ondisk_path), a->status_path_len) != 0 &&
4593 8ec7352a 2023-06-01 thomas !got_path_is_child(ondisk_path, a->status_path,
4594 8ec7352a 2023-06-01 thomas a->status_path_len))
4596 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4597 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4598 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4599 20a2ad1c 2020-10-20 stsp ondisk_path);
4602 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4603 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4606 97f9158f 2023-08-29 thomas if (got_fileindex_entry_has_blob(ie))
4607 97f9158f 2023-08-29 thomas got_fileindex_entry_mark_deleted_from_disk(ie);
4609 97f9158f 2023-08-29 thomas got_fileindex_entry_remove(a->fileindex, ie);
4611 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4612 f2a9dc41 2019-12-13 tracey if (err)
4613 f2a9dc41 2019-12-13 tracey return err;
4614 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4615 f2a9dc41 2019-12-13 tracey return NULL;
4616 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4617 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4620 2ec1f75b 2019-03-26 stsp const struct got_error *
4621 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4622 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4623 766841c2 2020-08-13 stsp const char *status_codes,
4624 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4625 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4627 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4628 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4629 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4630 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4631 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4633 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4635 2ec1f75b 2019-03-26 stsp return err;
4637 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4639 2ec1f75b 2019-03-26 stsp goto done;
4641 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4642 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4643 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4644 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4645 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4646 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4647 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4648 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4649 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4651 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4652 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
4654 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s%s%s",
4655 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree),
4656 8ec7352a 2023-06-01 thomas pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4657 8ec7352a 2023-06-01 thomas err = got_error_from_errno("asprintf");
4658 8ec7352a 2023-06-01 thomas goto done;
4660 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
4661 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
4662 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4663 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4664 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
4668 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4669 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4670 af12c6b9 2019-06-04 stsp err = sync_err;
4672 fb399478 2019-07-12 stsp free(fileindex_path);
4673 2ec1f75b 2019-03-26 stsp if (fileindex)
4674 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4675 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4676 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4677 2ec1f75b 2019-03-26 stsp err = unlockerr;
4678 33aa809d 2019-08-08 stsp return err;
4681 33aa809d 2019-08-08 stsp static const struct got_error *
4682 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4684 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4685 33aa809d 2019-08-08 stsp char *line = NULL;
4686 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4687 33aa809d 2019-08-08 stsp ssize_t linelen;
4689 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4690 33aa809d 2019-08-08 stsp if (linelen == -1) {
4691 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4692 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4693 33aa809d 2019-08-08 stsp goto done;
4695 33aa809d 2019-08-08 stsp return NULL;
4697 33aa809d 2019-08-08 stsp if (outfile) {
4698 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4699 33aa809d 2019-08-08 stsp if (n != linelen) {
4700 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4701 33aa809d 2019-08-08 stsp goto done;
4704 33aa809d 2019-08-08 stsp if (rejectfile) {
4705 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4706 33aa809d 2019-08-08 stsp if (n != linelen)
4707 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4710 33aa809d 2019-08-08 stsp free(line);
4711 2ec1f75b 2019-03-26 stsp return err;
4714 33aa809d 2019-08-08 stsp static const struct got_error *
4715 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4717 33aa809d 2019-08-08 stsp char *line = NULL;
4718 33aa809d 2019-08-08 stsp size_t linesize = 0;
4719 33aa809d 2019-08-08 stsp ssize_t linelen;
4721 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4722 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4723 500467ff 2019-09-25 hiltjo if (ferror(f))
4724 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4725 500467ff 2019-09-25 hiltjo return NULL;
4727 33aa809d 2019-08-08 stsp free(line);
4728 33aa809d 2019-08-08 stsp return NULL;
4731 33aa809d 2019-08-08 stsp static const struct got_error *
4732 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4733 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4734 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4736 33aa809d 2019-08-08 stsp const struct got_error *err;
4738 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4739 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4740 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4742 33aa809d 2019-08-08 stsp return err;
4743 33aa809d 2019-08-08 stsp (*line_cur1)++;
4745 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4746 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4747 33aa809d 2019-08-08 stsp if (rejectfile)
4748 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4750 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4752 33aa809d 2019-08-08 stsp return err;
4753 33aa809d 2019-08-08 stsp (*line_cur2)++;
4755 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4756 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4757 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4759 33aa809d 2019-08-08 stsp return err;
4760 33aa809d 2019-08-08 stsp (*line_cur2)++;
4762 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4763 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4764 33aa809d 2019-08-08 stsp if (rejectfile)
4765 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4767 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4769 33aa809d 2019-08-08 stsp return err;
4770 33aa809d 2019-08-08 stsp (*line_cur1)++;
4773 f1e81a05 2019-08-10 stsp return NULL;
4776 f1e81a05 2019-08-10 stsp static const struct got_error *
4777 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4778 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4780 f1e81a05 2019-08-10 stsp const struct got_error *err;
4782 f1e81a05 2019-08-10 stsp if (outfile) {
4783 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4784 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4785 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4787 f1e81a05 2019-08-10 stsp return err;
4788 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4791 f1e81a05 2019-08-10 stsp if (rejectfile) {
4792 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4793 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4794 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4796 f1e81a05 2019-08-10 stsp return err;
4797 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4801 33aa809d 2019-08-08 stsp return NULL;
4804 33aa809d 2019-08-08 stsp static const struct got_error *
4805 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4806 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4807 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4808 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4809 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4811 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4812 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4813 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4814 33aa809d 2019-08-08 stsp FILE *hunkfile;
4815 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4816 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4819 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4821 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4822 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4823 fe621944 2020-11-10 stsp start_old = cc.left.start;
4824 fe621944 2020-11-10 stsp end_old = cc.left.end;
4825 fe621944 2020-11-10 stsp start_new = cc.right.start;
4826 fe621944 2020-11-10 stsp end_new = cc.right.end;
4828 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4829 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4830 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4832 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4833 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4834 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4836 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4837 fe621944 2020-11-10 stsp if (diff_state == NULL)
4838 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4839 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4841 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4842 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4843 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4844 33aa809d 2019-08-08 stsp goto done;
4847 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4848 fe621944 2020-11-10 stsp diff_result, &cc);
4849 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4850 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4851 33aa809d 2019-08-08 stsp goto done;
4854 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4855 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4856 33aa809d 2019-08-08 stsp goto done;
4859 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4860 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4862 33aa809d 2019-08-08 stsp goto done;
4864 33aa809d 2019-08-08 stsp switch (*choice) {
4865 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4866 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4867 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4869 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4870 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4871 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4873 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4876 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4880 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4881 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4882 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4883 33aa809d 2019-08-08 stsp return err;
4886 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4887 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4888 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4889 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4890 1f1abb7e 2019-08-08 stsp void *progress_arg;
4891 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4892 33aa809d 2019-08-08 stsp void *patch_arg;
4893 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4894 10604dce 2021-09-24 thomas int unlink_added_files;
4895 8641a332 2023-04-14 thomas struct got_pathlist_head *added_files_to_unlink;
4898 e20a8b6f 2019-06-04 stsp static const struct got_error *
4899 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4900 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4901 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4902 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4903 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4905 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4906 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4907 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4908 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4909 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4910 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4911 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4912 fe621944 2020-11-10 stsp struct stat sb2;
4913 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4914 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4915 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4917 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4919 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4921 33aa809d 2019-08-08 stsp return err;
4923 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4924 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4925 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4926 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4927 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4928 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4929 fa3cef63 2020-07-23 stsp goto done;
4931 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4932 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4933 48b4f239 2021-12-31 thomas if (link_len == -1) {
4934 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4937 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4938 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4941 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4942 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4943 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4944 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4945 fa3cef63 2020-07-23 stsp goto done;
4947 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4948 fa3cef63 2020-07-23 stsp sizeof(link_target));
4949 fa3cef63 2020-07-23 stsp if (link_len == -1)
4950 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4951 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4952 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4955 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4956 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4957 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4958 fa3cef63 2020-07-23 stsp goto done;
4961 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4962 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4963 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4964 fa3cef63 2020-07-23 stsp goto done;
4969 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4970 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4971 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4972 fa3cef63 2020-07-23 stsp goto done;
4974 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4975 fa3cef63 2020-07-23 stsp if (n != link_len) {
4976 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4977 fa3cef63 2020-07-23 stsp goto done;
4979 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4980 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4981 fa3cef63 2020-07-23 stsp goto done;
4983 fa3cef63 2020-07-23 stsp rewind(f2);
4986 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4987 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4988 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4989 f4ae6ddb 2022-07-01 thomas goto done;
4992 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4994 33aa809d 2019-08-08 stsp goto done;
4996 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4998 33aa809d 2019-08-08 stsp goto done;
5000 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5002 33aa809d 2019-08-08 stsp goto done;
5004 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5005 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5007 33aa809d 2019-08-08 stsp goto done;
5009 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5012 33aa809d 2019-08-08 stsp goto done;
5014 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
5015 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
5016 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
5017 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
5019 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
5020 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5021 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
5022 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
5023 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
5024 fe621944 2020-11-10 stsp nchanges++;
5026 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5027 33aa809d 2019-08-08 stsp int choice;
5028 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
5029 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
5030 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
5031 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
5032 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
5033 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
5035 33aa809d 2019-08-08 stsp goto done;
5036 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
5037 33aa809d 2019-08-08 stsp have_content = 1;
5038 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
5041 1ebedb77 2019-10-19 stsp if (have_content) {
5042 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5043 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
5044 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
5046 1ebedb77 2019-10-19 stsp goto done;
5048 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
5049 a2c162eb 2022-10-30 thomas mode_t mode;
5051 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
5052 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
5053 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
5054 fa3cef63 2020-07-23 stsp goto done;
5059 33aa809d 2019-08-08 stsp free(id_str);
5060 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5061 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5063 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
5064 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
5065 fe621944 2020-11-10 stsp if (err == NULL)
5066 fe621944 2020-11-10 stsp err = free_err;
5067 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
5068 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
5069 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
5070 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
5071 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5072 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
5073 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
5074 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
5075 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
5076 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
5077 33aa809d 2019-08-08 stsp if (err || !have_content) {
5078 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5079 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
5080 33aa809d 2019-08-08 stsp free(*path_outfile);
5081 33aa809d 2019-08-08 stsp *path_outfile = NULL;
5083 33aa809d 2019-08-08 stsp free(path1);
5084 33aa809d 2019-08-08 stsp return err;
5087 33aa809d 2019-08-08 stsp static const struct got_error *
5088 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
5089 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
5090 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5091 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5093 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
5094 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
5095 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
5096 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
5097 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
5098 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
5099 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
5100 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
5101 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
5102 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
5103 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
5104 f4ae6ddb 2022-07-01 thomas int fd = -1;
5106 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
5107 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
5108 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
5109 d3bcc3d1 2019-08-08 stsp return NULL;
5111 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
5112 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
5113 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
5115 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5116 65084dad 2019-08-08 stsp if (ie == NULL)
5117 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
5119 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
5120 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
5121 a129376b 2019-03-28 stsp if (err) {
5122 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
5123 a129376b 2019-03-28 stsp goto done;
5124 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
5125 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
5126 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
5127 e20a8b6f 2019-06-04 stsp goto done;
5130 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
5131 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
5132 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5133 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5134 a129376b 2019-03-28 stsp goto done;
5137 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
5138 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
5139 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5140 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5141 a129376b 2019-03-28 stsp goto done;
5144 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
5145 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
5146 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5147 a129376b 2019-03-28 stsp goto done;
5152 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
5153 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
5154 945f9229 2022-04-16 thomas if (err)
5155 945f9229 2022-04-16 thomas goto done;
5157 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5158 a9fa2909 2019-07-27 stsp if (err) {
5159 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5160 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
5161 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
5162 a9fa2909 2019-07-27 stsp goto done;
5164 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
5166 a9fa2909 2019-07-27 stsp goto done;
5168 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
5170 a9fa2909 2019-07-27 stsp goto done;
5172 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
5173 1233e6b6 2020-10-19 stsp free(te_name);
5174 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
5175 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
5176 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5177 a9fa2909 2019-07-27 stsp goto done;
5181 a129376b 2019-03-28 stsp switch (status) {
5182 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
5183 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5184 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5185 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5186 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5188 33aa809d 2019-08-08 stsp goto done;
5189 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5192 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5193 1f1abb7e 2019-08-08 stsp ie->path);
5195 1ee397ad 2019-07-12 stsp goto done;
5196 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5197 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
5198 8641a332 2023-04-14 thomas int do_unlink = a->added_files_to_unlink ? 0 : 1;
5200 8641a332 2023-04-14 thomas if (a->added_files_to_unlink) {
5201 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
5203 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, a->added_files_to_unlink,
5204 8641a332 2023-04-14 thomas entry) {
5205 8641a332 2023-04-14 thomas if (got_path_cmp(pe->path, relpath,
5206 8641a332 2023-04-14 thomas pe->path_len, strlen(relpath)))
5207 8641a332 2023-04-14 thomas continue;
5208 8641a332 2023-04-14 thomas do_unlink = 1;
5213 8641a332 2023-04-14 thomas if (do_unlink) {
5214 8641a332 2023-04-14 thomas if (asprintf(&ondisk_path, "%s/%s",
5215 8641a332 2023-04-14 thomas got_worktree_get_root_path(a->worktree),
5216 8641a332 2023-04-14 thomas relpath) == -1) {
5217 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
5218 8641a332 2023-04-14 thomas goto done;
5220 8641a332 2023-04-14 thomas if (unlink(ondisk_path) == -1) {
5221 8641a332 2023-04-14 thomas err = got_error_from_errno2("unlink",
5222 8641a332 2023-04-14 thomas ondisk_path);
5228 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5229 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5230 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5231 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5232 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5234 33aa809d 2019-08-08 stsp goto done;
5235 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5238 33aa809d 2019-08-08 stsp /* fall through */
5239 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5240 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5241 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5242 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5243 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5244 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5245 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
5246 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
5248 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
5249 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5250 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5251 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5252 f4ae6ddb 2022-07-01 thomas goto done;
5255 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5257 65084dad 2019-08-08 stsp goto done;
5259 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5260 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5261 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5262 a129376b 2019-03-28 stsp goto done;
5265 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5266 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5267 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5268 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5269 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5270 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5271 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5273 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5274 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5275 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5276 369fd7e5 2020-07-23 stsp path_content);
5279 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5280 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5281 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5282 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5284 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5285 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5286 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5287 369fd7e5 2020-07-23 stsp goto done;
5291 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5292 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5293 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5294 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5295 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5296 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5298 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5300 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5301 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5302 aaf972e2 2023-09-18 thomas 0, 1, 0, 0, NULL, a->repo,
5303 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5306 a129376b 2019-03-28 stsp goto done;
5307 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5308 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5309 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5310 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5311 437adc9d 2020-12-10 yzhong blob->id.sha1,
5312 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5314 2e1fa222 2020-07-23 stsp goto done;
5316 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5317 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5318 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5327 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5328 33aa809d 2019-08-08 stsp free(path_content);
5329 e20a8b6f 2019-06-04 stsp free(parent_path);
5330 a129376b 2019-03-28 stsp free(tree_path);
5331 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5332 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5334 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5336 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5337 a129376b 2019-03-28 stsp free(tree_id);
5338 945f9229 2022-04-16 thomas if (base_commit)
5339 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
5340 e20a8b6f 2019-06-04 stsp return err;
5343 e20a8b6f 2019-06-04 stsp const struct got_error *
5344 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5345 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5346 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5347 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5348 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5350 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5351 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5352 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5353 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5354 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5355 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5357 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5359 e20a8b6f 2019-06-04 stsp return err;
5361 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5363 e20a8b6f 2019-06-04 stsp goto done;
5365 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5366 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5367 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5368 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5369 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5370 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5371 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5372 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
5373 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5374 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5375 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
5379 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5380 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5381 e20a8b6f 2019-06-04 stsp err = sync_err;
5383 fb399478 2019-07-12 stsp free(fileindex_path);
5384 a129376b 2019-03-28 stsp if (fileindex)
5385 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5386 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5387 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5388 a129376b 2019-03-28 stsp err = unlockerr;
5389 c4296144 2019-05-09 stsp return err;
5392 cf066bf8 2019-05-09 stsp static void
5393 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5395 24519714 2019-05-09 stsp free(ct->path);
5396 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5397 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5398 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5399 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5400 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5401 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5405 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5406 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5407 24519714 2019-05-09 stsp struct got_repository *repo;
5408 24519714 2019-05-09 stsp struct got_worktree *worktree;
5409 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5410 5f8a88c6 2019-08-03 stsp int have_staged_files;
5411 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5412 ef899790 2022-11-01 thomas int diff_header_shown;
5413 be94c032 2023-02-20 thomas int commit_conflicts;
5414 ef899790 2022-11-01 thomas FILE *diff_outfile;
5415 ef899790 2022-11-01 thomas FILE *f1;
5416 ef899790 2022-11-01 thomas FILE *f2;
5420 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5421 ef899790 2022-11-01 thomas * it as content to the diff engine.
5423 ef899790 2022-11-01 thomas static const struct got_error *
5424 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5425 ef899790 2022-11-01 thomas const char *abspath)
5427 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5428 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5429 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5431 ef899790 2022-11-01 thomas *fd = -1;
5433 ef899790 2022-11-01 thomas if (dirfd != -1) {
5434 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5435 ef899790 2022-11-01 thomas if (target_len == -1)
5436 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5437 ef899790 2022-11-01 thomas } else {
5438 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5439 ef899790 2022-11-01 thomas if (target_len == -1)
5440 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5443 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5444 ef899790 2022-11-01 thomas if (*fd == -1)
5445 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5447 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5448 ef899790 2022-11-01 thomas if (outlen == -1) {
5449 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5450 ef899790 2022-11-01 thomas goto done;
5453 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5454 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5455 ef899790 2022-11-01 thomas goto done;
5458 ef899790 2022-11-01 thomas if (err) {
5459 ef899790 2022-11-01 thomas close(*fd);
5460 ef899790 2022-11-01 thomas *fd = -1;
5462 ef899790 2022-11-01 thomas return err;
5465 ef899790 2022-11-01 thomas static const struct got_error *
5466 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5467 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5468 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5470 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5471 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5472 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5473 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5474 ef899790 2022-11-01 thomas char *label1 = NULL;
5475 ef899790 2022-11-01 thomas struct stat sb;
5476 ef899790 2022-11-01 thomas off_t size1 = 0;
5477 ef899790 2022-11-01 thomas int f2_exists = 0;
5478 ef899790 2022-11-01 thomas char *id_str = NULL;
5480 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5482 d259e491 2022-11-01 thomas if (diff_staged) {
5483 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5484 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5485 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5486 d259e491 2022-11-01 thomas return NULL;
5487 d259e491 2022-11-01 thomas } else {
5488 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5489 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5490 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5491 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5492 d259e491 2022-11-01 thomas return NULL;
5495 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5496 ef899790 2022-11-01 thomas if (err)
5497 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5498 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5499 ef899790 2022-11-01 thomas if (err)
5500 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5502 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5503 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5504 ef899790 2022-11-01 thomas if (err)
5505 ef899790 2022-11-01 thomas return err;
5506 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5507 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5508 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5509 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5510 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5511 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5512 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5515 ef899790 2022-11-01 thomas if (diff_staged) {
5516 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5517 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5518 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5519 ef899790 2022-11-01 thomas label1 = ct->path;
5520 ef899790 2022-11-01 thomas label2 = ct->path;
5522 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5523 ef899790 2022-11-01 thomas label2 = ct->path;
5525 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5526 ef899790 2022-11-01 thomas label1 = ct->path;
5528 ef899790 2022-11-01 thomas default:
5529 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5531 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5532 ef899790 2022-11-01 thomas if (fd1 == -1) {
5533 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5534 ef899790 2022-11-01 thomas goto done;
5536 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5537 ef899790 2022-11-01 thomas if (fd2 == -1) {
5538 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5539 ef899790 2022-11-01 thomas goto done;
5541 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5542 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5543 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5544 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5545 ef899790 2022-11-01 thomas goto done;
5548 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5549 ef899790 2022-11-01 thomas if (fd1 == -1) {
5550 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5551 ef899790 2022-11-01 thomas goto done;
5554 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5555 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5556 ef899790 2022-11-01 thomas 8192, fd1);
5557 ef899790 2022-11-01 thomas if (err)
5558 ef899790 2022-11-01 thomas goto done;
5561 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5562 ef899790 2022-11-01 thomas if (dirfd != -1) {
5563 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5564 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5565 ef899790 2022-11-01 thomas if (fd == -1) {
5566 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5567 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5568 ef899790 2022-11-01 thomas ct->ondisk_path);
5569 ef899790 2022-11-01 thomas goto done;
5571 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5572 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5573 ef899790 2022-11-01 thomas if (err)
5574 ef899790 2022-11-01 thomas goto done;
5576 ef899790 2022-11-01 thomas } else {
5577 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5578 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5579 ef899790 2022-11-01 thomas if (fd == -1) {
5580 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5581 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5582 ef899790 2022-11-01 thomas ct->ondisk_path);
5583 ef899790 2022-11-01 thomas goto done;
5585 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5586 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5587 ef899790 2022-11-01 thomas if (err)
5588 ef899790 2022-11-01 thomas goto done;
5591 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5592 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5593 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5594 ef899790 2022-11-01 thomas goto done;
5596 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5597 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5598 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5599 ef899790 2022-11-01 thomas goto done;
5601 ef899790 2022-11-01 thomas fd = -1;
5602 ef899790 2022-11-01 thomas f2_exists = 1;
5605 ef899790 2022-11-01 thomas if (blob1) {
5606 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5607 ef899790 2022-11-01 thomas f1, blob1);
5608 ef899790 2022-11-01 thomas if (err)
5609 ef899790 2022-11-01 thomas goto done;
5612 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5613 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5614 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5616 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5617 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5618 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5619 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5620 ef899790 2022-11-01 thomas if (blob1)
5621 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5622 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5623 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5624 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5625 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5626 ef899790 2022-11-01 thomas return err;
5629 c4296144 2019-05-09 stsp static const struct got_error *
5630 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5631 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5632 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5633 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5635 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5636 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5637 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5638 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5639 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5640 768aea60 2019-05-09 stsp struct stat sb;
5642 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5643 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5644 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5645 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5646 5f8a88c6 2019-08-03 stsp return NULL;
5648 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5649 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5650 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5653 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5654 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5655 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5656 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5657 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5658 5f8a88c6 2019-08-03 stsp return NULL;
5661 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5662 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5663 036813ee 2019-05-09 stsp goto done;
5665 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5666 036813ee 2019-05-09 stsp parent_path = strdup("");
5667 69960a46 2019-05-09 stsp if (parent_path == NULL)
5668 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5670 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5672 69960a46 2019-05-09 stsp return err;
5675 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5676 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5677 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5678 c4296144 2019-05-09 stsp goto done;
5681 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5682 768aea60 2019-05-09 stsp relpath) == -1) {
5683 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5684 768aea60 2019-05-09 stsp goto done;
5687 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5688 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5689 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5690 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5691 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5692 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5693 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5694 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5696 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5697 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5700 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5701 0aeb8099 2020-07-23 stsp goto done;
5703 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5704 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5705 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5706 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5707 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5708 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5709 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5710 12463d8b 2019-12-13 stsp ct->ondisk_path);
5711 12463d8b 2019-12-13 stsp goto done;
5713 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5714 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5715 768aea60 2019-05-09 stsp goto done;
5717 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5720 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5721 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5722 44d03001 2019-05-09 stsp relpath) == -1) {
5723 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5724 44d03001 2019-05-09 stsp goto done;
5727 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5728 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5729 35213c7c 2020-07-23 stsp int is_bad_symlink;
5730 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5731 35213c7c 2020-07-23 stsp ssize_t target_len;
5732 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5733 35213c7c 2020-07-23 stsp sizeof(target_path));
5734 35213c7c 2020-07-23 stsp if (target_len == -1) {
5735 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5736 35213c7c 2020-07-23 stsp ct->ondisk_path);
5737 35213c7c 2020-07-23 stsp goto done;
5739 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5740 ad10f64e 2023-07-19 thomas target_len, ct->ondisk_path, a->worktree->root_path,
5741 ad10f64e 2023-07-19 thomas a->worktree->meta_dir);
5743 35213c7c 2020-07-23 stsp goto done;
5744 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5745 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5746 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5747 35213c7c 2020-07-23 stsp goto done;
5752 cf066bf8 2019-05-09 stsp ct->status = status;
5753 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5754 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5755 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5756 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5757 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5758 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5759 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5760 b416585c 2019-05-13 stsp goto done;
5762 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5763 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5764 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5765 f0b75401 2019-08-03 stsp goto done;
5768 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5769 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5770 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5771 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5772 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5773 036813ee 2019-05-09 stsp goto done;
5776 24519714 2019-05-09 stsp ct->path = strdup(path);
5777 24519714 2019-05-09 stsp if (ct->path == NULL) {
5778 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5779 24519714 2019-05-09 stsp goto done;
5782 720b08bc 2023-08-29 thomas if (a->diff_outfile) {
5783 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5784 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5785 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5786 ef899790 2022-11-01 thomas if (err)
5787 ef899790 2022-11-01 thomas goto done;
5790 720b08bc 2023-08-29 thomas err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5792 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5793 ed175427 2019-05-09 stsp free_commitable(ct);
5794 24519714 2019-05-09 stsp free(parent_path);
5795 036813ee 2019-05-09 stsp free(path);
5796 a129376b 2019-03-28 stsp return err;
5799 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5800 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5801 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5802 036813ee 2019-05-09 stsp struct got_repository *);
5804 ed175427 2019-05-09 stsp static const struct got_error *
5805 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5806 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5807 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5808 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5809 afa376bf 2019-05-09 stsp struct got_repository *repo)
5811 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5812 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5813 036813ee 2019-05-09 stsp char *subpath;
5815 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5816 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5817 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5819 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5821 ed175427 2019-05-09 stsp return err;
5823 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5824 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5825 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5826 036813ee 2019-05-09 stsp free(subpath);
5827 036813ee 2019-05-09 stsp return err;
5830 036813ee 2019-05-09 stsp static const struct got_error *
5831 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5833 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5834 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5836 036813ee 2019-05-09 stsp *match = 0;
5838 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5839 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5840 0f63689d 2019-05-10 stsp return NULL;
5843 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5845 0f63689d 2019-05-10 stsp return err;
5846 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5847 036813ee 2019-05-09 stsp free(ct_parent_path);
5848 036813ee 2019-05-09 stsp return err;
5851 768aea60 2019-05-09 stsp static mode_t
5852 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5854 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5855 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5857 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5860 036813ee 2019-05-09 stsp static const struct got_error *
5861 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5862 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5864 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5866 036813ee 2019-05-09 stsp *new_te = NULL;
5868 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5870 036813ee 2019-05-09 stsp goto done;
5872 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5874 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5875 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5876 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5878 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5880 036813ee 2019-05-09 stsp if (err && *new_te) {
5881 56e0773d 2019-11-28 stsp free(*new_te);
5882 036813ee 2019-05-09 stsp *new_te = NULL;
5884 036813ee 2019-05-09 stsp return err;
5887 036813ee 2019-05-09 stsp static const struct got_error *
5888 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5889 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5891 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5892 102b254e 2020-10-19 stsp char *ct_name = NULL;
5894 9c57c4c6 2024-02-13 thomas *new_te = NULL;
5896 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5897 036813ee 2019-05-09 stsp if (*new_te == NULL)
5898 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5900 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5902 036813ee 2019-05-09 stsp goto done;
5903 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5904 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5905 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5906 036813ee 2019-05-09 stsp goto done;
5909 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5911 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5912 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5913 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5915 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5917 102b254e 2020-10-19 stsp free(ct_name);
5918 036813ee 2019-05-09 stsp if (err && *new_te) {
5919 56e0773d 2019-11-28 stsp free(*new_te);
5920 036813ee 2019-05-09 stsp *new_te = NULL;
5922 036813ee 2019-05-09 stsp return err;
5925 036813ee 2019-05-09 stsp static const struct got_error *
5926 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5927 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5929 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5930 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5932 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5934 036813ee 2019-05-09 stsp return err;
5935 036813ee 2019-05-09 stsp if (new_pe == NULL)
5936 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5937 036813ee 2019-05-09 stsp return NULL;
5940 afa376bf 2019-05-09 stsp static const struct got_error *
5941 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5942 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5944 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5945 5f8a88c6 2019-08-03 stsp unsigned char status;
5947 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5948 ae1e948a 2021-09-28 thomas return NULL;
5950 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5951 afa376bf 2019-05-09 stsp ct_path++;
5953 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5954 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5956 5f8a88c6 2019-08-03 stsp status = ct->status;
5958 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5959 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5962 036813ee 2019-05-09 stsp static const struct got_error *
5963 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5964 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5966 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5967 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5968 44d03001 2019-05-09 stsp char *te_path;
5970 44d03001 2019-05-09 stsp *modified = 0;
5972 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5973 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5974 44d03001 2019-05-09 stsp te->name) == -1)
5975 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5977 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5978 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5979 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5980 44d03001 2019-05-09 stsp strlen(te_path));
5981 62d463ca 2020-10-20 naddy if (*modified)
5985 44d03001 2019-05-09 stsp free(te_path);
5986 44d03001 2019-05-09 stsp return err;
5989 44d03001 2019-05-09 stsp static const struct got_error *
5990 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5991 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5992 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5994 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5995 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5997 036813ee 2019-05-09 stsp *ctp = NULL;
5999 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6000 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6001 036813ee 2019-05-09 stsp char *ct_name = NULL;
6002 036813ee 2019-05-09 stsp int path_matches;
6004 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6005 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
6006 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
6007 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
6008 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6011 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
6012 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
6016 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6019 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6021 036813ee 2019-05-09 stsp return err;
6022 036813ee 2019-05-09 stsp if (!path_matches)
6025 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
6027 d34b633e 2020-10-19 stsp return err;
6029 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
6030 d34b633e 2020-10-19 stsp free(ct_name);
6033 d34b633e 2020-10-19 stsp free(ct_name);
6035 036813ee 2019-05-09 stsp *ctp = ct;
6039 2b496619 2019-07-10 stsp return err;
6042 2b496619 2019-07-10 stsp static const struct got_error *
6043 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6044 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
6045 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
6046 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
6047 2b496619 2019-07-10 stsp struct got_repository *repo)
6049 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
6050 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
6051 2b496619 2019-07-10 stsp char *subtree_path;
6052 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
6053 ba580f68 2020-03-22 stsp int nentries;
6055 2b496619 2019-07-10 stsp *new_tep = NULL;
6057 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6058 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
6059 2b496619 2019-07-10 stsp child_path) == -1)
6060 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
6062 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
6063 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
6064 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
6065 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
6067 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6068 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
6069 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
6070 2b496619 2019-07-10 stsp goto done;
6072 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
6073 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
6074 2b496619 2019-07-10 stsp if (err) {
6075 56e0773d 2019-11-28 stsp free(new_te);
6076 2b496619 2019-07-10 stsp goto done;
6078 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
6081 2b496619 2019-07-10 stsp free(subtree_path);
6082 2b496619 2019-07-10 stsp if (err == NULL)
6083 2b496619 2019-07-10 stsp *new_tep = new_te;
6084 036813ee 2019-05-09 stsp return err;
6087 036813ee 2019-05-09 stsp static const struct got_error *
6088 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
6089 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
6090 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
6091 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6092 036813ee 2019-05-09 stsp struct got_repository *repo)
6094 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
6095 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
6096 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
6097 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
6099 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
6100 ba580f68 2020-03-22 stsp *nentries = 0;
6102 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
6103 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6104 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6105 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
6107 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
6108 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
6109 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
6112 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6113 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
6116 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6117 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
6119 036813ee 2019-05-09 stsp goto done;
6121 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
6122 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
6123 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
6125 036813ee 2019-05-09 stsp goto done;
6126 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
6128 afa376bf 2019-05-09 stsp goto done;
6129 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
6130 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6132 2f51b5b3 2019-05-09 stsp goto done;
6133 ba580f68 2020-03-22 stsp (*nentries)++;
6135 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
6136 2b496619 2019-07-10 stsp if (base_tree == NULL ||
6137 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
6138 2b496619 2019-07-10 stsp == NULL) {
6139 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
6140 2b496619 2019-07-10 stsp child_path, path_base_tree,
6141 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
6144 2b496619 2019-07-10 stsp goto done;
6145 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6147 2b496619 2019-07-10 stsp goto done;
6148 ba580f68 2020-03-22 stsp (*nentries)++;
6153 2f51b5b3 2019-05-09 stsp if (base_tree) {
6154 56e0773d 2019-11-28 stsp int i, nbase_entries;
6155 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
6156 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
6157 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
6158 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
6160 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
6161 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
6162 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
6163 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
6165 63c5ca5d 2019-08-24 stsp goto done;
6166 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
6168 63c5ca5d 2019-08-24 stsp goto done;
6169 ba580f68 2020-03-22 stsp (*nentries)++;
6173 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
6174 44d03001 2019-05-09 stsp int modified;
6175 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6177 036813ee 2019-05-09 stsp goto done;
6178 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
6179 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
6181 2f51b5b3 2019-05-09 stsp goto done;
6182 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
6183 44d03001 2019-05-09 stsp if (modified) {
6184 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
6185 ba580f68 2020-03-22 stsp int nsubentries;
6186 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
6187 ba580f68 2020-03-22 stsp &nsubentries, te,
6188 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
6189 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
6191 44d03001 2019-05-09 stsp goto done;
6192 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6193 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6194 ba580f68 2020-03-22 stsp free(new_id);
6197 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6198 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6199 56e0773d 2019-11-28 stsp free(new_id);
6201 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6203 036813ee 2019-05-09 stsp goto done;
6204 ba580f68 2020-03-22 stsp (*nentries)++;
6208 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6209 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6211 f66c734c 2019-09-22 stsp goto done;
6213 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6214 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6215 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6216 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
6217 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6218 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6219 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6221 2f51b5b3 2019-05-09 stsp goto done;
6222 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6224 2f51b5b3 2019-05-09 stsp goto done;
6225 ba580f68 2020-03-22 stsp (*nentries)++;
6227 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6228 b416585c 2019-05-13 stsp status_arg);
6230 afa376bf 2019-05-09 stsp goto done;
6232 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6233 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6235 2f51b5b3 2019-05-09 stsp goto done;
6236 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6238 2f51b5b3 2019-05-09 stsp goto done;
6239 ba580f68 2020-03-22 stsp (*nentries)++;
6244 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6245 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6247 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6248 2e1fa222 2020-07-23 stsp return err;
6251 2e1fa222 2020-07-23 stsp static const struct got_error *
6252 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6253 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6254 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6255 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6257 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6258 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6259 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6261 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6262 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6263 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6265 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6267 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6268 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6269 437adc9d 2020-12-10 yzhong if (err)
6270 437adc9d 2020-12-10 yzhong goto done;
6273 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6274 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6275 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6276 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6277 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6278 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6279 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6280 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6282 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6283 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6284 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6285 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6286 72fd46fa 2019-09-06 stsp !have_staged_files);
6288 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6289 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6290 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6291 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6292 72fd46fa 2019-09-06 stsp !have_staged_files);
6294 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6296 437adc9d 2020-12-10 yzhong goto done;
6297 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6298 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6299 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6300 3969253a 2020-03-07 stsp if (err) {
6301 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6302 437adc9d 2020-12-10 yzhong goto done;
6304 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6305 3969253a 2020-03-07 stsp if (err) {
6306 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6307 437adc9d 2020-12-10 yzhong goto done;
6310 437adc9d 2020-12-10 yzhong free(relpath);
6311 437adc9d 2020-12-10 yzhong relpath = NULL;
6314 437adc9d 2020-12-10 yzhong free(relpath);
6315 d56d26ce 2019-05-10 stsp return err;
6319 d56d26ce 2019-05-10 stsp static const struct got_error *
6320 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6321 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6322 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6323 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6324 735ef5ac 2019-08-03 stsp int ood_errcode)
6326 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6327 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6328 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6330 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6331 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6332 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6333 1a36436d 2019-06-10 stsp return NULL;
6335 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6336 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6338 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6339 945f9229 2022-04-16 thomas if (err)
6340 945f9229 2022-04-16 thomas goto done;
6341 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6342 9bead371 2019-07-28 stsp if (err) {
6343 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6344 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6345 1a36436d 2019-06-10 stsp goto done;
6346 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6347 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6349 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6350 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6351 945f9229 2022-04-16 thomas if (err)
6352 945f9229 2022-04-16 thomas goto done;
6353 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6354 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6355 1a36436d 2019-06-10 stsp goto done;
6356 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6360 945f9229 2022-04-16 thomas if (commit)
6361 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6362 1a36436d 2019-06-10 stsp return err;
6365 ef20f542 2022-06-26 thomas static const struct got_error *
6366 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6367 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6368 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
6369 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
6370 10604dce 2021-09-24 thomas struct got_worktree *worktree,
6371 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
6372 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6373 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6374 afa376bf 2019-05-09 stsp struct got_repository *repo)
6376 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6377 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6378 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6379 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6380 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6381 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6382 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6383 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6384 10604dce 2021-09-24 thomas int nentries, nparents = 0;
6385 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6386 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6387 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6388 892eab0a 2022-07-22 thomas time_t timestamp;
6390 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6392 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6394 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6396 588edf97 2019-05-10 stsp goto done;
6398 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6400 588edf97 2019-05-10 stsp goto done;
6402 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6403 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
6404 ef899790 2022-11-01 thomas &logmsg, commit_arg);
6409 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6410 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6414 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6415 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6416 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6417 cf066bf8 2019-05-09 stsp char *ondisk_path;
6419 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6420 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6421 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6424 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6425 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6426 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6427 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6430 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6431 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6432 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6433 cf066bf8 2019-05-09 stsp goto done;
6435 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6436 2e1fa222 2020-07-23 stsp free(ondisk_path);
6438 cf066bf8 2019-05-09 stsp goto done;
6441 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6442 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6443 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6445 036813ee 2019-05-09 stsp goto done;
6447 538aebb5 2023-04-22 thomas err = got_object_qid_alloc(&pid, head_commit_id);
6449 de18fc63 2019-05-09 stsp goto done;
6450 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6451 10604dce 2021-09-24 thomas nparents++;
6452 10604dce 2021-09-24 thomas if (parent_id2) {
6453 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6454 10604dce 2021-09-24 thomas if (err)
6455 10604dce 2021-09-24 thomas goto done;
6456 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6457 10604dce 2021-09-24 thomas nparents++;
6459 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6460 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6461 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6462 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6463 33ad4cbe 2019-05-12 jcs free(logmsg);
6465 9d40349a 2019-05-09 stsp goto done;
6467 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6468 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6469 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6470 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6471 09f5bd90 2019-05-09 stsp goto done;
6473 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6474 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6476 09f5bd90 2019-05-09 stsp goto done;
6477 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6479 ebf99748 2019-05-09 stsp goto done;
6480 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6481 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6482 09f5bd90 2019-05-09 stsp goto done;
6484 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6485 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6487 f2c16586 2019-05-09 stsp goto done;
6488 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6490 f2c16586 2019-05-09 stsp goto done;
6492 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6494 09f5bd90 2019-05-09 stsp goto done;
6496 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6498 ebf99748 2019-05-09 stsp goto done;
6500 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6501 588edf97 2019-05-10 stsp if (head_tree)
6502 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6503 588edf97 2019-05-10 stsp if (head_commit)
6504 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6505 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6506 2f17228e 2019-05-12 stsp if (head_ref2) {
6507 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6508 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6509 2f17228e 2019-05-12 stsp err = unlockerr;
6510 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6512 39cd0ff6 2019-07-12 stsp return err;
6515 5c1e53bc 2019-07-28 stsp static const struct got_error *
6516 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6517 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6519 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6520 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6522 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6523 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6524 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6526 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6527 5c1e53bc 2019-07-28 stsp ct_path++;
6529 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6530 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6534 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6535 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6537 5c1e53bc 2019-07-28 stsp return NULL;
6540 f0b75401 2019-08-03 stsp static const struct got_error *
6541 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6543 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6545 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6546 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6547 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6550 f0b75401 2019-08-03 stsp return NULL;
6553 5f8a88c6 2019-08-03 stsp static const struct got_error *
6554 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6555 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6557 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6558 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6560 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6561 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6563 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6564 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6565 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6566 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6567 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6568 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6571 5f8a88c6 2019-08-03 stsp return NULL;
6574 39cd0ff6 2019-07-12 stsp const struct got_error *
6575 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6576 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6577 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6578 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6579 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6580 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6581 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6583 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6584 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6585 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6586 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6587 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6588 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6589 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6590 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6591 ef899790 2022-11-01 thomas char *diff_path = NULL;
6592 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6594 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6596 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6597 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6599 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6601 39cd0ff6 2019-07-12 stsp goto done;
6603 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6605 39cd0ff6 2019-07-12 stsp goto done;
6607 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6609 39cd0ff6 2019-07-12 stsp goto done;
6611 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6613 39cd0ff6 2019-07-12 stsp goto done;
6615 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6616 5f8a88c6 2019-08-03 stsp &have_staged_files);
6617 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6618 5f8a88c6 2019-08-03 stsp goto done;
6619 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6620 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6622 5f8a88c6 2019-08-03 stsp goto done;
6625 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6626 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6627 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6628 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6629 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6630 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6631 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6632 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6633 ef899790 2022-11-01 thomas if (show_diff) {
6634 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6635 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6636 ef899790 2022-11-01 thomas if (err)
6637 ef899790 2022-11-01 thomas goto done;
6638 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6639 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6640 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6641 ef899790 2022-11-01 thomas goto done;
6643 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6644 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6645 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6646 ef899790 2022-11-01 thomas goto done;
6650 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6651 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6652 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6654 5c1e53bc 2019-07-28 stsp goto done;
6657 ef899790 2022-11-01 thomas if (show_diff) {
6658 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6659 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6660 ef899790 2022-11-01 thomas goto done;
6664 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6665 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6666 39cd0ff6 2019-07-12 stsp goto done;
6669 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6670 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6672 5c1e53bc 2019-07-28 stsp goto done;
6675 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6676 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6677 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6679 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6680 f0b75401 2019-08-03 stsp ct_path++;
6681 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6682 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6683 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6685 b50cabdf 2019-07-12 stsp goto done;
6689 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6690 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6691 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6692 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6694 39cd0ff6 2019-07-12 stsp goto done;
6696 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6697 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6698 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6699 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6700 39cd0ff6 2019-07-12 stsp err = sync_err;
6702 39cd0ff6 2019-07-12 stsp if (fileindex)
6703 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6704 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6705 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6706 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6707 39cd0ff6 2019-07-12 stsp err = unlockerr;
6708 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6709 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6711 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6713 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6714 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6715 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6716 ef899790 2022-11-01 thomas free(diff_path);
6717 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6718 ef899790 2022-11-01 thomas err == NULL)
6719 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6720 c4296144 2019-05-09 stsp return err;
6723 8656d6c4 2019-05-20 stsp const char *
6724 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6726 8656d6c4 2019-05-20 stsp return ct->path;
6729 8656d6c4 2019-05-20 stsp unsigned int
6730 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6732 8656d6c4 2019-05-20 stsp return ct->status;
6735 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6736 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6737 818c7501 2019-07-11 stsp struct got_repository *repo;
6740 818c7501 2019-07-11 stsp static const struct got_error *
6741 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6743 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6744 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6745 818c7501 2019-07-11 stsp unsigned char status;
6746 818c7501 2019-07-11 stsp struct stat sb;
6747 818c7501 2019-07-11 stsp char *ondisk_path;
6749 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6750 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6751 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6752 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6754 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6756 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6758 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6759 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6760 818c7501 2019-07-11 stsp free(ondisk_path);
6762 818c7501 2019-07-11 stsp return err;
6764 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6765 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6766 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6767 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6769 818c7501 2019-07-11 stsp return NULL;
6772 818c7501 2019-07-11 stsp const struct got_error *
6773 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6774 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6775 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6776 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6778 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6779 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6780 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6781 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6782 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6783 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6784 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6786 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6787 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6788 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6790 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6792 818c7501 2019-07-11 stsp return err;
6794 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6796 818c7501 2019-07-11 stsp goto done;
6798 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6799 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6800 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6803 818c7501 2019-07-11 stsp goto done;
6805 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6807 818c7501 2019-07-11 stsp goto done;
6809 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6811 818c7501 2019-07-11 stsp goto done;
6813 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6815 818c7501 2019-07-11 stsp goto done;
6817 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6820 e51d7b55 2020-01-04 stsp goto done;
6822 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6824 818c7501 2019-07-11 stsp goto done;
6825 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6826 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6827 e51d7b55 2020-01-04 stsp goto done;
6830 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6831 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6833 818c7501 2019-07-11 stsp goto done;
6834 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6836 818c7501 2019-07-11 stsp goto done;
6838 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6840 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6842 818c7501 2019-07-11 stsp goto done;
6844 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6846 818c7501 2019-07-11 stsp goto done;
6848 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6849 818c7501 2019-07-11 stsp worktree->base_commit_id);
6851 818c7501 2019-07-11 stsp goto done;
6852 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6854 818c7501 2019-07-11 stsp goto done;
6856 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6858 818c7501 2019-07-11 stsp goto done;
6860 818c7501 2019-07-11 stsp free(fileindex_path);
6861 818c7501 2019-07-11 stsp free(tmp_branch_name);
6862 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6863 818c7501 2019-07-11 stsp free(branch_ref_name);
6864 818c7501 2019-07-11 stsp if (branch_ref)
6865 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6866 818c7501 2019-07-11 stsp if (wt_branch)
6867 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6868 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6869 818c7501 2019-07-11 stsp if (err) {
6870 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6871 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6872 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6874 818c7501 2019-07-11 stsp if (*tmp_branch) {
6875 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6876 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6878 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6879 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6880 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6882 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6884 818c7501 2019-07-11 stsp return err;
6887 818c7501 2019-07-11 stsp const struct got_error *
6888 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6889 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6890 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6891 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6893 818c7501 2019-07-11 stsp const struct got_error *err;
6894 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6895 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6896 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6897 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6898 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6900 818c7501 2019-07-11 stsp *commit_id = NULL;
6901 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6902 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6903 3e3a69f1 2019-07-25 stsp *branch = NULL;
6904 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6906 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6908 3e3a69f1 2019-07-25 stsp return err;
6910 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6912 f032f1f7 2019-08-04 stsp goto done;
6914 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6915 f032f1f7 2019-08-04 stsp &have_staged_files);
6916 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6917 f032f1f7 2019-08-04 stsp goto done;
6918 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6919 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6920 3e3a69f1 2019-07-25 stsp goto done;
6923 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6925 f032f1f7 2019-08-04 stsp goto done;
6927 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6929 818c7501 2019-07-11 stsp goto done;
6931 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6933 818c7501 2019-07-11 stsp goto done;
6935 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6937 818c7501 2019-07-11 stsp goto done;
6939 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6941 818c7501 2019-07-11 stsp goto done;
6943 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6944 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6946 818c7501 2019-07-11 stsp goto done;
6948 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6950 818c7501 2019-07-11 stsp goto done;
6952 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6954 818c7501 2019-07-11 stsp goto done;
6956 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6957 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6959 818c7501 2019-07-11 stsp goto done;
6961 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6963 818c7501 2019-07-11 stsp goto done;
6965 818c7501 2019-07-11 stsp free(commit_ref_name);
6966 818c7501 2019-07-11 stsp free(branch_ref_name);
6967 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6968 818c7501 2019-07-11 stsp if (commit_ref)
6969 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6970 818c7501 2019-07-11 stsp if (branch_ref)
6971 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6972 818c7501 2019-07-11 stsp if (err) {
6973 818c7501 2019-07-11 stsp free(*commit_id);
6974 818c7501 2019-07-11 stsp *commit_id = NULL;
6975 818c7501 2019-07-11 stsp if (*tmp_branch) {
6976 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6977 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6979 818c7501 2019-07-11 stsp if (*new_base_branch) {
6980 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6981 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6983 818c7501 2019-07-11 stsp if (*branch) {
6984 818c7501 2019-07-11 stsp got_ref_close(*branch);
6985 818c7501 2019-07-11 stsp *branch = NULL;
6987 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6988 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6989 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6991 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6993 818c7501 2019-07-11 stsp return err;
6996 818c7501 2019-07-11 stsp const struct got_error *
6997 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6999 818c7501 2019-07-11 stsp const struct got_error *err;
7000 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
7002 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7004 818c7501 2019-07-11 stsp return err;
7006 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7007 818c7501 2019-07-11 stsp free(tmp_branch_name);
7008 818c7501 2019-07-11 stsp return NULL;
7011 cd634f2d 2024-03-30 thomas const struct got_error *
7012 cd634f2d 2024-03-30 thomas got_worktree_rebase_info(char **new_base_branch_name, char **branch_name,
7013 cd634f2d 2024-03-30 thomas struct got_worktree *worktree, struct got_repository *repo)
7015 cd634f2d 2024-03-30 thomas const struct got_error *err;
7016 cd634f2d 2024-03-30 thomas char *new_base_branch_ref_name = NULL;
7017 cd634f2d 2024-03-30 thomas char *branch_ref_name = NULL;
7018 cd634f2d 2024-03-30 thomas struct got_reference *branch_ref = NULL, *branch = NULL;
7019 cd634f2d 2024-03-30 thomas struct got_reference *new_base_branch = NULL;
7021 cd634f2d 2024-03-30 thomas *new_base_branch_name = NULL;
7022 cd634f2d 2024-03-30 thomas *branch_name = NULL;
7024 cd634f2d 2024-03-30 thomas err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7025 cd634f2d 2024-03-30 thomas if (err)
7026 cd634f2d 2024-03-30 thomas goto done;
7028 cd634f2d 2024-03-30 thomas err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7029 cd634f2d 2024-03-30 thomas if (err)
7030 cd634f2d 2024-03-30 thomas goto done;
7032 cd634f2d 2024-03-30 thomas err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
7033 cd634f2d 2024-03-30 thomas if (err)
7034 cd634f2d 2024-03-30 thomas goto done;
7036 cd634f2d 2024-03-30 thomas err = got_ref_open(&branch, repo,
7037 cd634f2d 2024-03-30 thomas got_ref_get_symref_target(branch_ref), 0);
7038 cd634f2d 2024-03-30 thomas if (err)
7039 cd634f2d 2024-03-30 thomas goto done;
7041 cd634f2d 2024-03-30 thomas err = got_ref_open(&new_base_branch, repo,
7042 cd634f2d 2024-03-30 thomas new_base_branch_ref_name, 0);
7043 cd634f2d 2024-03-30 thomas if (err)
7044 cd634f2d 2024-03-30 thomas goto done;
7046 cd634f2d 2024-03-30 thomas if (!got_ref_is_symbolic(new_base_branch)) {
7047 cd634f2d 2024-03-30 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7048 cd634f2d 2024-03-30 thomas "%s is not a symbolic reference",
7049 cd634f2d 2024-03-30 thomas got_ref_get_name(branch_ref));
7050 cd634f2d 2024-03-30 thomas goto done;
7053 cd634f2d 2024-03-30 thomas *new_base_branch_name = strdup(got_ref_get_symref_target(
7054 cd634f2d 2024-03-30 thomas new_base_branch));
7055 cd634f2d 2024-03-30 thomas if (*new_base_branch_name == NULL) {
7056 cd634f2d 2024-03-30 thomas err = got_error_from_errno("strdup");
7057 cd634f2d 2024-03-30 thomas goto done;
7059 cd634f2d 2024-03-30 thomas *branch_name = strdup(got_ref_get_name(branch));
7060 cd634f2d 2024-03-30 thomas if (*branch_name == NULL) {
7061 cd634f2d 2024-03-30 thomas err = got_error_from_errno("strdup");
7062 cd634f2d 2024-03-30 thomas goto done;
7065 cd634f2d 2024-03-30 thomas free(branch_ref_name);
7066 cd634f2d 2024-03-30 thomas if (branch_ref)
7067 cd634f2d 2024-03-30 thomas got_ref_close(branch_ref);
7068 cd634f2d 2024-03-30 thomas if (new_base_branch)
7069 cd634f2d 2024-03-30 thomas got_ref_close(new_base_branch);
7070 cd634f2d 2024-03-30 thomas if (branch)
7071 cd634f2d 2024-03-30 thomas got_ref_close(branch);
7072 cd634f2d 2024-03-30 thomas return err;
7075 818c7501 2019-07-11 stsp static const struct got_error *
7076 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7077 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
7079 0ebf8283 2019-07-24 stsp *logmsg = arg;
7080 818c7501 2019-07-11 stsp return NULL;
7083 818c7501 2019-07-11 stsp static const struct got_error *
7084 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7085 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7086 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7087 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7089 818c7501 2019-07-11 stsp return NULL;
7092 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
7093 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
7094 01757395 2019-07-12 stsp void *progress_arg;
7095 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
7098 01757395 2019-07-12 stsp static const struct got_error *
7099 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
7101 01757395 2019-07-12 stsp const struct got_error *err;
7102 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
7104 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
7106 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
7108 01757395 2019-07-12 stsp return err;
7110 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
7111 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
7112 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
7113 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
7114 01757395 2019-07-12 stsp return NULL;
7116 01757395 2019-07-12 stsp p = strdup(path);
7117 01757395 2019-07-12 stsp if (p == NULL)
7118 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
7120 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7121 01757395 2019-07-12 stsp if (err || new == NULL)
7123 01757395 2019-07-12 stsp return err;
7126 0ebf8283 2019-07-24 stsp static const struct got_error *
7127 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7128 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
7130 818c7501 2019-07-11 stsp const struct got_error *err;
7131 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
7133 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7134 818c7501 2019-07-11 stsp if (err) {
7135 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
7136 818c7501 2019-07-11 stsp goto done;
7137 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7139 818c7501 2019-07-11 stsp goto done;
7140 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
7142 818c7501 2019-07-11 stsp goto done;
7143 de05890f 2020-03-05 stsp } else if (is_rebase) {
7144 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
7147 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
7149 818c7501 2019-07-11 stsp goto done;
7150 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
7151 818c7501 2019-07-11 stsp free(stored_id);
7152 818c7501 2019-07-11 stsp if (cmp != 0) {
7153 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7154 818c7501 2019-07-11 stsp goto done;
7158 0ebf8283 2019-07-24 stsp if (commit_ref)
7159 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7160 0ebf8283 2019-07-24 stsp return err;
7163 0ebf8283 2019-07-24 stsp static const struct got_error *
7164 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
7165 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
7166 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7167 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
7168 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7169 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7171 0ebf8283 2019-07-24 stsp const struct got_error *err;
7172 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7173 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
7174 3e3a69f1 2019-07-25 stsp char *fileindex_path;
7176 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7178 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7180 0ebf8283 2019-07-24 stsp return err;
7182 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
7183 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
7184 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
7185 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
7186 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
7187 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
7188 818c7501 2019-07-11 stsp if (commit_ref)
7189 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
7190 818c7501 2019-07-11 stsp return err;
7193 818c7501 2019-07-11 stsp const struct got_error *
7194 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7195 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7196 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7197 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7198 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7199 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7201 0ebf8283 2019-07-24 stsp const struct got_error *err;
7202 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7204 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7206 0ebf8283 2019-07-24 stsp return err;
7208 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7210 0ebf8283 2019-07-24 stsp goto done;
7212 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7213 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7214 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7216 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7217 0ebf8283 2019-07-24 stsp return err;
7220 0ebf8283 2019-07-24 stsp const struct got_error *
7221 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7222 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7223 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7224 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7225 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7226 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7228 0ebf8283 2019-07-24 stsp const struct got_error *err;
7229 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7231 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7233 0ebf8283 2019-07-24 stsp return err;
7235 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7237 0ebf8283 2019-07-24 stsp goto done;
7239 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7240 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7241 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7243 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7244 0ebf8283 2019-07-24 stsp return err;
7247 0ebf8283 2019-07-24 stsp static const struct got_error *
7248 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
7249 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7250 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7251 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
7252 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
7253 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7255 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7256 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7257 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7258 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7259 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7260 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7261 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7263 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7264 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7265 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7267 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7269 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7271 3e3a69f1 2019-07-25 stsp return err;
7273 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7274 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7275 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7276 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7277 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
7279 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7280 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7282 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7283 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7284 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7285 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7286 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7287 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7289 01757395 2019-07-12 stsp if (merged_paths) {
7290 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7291 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7292 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7293 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7296 01757395 2019-07-12 stsp goto done;
7299 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7300 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7302 01757395 2019-07-12 stsp goto done;
7305 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7306 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7307 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7309 a0e95631 2019-07-12 stsp goto done;
7310 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7311 a0e95631 2019-07-12 stsp goto done;
7314 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7316 edd02c5e 2019-07-11 stsp goto done;
7318 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7320 818c7501 2019-07-11 stsp goto done;
7322 5943eee2 2019-08-13 stsp if (new_logmsg) {
7323 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7324 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7325 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7326 5943eee2 2019-08-13 stsp goto done;
7329 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7331 5943eee2 2019-08-13 stsp goto done;
7334 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7335 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7336 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
7337 8db00f97 2022-07-22 thomas committer ? committer :
7338 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
7339 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7341 a0e95631 2019-07-12 stsp goto done;
7343 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7345 a0e95631 2019-07-12 stsp goto done;
7347 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7349 a0e95631 2019-07-12 stsp goto done;
7351 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7352 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7353 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7354 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7355 a0e95631 2019-07-12 stsp err = sync_err;
7357 a0e95631 2019-07-12 stsp free(fileindex_path);
7358 a0e95631 2019-07-12 stsp free(head_commit_id);
7359 a0e95631 2019-07-12 stsp if (head_ref)
7360 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7361 818c7501 2019-07-11 stsp if (err) {
7362 818c7501 2019-07-11 stsp free(*new_commit_id);
7363 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7365 818c7501 2019-07-11 stsp return err;
7368 818c7501 2019-07-11 stsp const struct got_error *
7369 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7370 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7371 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7372 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7373 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
7374 be94c032 2023-02-20 thomas struct got_repository *repo)
7376 0ebf8283 2019-07-24 stsp const struct got_error *err;
7377 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7378 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7379 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7381 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7383 0ebf8283 2019-07-24 stsp return err;
7385 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7387 0ebf8283 2019-07-24 stsp goto done;
7388 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7390 0ebf8283 2019-07-24 stsp goto done;
7391 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7392 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7393 0ebf8283 2019-07-24 stsp goto done;
7396 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7397 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7398 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
7400 0ebf8283 2019-07-24 stsp if (commit_ref)
7401 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7402 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7403 0ebf8283 2019-07-24 stsp free(commit_id);
7404 0ebf8283 2019-07-24 stsp return err;
7407 0ebf8283 2019-07-24 stsp const struct got_error *
7408 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7409 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7410 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7411 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7412 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7413 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7415 0ebf8283 2019-07-24 stsp const struct got_error *err;
7416 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7417 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7419 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7421 0ebf8283 2019-07-24 stsp return err;
7423 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7425 0ebf8283 2019-07-24 stsp goto done;
7427 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7428 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7429 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
7431 0ebf8283 2019-07-24 stsp if (commit_ref)
7432 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7433 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7434 0ebf8283 2019-07-24 stsp return err;
7437 0ebf8283 2019-07-24 stsp const struct got_error *
7438 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7439 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7441 3e3a69f1 2019-07-25 stsp if (fileindex)
7442 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7443 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7446 69844fba 2019-07-11 stsp static const struct got_error *
7447 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7449 69844fba 2019-07-11 stsp const struct got_error *err;
7450 69844fba 2019-07-11 stsp struct got_reference *ref;
7452 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7453 69844fba 2019-07-11 stsp if (err) {
7454 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7455 69844fba 2019-07-11 stsp return NULL;
7456 69844fba 2019-07-11 stsp return err;
7459 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7460 69844fba 2019-07-11 stsp got_ref_close(ref);
7461 69844fba 2019-07-11 stsp return err;
7464 69844fba 2019-07-11 stsp static const struct got_error *
7465 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7467 69844fba 2019-07-11 stsp const struct got_error *err;
7468 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7469 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7471 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7473 69844fba 2019-07-11 stsp goto done;
7474 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7476 69844fba 2019-07-11 stsp goto done;
7478 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7480 69844fba 2019-07-11 stsp goto done;
7481 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7483 69844fba 2019-07-11 stsp goto done;
7485 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7487 69844fba 2019-07-11 stsp goto done;
7488 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7490 69844fba 2019-07-11 stsp goto done;
7492 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7494 69844fba 2019-07-11 stsp goto done;
7495 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7497 69844fba 2019-07-11 stsp goto done;
7500 69844fba 2019-07-11 stsp free(tmp_branch_name);
7501 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7502 69844fba 2019-07-11 stsp free(branch_ref_name);
7503 69844fba 2019-07-11 stsp free(commit_ref_name);
7504 e600f124 2021-03-21 stsp return err;
7507 ef20f542 2022-06-26 thomas static const struct got_error *
7508 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7509 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7511 e600f124 2021-03-21 stsp const struct got_error *err;
7512 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7513 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7514 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7515 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7516 e600f124 2021-03-21 stsp char *refname = NULL;
7518 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7519 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7520 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7521 e600f124 2021-03-21 stsp branch_name += 11;
7523 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7525 e600f124 2021-03-21 stsp return err;
7527 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7528 e600f124 2021-03-21 stsp new_id_str) == -1) {
7529 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7530 e600f124 2021-03-21 stsp goto done;
7533 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7535 e600f124 2021-03-21 stsp goto done;
7537 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7539 e600f124 2021-03-21 stsp goto done;
7541 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7543 e600f124 2021-03-21 stsp free(new_id_str);
7544 e600f124 2021-03-21 stsp free(refname);
7545 e600f124 2021-03-21 stsp free(old_commit_id);
7547 e600f124 2021-03-21 stsp got_ref_close(ref);
7548 69844fba 2019-07-11 stsp return err;
7551 818c7501 2019-07-11 stsp const struct got_error *
7552 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7553 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7554 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7555 f08f28be 2023-02-03 thomas int create_backup)
7557 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7558 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7559 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7561 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7563 818c7501 2019-07-11 stsp return err;
7565 e600f124 2021-03-21 stsp if (create_backup) {
7566 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7567 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7569 e600f124 2021-03-21 stsp goto done;
7572 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7574 818c7501 2019-07-11 stsp goto done;
7576 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7578 818c7501 2019-07-11 stsp goto done;
7580 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7582 818c7501 2019-07-11 stsp goto done;
7584 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7586 a615e0e7 2020-12-16 stsp goto done;
7588 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7590 a615e0e7 2020-12-16 stsp goto done;
7591 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7592 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7593 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7594 a615e0e7 2020-12-16 stsp err = sync_err;
7596 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7597 a615e0e7 2020-12-16 stsp free(fileindex_path);
7598 818c7501 2019-07-11 stsp free(new_head_commit_id);
7599 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7600 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7601 818c7501 2019-07-11 stsp err = unlockerr;
7602 818c7501 2019-07-11 stsp return err;
7605 8641a332 2023-04-14 thomas static const struct got_error *
7606 8641a332 2023-04-14 thomas get_paths_changed_between_commits(struct got_pathlist_head *paths,
7607 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7608 8641a332 2023-04-14 thomas struct got_repository *repo)
7610 8641a332 2023-04-14 thomas const struct got_error *err;
7611 8641a332 2023-04-14 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7612 8641a332 2023-04-14 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7614 8641a332 2023-04-14 thomas if (id1) {
7615 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit1, repo, id1);
7616 8641a332 2023-04-14 thomas if (err)
7617 8641a332 2023-04-14 thomas goto done;
7619 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree1, repo,
7620 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit1));
7621 8641a332 2023-04-14 thomas if (err)
7622 8641a332 2023-04-14 thomas goto done;
7625 8641a332 2023-04-14 thomas if (id2) {
7626 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit2, repo, id2);
7627 8641a332 2023-04-14 thomas if (err)
7628 8641a332 2023-04-14 thomas goto done;
7630 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree2, repo,
7631 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit2));
7632 8641a332 2023-04-14 thomas if (err)
7633 8641a332 2023-04-14 thomas goto done;
7636 8641a332 2023-04-14 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7637 8641a332 2023-04-14 thomas got_diff_tree_collect_changed_paths, paths, 0);
7638 8641a332 2023-04-14 thomas if (err)
7639 8641a332 2023-04-14 thomas goto done;
7641 8641a332 2023-04-14 thomas if (commit1)
7642 8641a332 2023-04-14 thomas got_object_commit_close(commit1);
7643 8641a332 2023-04-14 thomas if (commit2)
7644 8641a332 2023-04-14 thomas got_object_commit_close(commit2);
7645 8641a332 2023-04-14 thomas if (tree1)
7646 8641a332 2023-04-14 thomas got_object_tree_close(tree1);
7647 8641a332 2023-04-14 thomas if (tree2)
7648 8641a332 2023-04-14 thomas got_object_tree_close(tree2);
7649 8641a332 2023-04-14 thomas return err;
7652 8641a332 2023-04-14 thomas static const struct got_error *
7653 8641a332 2023-04-14 thomas get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7654 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7655 8641a332 2023-04-14 thomas const char *path_prefix, struct got_repository *repo)
7657 8641a332 2023-04-14 thomas const struct got_error *err;
7658 8641a332 2023-04-14 thomas struct got_pathlist_head merged_paths;
7659 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
7660 8641a332 2023-04-14 thomas char *abspath = NULL, *wt_path = NULL;
7662 8641a332 2023-04-14 thomas TAILQ_INIT(&merged_paths);
7664 8641a332 2023-04-14 thomas err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7665 8641a332 2023-04-14 thomas if (err)
7666 8641a332 2023-04-14 thomas goto done;
7668 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, &merged_paths, entry) {
7669 8641a332 2023-04-14 thomas struct got_diff_changed_path *change = pe->data;
7671 8641a332 2023-04-14 thomas if (change->status != GOT_STATUS_ADD)
7672 8641a332 2023-04-14 thomas continue;
7674 8641a332 2023-04-14 thomas if (got_path_is_root_dir(path_prefix)) {
7675 8641a332 2023-04-14 thomas wt_path = strdup(pe->path);
7676 8641a332 2023-04-14 thomas if (wt_path == NULL) {
7677 8641a332 2023-04-14 thomas err = got_error_from_errno("strdup");
7678 8641a332 2023-04-14 thomas goto done;
7680 8641a332 2023-04-14 thomas } else {
7681 8641a332 2023-04-14 thomas if (asprintf(&abspath, "/%s", pe->path) == -1) {
7682 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
7683 8641a332 2023-04-14 thomas goto done;
7686 8641a332 2023-04-14 thomas err = got_path_skip_common_ancestor(&wt_path,
7687 8641a332 2023-04-14 thomas path_prefix, abspath);
7688 8641a332 2023-04-14 thomas if (err)
7689 8641a332 2023-04-14 thomas goto done;
7690 8641a332 2023-04-14 thomas free(abspath);
7691 8641a332 2023-04-14 thomas abspath = NULL;
7694 8641a332 2023-04-14 thomas err = got_pathlist_append(added_paths, wt_path, NULL);
7695 8641a332 2023-04-14 thomas if (err)
7696 8641a332 2023-04-14 thomas goto done;
7697 8641a332 2023-04-14 thomas wt_path = NULL;
7701 8641a332 2023-04-14 thomas got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7702 8641a332 2023-04-14 thomas free(abspath);
7703 8641a332 2023-04-14 thomas free(wt_path);
7704 8641a332 2023-04-14 thomas return err;
7707 8641a332 2023-04-14 thomas static const struct got_error *
7708 8641a332 2023-04-14 thomas get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7709 8641a332 2023-04-14 thomas struct got_object_id *id, const char *path_prefix,
7710 8641a332 2023-04-14 thomas struct got_repository *repo)
7712 8641a332 2023-04-14 thomas const struct got_error *err;
7713 8641a332 2023-04-14 thomas struct got_commit_object *commit = NULL;
7714 8641a332 2023-04-14 thomas struct got_object_qid *pid;
7716 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo, id);
7717 8641a332 2023-04-14 thomas if (err)
7718 8641a332 2023-04-14 thomas goto done;
7720 8641a332 2023-04-14 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7722 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(added_paths,
7723 8641a332 2023-04-14 thomas pid ? &pid->id : NULL, id, path_prefix, repo);
7724 8641a332 2023-04-14 thomas if (err)
7725 8641a332 2023-04-14 thomas goto done;
7727 8641a332 2023-04-14 thomas if (commit)
7728 8641a332 2023-04-14 thomas got_object_commit_close(commit);
7729 8641a332 2023-04-14 thomas return err;
7732 818c7501 2019-07-11 stsp const struct got_error *
7733 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7734 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7735 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7736 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7738 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7739 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7740 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7741 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7742 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7743 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7744 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7745 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7746 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7747 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7748 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7750 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7752 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7754 818c7501 2019-07-11 stsp return err;
7756 8641a332 2023-04-14 thomas err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7757 8641a332 2023-04-14 thomas if (err)
7758 8641a332 2023-04-14 thomas goto done;
7760 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7761 8641a332 2023-04-14 thomas if (err)
7762 8641a332 2023-04-14 thomas goto done;
7764 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7765 8641a332 2023-04-14 thomas if (err)
7766 8641a332 2023-04-14 thomas goto done;
7769 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7770 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7771 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
7772 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
7774 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7775 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7776 8641a332 2023-04-14 thomas if (err)
7777 8641a332 2023-04-14 thomas goto done;
7779 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7780 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7782 818c7501 2019-07-11 stsp goto done;
7784 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7786 818c7501 2019-07-11 stsp goto done;
7789 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7790 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7791 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7793 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7795 818c7501 2019-07-11 stsp goto done;
7797 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7798 25a102ed 2023-04-14 thomas if (err)
7799 25a102ed 2023-04-14 thomas goto done;
7801 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7802 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7804 818c7501 2019-07-11 stsp goto done;
7806 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7807 945f9229 2022-04-16 thomas worktree->path_prefix);
7809 ca355955 2019-07-12 stsp goto done;
7811 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7813 ca355955 2019-07-12 stsp goto done;
7815 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7817 818c7501 2019-07-11 stsp goto done;
7819 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7820 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7821 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7822 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7823 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7824 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7825 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7826 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
7827 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
7828 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7829 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7831 1f1abb7e 2019-08-08 stsp goto sync;
7833 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7834 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7836 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7837 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7838 ca355955 2019-07-12 stsp err = sync_err;
7840 8641a332 2023-04-14 thomas got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7841 818c7501 2019-07-11 stsp got_ref_close(resolved);
7842 a3a2faf2 2019-07-12 stsp free(tree_id);
7843 818c7501 2019-07-11 stsp free(commit_id);
7844 8641a332 2023-04-14 thomas free(merged_commit_id);
7845 945f9229 2022-04-16 thomas if (commit)
7846 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7847 0ebf8283 2019-07-24 stsp if (fileindex)
7848 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7849 0ebf8283 2019-07-24 stsp free(fileindex_path);
7850 8641a332 2023-04-14 thomas free(commit_ref_name);
7851 8641a332 2023-04-14 thomas if (commit_ref)
7852 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
7854 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7855 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7856 0ebf8283 2019-07-24 stsp err = unlockerr;
7857 0ebf8283 2019-07-24 stsp return err;
7860 0ebf8283 2019-07-24 stsp const struct got_error *
7861 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7862 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7863 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7864 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7866 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7867 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7868 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7869 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7870 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7871 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7872 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7873 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7875 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7876 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7877 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7878 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7880 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7882 0ebf8283 2019-07-24 stsp return err;
7884 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7886 0ebf8283 2019-07-24 stsp goto done;
7888 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7889 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7890 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7893 0ebf8283 2019-07-24 stsp goto done;
7895 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7897 0ebf8283 2019-07-24 stsp goto done;
7899 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7901 0ebf8283 2019-07-24 stsp goto done;
7903 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7904 0ebf8283 2019-07-24 stsp worktree);
7906 0ebf8283 2019-07-24 stsp goto done;
7908 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7911 0ebf8283 2019-07-24 stsp goto done;
7913 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7915 0ebf8283 2019-07-24 stsp goto done;
7917 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7919 0ebf8283 2019-07-24 stsp goto done;
7921 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7922 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7924 0ebf8283 2019-07-24 stsp goto done;
7925 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7927 0ebf8283 2019-07-24 stsp goto done;
7928 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7929 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7930 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7931 0ebf8283 2019-07-24 stsp goto done;
7934 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7935 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7937 0ebf8283 2019-07-24 stsp goto done;
7938 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7940 0ebf8283 2019-07-24 stsp goto done;
7942 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7944 0ebf8283 2019-07-24 stsp goto done;
7946 0ebf8283 2019-07-24 stsp free(fileindex_path);
7947 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7948 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7949 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7950 0ebf8283 2019-07-24 stsp if (wt_branch)
7951 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7952 0ebf8283 2019-07-24 stsp if (err) {
7953 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7954 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7955 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7957 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7958 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7959 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7961 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7962 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7963 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7964 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7966 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7968 0ebf8283 2019-07-24 stsp return err;
7971 0ebf8283 2019-07-24 stsp const struct got_error *
7972 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7973 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7975 3e3a69f1 2019-07-25 stsp if (fileindex)
7976 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7977 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7980 0ebf8283 2019-07-24 stsp const struct got_error *
7981 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7982 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7984 0ebf8283 2019-07-24 stsp const struct got_error *err;
7985 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7987 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7989 0ebf8283 2019-07-24 stsp return err;
7991 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7992 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7993 0ebf8283 2019-07-24 stsp return NULL;
7996 0ebf8283 2019-07-24 stsp const struct got_error *
7997 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7998 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7999 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
8000 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8002 0ebf8283 2019-07-24 stsp const struct got_error *err;
8003 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
8004 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
8005 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
8006 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
8007 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
8008 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
8010 0ebf8283 2019-07-24 stsp *commit_id = NULL;
8011 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
8012 d4b2e62d 2024-03-30 thomas *branch_ref = NULL;
8013 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
8014 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
8016 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
8018 3e3a69f1 2019-07-25 stsp return err;
8020 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8022 f032f1f7 2019-08-04 stsp goto done;
8024 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8025 f032f1f7 2019-08-04 stsp &have_staged_files);
8026 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
8027 f032f1f7 2019-08-04 stsp goto done;
8028 f032f1f7 2019-08-04 stsp if (have_staged_files) {
8029 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
8030 3e3a69f1 2019-07-25 stsp goto done;
8033 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8035 f032f1f7 2019-08-04 stsp goto done;
8037 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8039 0ebf8283 2019-07-24 stsp goto done;
8041 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8043 0ebf8283 2019-07-24 stsp goto done;
8045 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8046 0ebf8283 2019-07-24 stsp worktree);
8048 0ebf8283 2019-07-24 stsp goto done;
8050 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
8052 0ebf8283 2019-07-24 stsp goto done;
8054 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8056 0ebf8283 2019-07-24 stsp goto done;
8057 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
8059 0ebf8283 2019-07-24 stsp goto done;
8061 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
8063 0ebf8283 2019-07-24 stsp goto done;
8064 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
8066 0ebf8283 2019-07-24 stsp goto done;
8068 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
8070 0ebf8283 2019-07-24 stsp goto done;
8072 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8073 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8074 3e3a69f1 2019-07-25 stsp free(fileindex_path);
8075 0ebf8283 2019-07-24 stsp if (commit_ref)
8076 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
8077 0ebf8283 2019-07-24 stsp if (base_commit_ref)
8078 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
8079 0ebf8283 2019-07-24 stsp if (err) {
8080 0ebf8283 2019-07-24 stsp free(*commit_id);
8081 0ebf8283 2019-07-24 stsp *commit_id = NULL;
8082 0ebf8283 2019-07-24 stsp free(*base_commit_id);
8083 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
8084 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
8085 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
8086 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
8088 d4b2e62d 2024-03-30 thomas if (*branch_ref) {
8089 d4b2e62d 2024-03-30 thomas got_ref_close(*branch_ref);
8090 d4b2e62d 2024-03-30 thomas *branch_ref = NULL;
8092 3e3a69f1 2019-07-25 stsp if (*fileindex) {
8093 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
8094 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
8096 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
8098 cd634f2d 2024-03-30 thomas return err;
8101 cd634f2d 2024-03-30 thomas const struct got_error *
8102 cd634f2d 2024-03-30 thomas got_worktree_histedit_info(char **branch_name,
8103 cd634f2d 2024-03-30 thomas struct got_worktree *worktree, struct got_repository *repo)
8105 cd634f2d 2024-03-30 thomas const struct got_error *err;
8106 cd634f2d 2024-03-30 thomas struct got_reference *branch_ref = NULL;
8107 cd634f2d 2024-03-30 thomas char *branch_ref_name = NULL;
8109 cd634f2d 2024-03-30 thomas *branch_name = NULL;
8111 cd634f2d 2024-03-30 thomas err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8112 cd634f2d 2024-03-30 thomas if (err)
8113 cd634f2d 2024-03-30 thomas goto done;
8115 cd634f2d 2024-03-30 thomas err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
8116 cd634f2d 2024-03-30 thomas if (err)
8117 cd634f2d 2024-03-30 thomas goto done;
8119 cd634f2d 2024-03-30 thomas if (!got_ref_is_symbolic(branch_ref)) {
8120 cd634f2d 2024-03-30 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8121 cd634f2d 2024-03-30 thomas "%s is not a symbolic reference",
8122 cd634f2d 2024-03-30 thomas got_ref_get_name(branch_ref));
8123 cd634f2d 2024-03-30 thomas goto done;
8126 cd634f2d 2024-03-30 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8127 cd634f2d 2024-03-30 thomas if (*branch_name == NULL) {
8128 cd634f2d 2024-03-30 thomas err = got_error_from_errno("strdup");
8129 cd634f2d 2024-03-30 thomas goto done;
8132 cd634f2d 2024-03-30 thomas free(branch_ref_name);
8133 cd634f2d 2024-03-30 thomas if (branch_ref)
8134 cd634f2d 2024-03-30 thomas got_ref_close(branch_ref);
8135 cd634f2d 2024-03-30 thomas if (err) {
8136 cd634f2d 2024-03-30 thomas free(*branch_name);
8137 cd634f2d 2024-03-30 thomas *branch_name = NULL;
8139 0ebf8283 2019-07-24 stsp return err;
8142 0ebf8283 2019-07-24 stsp static const struct got_error *
8143 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8145 0ebf8283 2019-07-24 stsp const struct got_error *err;
8146 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8147 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
8149 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8151 0ebf8283 2019-07-24 stsp goto done;
8152 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
8154 0ebf8283 2019-07-24 stsp goto done;
8156 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8157 0ebf8283 2019-07-24 stsp worktree);
8159 0ebf8283 2019-07-24 stsp goto done;
8160 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
8162 0ebf8283 2019-07-24 stsp goto done;
8164 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8166 0ebf8283 2019-07-24 stsp goto done;
8167 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
8169 0ebf8283 2019-07-24 stsp goto done;
8171 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8173 0ebf8283 2019-07-24 stsp goto done;
8174 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8176 0ebf8283 2019-07-24 stsp goto done;
8178 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
8179 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
8180 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8181 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8182 0ebf8283 2019-07-24 stsp return err;
8185 0ebf8283 2019-07-24 stsp const struct got_error *
8186 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
8187 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8188 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
8189 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8191 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8192 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8193 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
8194 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
8195 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8196 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
8197 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
8198 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
8199 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
8200 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
8202 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
8204 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
8206 0ebf8283 2019-07-24 stsp return err;
8208 8641a332 2023-04-14 thomas err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8209 8641a332 2023-04-14 thomas if (err)
8210 8641a332 2023-04-14 thomas goto done;
8212 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8213 8641a332 2023-04-14 thomas if (err) {
8214 8641a332 2023-04-14 thomas if (err->code != GOT_ERR_NOT_REF)
8215 8641a332 2023-04-14 thomas goto done;
8216 8641a332 2023-04-14 thomas /* Can happen on early abort due to invalid histedit script. */
8217 8641a332 2023-04-14 thomas commit_ref = NULL;
8220 8641a332 2023-04-14 thomas if (commit_ref) {
8221 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8222 8641a332 2023-04-14 thomas if (err)
8223 8641a332 2023-04-14 thomas goto done;
8226 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
8227 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
8228 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files added by the
8229 8641a332 2023-04-14 thomas * user during conflict resolution or during histedit -e.
8231 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8232 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
8233 8641a332 2023-04-14 thomas if (err)
8234 8641a332 2023-04-14 thomas goto done;
8237 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8238 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
8240 0ebf8283 2019-07-24 stsp goto done;
8242 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8244 0ebf8283 2019-07-24 stsp goto done;
8246 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8248 0ebf8283 2019-07-24 stsp goto done;
8250 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
8251 25a102ed 2023-04-14 thomas worktree->base_commit_id);
8252 25a102ed 2023-04-14 thomas if (err)
8253 25a102ed 2023-04-14 thomas goto done;
8255 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8256 0ebf8283 2019-07-24 stsp worktree->path_prefix);
8258 0ebf8283 2019-07-24 stsp goto done;
8260 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8262 0ebf8283 2019-07-24 stsp goto done;
8264 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
8266 0ebf8283 2019-07-24 stsp goto done;
8268 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
8269 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
8270 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
8271 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
8272 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
8273 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
8274 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
8275 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
8276 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8277 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8278 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8280 1f1abb7e 2019-08-08 stsp goto sync;
8282 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8283 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8285 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8286 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
8287 0ebf8283 2019-07-24 stsp err = sync_err;
8289 8641a332 2023-04-14 thomas if (resolved)
8290 8641a332 2023-04-14 thomas got_ref_close(resolved);
8291 8641a332 2023-04-14 thomas if (commit_ref)
8292 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8293 8641a332 2023-04-14 thomas free(merged_commit_id);
8294 0ebf8283 2019-07-24 stsp free(tree_id);
8295 818c7501 2019-07-11 stsp free(fileindex_path);
8296 8641a332 2023-04-14 thomas free(commit_ref_name);
8298 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8299 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
8300 818c7501 2019-07-11 stsp err = unlockerr;
8301 818c7501 2019-07-11 stsp return err;
8304 0ebf8283 2019-07-24 stsp const struct got_error *
8305 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8306 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8307 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8309 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8310 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8311 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8312 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8314 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8316 0ebf8283 2019-07-24 stsp return err;
8318 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8319 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8321 e600f124 2021-03-21 stsp goto done;
8323 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8324 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8326 0ebf8283 2019-07-24 stsp goto done;
8328 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8330 0ebf8283 2019-07-24 stsp goto done;
8332 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
8334 0ebf8283 2019-07-24 stsp goto done;
8336 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8338 0ebf8283 2019-07-24 stsp goto done;
8340 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8342 a615e0e7 2020-12-16 stsp goto done;
8344 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8346 a615e0e7 2020-12-16 stsp goto done;
8347 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8348 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8349 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8350 a615e0e7 2020-12-16 stsp err = sync_err;
8352 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8353 a615e0e7 2020-12-16 stsp free(fileindex_path);
8354 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8355 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8356 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8357 0ebf8283 2019-07-24 stsp err = unlockerr;
8358 0ebf8283 2019-07-24 stsp return err;
8361 0ebf8283 2019-07-24 stsp const struct got_error *
8362 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8363 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8365 0ebf8283 2019-07-24 stsp const struct got_error *err;
8366 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8368 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8370 0ebf8283 2019-07-24 stsp return err;
8372 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8374 0ebf8283 2019-07-24 stsp goto done;
8376 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8378 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8379 2822a352 2019-10-15 stsp return err;
8382 2822a352 2019-10-15 stsp const struct got_error *
8383 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8384 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8385 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8386 2822a352 2019-10-15 stsp struct got_repository *repo)
8388 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8389 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8390 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8392 2822a352 2019-10-15 stsp *fileindex = NULL;
8393 2822a352 2019-10-15 stsp *branch_ref = NULL;
8394 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8396 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8398 2822a352 2019-10-15 stsp return err;
8400 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8401 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8402 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8403 2822a352 2019-10-15 stsp "update -b or different branch name required");
8404 2822a352 2019-10-15 stsp goto done;
8407 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8409 2822a352 2019-10-15 stsp goto done;
8411 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8412 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8413 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8414 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8417 2822a352 2019-10-15 stsp goto done;
8419 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8421 2822a352 2019-10-15 stsp goto done;
8423 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8424 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8426 2822a352 2019-10-15 stsp if (err) {
8427 2822a352 2019-10-15 stsp if (*branch_ref) {
8428 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8429 2822a352 2019-10-15 stsp *branch_ref = NULL;
8431 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8432 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8433 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8435 2822a352 2019-10-15 stsp if (*fileindex) {
8436 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8437 2822a352 2019-10-15 stsp *fileindex = NULL;
8439 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8441 0cb83759 2019-08-03 stsp return err;
8444 2822a352 2019-10-15 stsp const struct got_error *
8445 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8446 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8447 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8448 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8449 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8451 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8452 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8453 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8454 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8456 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8458 2822a352 2019-10-15 stsp goto done;
8460 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8462 2822a352 2019-10-15 stsp goto done;
8464 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
8465 945f9229 2022-04-16 thomas if (err)
8466 945f9229 2022-04-16 thomas goto done;
8468 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8469 2822a352 2019-10-15 stsp worktree->path_prefix);
8471 2822a352 2019-10-15 stsp goto done;
8473 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8475 2822a352 2019-10-15 stsp goto done;
8477 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8478 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8480 2822a352 2019-10-15 stsp goto sync;
8482 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8484 2822a352 2019-10-15 stsp goto sync;
8486 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8488 6e210706 2021-01-22 stsp goto sync;
8490 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8492 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8493 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8494 2822a352 2019-10-15 stsp err = sync_err;
8497 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8498 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8499 2822a352 2019-10-15 stsp err = unlockerr;
8500 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8502 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8503 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8504 2822a352 2019-10-15 stsp err = unlockerr;
8505 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8507 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8508 2822a352 2019-10-15 stsp free(fileindex_path);
8509 2822a352 2019-10-15 stsp free(tree_id);
8510 945f9229 2022-04-16 thomas if (commit)
8511 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8513 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8514 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8515 2822a352 2019-10-15 stsp err = unlockerr;
8516 2822a352 2019-10-15 stsp return err;
8519 2822a352 2019-10-15 stsp const struct got_error *
8520 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8521 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8522 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8524 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8526 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8528 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8530 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8531 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8532 8b692cd0 2019-10-21 stsp err = unlockerr;
8533 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8535 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8536 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8537 8b692cd0 2019-10-21 stsp err = unlockerr;
8538 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8540 10604dce 2021-09-24 thomas return err;
8543 10604dce 2021-09-24 thomas const struct got_error *
8544 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
8545 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
8547 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
8548 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8550 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8551 10604dce 2021-09-24 thomas if (err)
8552 10604dce 2021-09-24 thomas goto done;
8554 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8556 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
8557 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8558 10604dce 2021-09-24 thomas err = sync_err;
8560 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8561 10604dce 2021-09-24 thomas free(fileindex_path);
8562 8b692cd0 2019-10-21 stsp return err;
8565 10604dce 2021-09-24 thomas static const struct got_error *
8566 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8568 10604dce 2021-09-24 thomas const struct got_error *err;
8569 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8571 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8572 10604dce 2021-09-24 thomas if (err)
8573 10604dce 2021-09-24 thomas goto done;
8574 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
8575 10604dce 2021-09-24 thomas if (err)
8576 10604dce 2021-09-24 thomas goto done;
8578 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8579 10604dce 2021-09-24 thomas if (err)
8580 10604dce 2021-09-24 thomas goto done;
8581 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
8582 10604dce 2021-09-24 thomas if (err)
8583 10604dce 2021-09-24 thomas goto done;
8586 10604dce 2021-09-24 thomas free(branch_refname);
8587 10604dce 2021-09-24 thomas free(commit_refname);
8588 10604dce 2021-09-24 thomas return err;
8591 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
8592 10604dce 2021-09-24 thomas struct got_worktree *worktree;
8593 10604dce 2021-09-24 thomas const char *branch_name;
8596 10604dce 2021-09-24 thomas static const struct got_error *
8597 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8598 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
8600 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
8602 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8603 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
8604 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
8606 10604dce 2021-09-24 thomas return NULL;
8610 10604dce 2021-09-24 thomas const struct got_error *
8611 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
8612 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
8613 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
8614 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
8615 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8616 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8618 10604dce 2021-09-24 thomas const struct got_error *err;
8619 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8620 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args cma;
8622 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8623 10604dce 2021-09-24 thomas if (err)
8624 10604dce 2021-09-24 thomas goto done;
8626 6ebb2263 2023-07-26 thomas cma.worktree = worktree;
8627 6ebb2263 2023-07-26 thomas cma.cancel_cb = cancel_cb;
8628 6ebb2263 2023-07-26 thomas cma.cancel_arg = cancel_arg;
8630 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8632 10604dce 2021-09-24 thomas if (err)
8633 10604dce 2021-09-24 thomas goto done;
8635 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8636 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
8637 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
8639 10604dce 2021-09-24 thomas free(fileindex_path);
8640 10604dce 2021-09-24 thomas return err;
8643 10604dce 2021-09-24 thomas const struct got_error *
8644 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
8645 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
8646 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
8647 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
8648 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
8649 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
8652 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
8653 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
8654 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
8655 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
8656 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
8657 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
8658 10604dce 2021-09-24 thomas int have_staged_files = 0;
8659 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
8660 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8662 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
8663 10604dce 2021-09-24 thomas *new_commit_id = NULL;
8665 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
8667 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8668 10604dce 2021-09-24 thomas if (err)
8669 10604dce 2021-09-24 thomas goto done;
8671 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8672 10604dce 2021-09-24 thomas if (err)
8673 10604dce 2021-09-24 thomas goto done;
8675 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
8676 10604dce 2021-09-24 thomas if (err)
8677 10604dce 2021-09-24 thomas goto done;
8679 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8680 10604dce 2021-09-24 thomas &have_staged_files);
8681 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8682 10604dce 2021-09-24 thomas goto done;
8683 10604dce 2021-09-24 thomas if (have_staged_files) {
8684 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8685 10604dce 2021-09-24 thomas goto done;
8688 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
8689 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
8690 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
8691 10604dce 2021-09-24 thomas cc_arg.repo = repo;
8692 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
8693 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8694 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
8695 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8696 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8697 10604dce 2021-09-24 thomas if (err)
8698 10604dce 2021-09-24 thomas goto done;
8700 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
8701 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
8702 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
8703 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
8704 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8705 10604dce 2021-09-24 thomas if (err)
8706 10604dce 2021-09-24 thomas goto done;
8708 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
8709 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
8710 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8711 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8712 10604dce 2021-09-24 thomas err = sync_err;
8714 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8715 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8717 10604dce 2021-09-24 thomas free_commitable(ct);
8719 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8720 10604dce 2021-09-24 thomas free(fileindex_path);
8721 10604dce 2021-09-24 thomas return err;
8724 10604dce 2021-09-24 thomas const struct got_error *
8725 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8726 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8728 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8729 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8731 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8732 10604dce 2021-09-24 thomas if (err)
8733 10604dce 2021-09-24 thomas goto done;
8735 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8736 10604dce 2021-09-24 thomas if (err)
8737 10604dce 2021-09-24 thomas goto done;
8738 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8739 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8740 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8741 10604dce 2021-09-24 thomas err = sync_err;
8743 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8744 10604dce 2021-09-24 thomas free(fileindex_path);
8745 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8746 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8747 10604dce 2021-09-24 thomas err = unlockerr;
8748 10604dce 2021-09-24 thomas return err;
8751 10604dce 2021-09-24 thomas const struct got_error *
8752 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8753 10604dce 2021-09-24 thomas struct got_repository *repo)
8755 10604dce 2021-09-24 thomas const struct got_error *err;
8756 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8757 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8759 10604dce 2021-09-24 thomas *in_progress = 0;
8761 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8762 10604dce 2021-09-24 thomas if (err)
8763 10604dce 2021-09-24 thomas return err;
8764 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8765 dfe86d1f 2021-09-24 thomas free(branch_refname);
8766 10604dce 2021-09-24 thomas if (err) {
8767 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8768 10604dce 2021-09-24 thomas return err;
8770 10604dce 2021-09-24 thomas *in_progress = 1;
8772 10604dce 2021-09-24 thomas return NULL;
8775 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8776 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8777 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8779 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8780 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8781 2b72f32d 2023-06-22 thomas struct got_reference *wt_branch = NULL;
8782 2b72f32d 2023-06-22 thomas struct got_object_id *wt_branch_tip = NULL;
8783 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8785 10604dce 2021-09-24 thomas *fileindex = NULL;
8787 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8788 10604dce 2021-09-24 thomas if (err)
8789 10604dce 2021-09-24 thomas return err;
8791 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8792 10604dce 2021-09-24 thomas if (err)
8793 10604dce 2021-09-24 thomas goto done;
8795 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8796 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8797 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8798 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8799 10604dce 2021-09-24 thomas &ok_arg);
8800 10604dce 2021-09-24 thomas if (err)
8801 10604dce 2021-09-24 thomas goto done;
8803 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8805 10604dce 2021-09-24 thomas if (err)
8806 10604dce 2021-09-24 thomas goto done;
8808 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8809 10604dce 2021-09-24 thomas if (err)
8810 10604dce 2021-09-24 thomas goto done;
8812 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8813 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8814 10604dce 2021-09-24 thomas goto done;
8818 2b72f32d 2023-06-22 thomas free(fileindex_path);
8819 2b72f32d 2023-06-22 thomas if (wt_branch)
8820 2b72f32d 2023-06-22 thomas got_ref_close(wt_branch);
8821 2b72f32d 2023-06-22 thomas free(wt_branch_tip);
8822 2b72f32d 2023-06-22 thomas if (err) {
8823 2b72f32d 2023-06-22 thomas if (*fileindex) {
8824 2b72f32d 2023-06-22 thomas got_fileindex_free(*fileindex);
8825 2b72f32d 2023-06-22 thomas *fileindex = NULL;
8827 2b72f32d 2023-06-22 thomas lock_worktree(worktree, LOCK_SH);
8829 2b72f32d 2023-06-22 thomas return err;
8832 2b72f32d 2023-06-22 thomas const struct got_error *got_worktree_merge_write_refs(
8833 2b72f32d 2023-06-22 thomas struct got_worktree *worktree, struct got_reference *branch,
8834 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8836 2b72f32d 2023-06-22 thomas const struct got_error *err = NULL;
8837 2b72f32d 2023-06-22 thomas char *branch_refname = NULL, *commit_refname = NULL;
8838 2b72f32d 2023-06-22 thomas struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8839 2b72f32d 2023-06-22 thomas struct got_object_id *branch_tip = NULL;
8841 2b72f32d 2023-06-22 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8842 2b72f32d 2023-06-22 thomas if (err)
8843 2b72f32d 2023-06-22 thomas return err;
8845 2b72f32d 2023-06-22 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8846 2b72f32d 2023-06-22 thomas if (err)
8847 2b72f32d 2023-06-22 thomas return err;
8849 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8850 10604dce 2021-09-24 thomas if (err)
8851 10604dce 2021-09-24 thomas goto done;
8853 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8854 10604dce 2021-09-24 thomas if (err)
8855 10604dce 2021-09-24 thomas goto done;
8856 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8857 10604dce 2021-09-24 thomas if (err)
8858 10604dce 2021-09-24 thomas goto done;
8860 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8861 10604dce 2021-09-24 thomas if (err)
8862 10604dce 2021-09-24 thomas goto done;
8863 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8864 10604dce 2021-09-24 thomas if (err)
8865 10604dce 2021-09-24 thomas goto done;
8868 10604dce 2021-09-24 thomas free(branch_refname);
8869 10604dce 2021-09-24 thomas free(commit_refname);
8870 10604dce 2021-09-24 thomas if (branch_ref)
8871 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8872 10604dce 2021-09-24 thomas if (commit_ref)
8873 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8874 2b72f32d 2023-06-22 thomas free(branch_tip);
8875 10604dce 2021-09-24 thomas return err;
8878 10604dce 2021-09-24 thomas const struct got_error *
8879 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8880 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8881 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8883 10604dce 2021-09-24 thomas const struct got_error *err;
8884 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8885 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8886 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8887 10604dce 2021-09-24 thomas int have_staged_files = 0;
8889 10604dce 2021-09-24 thomas *branch_name = NULL;
8890 10604dce 2021-09-24 thomas *branch_tip = NULL;
8891 10604dce 2021-09-24 thomas *fileindex = NULL;
8893 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8894 10604dce 2021-09-24 thomas if (err)
8895 10604dce 2021-09-24 thomas return err;
8897 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8898 10604dce 2021-09-24 thomas if (err)
8899 10604dce 2021-09-24 thomas goto done;
8901 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8902 10604dce 2021-09-24 thomas &have_staged_files);
8903 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8904 10604dce 2021-09-24 thomas goto done;
8905 10604dce 2021-09-24 thomas if (have_staged_files) {
8906 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8907 10604dce 2021-09-24 thomas goto done;
8910 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8911 10604dce 2021-09-24 thomas if (err)
8912 10604dce 2021-09-24 thomas goto done;
8914 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8915 10604dce 2021-09-24 thomas if (err)
8916 10604dce 2021-09-24 thomas goto done;
8918 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8919 10604dce 2021-09-24 thomas if (err)
8920 10604dce 2021-09-24 thomas goto done;
8922 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8923 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8924 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8925 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8926 10604dce 2021-09-24 thomas goto done;
8928 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8929 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8930 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8931 10604dce 2021-09-24 thomas goto done;
8934 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8935 10604dce 2021-09-24 thomas if (err)
8936 10604dce 2021-09-24 thomas goto done;
8938 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8939 10604dce 2021-09-24 thomas if (err)
8940 10604dce 2021-09-24 thomas goto done;
8942 10604dce 2021-09-24 thomas free(commit_refname);
8943 10604dce 2021-09-24 thomas free(branch_refname);
8944 10604dce 2021-09-24 thomas free(fileindex_path);
8945 10604dce 2021-09-24 thomas if (commit_ref)
8946 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8947 10604dce 2021-09-24 thomas if (branch_ref)
8948 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8949 10604dce 2021-09-24 thomas if (err) {
8950 10604dce 2021-09-24 thomas if (*branch_name) {
8951 10604dce 2021-09-24 thomas free(*branch_name);
8952 10604dce 2021-09-24 thomas *branch_name = NULL;
8954 10604dce 2021-09-24 thomas free(*branch_tip);
8955 10604dce 2021-09-24 thomas *branch_tip = NULL;
8956 10604dce 2021-09-24 thomas if (*fileindex) {
8957 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8958 10604dce 2021-09-24 thomas *fileindex = NULL;
8960 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8962 10604dce 2021-09-24 thomas return err;
8965 10604dce 2021-09-24 thomas const struct got_error *
8966 cd634f2d 2024-03-30 thomas got_worktree_merge_info(char **branch_name, struct got_worktree *worktree,
8967 cd634f2d 2024-03-30 thomas struct got_repository *repo)
8969 cd634f2d 2024-03-30 thomas const struct got_error *err;
8970 cd634f2d 2024-03-30 thomas char *branch_refname = NULL;
8971 cd634f2d 2024-03-30 thomas struct got_reference *branch_ref = NULL;
8973 cd634f2d 2024-03-30 thomas *branch_name = NULL;
8975 cd634f2d 2024-03-30 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8976 cd634f2d 2024-03-30 thomas if (err)
8977 cd634f2d 2024-03-30 thomas goto done;
8979 cd634f2d 2024-03-30 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8980 cd634f2d 2024-03-30 thomas if (err)
8981 cd634f2d 2024-03-30 thomas goto done;
8983 cd634f2d 2024-03-30 thomas if (!got_ref_is_symbolic(branch_ref)) {
8984 cd634f2d 2024-03-30 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8985 cd634f2d 2024-03-30 thomas "%s is not a symbolic reference",
8986 cd634f2d 2024-03-30 thomas got_ref_get_name(branch_ref));
8987 cd634f2d 2024-03-30 thomas goto done;
8989 cd634f2d 2024-03-30 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8990 cd634f2d 2024-03-30 thomas if (*branch_name == NULL) {
8991 cd634f2d 2024-03-30 thomas err = got_error_from_errno("strdup");
8992 cd634f2d 2024-03-30 thomas goto done;
8996 cd634f2d 2024-03-30 thomas free(branch_refname);
8997 cd634f2d 2024-03-30 thomas if (branch_ref)
8998 cd634f2d 2024-03-30 thomas got_ref_close(branch_ref);
8999 cd634f2d 2024-03-30 thomas if (err) {
9000 cd634f2d 2024-03-30 thomas if (*branch_name) {
9001 cd634f2d 2024-03-30 thomas free(*branch_name);
9002 cd634f2d 2024-03-30 thomas *branch_name = NULL;
9005 cd634f2d 2024-03-30 thomas return err;
9008 cd634f2d 2024-03-30 thomas const struct got_error *
9009 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
9010 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
9011 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9013 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
9014 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
9015 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
9016 10604dce 2021-09-24 thomas struct revert_file_args rfa;
9017 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
9018 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
9019 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
9020 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
9021 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
9023 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
9025 8641a332 2023-04-14 thomas err = get_merge_commit_ref_name(&commit_ref_name, worktree);
9026 8641a332 2023-04-14 thomas if (err)
9027 8641a332 2023-04-14 thomas goto done;
9029 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
9030 8641a332 2023-04-14 thomas if (err)
9031 8641a332 2023-04-14 thomas goto done;
9033 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
9034 8641a332 2023-04-14 thomas if (err)
9035 8641a332 2023-04-14 thomas goto done;
9038 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
9039 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
9040 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
9041 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
9043 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(&added_paths,
9044 8641a332 2023-04-14 thomas got_worktree_get_base_commit_id(worktree), merged_commit_id,
9045 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
9046 8641a332 2023-04-14 thomas if (err)
9047 8641a332 2023-04-14 thomas goto done;
9050 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
9051 945f9229 2022-04-16 thomas worktree->base_commit_id);
9052 945f9229 2022-04-16 thomas if (err)
9053 945f9229 2022-04-16 thomas goto done;
9055 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
9056 945f9229 2022-04-16 thomas worktree->path_prefix);
9057 10604dce 2021-09-24 thomas if (err)
9058 10604dce 2021-09-24 thomas goto done;
9060 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
9061 10604dce 2021-09-24 thomas if (err)
9062 10604dce 2021-09-24 thomas goto done;
9064 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
9065 10604dce 2021-09-24 thomas if (err)
9066 10604dce 2021-09-24 thomas goto done;
9068 10604dce 2021-09-24 thomas rfa.worktree = worktree;
9069 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
9070 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
9071 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
9072 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
9073 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
9074 10604dce 2021-09-24 thomas rfa.repo = repo;
9075 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
9076 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
9077 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
9078 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
9079 10604dce 2021-09-24 thomas if (err)
9080 10604dce 2021-09-24 thomas goto sync;
9082 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
9083 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
9085 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
9086 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
9087 10604dce 2021-09-24 thomas err = sync_err;
9089 10604dce 2021-09-24 thomas free(tree_id);
9090 8641a332 2023-04-14 thomas free(merged_commit_id);
9091 945f9229 2022-04-16 thomas if (commit)
9092 945f9229 2022-04-16 thomas got_object_commit_close(commit);
9093 10604dce 2021-09-24 thomas if (fileindex)
9094 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
9095 10604dce 2021-09-24 thomas free(fileindex_path);
9096 8641a332 2023-04-14 thomas if (commit_ref)
9097 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
9098 8641a332 2023-04-14 thomas free(commit_ref_name);
9100 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
9101 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
9102 10604dce 2021-09-24 thomas err = unlockerr;
9103 10604dce 2021-09-24 thomas return err;
9106 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
9107 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
9108 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
9109 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
9110 2db2652d 2019-08-07 stsp struct got_repository *repo;
9111 2db2652d 2019-08-07 stsp int have_changes;
9114 ef20f542 2022-06-26 thomas static const struct got_error *
9115 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
9116 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
9117 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9118 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9120 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
9121 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
9122 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
9123 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
9124 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
9125 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
9127 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
9128 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
9129 8b13ce36 2019-08-08 stsp return NULL;
9130 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
9131 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
9133 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9134 735ef5ac 2019-08-03 stsp if (ie == NULL)
9135 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9137 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
9138 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
9139 735ef5ac 2019-08-03 stsp relpath) == -1)
9140 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
9142 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
9143 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
9144 43010591 2023-02-17 thomas &base_commit_id, ie);
9147 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
9148 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
9149 3aa5969e 2019-08-06 stsp goto done;
9150 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
9151 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
9152 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
9153 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9154 735ef5ac 2019-08-03 stsp goto done;
9157 2db2652d 2019-08-07 stsp a->have_changes = 1;
9159 735ef5ac 2019-08-03 stsp p = in_repo_path;
9160 735ef5ac 2019-08-03 stsp while (p[0] == '/')
9162 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
9163 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
9164 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
9166 735ef5ac 2019-08-03 stsp free(in_repo_path);
9167 dc424a06 2019-08-07 stsp return err;
9170 2db2652d 2019-08-07 stsp struct stage_path_arg {
9171 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
9172 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
9173 2db2652d 2019-08-07 stsp struct got_repository *repo;
9174 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
9175 2db2652d 2019-08-07 stsp void *status_arg;
9176 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
9177 2db2652d 2019-08-07 stsp void *patch_arg;
9178 7b5dc508 2019-10-28 stsp int staged_something;
9179 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
9182 2db2652d 2019-08-07 stsp static const struct got_error *
9183 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
9184 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
9185 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9186 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9188 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
9189 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
9190 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
9191 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
9192 0cb83759 2019-08-03 stsp uint32_t stage;
9193 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
9194 0aeb8099 2020-07-23 stsp struct stat sb;
9196 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
9197 8b13ce36 2019-08-08 stsp return NULL;
9199 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9200 d3e7c587 2019-08-03 stsp if (ie == NULL)
9201 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9203 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9204 2db2652d 2019-08-07 stsp relpath)== -1)
9205 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
9207 0cb83759 2019-08-03 stsp switch (status) {
9208 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
9209 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
9210 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
9211 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
9212 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
9215 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9216 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
9217 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9218 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9219 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
9222 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
9225 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
9226 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
9227 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
9228 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
9229 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
9233 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
9234 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
9237 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9238 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
9239 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9240 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
9242 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
9243 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9244 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
9245 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
9246 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
9247 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
9248 35213c7c 2020-07-23 stsp ssize_t target_len;
9249 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
9250 35213c7c 2020-07-23 stsp sizeof(target_path));
9251 35213c7c 2020-07-23 stsp if (target_len == -1) {
9252 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
9253 35213c7c 2020-07-23 stsp ondisk_path);
9256 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
9257 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
9258 ad10f64e 2023-07-19 thomas a->worktree->root_path,
9259 ad10f64e 2023-07-19 thomas a->worktree->meta_dir);
9262 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
9263 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
9264 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
9268 35213c7c 2020-07-23 stsp if (is_bad_symlink)
9269 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9270 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
9272 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9273 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
9275 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9276 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
9278 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9279 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9281 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9282 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
9283 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
9284 f289c82c 2022-06-13 thomas if (err)
9287 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
9288 f289c82c 2022-06-13 thomas * implicitly unstage the file.
9290 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9291 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
9292 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
9293 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
9296 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
9297 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
9299 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9300 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9301 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
9302 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
9305 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9307 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9308 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9312 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
9313 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9314 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9315 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9317 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9318 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9321 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
9323 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
9324 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9326 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
9327 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
9330 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9334 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
9335 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
9336 dc424a06 2019-08-07 stsp free(path_content);
9337 2db2652d 2019-08-07 stsp free(ondisk_path);
9338 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
9339 0ebf8283 2019-07-24 stsp return err;
9342 0cb83759 2019-08-03 stsp const struct got_error *
9343 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
9344 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
9345 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
9346 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9347 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
9349 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9350 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9351 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9352 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9353 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9354 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9355 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9356 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9358 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9360 0cb83759 2019-08-03 stsp return err;
9362 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9363 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9365 735ef5ac 2019-08-03 stsp goto done;
9366 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9368 735ef5ac 2019-08-03 stsp goto done;
9369 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9371 0cb83759 2019-08-03 stsp goto done;
9373 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9374 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9375 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9376 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9377 2db2652d 2019-08-07 stsp oka.repo = repo;
9378 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9379 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9380 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9381 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
9383 735ef5ac 2019-08-03 stsp goto done;
9385 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9386 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9387 2db2652d 2019-08-07 stsp goto done;
9390 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9391 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9392 2db2652d 2019-08-07 stsp spa.repo = repo;
9393 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9394 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9395 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9396 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9397 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9398 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9399 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9400 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9401 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
9403 2db2652d 2019-08-07 stsp goto done;
9405 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9406 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9407 7b5dc508 2019-10-28 stsp goto done;
9410 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9411 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9412 0cb83759 2019-08-03 stsp err = sync_err;
9414 735ef5ac 2019-08-03 stsp if (head_ref)
9415 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9416 735ef5ac 2019-08-03 stsp free(head_commit_id);
9417 0cb83759 2019-08-03 stsp free(fileindex_path);
9418 0cb83759 2019-08-03 stsp if (fileindex)
9419 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9420 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9421 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9422 0cb83759 2019-08-03 stsp err = unlockerr;
9423 0cb83759 2019-08-03 stsp return err;
9426 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9427 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9428 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9429 ad493afc 2019-08-03 stsp struct got_repository *repo;
9430 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9431 ad493afc 2019-08-03 stsp void *progress_arg;
9432 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9433 2e1f37b0 2019-08-08 stsp void *patch_arg;
9436 2e1f37b0 2019-08-08 stsp static const struct got_error *
9437 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9438 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9439 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9440 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9441 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9443 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9444 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9445 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9446 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9447 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9448 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9449 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9450 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9452 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9453 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9455 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9457 2e1f37b0 2019-08-08 stsp return err;
9459 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9460 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9461 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9462 f4ae6ddb 2022-07-01 thomas goto done;
9464 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9465 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9466 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9467 f4ae6ddb 2022-07-01 thomas goto done;
9470 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9472 2e1f37b0 2019-08-08 stsp goto done;
9474 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9476 2e1f37b0 2019-08-08 stsp goto done;
9478 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9480 2e1f37b0 2019-08-08 stsp goto done;
9482 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9485 2e1f37b0 2019-08-08 stsp goto done;
9487 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9489 2e1f37b0 2019-08-08 stsp goto done;
9491 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9493 2e1f37b0 2019-08-08 stsp goto done;
9495 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9496 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9498 2e1f37b0 2019-08-08 stsp goto done;
9500 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9501 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
9503 2e1f37b0 2019-08-08 stsp goto done;
9504 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9505 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
9507 2e1f37b0 2019-08-08 stsp goto done;
9509 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9510 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9511 2e1f37b0 2019-08-08 stsp goto done;
9513 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9514 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9515 2e1f37b0 2019-08-08 stsp goto done;
9517 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9518 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9519 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
9520 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9521 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9522 fe621944 2020-11-10 stsp nchanges++;
9524 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9525 2e1f37b0 2019-08-08 stsp int choice;
9526 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9527 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9528 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9529 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9531 2e1f37b0 2019-08-08 stsp goto done;
9532 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9533 2e1f37b0 2019-08-08 stsp have_content = 1;
9535 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9536 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9539 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9540 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9541 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9543 2e1f37b0 2019-08-08 stsp free(label1);
9544 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9545 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9547 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9548 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9549 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9550 2e1f37b0 2019-08-08 stsp if (staged_blob)
9551 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9552 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9553 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9554 fe621944 2020-11-10 stsp err = free_err;
9555 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9556 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9557 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9558 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9559 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9560 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9561 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9562 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9563 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9564 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9565 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9566 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9567 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9568 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9569 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9570 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9571 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9572 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9573 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9575 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9576 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9577 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9578 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9579 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9580 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9581 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9583 2e1f37b0 2019-08-08 stsp free(path1);
9584 2e1f37b0 2019-08-08 stsp free(path2);
9585 fda8017d 2020-07-23 stsp return err;
9588 fda8017d 2020-07-23 stsp static const struct got_error *
9589 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9590 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9591 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9592 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9593 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9594 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9595 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9597 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9598 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9599 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9600 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9601 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9602 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9603 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9604 fda8017d 2020-07-23 stsp struct stat sb;
9606 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9607 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9608 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9610 fda8017d 2020-07-23 stsp return err;
9612 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9613 fda8017d 2020-07-23 stsp return NULL;
9615 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9616 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9617 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9619 fda8017d 2020-07-23 stsp goto done;
9622 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
9623 fda8017d 2020-07-23 stsp if (f == NULL) {
9624 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9625 fda8017d 2020-07-23 stsp path_unstaged_content);
9626 fda8017d 2020-07-23 stsp goto done;
9628 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9629 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9630 fda8017d 2020-07-23 stsp goto done;
9632 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9633 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9634 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9636 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9637 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9638 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9639 fda8017d 2020-07-23 stsp goto done;
9641 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9642 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9643 fda8017d 2020-07-23 stsp goto done;
9645 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9646 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9647 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9648 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9649 fda8017d 2020-07-23 stsp progress_arg);
9651 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9653 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9655 67a66647 2021-05-31 stsp return err;
9657 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9658 67a66647 2021-05-31 stsp parent) == -1) {
9659 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9660 67a66647 2021-05-31 stsp base_path = NULL;
9661 67a66647 2021-05-31 stsp goto done;
9664 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
9665 fc2a50f2 2022-11-01 thomas base_path, "");
9667 67a66647 2021-05-31 stsp goto done;
9668 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9669 67a66647 2021-05-31 stsp blob_base);
9671 67a66647 2021-05-31 stsp goto done;
9674 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9675 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9677 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9678 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9679 eec2f5a9 2021-06-03 stsp ondisk_path);
9681 eec2f5a9 2021-06-03 stsp goto done;
9684 06340621 2021-12-31 thomas fd = open(ondisk_path,
9685 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9686 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9687 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9688 eec2f5a9 2021-06-03 stsp goto done;
9690 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9691 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9692 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9693 eec2f5a9 2021-06-03 stsp close(fd);
9694 eec2f5a9 2021-06-03 stsp goto done;
9698 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9699 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9700 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9701 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9702 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9705 fda8017d 2020-07-23 stsp goto done;
9707 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9708 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9709 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9711 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9712 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9715 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9716 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9717 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9718 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9719 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9720 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9721 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9722 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9723 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9724 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9725 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9726 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9727 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9728 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9729 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9730 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9731 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9732 67a66647 2021-05-31 stsp free(blob_base_path);
9733 67a66647 2021-05-31 stsp free(parent);
9734 67a66647 2021-05-31 stsp free(base_path);
9735 2e1f37b0 2019-08-08 stsp return err;
9738 ad493afc 2019-08-03 stsp static const struct got_error *
9739 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9740 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9741 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9742 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9744 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9745 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9746 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9747 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9748 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9749 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9750 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9751 9bc94a15 2019-08-03 stsp struct stat sb;
9752 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9754 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9755 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9756 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9757 2e1f37b0 2019-08-08 stsp return NULL;
9759 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9760 ad493afc 2019-08-03 stsp if (ie == NULL)
9761 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9763 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9765 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9767 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9768 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9770 f69721c3 2019-10-21 stsp goto done;
9771 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9772 f69721c3 2019-10-21 stsp id_str) == -1) {
9773 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9774 f4ae6ddb 2022-07-01 thomas goto done;
9777 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9778 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9779 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9780 f4ae6ddb 2022-07-01 thomas goto done;
9782 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9783 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9784 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9785 f69721c3 2019-10-21 stsp goto done;
9788 ad493afc 2019-08-03 stsp switch (staged_status) {
9789 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9790 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9791 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
9794 ad493afc 2019-08-03 stsp /* fall through */
9795 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9796 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9797 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9798 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9799 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9800 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9803 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9806 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9807 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9808 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9809 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9810 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9811 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9814 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9815 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9818 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9819 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9820 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9821 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9822 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9823 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9824 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9825 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9826 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9828 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9829 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9830 36bf999c 2020-07-23 stsp char *staged_target;
9831 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9832 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9834 36bf999c 2020-07-23 stsp goto done;
9835 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9836 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9837 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9838 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9839 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9840 36bf999c 2020-07-23 stsp free(staged_target);
9842 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9843 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9844 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9845 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9846 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9847 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9848 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9852 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9855 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9856 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9857 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9858 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9861 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9862 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9863 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9864 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9865 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9868 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9870 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9871 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9875 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9876 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9877 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9878 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9881 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9885 ad493afc 2019-08-03 stsp free(ondisk_path);
9886 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9887 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9888 ad493afc 2019-08-03 stsp if (blob_base)
9889 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9890 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9891 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9892 ad493afc 2019-08-03 stsp if (blob_staged)
9893 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9894 f69721c3 2019-10-21 stsp free(id_str);
9895 f69721c3 2019-10-21 stsp free(label_orig);
9896 ad493afc 2019-08-03 stsp return err;
9899 ad493afc 2019-08-03 stsp const struct got_error *
9900 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9901 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9902 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9903 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9904 ad493afc 2019-08-03 stsp struct got_repository *repo)
9906 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9907 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9908 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9909 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9910 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9912 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9914 ad493afc 2019-08-03 stsp return err;
9916 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9918 ad493afc 2019-08-03 stsp goto done;
9920 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9921 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9922 ad493afc 2019-08-03 stsp upa.repo = repo;
9923 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9924 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9925 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9926 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9927 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9928 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9929 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9931 ad493afc 2019-08-03 stsp goto done;
9934 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9935 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9936 ad493afc 2019-08-03 stsp err = sync_err;
9938 ad493afc 2019-08-03 stsp free(fileindex_path);
9939 ad493afc 2019-08-03 stsp if (fileindex)
9940 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9941 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9942 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9943 ad493afc 2019-08-03 stsp err = unlockerr;
9944 ad493afc 2019-08-03 stsp return err;
9947 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9948 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9949 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9950 b2118c49 2020-07-28 stsp void *info_arg;
9951 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9952 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9953 b2118c49 2020-07-28 stsp void *cancel_arg;
9956 b2118c49 2020-07-28 stsp static const struct got_error *
9957 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9959 eb6fb9cd 2023-07-26 thomas const struct got_error *err;
9960 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9961 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9962 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9963 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9964 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9965 b2118c49 2020-07-28 stsp int stage;
9967 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
9968 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
9969 eb6fb9cd 2023-07-26 thomas if (err)
9970 eb6fb9cd 2023-07-26 thomas return err;
9973 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9974 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9975 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9978 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9979 b2118c49 2020-07-28 stsp return NULL;
9981 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9982 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9983 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9984 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9985 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9986 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9987 43010591 2023-02-17 thomas &staged_blob_id, ie);
9990 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9991 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9993 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9994 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9997 b2118c49 2020-07-28 stsp const struct got_error *
9998 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9999 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
10000 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
10001 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
10004 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
10005 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
10006 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
10007 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
10009 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
10011 b2118c49 2020-07-28 stsp return err;
10013 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
10015 b2118c49 2020-07-28 stsp goto done;
10017 b2118c49 2020-07-28 stsp arg.worktree = worktree;
10018 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
10019 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
10020 b2118c49 2020-07-28 stsp arg.paths = paths;
10021 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
10022 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
10023 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
10026 b2118c49 2020-07-28 stsp free(fileindex_path);
10027 b2118c49 2020-07-28 stsp if (fileindex)
10028 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
10029 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
10030 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
10031 b2118c49 2020-07-28 stsp err = unlockerr;
10032 b2118c49 2020-07-28 stsp return err;
10035 814624e7 2022-03-22 thomas static const struct got_error *
10036 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
10037 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
10038 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
10040 814624e7 2022-03-22 thomas const struct got_error *err;
10041 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
10042 814624e7 2022-03-22 thomas struct stat sb;
10043 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
10045 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
10046 814624e7 2022-03-22 thomas if (err)
10047 814624e7 2022-03-22 thomas return err;
10049 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
10050 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
10051 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
10053 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
10054 814624e7 2022-03-22 thomas if (ie) {
10055 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
10056 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
10058 814624e7 2022-03-22 thomas if (err)
10059 814624e7 2022-03-22 thomas goto done;
10060 814624e7 2022-03-22 thomas } else {
10061 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
10062 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
10063 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
10064 814624e7 2022-03-22 thomas if (errno != ENOENT) {
10065 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
10066 814624e7 2022-03-22 thomas ondisk_path);
10067 814624e7 2022-03-22 thomas goto done;
10069 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
10074 814624e7 2022-03-22 thomas free(ondisk_path);
10075 814624e7 2022-03-22 thomas return err;
10078 814624e7 2022-03-22 thomas static const struct got_error *
10079 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
10080 814624e7 2022-03-22 thomas unsigned char staged_status)
10082 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
10083 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
10084 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
10085 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
10086 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
10087 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
10088 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
10089 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
10090 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
10091 814624e7 2022-03-22 thomas return NULL;
10094 814624e7 2022-03-22 thomas static const struct got_error *
10095 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
10097 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
10098 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
10099 814624e7 2022-03-22 thomas return NULL;
10102 814624e7 2022-03-22 thomas static const struct got_error *
10103 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
10104 814624e7 2022-03-22 thomas unsigned char staged_status)
10106 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
10107 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
10108 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
10109 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
10110 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
10111 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
10112 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
10113 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
10114 814624e7 2022-03-22 thomas return NULL;
10117 814624e7 2022-03-22 thomas const struct got_error *
10118 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
10119 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
10121 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
10124 814624e7 2022-03-22 thomas const struct got_error *
10125 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
10126 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
10127 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
10129 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
10130 814624e7 2022-03-22 thomas int file_renamed = 0;
10131 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
10132 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
10134 814624e7 2022-03-22 thomas *oldpath = NULL;
10135 814624e7 2022-03-22 thomas *newpath = NULL;
10137 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
10138 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
10139 814624e7 2022-03-22 thomas if (err)
10140 814624e7 2022-03-22 thomas goto done;
10142 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
10143 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
10144 814624e7 2022-03-22 thomas if (err)
10145 814624e7 2022-03-22 thomas goto done;
10147 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
10148 814624e7 2022-03-22 thomas file_renamed = 1;
10150 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
10151 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
10152 814624e7 2022-03-22 thomas else if (file_renamed) {
10153 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
10154 814624e7 2022-03-22 thomas if (err == NULL)
10155 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
10156 814624e7 2022-03-22 thomas } else if (old == NULL)
10157 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
10159 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
10162 814624e7 2022-03-22 thomas if (err) {
10163 814624e7 2022-03-22 thomas free(*oldpath);
10164 814624e7 2022-03-22 thomas *oldpath = NULL;
10165 814624e7 2022-03-22 thomas free(*newpath);
10166 814624e7 2022-03-22 thomas *newpath = NULL;
10168 814624e7 2022-03-22 thomas return err;
10171 814624e7 2022-03-22 thomas const struct got_error *
10172 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10173 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
10174 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
10176 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
10178 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
10179 5e22b737 2022-05-31 thomas saa.worktree = worktree;
10180 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
10181 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
10182 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
10183 5e22b737 2022-05-31 thomas saa.repo = repo;
10185 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
10186 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
10189 5e22b737 2022-05-31 thomas const struct got_error *
10190 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10191 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
10192 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
10194 8ec7352a 2023-06-01 thomas const struct got_error *err;
10195 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
10196 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
10198 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
10199 5e22b737 2022-05-31 thomas sda.worktree = worktree;
10200 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
10201 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
10202 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
10203 5e22b737 2022-05-31 thomas sda.repo = repo;
10204 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
10205 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
10206 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
10207 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
10208 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s/%s",
10209 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree), path) == -1)
10210 8ec7352a 2023-06-01 thomas return got_error_from_errno("asprintf");
10211 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
10212 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
10214 8ec7352a 2023-06-01 thomas err = worktree_status(worktree, path, fileindex, repo,
10215 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10216 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
10217 8ec7352a 2023-06-01 thomas return err;
10220 5e22b737 2022-05-31 thomas const struct got_error *
10221 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
10222 36832a8e 2022-05-31 thomas const char *fileindex_path)
10224 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
10226 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
10227 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
10229 5e22b737 2022-05-31 thomas return err;