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 aaf972e2 2023-09-18 thomas int update_timestamps;
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 int update_timestamps;
2045 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2046 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2047 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2048 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
2049 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
2050 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2051 f4ae6ddb 2022-07-01 thomas goto done;
2053 234035bc 2019-06-01 stsp struct got_object_id id2;
2054 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id2, ie);
2055 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2058 f69721c3 2019-10-21 stsp goto done;
2060 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2061 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2062 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2063 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2064 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2065 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2066 f69721c3 2019-10-21 stsp goto done;
2068 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2069 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2070 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2071 234035bc 2019-06-01 stsp goto done;
2074 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2075 36bf999c 2020-07-23 stsp char *link_target;
2076 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2078 36bf999c 2020-07-23 stsp goto done;
2079 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2080 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2081 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2082 36bf999c 2020-07-23 stsp free(link_target);
2084 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2085 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2086 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2087 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2089 f69721c3 2019-10-21 stsp free(label_orig);
2090 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2091 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2092 f4ae6ddb 2022-07-01 thomas goto done;
2094 234035bc 2019-06-01 stsp if (blob2)
2095 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2097 909d120e 2019-09-22 stsp goto done;
2099 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2100 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2101 234035bc 2019-06-01 stsp * unmodified files again.
2103 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2104 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2105 234035bc 2019-06-01 stsp update_timestamps);
2106 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2107 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2108 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2109 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2110 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2112 1ee397ad 2019-07-12 stsp goto done;
2113 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2114 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2116 13d9040b 2019-03-27 stsp goto done;
2118 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2119 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2120 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2121 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2122 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2123 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2124 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2126 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2127 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2128 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2129 aaf972e2 2023-09-18 thomas status == GOT_STATUS_UNVERSIONED,
2130 aaf972e2 2023-09-18 thomas &update_timestamps,
2131 aaf972e2 2023-09-18 thomas repo, progress_cb, progress_arg);
2134 13d9040b 2019-03-27 stsp goto done;
2137 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2138 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2139 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2141 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2142 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2143 aaf972e2 2023-09-18 thomas &blob->id, update_timestamps);
2146 13d9040b 2019-03-27 stsp goto done;
2148 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2149 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2150 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2154 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2155 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2156 f4ae6ddb 2022-07-01 thomas goto done;
2158 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2160 6353ad76 2019-02-08 stsp free(ondisk_path);
2161 8da9e5f4 2019-01-12 stsp return err;
2164 90285c3b 2019-01-08 stsp static const struct got_error *
2165 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2167 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2168 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2170 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2171 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2173 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2174 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2175 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2177 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2178 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2180 1c4cdd89 2021-06-20 stsp goto done;
2181 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2182 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2183 fddefe3b 2020-10-20 stsp free(ondisk_path);
2184 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2185 1c4cdd89 2021-06-20 stsp parent = NULL;
2186 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2187 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2188 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2189 fddefe3b 2020-10-20 stsp ondisk_path);
2192 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2198 90285c3b 2019-01-08 stsp free(ondisk_path);
2199 1c4cdd89 2021-06-20 stsp free(parent);
2200 90285c3b 2019-01-08 stsp return err;
2203 708d8e67 2019-03-27 stsp static const struct got_error *
2204 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2205 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2206 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2208 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2209 708d8e67 2019-03-27 stsp unsigned char status;
2210 708d8e67 2019-03-27 stsp struct stat sb;
2211 708d8e67 2019-03-27 stsp char *ondisk_path;
2213 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2214 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2216 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2218 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2220 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2222 5a58a424 2020-06-23 stsp goto done;
2224 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2225 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2226 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2227 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2228 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2229 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2230 993e2a1b 2020-07-23 stsp goto done;
2232 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2233 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2234 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2235 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2237 993e2a1b 2020-07-23 stsp goto done;
2238 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2239 993e2a1b 2020-07-23 stsp ie->path);
2240 993e2a1b 2020-07-23 stsp goto done;
2243 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2244 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2245 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2247 5a58a424 2020-06-23 stsp goto done;
2249 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2250 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2252 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2253 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2255 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2257 5a58a424 2020-06-23 stsp goto done;
2258 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2259 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2261 5a58a424 2020-06-23 stsp goto done;
2263 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2266 5a58a424 2020-06-23 stsp free(ondisk_path);
2267 fc6346c4 2019-03-27 stsp return err;
2270 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2271 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2272 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2273 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2274 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2275 8da9e5f4 2019-01-12 stsp void *progress_arg;
2276 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2277 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2280 512f0d0e 2019-01-02 stsp static const struct got_error *
2281 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2282 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2284 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
2285 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2287 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
2288 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
2289 eb6fb9cd 2023-07-26 thomas if (err)
2290 eb6fb9cd 2023-07-26 thomas return err;
2293 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2294 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2297 8da9e5f4 2019-01-12 stsp static const struct got_error *
2298 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2300 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
2301 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2303 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
2304 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
2305 eb6fb9cd 2023-07-26 thomas if (err)
2306 eb6fb9cd 2023-07-26 thomas return err;
2309 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2310 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2313 9d31a1d8 2018-03-11 stsp static const struct got_error *
2314 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2316 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
2317 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2318 8da9e5f4 2019-01-12 stsp char *path;
2320 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
2321 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
2322 eb6fb9cd 2023-07-26 thomas if (err)
2323 eb6fb9cd 2023-07-26 thomas return err;
2326 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2327 59530ea7 2023-07-17 thomas return NULL;
2329 59530ea7 2023-07-17 thomas if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2330 63c5ca5d 2019-08-24 stsp return NULL;
2332 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2333 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2335 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2337 59530ea7 2023-07-17 thomas err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2338 59530ea7 2023-07-17 thomas a->repo, a->progress_cb, a->progress_arg);
2340 8da9e5f4 2019-01-12 stsp free(path);
2341 0cd1c46a 2019-03-11 stsp return err;
2344 b2118c49 2020-07-28 stsp const struct got_error *
2345 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2347 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2349 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2350 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2351 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2352 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2355 b2118c49 2020-07-28 stsp return NULL;
2358 818c7501 2019-07-11 stsp static const struct got_error *
2359 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2361 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2362 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2364 517bab73 2019-03-11 stsp *refname = NULL;
2366 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2368 b2118c49 2020-07-28 stsp return err;
2370 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2371 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2372 0647c563 2019-03-11 stsp *refname = NULL;
2374 517bab73 2019-03-11 stsp free(uuidstr);
2375 517bab73 2019-03-11 stsp return err;
2378 818c7501 2019-07-11 stsp const struct got_error *
2379 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2380 f6cd0243 2023-01-28 thomas const char *prefix)
2382 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2385 fcfaa6d5 2023-07-05 thomas static const struct got_error *
2386 fcfaa6d5 2023-07-05 thomas get_base_ref_name(char **refname, struct got_worktree *worktree)
2388 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2391 818c7501 2019-07-11 stsp static const struct got_error *
2392 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2394 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2395 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2398 818c7501 2019-07-11 stsp static const struct got_error *
2399 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2401 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2404 818c7501 2019-07-11 stsp static const struct got_error *
2405 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2407 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2408 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2411 818c7501 2019-07-11 stsp static const struct got_error *
2412 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2414 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2415 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2418 0ebf8283 2019-07-24 stsp static const struct got_error *
2419 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2421 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2422 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2425 0ebf8283 2019-07-24 stsp static const struct got_error *
2426 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2428 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2429 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2432 0ebf8283 2019-07-24 stsp static const struct got_error *
2433 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2435 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2436 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2439 0ebf8283 2019-07-24 stsp static const struct got_error *
2440 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2442 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2443 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2446 0ebf8283 2019-07-24 stsp const struct got_error *
2447 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2448 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2450 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2451 ad10f64e 2023-07-19 thomas worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2452 0ebf8283 2019-07-24 stsp *path = NULL;
2453 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2455 0ebf8283 2019-07-24 stsp return NULL;
2458 10604dce 2021-09-24 thomas static const struct got_error *
2459 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2461 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2462 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2465 10604dce 2021-09-24 thomas static const struct got_error *
2466 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2468 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2469 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2473 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2474 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2476 517bab73 2019-03-11 stsp static const struct got_error *
2477 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2479 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2480 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2481 517bab73 2019-03-11 stsp char *refname;
2483 fcfaa6d5 2023-07-05 thomas err = get_base_ref_name(&refname, worktree);
2485 517bab73 2019-03-11 stsp return err;
2487 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2489 0cd1c46a 2019-03-11 stsp goto done;
2491 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2493 0cd1c46a 2019-03-11 stsp free(refname);
2495 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2496 3e3a69f1 2019-07-25 stsp return err;
2499 3e3a69f1 2019-07-25 stsp static const struct got_error *
2500 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2502 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2504 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2505 ad10f64e 2023-07-19 thomas worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2506 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2507 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2509 9d31a1d8 2018-03-11 stsp return err;
2513 ebf99748 2019-05-09 stsp static const struct got_error *
2514 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2515 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2517 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2518 ebf99748 2019-05-09 stsp FILE *index = NULL;
2520 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2521 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2522 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2523 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2525 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2527 ebf99748 2019-05-09 stsp goto done;
2529 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2530 ebf99748 2019-05-09 stsp if (index == NULL) {
2531 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2532 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2534 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2535 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2536 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2539 ebf99748 2019-05-09 stsp if (err) {
2540 ebf99748 2019-05-09 stsp free(*fileindex_path);
2541 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2542 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2543 ebf99748 2019-05-09 stsp *fileindex = NULL;
2545 ebf99748 2019-05-09 stsp return err;
2548 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2549 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2550 c932eeeb 2019-05-22 stsp const char *path;
2551 c932eeeb 2019-05-22 stsp size_t path_len;
2552 c932eeeb 2019-05-22 stsp const char *entry_name;
2553 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2554 a484d721 2019-06-10 stsp void *progress_arg;
2557 c932eeeb 2019-05-22 stsp static const struct got_error *
2558 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2560 1ee397ad 2019-07-12 stsp const struct got_error *err;
2561 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2563 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2564 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2565 c932eeeb 2019-05-22 stsp return NULL;
2566 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2567 102ff934 2019-06-11 stsp return NULL;
2569 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2570 a769b60b 2021-06-27 stsp return NULL;
2572 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2573 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2574 c932eeeb 2019-05-22 stsp return NULL;
2576 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2577 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2578 edd02c5e 2019-07-11 stsp ie->path);
2580 1ee397ad 2019-07-12 stsp return err;
2582 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2583 c932eeeb 2019-05-22 stsp return NULL;
2586 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2587 a615e0e7 2020-12-16 stsp static const struct got_error *
2588 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2589 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2590 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2592 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2594 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2595 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2596 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2597 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2598 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2599 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2601 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2602 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2605 9c6338c4 2019-06-02 stsp static const struct got_error *
2606 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2608 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2609 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2610 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2611 867630bb 2020-01-17 stsp struct timespec timeout;
2613 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2614 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2616 9c6338c4 2019-06-02 stsp goto done;
2618 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2620 9c6338c4 2019-06-02 stsp goto done;
2622 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2623 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2624 9c6338c4 2019-06-02 stsp fileindex_path);
2625 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2629 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2630 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2631 867630bb 2020-01-17 stsp * was recorded in the file index.
2633 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2634 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2635 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2637 9c6338c4 2019-06-02 stsp if (new_index)
2638 9c6338c4 2019-06-02 stsp fclose(new_index);
2639 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2640 9c6338c4 2019-06-02 stsp return err;
2643 6ced4176 2019-07-12 stsp static const struct got_error *
2644 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2645 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2646 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2647 945f9229 2022-04-16 thomas struct got_repository *repo)
2649 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2650 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2651 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2652 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2654 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2655 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2656 6ced4176 2019-07-12 stsp *tree_id = NULL;
2658 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2659 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2660 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2661 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2662 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2663 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2664 6ced4176 2019-07-12 stsp goto done;
2666 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2667 945f9229 2022-04-16 thomas worktree->path_prefix);
2669 6ced4176 2019-07-12 stsp goto done;
2670 6ced4176 2019-07-12 stsp return NULL;
2673 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2675 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2676 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2677 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2678 6ced4176 2019-07-12 stsp goto done;
2681 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2683 6ced4176 2019-07-12 stsp goto done;
2685 6ced4176 2019-07-12 stsp free(in_repo_path);
2686 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2688 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2690 6ced4176 2019-07-12 stsp goto done;
2692 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2693 6ced4176 2019-07-12 stsp /* Check out a single file. */
2694 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2695 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2696 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2697 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2698 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2699 6ced4176 2019-07-12 stsp goto done;
2701 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2702 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2703 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2704 6ced4176 2019-07-12 stsp goto done;
2707 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2708 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2710 6ced4176 2019-07-12 stsp return err;
2711 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2712 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2713 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2714 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2715 6ced4176 2019-07-12 stsp goto done;
2718 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2719 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2721 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2722 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2723 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2724 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2725 6ced4176 2019-07-12 stsp goto done;
2727 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2728 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2729 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2730 6ced4176 2019-07-12 stsp goto done;
2735 6ced4176 2019-07-12 stsp free(in_repo_path);
2736 6ced4176 2019-07-12 stsp if (err) {
2737 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2738 6ced4176 2019-07-12 stsp free(*tree_relpath);
2739 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2740 6ced4176 2019-07-12 stsp free(*tree_id);
2741 6ced4176 2019-07-12 stsp *tree_id = NULL;
2743 07ed472d 2019-07-12 stsp return err;
2746 07ed472d 2019-07-12 stsp static const struct got_error *
2747 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2748 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2749 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2750 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2752 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2753 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2754 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2755 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2756 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2758 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2759 7f47418f 2019-12-20 stsp if (err) {
2760 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2761 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2762 7f47418f 2019-12-20 stsp goto done;
2763 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2764 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2766 7f47418f 2019-12-20 stsp return err;
2769 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2770 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2772 07ed472d 2019-07-12 stsp goto done;
2774 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2776 07ed472d 2019-07-12 stsp goto done;
2778 07ed472d 2019-07-12 stsp if (entry_name &&
2779 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2780 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2781 07ed472d 2019-07-12 stsp goto done;
2784 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2785 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2786 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2787 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2788 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2789 07ed472d 2019-07-12 stsp arg.repo = repo;
2790 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2791 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2792 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2793 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2794 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2795 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2798 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2799 07ed472d 2019-07-12 stsp if (commit)
2800 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2801 6ced4176 2019-07-12 stsp return err;
2804 9d31a1d8 2018-03-11 stsp const struct got_error *
2805 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2806 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2807 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2808 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2810 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2811 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2812 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2813 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2814 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2815 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2816 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2817 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2818 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2819 f2ea84fa 2019-07-27 stsp int entry_type;
2820 f2ea84fa 2019-07-27 stsp char *relpath;
2821 f2ea84fa 2019-07-27 stsp char *entry_name;
2822 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2823 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2825 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2827 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2829 9d31a1d8 2018-03-11 stsp return err;
2831 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2832 945f9229 2022-04-16 thomas worktree->base_commit_id);
2833 945f9229 2022-04-16 thomas if (err)
2834 945f9229 2022-04-16 thomas goto done;
2836 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2837 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2838 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2839 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2840 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2841 f2ea84fa 2019-07-27 stsp goto done;
2844 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2845 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2846 945f9229 2022-04-16 thomas worktree, repo);
2847 f2ea84fa 2019-07-27 stsp if (err) {
2848 f2ea84fa 2019-07-27 stsp free(tpd);
2849 6ced4176 2019-07-12 stsp goto done;
2852 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2853 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2854 f2ea84fa 2019-07-27 stsp if (err) {
2855 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2856 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2857 f2ea84fa 2019-07-27 stsp free(tpd);
2858 f2ea84fa 2019-07-27 stsp goto done;
2861 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2863 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2867 51514078 2018-12-25 stsp * Read the file index.
2868 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2869 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2871 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2873 c4cdcb68 2019-04-03 stsp goto done;
2875 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2876 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2877 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2879 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2880 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2881 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2885 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2886 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2887 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2888 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2889 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2890 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2891 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2892 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2896 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2898 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2899 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2900 af12c6b9 2019-06-04 stsp err = sync_err;
2902 9c6338c4 2019-06-02 stsp free(fileindex_path);
2904 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2905 9d31a1d8 2018-03-11 stsp if (commit)
2906 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2907 5ade8233 2019-07-12 stsp if (fileindex)
2908 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2909 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2910 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2911 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2912 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2913 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2914 f2ea84fa 2019-07-27 stsp free(tpd);
2916 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2917 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2918 9d31a1d8 2018-03-11 stsp err = unlockerr;
2919 f8d1f275 2019-02-04 stsp return err;
2922 0529f8df 2023-03-10 thomas static const struct got_error *
2923 0529f8df 2023-03-10 thomas add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2924 0529f8df 2023-03-10 thomas struct got_fileindex_entry *ie, const char *ondisk_path,
2925 0529f8df 2023-03-10 thomas const char *path2, struct got_blob_object *blob2, mode_t mode2,
2926 0529f8df 2023-03-10 thomas int restoring_missing_file, int reverting_versioned_file,
2927 0529f8df 2023-03-10 thomas int path_is_unversioned, int allow_bad_symlinks,
2928 0529f8df 2023-03-10 thomas struct got_repository *repo,
2929 0529f8df 2023-03-10 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
2931 0529f8df 2023-03-10 thomas const struct got_error *err = NULL;
2932 0529f8df 2023-03-10 thomas int is_bad_symlink = 0;
2934 0529f8df 2023-03-10 thomas if (S_ISLNK(mode2)) {
2935 0529f8df 2023-03-10 thomas err = install_symlink(&is_bad_symlink,
2936 0529f8df 2023-03-10 thomas worktree, ondisk_path, path2, blob2,
2937 0529f8df 2023-03-10 thomas restoring_missing_file,
2938 0529f8df 2023-03-10 thomas reverting_versioned_file,
2939 0529f8df 2023-03-10 thomas path_is_unversioned, allow_bad_symlinks,
2940 0529f8df 2023-03-10 thomas repo, progress_cb, progress_arg);
2941 0529f8df 2023-03-10 thomas } else {
2942 0529f8df 2023-03-10 thomas err = install_blob(worktree, ondisk_path, path2,
2943 0529f8df 2023-03-10 thomas mode2, GOT_DEFAULT_FILE_MODE, blob2,
2944 0529f8df 2023-03-10 thomas restoring_missing_file, reverting_versioned_file, 0,
2945 aaf972e2 2023-09-18 thomas path_is_unversioned, NULL, repo,
2946 aaf972e2 2023-09-18 thomas progress_cb, progress_arg);
2948 0529f8df 2023-03-10 thomas if (err)
2949 0529f8df 2023-03-10 thomas return err;
2950 0529f8df 2023-03-10 thomas if (ie == NULL) {
2951 0529f8df 2023-03-10 thomas /* Adding an unversioned file. */
2952 0529f8df 2023-03-10 thomas err = got_fileindex_entry_alloc(&ie, path2);
2953 0529f8df 2023-03-10 thomas if (err)
2954 0529f8df 2023-03-10 thomas return err;
2955 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2956 0529f8df 2023-03-10 thomas worktree->root_fd, path2, NULL, NULL, 1);
2957 0529f8df 2023-03-10 thomas if (err) {
2958 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2959 0529f8df 2023-03-10 thomas return err;
2961 0529f8df 2023-03-10 thomas err = got_fileindex_entry_add(fileindex, ie);
2962 0529f8df 2023-03-10 thomas if (err) {
2963 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2964 0529f8df 2023-03-10 thomas return err;
2966 0529f8df 2023-03-10 thomas } else {
2967 0529f8df 2023-03-10 thomas /* Re-adding a locally deleted file. */
2968 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2969 0529f8df 2023-03-10 thomas worktree->root_fd, path2, ie->blob_sha1,
2970 0529f8df 2023-03-10 thomas worktree->base_commit_id->sha1, 0);
2971 0529f8df 2023-03-10 thomas if (err)
2972 0529f8df 2023-03-10 thomas return err;
2975 0529f8df 2023-03-10 thomas if (is_bad_symlink) {
2976 0529f8df 2023-03-10 thomas got_fileindex_entry_filetype_set(ie,
2977 0529f8df 2023-03-10 thomas GOT_FILEIDX_MODE_BAD_SYMLINK);
2980 0529f8df 2023-03-10 thomas return NULL;
2983 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2984 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2985 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2986 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2987 234035bc 2019-06-01 stsp void *progress_arg;
2988 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2989 234035bc 2019-06-01 stsp void *cancel_arg;
2990 f69721c3 2019-10-21 stsp const char *label_orig;
2991 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2992 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2995 234035bc 2019-06-01 stsp static const struct got_error *
2996 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2997 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2998 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2999 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3000 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3002 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
3003 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
3004 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
3005 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
3006 234035bc 2019-06-01 stsp struct stat sb;
3007 234035bc 2019-06-01 stsp unsigned char status;
3008 234035bc 2019-06-01 stsp int local_changes_subsumed;
3009 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3010 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
3011 4c3671a9 2023-07-26 thomas struct got_object_id *id = NULL;
3013 234035bc 2019-06-01 stsp if (blob1 && blob2) {
3014 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3015 d6c87207 2019-08-02 stsp strlen(path2));
3016 1ee397ad 2019-07-12 stsp if (ie == NULL)
3017 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3018 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
3020 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3021 234035bc 2019-06-01 stsp path2) == -1)
3022 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3024 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3027 234035bc 2019-06-01 stsp goto done;
3029 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3030 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3031 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
3032 234035bc 2019-06-01 stsp goto done;
3034 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3035 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3036 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3037 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3038 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3039 234035bc 2019-06-01 stsp goto done;
3042 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3043 36bf999c 2020-07-23 stsp char *link_target2;
3044 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3046 36bf999c 2020-07-23 stsp goto done;
3047 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3048 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3049 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3050 36bf999c 2020-07-23 stsp free(link_target2);
3054 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3055 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3056 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3057 1af628f4 2021-06-03 stsp goto done;
3059 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3060 1af628f4 2021-06-03 stsp f_orig, blob1);
3062 1af628f4 2021-06-03 stsp goto done;
3064 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3065 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3066 1af628f4 2021-06-03 stsp goto done;
3067 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3068 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3070 1af628f4 2021-06-03 stsp goto done;
3072 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
3073 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3074 1af628f4 2021-06-03 stsp if (fd == -1) {
3075 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3076 1af628f4 2021-06-03 stsp ondisk_path);
3077 1af628f4 2021-06-03 stsp goto done;
3079 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3080 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3081 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3082 1af628f4 2021-06-03 stsp ondisk_path);
3083 1af628f4 2021-06-03 stsp close(fd);
3084 1af628f4 2021-06-03 stsp goto done;
3086 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3088 1af628f4 2021-06-03 stsp goto done;
3089 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3090 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3091 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3092 1af628f4 2021-06-03 stsp goto done;
3094 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3095 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3096 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
3097 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3098 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3100 234035bc 2019-06-01 stsp } else if (blob1) {
3101 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3102 d6c87207 2019-08-02 stsp strlen(path1));
3103 1ee397ad 2019-07-12 stsp if (ie == NULL)
3104 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3105 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3107 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3108 2b92fad7 2019-06-02 stsp path1) == -1)
3109 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3111 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3114 2b92fad7 2019-06-02 stsp goto done;
3116 2b92fad7 2019-06-02 stsp switch (status) {
3117 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3118 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3119 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3121 1ee397ad 2019-07-12 stsp goto done;
3122 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3124 2b92fad7 2019-06-02 stsp goto done;
3126 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3128 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3129 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3130 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3131 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3133 1ee397ad 2019-07-12 stsp goto done;
3135 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3137 4c3671a9 2023-07-26 thomas case GOT_STATUS_MODIFY: {
3138 4c3671a9 2023-07-26 thomas FILE *blob1_f;
3139 4c3671a9 2023-07-26 thomas off_t blob1_size;
3140 4c3671a9 2023-07-26 thomas int obj_type;
3142 4c3671a9 2023-07-26 thomas * Delete the file only if its content already
3143 4c3671a9 2023-07-26 thomas * exists in the repository.
3145 4c3671a9 2023-07-26 thomas err = got_object_blob_file_create(&id, &blob1_f,
3146 4c3671a9 2023-07-26 thomas &blob1_size, path1);
3147 4c3671a9 2023-07-26 thomas if (err)
3148 4c3671a9 2023-07-26 thomas goto done;
3149 4c3671a9 2023-07-26 thomas if (fclose(blob1_f) == EOF) {
3150 4c3671a9 2023-07-26 thomas err = got_error_from_errno("fclose");
3151 4c3671a9 2023-07-26 thomas goto done;
3154 4c3671a9 2023-07-26 thomas /* Implied existence check. */
3155 4c3671a9 2023-07-26 thomas err = got_object_get_type(&obj_type, repo, id);
3156 4c3671a9 2023-07-26 thomas if (err) {
3157 4c3671a9 2023-07-26 thomas if (err->code != GOT_ERR_NO_OBJ)
3158 4c3671a9 2023-07-26 thomas goto done;
3159 4c3671a9 2023-07-26 thomas err = (*a->progress_cb)(a->progress_arg,
3160 4c3671a9 2023-07-26 thomas GOT_STATUS_CANNOT_DELETE, path1);
3161 4c3671a9 2023-07-26 thomas goto done;
3162 4c3671a9 2023-07-26 thomas } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3163 4c3671a9 2023-07-26 thomas err = (*a->progress_cb)(a->progress_arg,
3164 4c3671a9 2023-07-26 thomas GOT_STATUS_CANNOT_DELETE, path1);
3165 4c3671a9 2023-07-26 thomas goto done;
3167 4c3671a9 2023-07-26 thomas err = (*a->progress_cb)(a->progress_arg,
3168 4c3671a9 2023-07-26 thomas GOT_STATUS_DELETE, path1);
3169 4c3671a9 2023-07-26 thomas if (err)
3170 4c3671a9 2023-07-26 thomas goto done;
3171 4c3671a9 2023-07-26 thomas err = remove_ondisk_file(a->worktree->root_path,
3173 4c3671a9 2023-07-26 thomas if (err)
3174 4c3671a9 2023-07-26 thomas goto done;
3176 4c3671a9 2023-07-26 thomas got_fileindex_entry_mark_deleted_from_disk(ie);
3179 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3180 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3181 e8f02263 2022-01-23 thomas off_t blob1_size;
3183 4c3671a9 2023-07-26 thomas * Delete the file only if its content already
3184 0a22ca1a 2020-09-23 stsp * exists in the repository.
3186 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
3187 e8f02263 2022-01-23 thomas &blob1_size, path1);
3189 0a22ca1a 2020-09-23 stsp goto done;
3190 4c3671a9 2023-07-26 thomas if (fclose(blob1_f) == EOF) {
3191 4c3671a9 2023-07-26 thomas err = got_error_from_errno("fclose");
3192 4c3671a9 2023-07-26 thomas goto done;
3194 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3195 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3196 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3198 0a22ca1a 2020-09-23 stsp goto done;
3199 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3202 0a22ca1a 2020-09-23 stsp goto done;
3204 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3207 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3208 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3211 0a22ca1a 2020-09-23 stsp goto done;
3214 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3215 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3216 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3218 1ee397ad 2019-07-12 stsp goto done;
3220 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3221 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3223 1ee397ad 2019-07-12 stsp goto done;
3228 234035bc 2019-06-01 stsp } else if (blob2) {
3229 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3230 234035bc 2019-06-01 stsp path2) == -1)
3231 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3232 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3233 d6c87207 2019-08-02 stsp strlen(path2));
3235 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3236 7f91a133 2019-12-13 stsp -1, NULL, repo);
3238 234035bc 2019-06-01 stsp goto done;
3239 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3240 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3241 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3242 0529f8df 2023-03-10 thomas status != GOT_STATUS_ADD &&
3243 0529f8df 2023-03-10 thomas status != GOT_STATUS_DELETE) {
3244 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3245 1ee397ad 2019-07-12 stsp status, path2);
3246 234035bc 2019-06-01 stsp goto done;
3248 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3249 36bf999c 2020-07-23 stsp char *link_target2;
3250 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3253 36bf999c 2020-07-23 stsp goto done;
3254 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3255 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3256 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3257 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3258 36bf999c 2020-07-23 stsp free(link_target2);
3259 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3260 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3261 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3262 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3263 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3264 526a746f 2020-07-23 stsp a->progress_arg);
3265 0529f8df 2023-03-10 thomas } else if (status != GOT_STATUS_DELETE) {
3266 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3267 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3270 dfe9fba0 2020-07-23 stsp goto done;
3271 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3272 0529f8df 2023-03-10 thomas /* Re-add file with content from new blob. */
3273 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, ie,
3274 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3275 0529f8df 2023-03-10 thomas 0, 0, 0, a->allow_bad_symlinks,
3276 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3278 234035bc 2019-06-01 stsp goto done;
3281 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, NULL,
3282 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3283 0529f8df 2023-03-10 thomas 0, 0, 1, a->allow_bad_symlinks,
3284 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3286 2b92fad7 2019-06-02 stsp goto done;
3290 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3291 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3292 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3293 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3294 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3295 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3296 1af628f4 2021-06-03 stsp free(id_str);
3297 4c3671a9 2023-07-26 thomas free(id);
3298 54d5be07 2021-06-03 stsp free(label_deriv2);
3299 234035bc 2019-06-01 stsp free(ondisk_path);
3300 234035bc 2019-06-01 stsp return err;
3303 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args {
3304 6ebb2263 2023-07-26 thomas struct got_worktree *worktree;
3305 6ebb2263 2023-07-26 thomas got_cancel_cb cancel_cb;
3306 6ebb2263 2023-07-26 thomas void *cancel_arg;
3309 69de9dd4 2021-09-03 stsp static const struct got_error *
3310 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3312 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
3313 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args *a = arg;
3315 6ebb2263 2023-07-26 thomas if (a->cancel_cb) {
3316 6ebb2263 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3317 6ebb2263 2023-07-26 thomas if (err)
3318 6ebb2263 2023-07-26 thomas return err;
3321 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3322 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3323 6ebb2263 2023-07-26 thomas memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3324 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3325 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3327 69de9dd4 2021-09-03 stsp return NULL;
3330 349dfd1e 2023-07-23 thomas const struct got_error *
3331 349dfd1e 2023-07-23 thomas got_worktree_get_state(char *state, struct got_repository *repo,
3332 6ebb2263 2023-07-26 thomas struct got_worktree *worktree,
3333 6ebb2263 2023-07-26 thomas got_cancel_cb cancel_cb, void *cancel_arg)
3335 349dfd1e 2023-07-23 thomas const struct got_error *err;
3336 349dfd1e 2023-07-23 thomas struct got_object_id *base_id, *head_id = NULL;
3337 349dfd1e 2023-07-23 thomas struct got_reference *head_ref;
3338 349dfd1e 2023-07-23 thomas struct got_fileindex *fileindex = NULL;
3339 349dfd1e 2023-07-23 thomas char *fileindex_path = NULL;
3340 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args cma;
3342 349dfd1e 2023-07-23 thomas if (worktree == NULL)
3343 349dfd1e 2023-07-23 thomas return got_error(GOT_ERR_NOT_WORKTREE);
3345 349dfd1e 2023-07-23 thomas err = got_ref_open(&head_ref, repo,
3346 349dfd1e 2023-07-23 thomas got_worktree_get_head_ref_name(worktree), 0);
3347 349dfd1e 2023-07-23 thomas if (err)
3348 349dfd1e 2023-07-23 thomas return err;
3350 349dfd1e 2023-07-23 thomas err = got_ref_resolve(&head_id, repo, head_ref);
3351 349dfd1e 2023-07-23 thomas if (err)
3352 349dfd1e 2023-07-23 thomas goto done;
3354 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_UNKNOWN;
3355 349dfd1e 2023-07-23 thomas base_id = got_worktree_get_base_commit_id(worktree);
3357 6ebb2263 2023-07-26 thomas cma.worktree = worktree;
3358 6ebb2263 2023-07-26 thomas cma.cancel_cb = cancel_cb;
3359 6ebb2263 2023-07-26 thomas cma.cancel_arg = cancel_arg;
3361 349dfd1e 2023-07-23 thomas if (got_object_id_cmp(base_id, head_id) == 0) {
3362 349dfd1e 2023-07-23 thomas err = open_fileindex(&fileindex, &fileindex_path, worktree);
3363 349dfd1e 2023-07-23 thomas if (err)
3364 349dfd1e 2023-07-23 thomas goto done;
3366 349dfd1e 2023-07-23 thomas err = got_fileindex_for_each_entry_safe(fileindex,
3367 6ebb2263 2023-07-26 thomas check_mixed_commits, &cma);
3368 349dfd1e 2023-07-23 thomas if (err == NULL)
3369 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_UPTODATE;
3370 92845f09 2023-07-26 thomas else if (err->code == GOT_ERR_MIXED_COMMITS) {
3371 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_OUTOFDATE;
3372 349dfd1e 2023-07-23 thomas err = NULL;
3375 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_OUTOFDATE;
3378 349dfd1e 2023-07-23 thomas free(head_id);
3379 349dfd1e 2023-07-23 thomas free(fileindex_path);
3380 349dfd1e 2023-07-23 thomas got_ref_close(head_ref);
3381 349dfd1e 2023-07-23 thomas if (fileindex != NULL)
3382 349dfd1e 2023-07-23 thomas got_fileindex_free(fileindex);
3383 349dfd1e 2023-07-23 thomas return err;
3386 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3387 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3388 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3389 234035bc 2019-06-01 stsp struct got_repository *repo;
3392 234035bc 2019-06-01 stsp static const struct got_error *
3393 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3394 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3395 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3396 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3397 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3399 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3400 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3401 234035bc 2019-06-01 stsp unsigned char status;
3402 234035bc 2019-06-01 stsp struct stat sb;
3403 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3404 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3405 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3406 234035bc 2019-06-01 stsp char *ondisk_path;
3408 69de9dd4 2021-09-03 stsp if (id == NULL)
3409 69de9dd4 2021-09-03 stsp return NULL;
3411 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3412 69de9dd4 2021-09-03 stsp if (ie == NULL)
3413 69de9dd4 2021-09-03 stsp return NULL;
3415 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3417 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3419 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3420 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3421 5546d466 2021-09-02 stsp free(ondisk_path);
3423 234035bc 2019-06-01 stsp return err;
3424 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3425 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3427 234035bc 2019-06-01 stsp return NULL;
3430 818c7501 2019-07-11 stsp static const struct got_error *
3431 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3432 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3433 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3434 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3435 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3437 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3438 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3439 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3440 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3441 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3442 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3443 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3444 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3445 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3447 03415a1a 2019-06-02 stsp if (commit_id1) {
3448 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3449 945f9229 2022-04-16 thomas if (err)
3450 945f9229 2022-04-16 thomas goto done;
3451 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3452 03415a1a 2019-06-02 stsp worktree->path_prefix);
3453 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3454 03415a1a 2019-06-02 stsp goto done;
3456 69d57f3d 2020-07-31 stsp if (tree_id1) {
3457 69d57f3d 2020-07-31 stsp char *id_str;
3459 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3461 03415a1a 2019-06-02 stsp goto done;
3463 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3465 f69721c3 2019-10-21 stsp goto done;
3467 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3468 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3469 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3470 f69721c3 2019-10-21 stsp free(id_str);
3471 f69721c3 2019-10-21 stsp goto done;
3473 f69721c3 2019-10-21 stsp free(id_str);
3475 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3476 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3477 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3478 a0f32f33 2022-06-13 thomas goto done;
3482 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3483 945f9229 2022-04-16 thomas if (err)
3484 945f9229 2022-04-16 thomas goto done;
3486 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3487 234035bc 2019-06-01 stsp worktree->path_prefix);
3489 234035bc 2019-06-01 stsp goto done;
3491 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3493 234035bc 2019-06-01 stsp goto done;
3495 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3496 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3497 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3498 19a6a6b5 2022-07-01 thomas goto done;
3501 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3502 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3503 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3504 a0f32f33 2022-06-13 thomas goto done;
3507 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3508 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3509 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3510 19a6a6b5 2022-07-01 thomas goto done;
3513 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3514 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3515 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3516 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3517 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3519 69de9dd4 2021-09-03 stsp goto done;
3521 234035bc 2019-06-01 stsp arg.worktree = worktree;
3522 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3523 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3524 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3525 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3526 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3527 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3528 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3529 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3530 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3531 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3532 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3533 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3534 af12c6b9 2019-06-04 stsp err = sync_err;
3536 945f9229 2022-04-16 thomas if (commit1)
3537 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3538 945f9229 2022-04-16 thomas if (commit2)
3539 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3540 234035bc 2019-06-01 stsp if (tree1)
3541 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3542 234035bc 2019-06-01 stsp if (tree2)
3543 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3544 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3545 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3546 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3547 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3548 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3549 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3550 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3551 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3552 f69721c3 2019-10-21 stsp free(label_orig);
3553 818c7501 2019-07-11 stsp return err;
3556 818c7501 2019-07-11 stsp const struct got_error *
3557 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3558 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3559 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3560 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3562 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3563 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3564 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3565 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args cma;
3567 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3569 818c7501 2019-07-11 stsp return err;
3571 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3573 818c7501 2019-07-11 stsp goto done;
3575 6ebb2263 2023-07-26 thomas cma.worktree = worktree;
3576 6ebb2263 2023-07-26 thomas cma.cancel_cb = cancel_cb;
3577 6ebb2263 2023-07-26 thomas cma.cancel_arg = cancel_arg;
3579 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3582 818c7501 2019-07-11 stsp goto done;
3584 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3585 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3586 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3588 818c7501 2019-07-11 stsp if (fileindex)
3589 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3590 818c7501 2019-07-11 stsp free(fileindex_path);
3591 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3592 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3593 234035bc 2019-06-01 stsp err = unlockerr;
3594 234035bc 2019-06-01 stsp return err;
3597 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3598 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3599 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3600 927df6b7 2019-02-10 stsp const char *status_path;
3601 927df6b7 2019-02-10 stsp size_t status_path_len;
3602 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3603 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3604 f8d1f275 2019-02-04 stsp void *status_arg;
3605 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3606 f8d1f275 2019-02-04 stsp void *cancel_arg;
3607 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3608 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3609 f2a9dc41 2019-12-13 tracey int report_unchanged;
3610 3143d852 2020-06-25 stsp int no_ignores;
3613 f8d1f275 2019-02-04 stsp static const struct got_error *
3614 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3615 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3616 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3617 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3619 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3620 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3621 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3622 927df6b7 2019-02-10 stsp struct stat sb;
3623 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3624 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3625 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3627 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3628 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3630 98eaaa12 2019-08-03 stsp return err;
3632 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3633 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3634 98eaaa12 2019-08-03 stsp return NULL;
3636 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3637 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3638 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3639 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3640 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3641 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3642 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3643 43010591 2023-02-17 thomas &staged_blob_id, ie);
3646 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3647 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3650 927df6b7 2019-02-10 stsp static const struct got_error *
3651 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3652 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3654 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3655 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3656 f8d1f275 2019-02-04 stsp char *abspath;
3658 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
3659 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3660 eb6fb9cd 2023-07-26 thomas if (err)
3661 eb6fb9cd 2023-07-26 thomas return err;
3664 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3665 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3666 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3667 927df6b7 2019-02-10 stsp return NULL;
3669 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3670 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3671 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3672 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3674 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3675 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3676 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3679 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3680 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3681 f8d1f275 2019-02-04 stsp free(abspath);
3682 9d31a1d8 2018-03-11 stsp return err;
3685 f8d1f275 2019-02-04 stsp static const struct got_error *
3686 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3688 eb6fb9cd 2023-07-26 thomas const struct got_error *err;
3689 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3690 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3691 2ec1f75b 2019-03-26 stsp unsigned char status;
3693 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
3694 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3695 eb6fb9cd 2023-07-26 thomas if (err)
3696 eb6fb9cd 2023-07-26 thomas return err;
3699 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3700 927df6b7 2019-02-10 stsp return NULL;
3702 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3703 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3704 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3705 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3707 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3708 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3709 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3712 ef20f542 2022-06-26 thomas static void
3713 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3715 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3717 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3718 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3720 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3722 5f42e012 2023-12-09 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_ALL);
3725 6841da00 2019-08-08 stsp static const struct got_error *
3726 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3728 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3729 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3730 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3731 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3732 6841da00 2019-08-08 stsp size_t linesize = 0;
3733 6841da00 2019-08-08 stsp ssize_t linelen;
3735 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3736 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3737 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3738 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3740 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3741 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3742 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3744 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3745 bd8de430 2019-10-04 stsp if (line[0] == '#')
3748 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3749 bd8de430 2019-10-04 stsp if (line[0] == '!')
3752 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3753 6841da00 2019-08-08 stsp line) == -1) {
3754 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3755 6841da00 2019-08-08 stsp goto done;
3757 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3759 6841da00 2019-08-08 stsp goto done;
3761 6841da00 2019-08-08 stsp if (ferror(f)) {
3762 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3763 6841da00 2019-08-08 stsp goto done;
3766 6841da00 2019-08-08 stsp dirpath = strdup(path);
3767 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3768 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3769 6841da00 2019-08-08 stsp goto done;
3771 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3773 6841da00 2019-08-08 stsp free(line);
3774 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3775 6841da00 2019-08-08 stsp free(dirpath);
3776 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3777 5f42e012 2023-12-09 thomas free(ignorelist);
3779 6841da00 2019-08-08 stsp return err;
3782 1b5d300f 2023-02-20 thomas static int
3783 1b5d300f 2023-02-20 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
3784 1b5d300f 2023-02-20 thomas int flags)
3786 1b5d300f 2023-02-20 thomas char buf[PATH_MAX];
3789 1b5d300f 2023-02-20 thomas * Trailing slashes signify directories.
3790 1b5d300f 2023-02-20 thomas * Append a * to make such patterns conform to fnmatch rules.
3792 1b5d300f 2023-02-20 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3793 1b5d300f 2023-02-20 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3794 1b5d300f 2023-02-20 thomas return FNM_NOMATCH; /* XXX */
3796 1b5d300f 2023-02-20 thomas return fnmatch(buf, path, flags);
3799 1b5d300f 2023-02-20 thomas return fnmatch(pattern, path, flags);
3802 ef20f542 2022-06-26 thomas static int
3803 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3805 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3807 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3808 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3809 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3810 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3812 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3813 1b5d300f 2023-02-20 thomas const char *p;
3815 1b5d300f 2023-02-20 thomas if (pi->path_len < 3 ||
3816 1b5d300f 2023-02-20 thomas strncmp(pi->path, "**/", 3) != 0)
3819 bd8de430 2019-10-04 stsp while (*p) {
3820 1b5d300f 2023-02-20 thomas if (match_path(pi->path + 3,
3821 1b5d300f 2023-02-20 thomas pi->path_len - 3, p,
3822 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3823 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3824 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3826 bd8de430 2019-10-04 stsp while (*p == '/')
3836 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3837 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3838 6841da00 2019-08-08 stsp * ignores backwards.
3840 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3841 6841da00 2019-08-08 stsp while (pe) {
3842 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3843 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3844 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3845 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3846 1b5d300f 2023-02-20 thomas int flags = FNM_LEADING_DIR;
3847 1b5d300f 2023-02-20 thomas if (strstr(pi->path, "/**/") == NULL)
3848 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3849 1b5d300f 2023-02-20 thomas if (match_path(pi->path, pi->path_len,
3850 1b5d300f 2023-02-20 thomas path, flags))
3855 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3861 6841da00 2019-08-08 stsp static const struct got_error *
3862 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3863 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3865 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3866 6841da00 2019-08-08 stsp char *ignorespath;
3867 886cec17 2019-12-15 stsp int fd = -1;
3868 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3870 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3871 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3872 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3874 886cec17 2019-12-15 stsp if (dirfd != -1) {
3875 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3876 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3877 886cec17 2019-12-15 stsp if (fd == -1) {
3878 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3879 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3880 886cec17 2019-12-15 stsp ignorespath);
3882 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3883 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3884 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3885 886cec17 2019-12-15 stsp ignorespath);
3888 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3892 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3893 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3894 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3895 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3896 886cec17 2019-12-15 stsp ignorespath);
3898 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3901 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3902 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3903 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3904 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3905 6841da00 2019-08-08 stsp free(ignorespath);
3906 6841da00 2019-08-08 stsp return err;
3909 f8d1f275 2019-02-04 stsp static const struct got_error *
3910 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3911 6092c299 2021-10-04 thomas int dirfd)
3913 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3914 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3915 f8d1f275 2019-02-04 stsp char *path = NULL;
3917 6092c299 2021-10-04 thomas if (ignore != NULL)
3918 6092c299 2021-10-04 thomas *ignore = 0;
3920 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
3921 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3922 eb6fb9cd 2023-07-26 thomas if (err)
3923 eb6fb9cd 2023-07-26 thomas return err;
3926 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3927 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3928 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3930 f8d1f275 2019-02-04 stsp path = de->d_name;
3933 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3934 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3935 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3936 6092c299 2021-10-04 thomas *ignore = 1;
3937 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3938 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3939 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3940 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3941 f8d1f275 2019-02-04 stsp if (parent_path[0])
3942 f8d1f275 2019-02-04 stsp free(path);
3943 b72f483a 2019-02-05 stsp return err;
3946 347d1d3e 2019-07-12 stsp static const struct got_error *
3947 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3949 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3950 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3952 3143d852 2020-06-25 stsp if (a->no_ignores)
3953 3143d852 2020-06-25 stsp return NULL;
3955 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3956 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3958 3143d852 2020-06-25 stsp return err;
3960 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3961 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3963 3143d852 2020-06-25 stsp return err;
3966 3143d852 2020-06-25 stsp static const struct got_error *
3967 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3968 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3969 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3970 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3972 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3973 abb4604f 2019-07-27 stsp struct stat sb;
3975 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3977 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3978 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3980 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3981 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3982 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3983 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3984 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3987 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3988 fe1d8685 2022-02-12 thomas return NULL;
3990 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3991 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3992 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3994 abb4604f 2019-07-27 stsp return NULL;
3997 3143d852 2020-06-25 stsp static const struct got_error *
3998 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3999 3143d852 2020-06-25 stsp const char *root_path, const char *path)
4001 3143d852 2020-06-25 stsp const struct got_error *err;
4002 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
4004 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
4005 3143d852 2020-06-25 stsp ".cvsignore");
4007 3143d852 2020-06-25 stsp return err;
4009 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
4010 3143d852 2020-06-25 stsp ".gitignore");
4012 3143d852 2020-06-25 stsp return err;
4014 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
4015 3143d852 2020-06-25 stsp if (err) {
4016 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
4017 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
4018 3143d852 2020-06-25 stsp return err;
4020 3143d852 2020-06-25 stsp for (;;) {
4021 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
4022 3143d852 2020-06-25 stsp ".cvsignore");
4025 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
4026 3143d852 2020-06-25 stsp ".gitignore");
4029 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
4030 3143d852 2020-06-25 stsp if (err) {
4031 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
4032 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
4035 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
4037 b737c85a 2020-06-26 stsp free(parent_path);
4038 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
4039 b737c85a 2020-06-26 stsp next_parent_path = NULL;
4042 b737c85a 2020-06-26 stsp free(parent_path);
4043 b737c85a 2020-06-26 stsp free(next_parent_path);
4044 3143d852 2020-06-25 stsp return err;
4047 f8836425 2023-04-14 thomas struct find_missing_children_args {
4048 f8836425 2023-04-14 thomas const char *parent_path;
4049 f8836425 2023-04-14 thomas size_t parent_len;
4050 f8836425 2023-04-14 thomas struct got_pathlist_head *children;
4051 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb;
4052 f8836425 2023-04-14 thomas void *cancel_arg;
4055 abb4604f 2019-07-27 stsp static const struct got_error *
4056 f8836425 2023-04-14 thomas find_missing_children(void *arg, struct got_fileindex_entry *ie)
4058 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
4059 f8836425 2023-04-14 thomas struct find_missing_children_args *a = arg;
4061 f8836425 2023-04-14 thomas if (a->cancel_cb) {
4062 f8836425 2023-04-14 thomas err = a->cancel_cb(a->cancel_arg);
4063 f8836425 2023-04-14 thomas if (err)
4064 f8836425 2023-04-14 thomas return err;
4067 f8836425 2023-04-14 thomas if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4068 f8836425 2023-04-14 thomas err = got_pathlist_append(a->children, ie->path, NULL);
4070 f8836425 2023-04-14 thomas return err;
4073 f8836425 2023-04-14 thomas static const struct got_error *
4074 f8836425 2023-04-14 thomas report_children(struct got_pathlist_head *children,
4075 f8836425 2023-04-14 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
4076 f8836425 2023-04-14 thomas struct got_repository *repo, int is_root_dir, int report_unchanged,
4077 f8836425 2023-04-14 thomas struct got_pathlist_head *ignores, int no_ignores,
4078 f8836425 2023-04-14 thomas got_worktree_status_cb status_cb, void *status_arg,
4079 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb, void *cancel_arg)
4081 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
4082 f8836425 2023-04-14 thomas struct got_pathlist_entry *pe;
4083 f8836425 2023-04-14 thomas char *ondisk_path = NULL;
4085 f8836425 2023-04-14 thomas TAILQ_FOREACH(pe, children, entry) {
4086 f8836425 2023-04-14 thomas if (cancel_cb) {
4087 f8836425 2023-04-14 thomas err = cancel_cb(cancel_arg);
4088 f8836425 2023-04-14 thomas if (err)
4092 f8836425 2023-04-14 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4093 f8836425 2023-04-14 thomas !is_root_dir ? "/" : "", pe->path) == -1) {
4094 f8836425 2023-04-14 thomas err = got_error_from_errno("asprintf");
4095 f8836425 2023-04-14 thomas ondisk_path = NULL;
4099 f8836425 2023-04-14 thomas err = report_single_file_status(pe->path, ondisk_path,
4100 f8836425 2023-04-14 thomas fileindex, status_cb, status_arg, repo, report_unchanged,
4101 f8836425 2023-04-14 thomas ignores, no_ignores);
4102 f8836425 2023-04-14 thomas if (err)
4105 f8836425 2023-04-14 thomas free(ondisk_path);
4106 f8836425 2023-04-14 thomas ondisk_path = NULL;
4109 f8836425 2023-04-14 thomas free(ondisk_path);
4110 f8836425 2023-04-14 thomas return err;
4113 f8836425 2023-04-14 thomas static const struct got_error *
4114 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
4115 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4116 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4117 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4118 f2a9dc41 2019-12-13 tracey int report_unchanged)
4120 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
4121 6fc93f37 2019-12-13 stsp int fd = -1;
4122 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
4123 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
4124 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
4125 f8836425 2023-04-14 thomas struct got_pathlist_head ignores, missing_children;
4126 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
4128 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
4129 f8836425 2023-04-14 thomas TAILQ_INIT(&missing_children);
4131 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
4132 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
4133 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
4135 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4136 a78810f8 2022-03-13 thomas if (ie) {
4137 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
4138 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
4139 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
4140 a78810f8 2022-03-13 thomas goto done;
4141 f8836425 2023-04-14 thomas } else {
4142 f8836425 2023-04-14 thomas struct find_missing_children_args fmca;
4143 f8836425 2023-04-14 thomas fmca.parent_path = path;
4144 f8836425 2023-04-14 thomas fmca.parent_len = strlen(path);
4145 f8836425 2023-04-14 thomas fmca.children = &missing_children;
4146 f8836425 2023-04-14 thomas fmca.cancel_cb = cancel_cb;
4147 f8836425 2023-04-14 thomas fmca.cancel_arg = cancel_arg;
4148 f8836425 2023-04-14 thomas err = got_fileindex_for_each_entry_safe(fileindex,
4149 f8836425 2023-04-14 thomas find_missing_children, &fmca);
4150 f8836425 2023-04-14 thomas if (err)
4151 f8836425 2023-04-14 thomas goto done;
4154 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4155 6fc93f37 2019-12-13 stsp if (fd == -1) {
4156 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4157 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
4158 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
4160 ff56836b 2021-07-08 stsp if (!no_ignores) {
4161 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4162 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
4164 ff56836b 2021-07-08 stsp goto done;
4166 f8836425 2023-04-14 thomas if (TAILQ_EMPTY(&missing_children)) {
4167 f8836425 2023-04-14 thomas err = report_single_file_status(path,
4168 f8836425 2023-04-14 thomas ondisk_path, fileindex,
4169 f8836425 2023-04-14 thomas status_cb, status_arg, repo,
4170 f8836425 2023-04-14 thomas report_unchanged, &ignores, no_ignores);
4171 f8836425 2023-04-14 thomas if (err)
4172 f8836425 2023-04-14 thomas goto done;
4173 f8836425 2023-04-14 thomas } else {
4174 f8836425 2023-04-14 thomas err = report_children(&missing_children,
4175 f8836425 2023-04-14 thomas worktree, fileindex, repo,
4176 f8836425 2023-04-14 thomas (path[0] == '\0'), report_unchanged,
4177 f8836425 2023-04-14 thomas &ignores, no_ignores,
4178 f8836425 2023-04-14 thomas status_cb, status_arg,
4179 f8836425 2023-04-14 thomas cancel_cb, cancel_arg);
4180 6f97aa83 2023-04-14 thomas if (err)
4181 6f97aa83 2023-04-14 thomas goto done;
4185 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4186 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4187 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4188 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4189 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4190 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4191 abb4604f 2019-07-27 stsp arg.status_path = path;
4192 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4193 abb4604f 2019-07-27 stsp arg.repo = repo;
4194 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4195 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4196 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4197 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4198 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4199 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4200 022fae89 2019-12-06 tracey if (!no_ignores) {
4201 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4202 3143d852 2020-06-25 stsp worktree->root_path, path);
4204 3143d852 2020-06-25 stsp goto done;
4206 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4207 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4208 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4211 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4212 b2dcbbc7 2023-12-05 thomas got_pathlist_free(&missing_children, GOT_PATHLIST_FREE_NONE);
4213 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4214 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4215 927df6b7 2019-02-10 stsp free(ondisk_path);
4216 347d1d3e 2019-07-12 stsp return err;
4219 347d1d3e 2019-07-12 stsp const struct got_error *
4220 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4221 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4222 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4223 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4225 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4226 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4227 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4228 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4230 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4232 347d1d3e 2019-07-12 stsp return err;
4234 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4235 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4236 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4237 f6343036 2021-06-22 stsp no_ignores, 0);
4241 f8d1f275 2019-02-04 stsp free(fileindex_path);
4242 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4243 f8d1f275 2019-02-04 stsp return err;
4246 6c7ab921 2019-03-18 stsp const struct got_error *
4247 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4248 6c7ab921 2019-03-18 stsp const char *arg)
4250 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4251 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4252 6c7ab921 2019-03-18 stsp size_t len;
4253 00bb5ea0 2020-07-23 stsp struct stat sb;
4254 01740607 2020-11-04 stsp char *abspath = NULL;
4255 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4257 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4259 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4260 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4261 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4263 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4264 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4265 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4266 00bb5ea0 2020-07-23 stsp goto done;
4268 727173c3 2020-11-06 stsp sb.st_mode = 0;
4270 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4272 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4273 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4274 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4275 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4277 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4278 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4279 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4280 00bb5ea0 2020-07-23 stsp goto done;
4284 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4285 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4287 d0710d08 2019-07-22 stsp goto done;
4288 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4289 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4290 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4291 00bb5ea0 2020-07-23 stsp goto done;
4294 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4295 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4296 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4297 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4298 00bb5ea0 2020-07-23 stsp goto done;
4300 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4301 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4302 01740607 2020-11-04 stsp goto done;
4304 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4305 01740607 2020-11-04 stsp sizeof(canonpath));
4307 00bb5ea0 2020-07-23 stsp goto done;
4308 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4309 01740607 2020-11-04 stsp if (resolved == NULL) {
4310 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4311 01740607 2020-11-04 stsp goto done;
4316 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4317 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4318 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4319 6c7ab921 2019-03-18 stsp goto done;
4322 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4323 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4324 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4326 f6d88e1a 2019-05-29 stsp goto done;
4328 f6d88e1a 2019-05-29 stsp path = strdup("");
4329 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4330 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4331 f6d88e1a 2019-05-29 stsp goto done;
4335 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4336 6c7ab921 2019-03-18 stsp len = strlen(path);
4337 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4338 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4342 01740607 2020-11-04 stsp free(abspath);
4343 6c7ab921 2019-03-18 stsp free(resolved);
4344 d0710d08 2019-07-22 stsp free(cwd);
4345 6c7ab921 2019-03-18 stsp if (err == NULL)
4346 6c7ab921 2019-03-18 stsp *wt_path = path;
4348 6c7ab921 2019-03-18 stsp free(path);
4349 d00136be 2019-03-26 stsp return err;
4352 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4353 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4354 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4355 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4356 4e68cba3 2019-11-23 stsp void *progress_arg;
4357 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4360 b21ebdb0 2023-06-22 thomas static int
4361 b21ebdb0 2023-06-22 thomas add_noop_status(unsigned char status)
4363 b21ebdb0 2023-06-22 thomas return (status == GOT_STATUS_ADD ||
4364 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODIFY ||
4365 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_CONFLICT ||
4366 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODE_CHANGE ||
4367 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_NO_CHANGE);
4370 4e68cba3 2019-11-23 stsp static const struct got_error *
4371 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4372 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4373 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4374 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4376 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4377 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4378 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4379 6d022e97 2019-08-04 stsp struct stat sb;
4380 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4382 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4383 dbb83fbd 2019-12-12 stsp relpath) == -1)
4384 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4386 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4388 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4389 12463d8b 2019-12-13 stsp de_name, a->repo);
4391 dbb83fbd 2019-12-12 stsp goto done;
4392 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4393 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE &&
4394 b21ebdb0 2023-06-22 thomas add_noop_status(status))
4395 dbb83fbd 2019-12-12 stsp goto done;
4396 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4398 dbb83fbd 2019-12-12 stsp goto done;
4401 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4402 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4403 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4405 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4406 dbb83fbd 2019-12-12 stsp goto done;
4409 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4411 4e68cba3 2019-11-23 stsp goto done;
4412 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4413 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4414 3969253a 2020-03-07 stsp if (err) {
4415 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4416 3969253a 2020-03-07 stsp goto done;
4418 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4419 07f5b47a 2019-06-02 stsp if (err) {
4420 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4421 4e68cba3 2019-11-23 stsp goto done;
4424 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4426 dbb83fbd 2019-12-12 stsp return err;
4427 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4428 dbb83fbd 2019-12-12 stsp return NULL;
4429 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4432 d00136be 2019-03-26 stsp const struct got_error *
4433 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4434 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4435 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4436 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4438 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4439 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4440 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4441 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4442 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4444 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4446 d00136be 2019-03-26 stsp return err;
4448 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4450 d00136be 2019-03-26 stsp goto done;
4452 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4453 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4454 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4455 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4456 4e68cba3 2019-11-23 stsp saa.repo = repo;
4458 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4459 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4460 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4464 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4465 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4466 af12c6b9 2019-06-04 stsp err = sync_err;
4468 fb399478 2019-07-12 stsp free(fileindex_path);
4469 d00136be 2019-03-26 stsp if (fileindex)
4470 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4471 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4472 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4473 d00136be 2019-03-26 stsp err = unlockerr;
4474 6c7ab921 2019-03-18 stsp return err;
4477 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4478 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4479 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4480 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4481 f2a9dc41 2019-12-13 tracey void *progress_arg;
4482 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4483 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4484 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4485 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4486 8ec7352a 2023-06-01 thomas const char *status_path;
4487 8ec7352a 2023-06-01 thomas size_t status_path_len;
4488 766841c2 2020-08-13 stsp const char *status_codes;
4491 f2a9dc41 2019-12-13 tracey static const struct got_error *
4492 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4493 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4494 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4495 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4497 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4498 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4499 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4500 17ed4618 2019-06-02 stsp struct stat sb;
4501 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4503 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4504 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4505 d64cc78a 2022-01-25 thomas return NULL;
4506 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4509 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4510 17ed4618 2019-06-02 stsp if (ie == NULL)
4511 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4513 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4514 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4515 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4516 9acbc4fa 2019-08-03 stsp return NULL;
4517 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4520 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4521 f2a9dc41 2019-12-13 tracey relpath) == -1)
4522 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4524 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4527 f2a9dc41 2019-12-13 tracey goto done;
4529 766841c2 2020-08-13 stsp if (a->status_codes) {
4530 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4532 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4533 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4536 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4537 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4538 766841c2 2020-08-13 stsp free(ondisk_path);
4539 766841c2 2020-08-13 stsp return NULL;
4541 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4542 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4543 766841c2 2020-08-13 stsp static char msg[64];
4544 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4545 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4546 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4547 766841c2 2020-08-13 stsp goto done;
4551 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4552 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4553 f2a9dc41 2019-12-13 tracey goto done;
4554 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4555 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4556 f2a9dc41 2019-12-13 tracey goto done;
4558 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4559 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4560 d64cc78a 2022-01-25 thomas goto done;
4562 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4563 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4564 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4565 f2a9dc41 2019-12-13 tracey goto done;
4569 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4570 20a2ad1c 2020-10-20 stsp size_t root_len;
4572 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4573 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4574 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4575 12463d8b 2019-12-13 stsp ondisk_path);
4576 12463d8b 2019-12-13 stsp goto done;
4578 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4579 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4580 12463d8b 2019-12-13 stsp goto done;
4583 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4585 20a2ad1c 2020-10-20 stsp char *parent;
4587 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4589 2513f20a 2020-10-20 stsp goto done;
4590 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4591 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4592 8ec7352a 2023-06-01 thomas if (got_path_cmp(ondisk_path, a->status_path,
4593 8ec7352a 2023-06-01 thomas strlen(ondisk_path), a->status_path_len) != 0 &&
4594 8ec7352a 2023-06-01 thomas !got_path_is_child(ondisk_path, a->status_path,
4595 8ec7352a 2023-06-01 thomas a->status_path_len))
4597 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4598 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4599 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4600 20a2ad1c 2020-10-20 stsp ondisk_path);
4603 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4604 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4607 97f9158f 2023-08-29 thomas if (got_fileindex_entry_has_blob(ie))
4608 97f9158f 2023-08-29 thomas got_fileindex_entry_mark_deleted_from_disk(ie);
4610 97f9158f 2023-08-29 thomas got_fileindex_entry_remove(a->fileindex, ie);
4612 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4613 f2a9dc41 2019-12-13 tracey if (err)
4614 f2a9dc41 2019-12-13 tracey return err;
4615 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4616 f2a9dc41 2019-12-13 tracey return NULL;
4617 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4618 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4621 2ec1f75b 2019-03-26 stsp const struct got_error *
4622 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4623 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4624 766841c2 2020-08-13 stsp const char *status_codes,
4625 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4626 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4628 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4629 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4630 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4631 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4632 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4634 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4636 2ec1f75b 2019-03-26 stsp return err;
4638 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4640 2ec1f75b 2019-03-26 stsp goto done;
4642 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4643 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4644 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4645 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4646 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4647 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4648 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4649 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4650 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4652 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4653 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
4655 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s%s%s",
4656 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree),
4657 8ec7352a 2023-06-01 thomas pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4658 8ec7352a 2023-06-01 thomas err = got_error_from_errno("asprintf");
4659 8ec7352a 2023-06-01 thomas goto done;
4661 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
4662 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
4663 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4664 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4665 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
4669 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4670 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4671 af12c6b9 2019-06-04 stsp err = sync_err;
4673 fb399478 2019-07-12 stsp free(fileindex_path);
4674 2ec1f75b 2019-03-26 stsp if (fileindex)
4675 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4676 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4677 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4678 2ec1f75b 2019-03-26 stsp err = unlockerr;
4679 33aa809d 2019-08-08 stsp return err;
4682 33aa809d 2019-08-08 stsp static const struct got_error *
4683 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4685 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4686 33aa809d 2019-08-08 stsp char *line = NULL;
4687 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4688 33aa809d 2019-08-08 stsp ssize_t linelen;
4690 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4691 33aa809d 2019-08-08 stsp if (linelen == -1) {
4692 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4693 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4694 33aa809d 2019-08-08 stsp goto done;
4696 33aa809d 2019-08-08 stsp return NULL;
4698 33aa809d 2019-08-08 stsp if (outfile) {
4699 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4700 33aa809d 2019-08-08 stsp if (n != linelen) {
4701 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4702 33aa809d 2019-08-08 stsp goto done;
4705 33aa809d 2019-08-08 stsp if (rejectfile) {
4706 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4707 33aa809d 2019-08-08 stsp if (n != linelen)
4708 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4711 33aa809d 2019-08-08 stsp free(line);
4712 2ec1f75b 2019-03-26 stsp return err;
4715 33aa809d 2019-08-08 stsp static const struct got_error *
4716 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4718 33aa809d 2019-08-08 stsp char *line = NULL;
4719 33aa809d 2019-08-08 stsp size_t linesize = 0;
4720 33aa809d 2019-08-08 stsp ssize_t linelen;
4722 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4723 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4724 500467ff 2019-09-25 hiltjo if (ferror(f))
4725 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4726 500467ff 2019-09-25 hiltjo return NULL;
4728 33aa809d 2019-08-08 stsp free(line);
4729 33aa809d 2019-08-08 stsp return NULL;
4732 33aa809d 2019-08-08 stsp static const struct got_error *
4733 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4734 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4735 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4737 33aa809d 2019-08-08 stsp const struct got_error *err;
4739 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4740 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4741 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4743 33aa809d 2019-08-08 stsp return err;
4744 33aa809d 2019-08-08 stsp (*line_cur1)++;
4746 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4747 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4748 33aa809d 2019-08-08 stsp if (rejectfile)
4749 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4751 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4753 33aa809d 2019-08-08 stsp return err;
4754 33aa809d 2019-08-08 stsp (*line_cur2)++;
4756 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4757 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4758 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4760 33aa809d 2019-08-08 stsp return err;
4761 33aa809d 2019-08-08 stsp (*line_cur2)++;
4763 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4764 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4765 33aa809d 2019-08-08 stsp if (rejectfile)
4766 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4768 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4770 33aa809d 2019-08-08 stsp return err;
4771 33aa809d 2019-08-08 stsp (*line_cur1)++;
4774 f1e81a05 2019-08-10 stsp return NULL;
4777 f1e81a05 2019-08-10 stsp static const struct got_error *
4778 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4779 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4781 f1e81a05 2019-08-10 stsp const struct got_error *err;
4783 f1e81a05 2019-08-10 stsp if (outfile) {
4784 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4785 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4786 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4788 f1e81a05 2019-08-10 stsp return err;
4789 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4792 f1e81a05 2019-08-10 stsp if (rejectfile) {
4793 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4794 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4795 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4797 f1e81a05 2019-08-10 stsp return err;
4798 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4802 33aa809d 2019-08-08 stsp return NULL;
4805 33aa809d 2019-08-08 stsp static const struct got_error *
4806 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4807 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4808 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4809 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4810 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4812 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4813 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4814 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4815 33aa809d 2019-08-08 stsp FILE *hunkfile;
4816 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4817 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4820 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4822 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4823 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4824 fe621944 2020-11-10 stsp start_old = cc.left.start;
4825 fe621944 2020-11-10 stsp end_old = cc.left.end;
4826 fe621944 2020-11-10 stsp start_new = cc.right.start;
4827 fe621944 2020-11-10 stsp end_new = cc.right.end;
4829 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4830 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4831 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4833 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4834 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4835 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4837 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4838 fe621944 2020-11-10 stsp if (diff_state == NULL)
4839 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4840 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4842 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4843 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4844 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4845 33aa809d 2019-08-08 stsp goto done;
4848 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4849 fe621944 2020-11-10 stsp diff_result, &cc);
4850 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4851 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4852 33aa809d 2019-08-08 stsp goto done;
4855 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4856 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4857 33aa809d 2019-08-08 stsp goto done;
4860 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4861 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4863 33aa809d 2019-08-08 stsp goto done;
4865 33aa809d 2019-08-08 stsp switch (*choice) {
4866 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4867 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4868 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4870 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4871 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4872 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4874 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4877 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4881 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4882 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4883 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4884 33aa809d 2019-08-08 stsp return err;
4887 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4888 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4889 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4890 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4891 1f1abb7e 2019-08-08 stsp void *progress_arg;
4892 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4893 33aa809d 2019-08-08 stsp void *patch_arg;
4894 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4895 10604dce 2021-09-24 thomas int unlink_added_files;
4896 8641a332 2023-04-14 thomas struct got_pathlist_head *added_files_to_unlink;
4899 e20a8b6f 2019-06-04 stsp static const struct got_error *
4900 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4901 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4902 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4903 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4904 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4906 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4907 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4908 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4909 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4910 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4911 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4912 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4913 fe621944 2020-11-10 stsp struct stat sb2;
4914 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4915 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4916 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4918 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4920 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4922 33aa809d 2019-08-08 stsp return err;
4924 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4925 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4926 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4927 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4928 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4929 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4930 fa3cef63 2020-07-23 stsp goto done;
4932 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4933 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4934 48b4f239 2021-12-31 thomas if (link_len == -1) {
4935 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4938 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4939 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4942 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4943 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4944 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4945 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4946 fa3cef63 2020-07-23 stsp goto done;
4948 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4949 fa3cef63 2020-07-23 stsp sizeof(link_target));
4950 fa3cef63 2020-07-23 stsp if (link_len == -1)
4951 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4952 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4953 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4956 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4957 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4958 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4959 fa3cef63 2020-07-23 stsp goto done;
4962 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4963 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4964 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4965 fa3cef63 2020-07-23 stsp goto done;
4970 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4971 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4972 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4973 fa3cef63 2020-07-23 stsp goto done;
4975 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4976 fa3cef63 2020-07-23 stsp if (n != link_len) {
4977 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4978 fa3cef63 2020-07-23 stsp goto done;
4980 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4981 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4982 fa3cef63 2020-07-23 stsp goto done;
4984 fa3cef63 2020-07-23 stsp rewind(f2);
4987 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4988 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4989 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4990 f4ae6ddb 2022-07-01 thomas goto done;
4993 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4995 33aa809d 2019-08-08 stsp goto done;
4997 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4999 33aa809d 2019-08-08 stsp goto done;
5001 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5003 33aa809d 2019-08-08 stsp goto done;
5005 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5006 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5008 33aa809d 2019-08-08 stsp goto done;
5010 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5013 33aa809d 2019-08-08 stsp goto done;
5015 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
5016 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
5017 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
5018 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
5020 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
5021 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5022 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
5023 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
5024 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
5025 fe621944 2020-11-10 stsp nchanges++;
5027 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5028 33aa809d 2019-08-08 stsp int choice;
5029 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
5030 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
5031 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
5032 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
5033 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
5034 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
5036 33aa809d 2019-08-08 stsp goto done;
5037 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
5038 33aa809d 2019-08-08 stsp have_content = 1;
5039 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
5042 1ebedb77 2019-10-19 stsp if (have_content) {
5043 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5044 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
5045 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
5047 1ebedb77 2019-10-19 stsp goto done;
5049 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
5050 a2c162eb 2022-10-30 thomas mode_t mode;
5052 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
5053 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
5054 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
5055 fa3cef63 2020-07-23 stsp goto done;
5060 33aa809d 2019-08-08 stsp free(id_str);
5061 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5062 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5064 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
5065 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
5066 fe621944 2020-11-10 stsp if (err == NULL)
5067 fe621944 2020-11-10 stsp err = free_err;
5068 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
5069 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
5070 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
5071 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
5072 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5073 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
5074 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
5075 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
5076 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
5077 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
5078 33aa809d 2019-08-08 stsp if (err || !have_content) {
5079 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5080 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
5081 33aa809d 2019-08-08 stsp free(*path_outfile);
5082 33aa809d 2019-08-08 stsp *path_outfile = NULL;
5084 33aa809d 2019-08-08 stsp free(path1);
5085 33aa809d 2019-08-08 stsp return err;
5088 33aa809d 2019-08-08 stsp static const struct got_error *
5089 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
5090 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
5091 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5092 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5094 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
5095 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
5096 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
5097 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
5098 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
5099 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
5100 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
5101 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
5102 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
5103 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
5104 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
5105 f4ae6ddb 2022-07-01 thomas int fd = -1;
5107 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
5108 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
5109 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
5110 d3bcc3d1 2019-08-08 stsp return NULL;
5112 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
5113 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
5114 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
5116 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5117 65084dad 2019-08-08 stsp if (ie == NULL)
5118 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
5120 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
5121 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
5122 a129376b 2019-03-28 stsp if (err) {
5123 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
5124 a129376b 2019-03-28 stsp goto done;
5125 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
5126 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
5127 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
5128 e20a8b6f 2019-06-04 stsp goto done;
5131 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
5132 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
5133 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5134 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5135 a129376b 2019-03-28 stsp goto done;
5138 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
5139 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
5140 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5141 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5142 a129376b 2019-03-28 stsp goto done;
5145 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
5146 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
5147 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5148 a129376b 2019-03-28 stsp goto done;
5153 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
5154 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
5155 945f9229 2022-04-16 thomas if (err)
5156 945f9229 2022-04-16 thomas goto done;
5158 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5159 a9fa2909 2019-07-27 stsp if (err) {
5160 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5161 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
5162 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
5163 a9fa2909 2019-07-27 stsp goto done;
5165 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
5167 a9fa2909 2019-07-27 stsp goto done;
5169 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
5171 a9fa2909 2019-07-27 stsp goto done;
5173 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
5174 1233e6b6 2020-10-19 stsp free(te_name);
5175 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
5176 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
5177 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5178 a9fa2909 2019-07-27 stsp goto done;
5182 a129376b 2019-03-28 stsp switch (status) {
5183 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
5184 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5185 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5186 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5187 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5189 33aa809d 2019-08-08 stsp goto done;
5190 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5193 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5194 1f1abb7e 2019-08-08 stsp ie->path);
5196 1ee397ad 2019-07-12 stsp goto done;
5197 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5198 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
5199 8641a332 2023-04-14 thomas int do_unlink = a->added_files_to_unlink ? 0 : 1;
5201 8641a332 2023-04-14 thomas if (a->added_files_to_unlink) {
5202 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
5204 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, a->added_files_to_unlink,
5205 8641a332 2023-04-14 thomas entry) {
5206 8641a332 2023-04-14 thomas if (got_path_cmp(pe->path, relpath,
5207 8641a332 2023-04-14 thomas pe->path_len, strlen(relpath)))
5208 8641a332 2023-04-14 thomas continue;
5209 8641a332 2023-04-14 thomas do_unlink = 1;
5214 8641a332 2023-04-14 thomas if (do_unlink) {
5215 8641a332 2023-04-14 thomas if (asprintf(&ondisk_path, "%s/%s",
5216 8641a332 2023-04-14 thomas got_worktree_get_root_path(a->worktree),
5217 8641a332 2023-04-14 thomas relpath) == -1) {
5218 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
5219 8641a332 2023-04-14 thomas goto done;
5221 8641a332 2023-04-14 thomas if (unlink(ondisk_path) == -1) {
5222 8641a332 2023-04-14 thomas err = got_error_from_errno2("unlink",
5223 8641a332 2023-04-14 thomas ondisk_path);
5229 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5230 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5231 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5232 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5233 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5235 33aa809d 2019-08-08 stsp goto done;
5236 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5239 33aa809d 2019-08-08 stsp /* fall through */
5240 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5241 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5242 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5243 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5244 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5245 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5246 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
5247 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
5249 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
5250 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5251 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5252 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5253 f4ae6ddb 2022-07-01 thomas goto done;
5256 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5258 65084dad 2019-08-08 stsp goto done;
5260 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5261 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5262 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5263 a129376b 2019-03-28 stsp goto done;
5266 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5267 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5268 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5269 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5270 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5271 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5272 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5274 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5275 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5276 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5277 369fd7e5 2020-07-23 stsp path_content);
5280 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5281 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5282 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5283 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5285 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5286 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5287 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5288 369fd7e5 2020-07-23 stsp goto done;
5292 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5293 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5294 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5295 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5296 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5297 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5299 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5301 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5302 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5303 aaf972e2 2023-09-18 thomas 0, 1, 0, 0, NULL, a->repo,
5304 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5307 a129376b 2019-03-28 stsp goto done;
5308 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5309 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5310 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5311 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5312 437adc9d 2020-12-10 yzhong blob->id.sha1,
5313 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5315 2e1fa222 2020-07-23 stsp goto done;
5317 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5318 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5319 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5328 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5329 33aa809d 2019-08-08 stsp free(path_content);
5330 e20a8b6f 2019-06-04 stsp free(parent_path);
5331 a129376b 2019-03-28 stsp free(tree_path);
5332 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5333 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5335 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5337 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5338 a129376b 2019-03-28 stsp free(tree_id);
5339 945f9229 2022-04-16 thomas if (base_commit)
5340 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
5341 e20a8b6f 2019-06-04 stsp return err;
5344 e20a8b6f 2019-06-04 stsp const struct got_error *
5345 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5346 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5347 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5348 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5349 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5351 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5352 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5353 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5354 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5355 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5356 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5358 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5360 e20a8b6f 2019-06-04 stsp return err;
5362 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5364 e20a8b6f 2019-06-04 stsp goto done;
5366 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5367 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5368 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5369 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5370 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5371 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5372 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5373 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
5374 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5375 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5376 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
5380 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5381 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5382 e20a8b6f 2019-06-04 stsp err = sync_err;
5384 fb399478 2019-07-12 stsp free(fileindex_path);
5385 a129376b 2019-03-28 stsp if (fileindex)
5386 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5387 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5388 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5389 a129376b 2019-03-28 stsp err = unlockerr;
5390 c4296144 2019-05-09 stsp return err;
5393 cf066bf8 2019-05-09 stsp static void
5394 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5396 24519714 2019-05-09 stsp free(ct->path);
5397 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5398 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5399 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5400 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5401 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5402 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5406 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5407 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5408 24519714 2019-05-09 stsp struct got_repository *repo;
5409 24519714 2019-05-09 stsp struct got_worktree *worktree;
5410 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5411 5f8a88c6 2019-08-03 stsp int have_staged_files;
5412 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5413 ef899790 2022-11-01 thomas int diff_header_shown;
5414 be94c032 2023-02-20 thomas int commit_conflicts;
5415 ef899790 2022-11-01 thomas FILE *diff_outfile;
5416 ef899790 2022-11-01 thomas FILE *f1;
5417 ef899790 2022-11-01 thomas FILE *f2;
5421 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5422 ef899790 2022-11-01 thomas * it as content to the diff engine.
5424 ef899790 2022-11-01 thomas static const struct got_error *
5425 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5426 ef899790 2022-11-01 thomas const char *abspath)
5428 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5429 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5430 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5432 ef899790 2022-11-01 thomas *fd = -1;
5434 ef899790 2022-11-01 thomas if (dirfd != -1) {
5435 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5436 ef899790 2022-11-01 thomas if (target_len == -1)
5437 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5438 ef899790 2022-11-01 thomas } else {
5439 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5440 ef899790 2022-11-01 thomas if (target_len == -1)
5441 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5444 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5445 ef899790 2022-11-01 thomas if (*fd == -1)
5446 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5448 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5449 ef899790 2022-11-01 thomas if (outlen == -1) {
5450 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5451 ef899790 2022-11-01 thomas goto done;
5454 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5455 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5456 ef899790 2022-11-01 thomas goto done;
5459 ef899790 2022-11-01 thomas if (err) {
5460 ef899790 2022-11-01 thomas close(*fd);
5461 ef899790 2022-11-01 thomas *fd = -1;
5463 ef899790 2022-11-01 thomas return err;
5466 ef899790 2022-11-01 thomas static const struct got_error *
5467 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5468 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5469 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5471 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5472 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5473 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5474 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5475 ef899790 2022-11-01 thomas char *label1 = NULL;
5476 ef899790 2022-11-01 thomas struct stat sb;
5477 ef899790 2022-11-01 thomas off_t size1 = 0;
5478 ef899790 2022-11-01 thomas int f2_exists = 0;
5479 ef899790 2022-11-01 thomas char *id_str = NULL;
5481 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5483 d259e491 2022-11-01 thomas if (diff_staged) {
5484 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5485 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5486 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5487 d259e491 2022-11-01 thomas return NULL;
5488 d259e491 2022-11-01 thomas } else {
5489 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5490 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5491 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5492 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5493 d259e491 2022-11-01 thomas return NULL;
5496 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5497 ef899790 2022-11-01 thomas if (err)
5498 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5499 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5500 ef899790 2022-11-01 thomas if (err)
5501 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5503 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5504 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5505 ef899790 2022-11-01 thomas if (err)
5506 ef899790 2022-11-01 thomas return err;
5507 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5508 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5509 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5510 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5511 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5512 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5513 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5516 ef899790 2022-11-01 thomas if (diff_staged) {
5517 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5518 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5519 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5520 ef899790 2022-11-01 thomas label1 = ct->path;
5521 ef899790 2022-11-01 thomas label2 = ct->path;
5523 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5524 ef899790 2022-11-01 thomas label2 = ct->path;
5526 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5527 ef899790 2022-11-01 thomas label1 = ct->path;
5529 ef899790 2022-11-01 thomas default:
5530 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5532 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5533 ef899790 2022-11-01 thomas if (fd1 == -1) {
5534 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5535 ef899790 2022-11-01 thomas goto done;
5537 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5538 ef899790 2022-11-01 thomas if (fd2 == -1) {
5539 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5540 ef899790 2022-11-01 thomas goto done;
5542 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5543 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5544 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5545 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5546 ef899790 2022-11-01 thomas goto done;
5549 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5550 ef899790 2022-11-01 thomas if (fd1 == -1) {
5551 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5552 ef899790 2022-11-01 thomas goto done;
5555 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5556 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5557 ef899790 2022-11-01 thomas 8192, fd1);
5558 ef899790 2022-11-01 thomas if (err)
5559 ef899790 2022-11-01 thomas goto done;
5562 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5563 ef899790 2022-11-01 thomas if (dirfd != -1) {
5564 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5565 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5566 ef899790 2022-11-01 thomas if (fd == -1) {
5567 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5568 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5569 ef899790 2022-11-01 thomas ct->ondisk_path);
5570 ef899790 2022-11-01 thomas goto done;
5572 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5573 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5574 ef899790 2022-11-01 thomas if (err)
5575 ef899790 2022-11-01 thomas goto done;
5577 ef899790 2022-11-01 thomas } else {
5578 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5579 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5580 ef899790 2022-11-01 thomas if (fd == -1) {
5581 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5582 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5583 ef899790 2022-11-01 thomas ct->ondisk_path);
5584 ef899790 2022-11-01 thomas goto done;
5586 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5587 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5588 ef899790 2022-11-01 thomas if (err)
5589 ef899790 2022-11-01 thomas goto done;
5592 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5593 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5594 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5595 ef899790 2022-11-01 thomas goto done;
5597 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5598 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5599 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5600 ef899790 2022-11-01 thomas goto done;
5602 ef899790 2022-11-01 thomas fd = -1;
5603 ef899790 2022-11-01 thomas f2_exists = 1;
5606 ef899790 2022-11-01 thomas if (blob1) {
5607 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5608 ef899790 2022-11-01 thomas f1, blob1);
5609 ef899790 2022-11-01 thomas if (err)
5610 ef899790 2022-11-01 thomas goto done;
5613 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5614 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5615 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5617 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5618 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5619 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5620 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5621 ef899790 2022-11-01 thomas if (blob1)
5622 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5623 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5624 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5625 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5626 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5627 ef899790 2022-11-01 thomas return err;
5630 c4296144 2019-05-09 stsp static const struct got_error *
5631 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5632 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5633 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5634 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5636 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5637 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5638 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5639 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5640 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5641 768aea60 2019-05-09 stsp struct stat sb;
5643 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5644 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5645 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5646 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5647 5f8a88c6 2019-08-03 stsp return NULL;
5649 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5650 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5651 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5654 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5655 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5656 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5657 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5658 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5659 5f8a88c6 2019-08-03 stsp return NULL;
5662 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5663 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5664 036813ee 2019-05-09 stsp goto done;
5666 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5667 036813ee 2019-05-09 stsp parent_path = strdup("");
5668 69960a46 2019-05-09 stsp if (parent_path == NULL)
5669 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5671 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5673 69960a46 2019-05-09 stsp return err;
5676 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5677 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5678 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5679 c4296144 2019-05-09 stsp goto done;
5682 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5683 768aea60 2019-05-09 stsp relpath) == -1) {
5684 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5685 768aea60 2019-05-09 stsp goto done;
5688 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5689 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5690 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5691 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5692 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5693 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5694 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5695 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5697 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5698 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5701 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5702 0aeb8099 2020-07-23 stsp goto done;
5704 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5705 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5706 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5707 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5708 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5709 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5710 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5711 12463d8b 2019-12-13 stsp ct->ondisk_path);
5712 12463d8b 2019-12-13 stsp goto done;
5714 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5715 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5716 768aea60 2019-05-09 stsp goto done;
5718 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5721 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5722 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5723 44d03001 2019-05-09 stsp relpath) == -1) {
5724 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5725 44d03001 2019-05-09 stsp goto done;
5728 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5729 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5730 35213c7c 2020-07-23 stsp int is_bad_symlink;
5731 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5732 35213c7c 2020-07-23 stsp ssize_t target_len;
5733 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5734 35213c7c 2020-07-23 stsp sizeof(target_path));
5735 35213c7c 2020-07-23 stsp if (target_len == -1) {
5736 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5737 35213c7c 2020-07-23 stsp ct->ondisk_path);
5738 35213c7c 2020-07-23 stsp goto done;
5740 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5741 ad10f64e 2023-07-19 thomas target_len, ct->ondisk_path, a->worktree->root_path,
5742 ad10f64e 2023-07-19 thomas a->worktree->meta_dir);
5744 35213c7c 2020-07-23 stsp goto done;
5745 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5746 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5747 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5748 35213c7c 2020-07-23 stsp goto done;
5753 cf066bf8 2019-05-09 stsp ct->status = status;
5754 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5755 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5756 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5757 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5758 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5759 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5760 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5761 b416585c 2019-05-13 stsp goto done;
5763 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5764 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5765 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5766 f0b75401 2019-08-03 stsp goto done;
5769 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5770 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5771 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5772 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5773 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5774 036813ee 2019-05-09 stsp goto done;
5777 24519714 2019-05-09 stsp ct->path = strdup(path);
5778 24519714 2019-05-09 stsp if (ct->path == NULL) {
5779 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5780 24519714 2019-05-09 stsp goto done;
5783 720b08bc 2023-08-29 thomas if (a->diff_outfile) {
5784 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5785 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5786 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5787 ef899790 2022-11-01 thomas if (err)
5788 ef899790 2022-11-01 thomas goto done;
5791 720b08bc 2023-08-29 thomas err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5793 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5794 ed175427 2019-05-09 stsp free_commitable(ct);
5795 24519714 2019-05-09 stsp free(parent_path);
5796 036813ee 2019-05-09 stsp free(path);
5797 a129376b 2019-03-28 stsp return err;
5800 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5801 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5802 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5803 036813ee 2019-05-09 stsp struct got_repository *);
5805 ed175427 2019-05-09 stsp static const struct got_error *
5806 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5807 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5808 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5809 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5810 afa376bf 2019-05-09 stsp struct got_repository *repo)
5812 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5813 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5814 036813ee 2019-05-09 stsp char *subpath;
5816 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5817 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5818 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5820 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5822 ed175427 2019-05-09 stsp return err;
5824 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5825 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5826 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5827 036813ee 2019-05-09 stsp free(subpath);
5828 036813ee 2019-05-09 stsp return err;
5831 036813ee 2019-05-09 stsp static const struct got_error *
5832 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5834 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5835 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5837 036813ee 2019-05-09 stsp *match = 0;
5839 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5840 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5841 0f63689d 2019-05-10 stsp return NULL;
5844 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5846 0f63689d 2019-05-10 stsp return err;
5847 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5848 036813ee 2019-05-09 stsp free(ct_parent_path);
5849 036813ee 2019-05-09 stsp return err;
5852 768aea60 2019-05-09 stsp static mode_t
5853 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5855 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5856 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5858 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5861 036813ee 2019-05-09 stsp static const struct got_error *
5862 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5863 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5865 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5867 036813ee 2019-05-09 stsp *new_te = NULL;
5869 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5871 036813ee 2019-05-09 stsp goto done;
5873 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5875 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5876 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5877 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5879 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5881 036813ee 2019-05-09 stsp if (err && *new_te) {
5882 56e0773d 2019-11-28 stsp free(*new_te);
5883 036813ee 2019-05-09 stsp *new_te = NULL;
5885 036813ee 2019-05-09 stsp return err;
5888 036813ee 2019-05-09 stsp static const struct got_error *
5889 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5890 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5892 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5893 102b254e 2020-10-19 stsp char *ct_name = NULL;
5895 036813ee 2019-05-09 stsp *new_te = NULL;
5897 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5898 036813ee 2019-05-09 stsp if (*new_te == NULL)
5899 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5901 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5903 036813ee 2019-05-09 stsp goto done;
5904 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5905 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5906 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5907 036813ee 2019-05-09 stsp goto done;
5910 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5912 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5913 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5914 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5916 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5918 102b254e 2020-10-19 stsp free(ct_name);
5919 036813ee 2019-05-09 stsp if (err && *new_te) {
5920 56e0773d 2019-11-28 stsp free(*new_te);
5921 036813ee 2019-05-09 stsp *new_te = NULL;
5923 036813ee 2019-05-09 stsp return err;
5926 036813ee 2019-05-09 stsp static const struct got_error *
5927 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5928 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5930 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5931 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5933 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5935 036813ee 2019-05-09 stsp return err;
5936 036813ee 2019-05-09 stsp if (new_pe == NULL)
5937 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5938 036813ee 2019-05-09 stsp return NULL;
5941 afa376bf 2019-05-09 stsp static const struct got_error *
5942 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5943 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5945 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5946 5f8a88c6 2019-08-03 stsp unsigned char status;
5948 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5949 ae1e948a 2021-09-28 thomas return NULL;
5951 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5952 afa376bf 2019-05-09 stsp ct_path++;
5954 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5955 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5957 5f8a88c6 2019-08-03 stsp status = ct->status;
5959 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5960 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5963 036813ee 2019-05-09 stsp static const struct got_error *
5964 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5965 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5967 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5968 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5969 44d03001 2019-05-09 stsp char *te_path;
5971 44d03001 2019-05-09 stsp *modified = 0;
5973 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5974 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5975 44d03001 2019-05-09 stsp te->name) == -1)
5976 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5978 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5979 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5980 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5981 44d03001 2019-05-09 stsp strlen(te_path));
5982 62d463ca 2020-10-20 naddy if (*modified)
5986 44d03001 2019-05-09 stsp free(te_path);
5987 44d03001 2019-05-09 stsp return err;
5990 44d03001 2019-05-09 stsp static const struct got_error *
5991 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5992 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5993 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5995 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5996 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5998 036813ee 2019-05-09 stsp *ctp = NULL;
6000 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6001 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6002 036813ee 2019-05-09 stsp char *ct_name = NULL;
6003 036813ee 2019-05-09 stsp int path_matches;
6005 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6006 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
6007 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
6008 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
6009 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6012 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
6013 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
6017 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6020 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6022 036813ee 2019-05-09 stsp return err;
6023 036813ee 2019-05-09 stsp if (!path_matches)
6026 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
6028 d34b633e 2020-10-19 stsp return err;
6030 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
6031 d34b633e 2020-10-19 stsp free(ct_name);
6034 d34b633e 2020-10-19 stsp free(ct_name);
6036 036813ee 2019-05-09 stsp *ctp = ct;
6040 2b496619 2019-07-10 stsp return err;
6043 2b496619 2019-07-10 stsp static const struct got_error *
6044 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6045 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
6046 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
6047 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
6048 2b496619 2019-07-10 stsp struct got_repository *repo)
6050 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
6051 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
6052 2b496619 2019-07-10 stsp char *subtree_path;
6053 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
6054 ba580f68 2020-03-22 stsp int nentries;
6056 2b496619 2019-07-10 stsp *new_tep = NULL;
6058 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6059 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
6060 2b496619 2019-07-10 stsp child_path) == -1)
6061 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
6063 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
6064 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
6065 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
6066 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
6068 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6069 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
6070 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
6071 2b496619 2019-07-10 stsp goto done;
6073 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
6074 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
6075 2b496619 2019-07-10 stsp if (err) {
6076 56e0773d 2019-11-28 stsp free(new_te);
6077 2b496619 2019-07-10 stsp goto done;
6079 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
6082 2b496619 2019-07-10 stsp free(subtree_path);
6083 2b496619 2019-07-10 stsp if (err == NULL)
6084 2b496619 2019-07-10 stsp *new_tep = new_te;
6085 036813ee 2019-05-09 stsp return err;
6088 036813ee 2019-05-09 stsp static const struct got_error *
6089 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
6090 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
6091 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
6092 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6093 036813ee 2019-05-09 stsp struct got_repository *repo)
6095 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
6096 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
6097 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
6098 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
6100 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
6101 ba580f68 2020-03-22 stsp *nentries = 0;
6103 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
6104 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6105 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6106 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
6108 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
6109 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
6110 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
6113 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6114 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
6117 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6118 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
6120 036813ee 2019-05-09 stsp goto done;
6122 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
6123 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
6124 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
6126 036813ee 2019-05-09 stsp goto done;
6127 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
6129 afa376bf 2019-05-09 stsp goto done;
6130 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
6131 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6133 2f51b5b3 2019-05-09 stsp goto done;
6134 ba580f68 2020-03-22 stsp (*nentries)++;
6136 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
6137 2b496619 2019-07-10 stsp if (base_tree == NULL ||
6138 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
6139 2b496619 2019-07-10 stsp == NULL) {
6140 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
6141 2b496619 2019-07-10 stsp child_path, path_base_tree,
6142 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
6145 2b496619 2019-07-10 stsp goto done;
6146 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6148 2b496619 2019-07-10 stsp goto done;
6149 ba580f68 2020-03-22 stsp (*nentries)++;
6154 2f51b5b3 2019-05-09 stsp if (base_tree) {
6155 56e0773d 2019-11-28 stsp int i, nbase_entries;
6156 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
6157 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
6158 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
6159 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
6161 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
6162 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
6163 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
6164 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
6166 63c5ca5d 2019-08-24 stsp goto done;
6167 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
6169 63c5ca5d 2019-08-24 stsp goto done;
6170 ba580f68 2020-03-22 stsp (*nentries)++;
6174 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
6175 44d03001 2019-05-09 stsp int modified;
6176 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6178 036813ee 2019-05-09 stsp goto done;
6179 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
6180 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
6182 2f51b5b3 2019-05-09 stsp goto done;
6183 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
6184 44d03001 2019-05-09 stsp if (modified) {
6185 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
6186 ba580f68 2020-03-22 stsp int nsubentries;
6187 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
6188 ba580f68 2020-03-22 stsp &nsubentries, te,
6189 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
6190 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
6192 44d03001 2019-05-09 stsp goto done;
6193 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6194 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6195 ba580f68 2020-03-22 stsp free(new_id);
6198 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6199 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6200 56e0773d 2019-11-28 stsp free(new_id);
6202 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6204 036813ee 2019-05-09 stsp goto done;
6205 ba580f68 2020-03-22 stsp (*nentries)++;
6209 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6210 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6212 f66c734c 2019-09-22 stsp goto done;
6214 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6215 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6216 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6217 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
6218 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6219 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6220 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6222 2f51b5b3 2019-05-09 stsp goto done;
6223 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6225 2f51b5b3 2019-05-09 stsp goto done;
6226 ba580f68 2020-03-22 stsp (*nentries)++;
6228 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6229 b416585c 2019-05-13 stsp status_arg);
6231 afa376bf 2019-05-09 stsp goto done;
6233 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6234 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6236 2f51b5b3 2019-05-09 stsp goto done;
6237 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6239 2f51b5b3 2019-05-09 stsp goto done;
6240 ba580f68 2020-03-22 stsp (*nentries)++;
6245 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6246 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6248 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6249 2e1fa222 2020-07-23 stsp return err;
6252 2e1fa222 2020-07-23 stsp static const struct got_error *
6253 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6254 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6255 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6256 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6258 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6259 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6260 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6262 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6263 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6264 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6266 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6268 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6269 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6270 437adc9d 2020-12-10 yzhong if (err)
6271 437adc9d 2020-12-10 yzhong goto done;
6274 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6275 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6276 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6277 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6278 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6279 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6280 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6281 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6283 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6284 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6285 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6286 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6287 72fd46fa 2019-09-06 stsp !have_staged_files);
6289 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6290 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6291 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6292 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6293 72fd46fa 2019-09-06 stsp !have_staged_files);
6295 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6297 437adc9d 2020-12-10 yzhong goto done;
6298 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6299 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6300 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6301 3969253a 2020-03-07 stsp if (err) {
6302 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6303 437adc9d 2020-12-10 yzhong goto done;
6305 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6306 3969253a 2020-03-07 stsp if (err) {
6307 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6308 437adc9d 2020-12-10 yzhong goto done;
6311 437adc9d 2020-12-10 yzhong free(relpath);
6312 437adc9d 2020-12-10 yzhong relpath = NULL;
6315 437adc9d 2020-12-10 yzhong free(relpath);
6316 d56d26ce 2019-05-10 stsp return err;
6320 d56d26ce 2019-05-10 stsp static const struct got_error *
6321 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6322 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6323 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6324 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6325 735ef5ac 2019-08-03 stsp int ood_errcode)
6327 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6328 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6329 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6331 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6332 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6333 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6334 1a36436d 2019-06-10 stsp return NULL;
6336 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6337 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6339 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6340 945f9229 2022-04-16 thomas if (err)
6341 945f9229 2022-04-16 thomas goto done;
6342 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6343 9bead371 2019-07-28 stsp if (err) {
6344 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6345 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6346 1a36436d 2019-06-10 stsp goto done;
6347 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6348 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6350 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6351 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6352 945f9229 2022-04-16 thomas if (err)
6353 945f9229 2022-04-16 thomas goto done;
6354 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6355 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6356 1a36436d 2019-06-10 stsp goto done;
6357 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6361 945f9229 2022-04-16 thomas if (commit)
6362 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6363 1a36436d 2019-06-10 stsp return err;
6366 ef20f542 2022-06-26 thomas static const struct got_error *
6367 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6368 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6369 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
6370 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
6371 10604dce 2021-09-24 thomas struct got_worktree *worktree,
6372 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
6373 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6374 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6375 afa376bf 2019-05-09 stsp struct got_repository *repo)
6377 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6378 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6379 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6380 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6381 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6382 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6383 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6384 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6385 10604dce 2021-09-24 thomas int nentries, nparents = 0;
6386 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6387 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6388 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6389 892eab0a 2022-07-22 thomas time_t timestamp;
6391 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6393 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6395 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6397 588edf97 2019-05-10 stsp goto done;
6399 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6401 588edf97 2019-05-10 stsp goto done;
6403 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6404 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
6405 ef899790 2022-11-01 thomas &logmsg, commit_arg);
6410 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6411 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6415 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6416 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6417 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6418 cf066bf8 2019-05-09 stsp char *ondisk_path;
6420 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6421 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6422 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6425 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6426 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6427 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6428 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6431 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6432 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6433 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6434 cf066bf8 2019-05-09 stsp goto done;
6436 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6437 2e1fa222 2020-07-23 stsp free(ondisk_path);
6439 cf066bf8 2019-05-09 stsp goto done;
6442 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6443 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6444 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6446 036813ee 2019-05-09 stsp goto done;
6448 538aebb5 2023-04-22 thomas err = got_object_qid_alloc(&pid, head_commit_id);
6450 de18fc63 2019-05-09 stsp goto done;
6451 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6452 10604dce 2021-09-24 thomas nparents++;
6453 10604dce 2021-09-24 thomas if (parent_id2) {
6454 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6455 10604dce 2021-09-24 thomas if (err)
6456 10604dce 2021-09-24 thomas goto done;
6457 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6458 10604dce 2021-09-24 thomas nparents++;
6460 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6461 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6462 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6463 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6464 33ad4cbe 2019-05-12 jcs free(logmsg);
6466 9d40349a 2019-05-09 stsp goto done;
6468 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6469 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6470 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6471 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6472 09f5bd90 2019-05-09 stsp goto done;
6474 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6475 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6477 09f5bd90 2019-05-09 stsp goto done;
6478 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6480 ebf99748 2019-05-09 stsp goto done;
6481 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6482 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6483 09f5bd90 2019-05-09 stsp goto done;
6485 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6486 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6488 f2c16586 2019-05-09 stsp goto done;
6489 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6491 f2c16586 2019-05-09 stsp goto done;
6493 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6495 09f5bd90 2019-05-09 stsp goto done;
6497 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6499 ebf99748 2019-05-09 stsp goto done;
6501 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6502 588edf97 2019-05-10 stsp if (head_tree)
6503 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6504 588edf97 2019-05-10 stsp if (head_commit)
6505 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6506 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6507 2f17228e 2019-05-12 stsp if (head_ref2) {
6508 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6509 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6510 2f17228e 2019-05-12 stsp err = unlockerr;
6511 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6513 39cd0ff6 2019-07-12 stsp return err;
6516 5c1e53bc 2019-07-28 stsp static const struct got_error *
6517 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6518 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6520 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6521 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6523 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6524 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6525 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6527 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6528 5c1e53bc 2019-07-28 stsp ct_path++;
6530 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6531 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6535 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6536 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6538 5c1e53bc 2019-07-28 stsp return NULL;
6541 f0b75401 2019-08-03 stsp static const struct got_error *
6542 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6544 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6546 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6547 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6548 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6551 f0b75401 2019-08-03 stsp return NULL;
6554 5f8a88c6 2019-08-03 stsp static const struct got_error *
6555 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6556 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6558 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6559 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6561 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6562 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6564 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6565 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6566 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6567 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6568 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6569 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6572 5f8a88c6 2019-08-03 stsp return NULL;
6575 39cd0ff6 2019-07-12 stsp const struct got_error *
6576 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6577 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6578 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6579 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6580 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6581 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6582 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6584 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6585 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6586 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6587 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6588 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6589 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6590 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6591 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6592 ef899790 2022-11-01 thomas char *diff_path = NULL;
6593 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6595 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6597 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6598 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6600 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6602 39cd0ff6 2019-07-12 stsp goto done;
6604 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6606 39cd0ff6 2019-07-12 stsp goto done;
6608 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6610 39cd0ff6 2019-07-12 stsp goto done;
6612 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6614 39cd0ff6 2019-07-12 stsp goto done;
6616 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6617 5f8a88c6 2019-08-03 stsp &have_staged_files);
6618 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6619 5f8a88c6 2019-08-03 stsp goto done;
6620 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6621 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6623 5f8a88c6 2019-08-03 stsp goto done;
6626 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6627 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6628 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6629 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6630 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6631 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6632 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6633 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6634 ef899790 2022-11-01 thomas if (show_diff) {
6635 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6636 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6637 ef899790 2022-11-01 thomas if (err)
6638 ef899790 2022-11-01 thomas goto done;
6639 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6640 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6641 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6642 ef899790 2022-11-01 thomas goto done;
6644 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6645 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6646 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6647 ef899790 2022-11-01 thomas goto done;
6651 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6652 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6653 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6655 5c1e53bc 2019-07-28 stsp goto done;
6658 ef899790 2022-11-01 thomas if (show_diff) {
6659 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6660 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6661 ef899790 2022-11-01 thomas goto done;
6665 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6666 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6667 39cd0ff6 2019-07-12 stsp goto done;
6670 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6671 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6673 5c1e53bc 2019-07-28 stsp goto done;
6676 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6677 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6678 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6680 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6681 f0b75401 2019-08-03 stsp ct_path++;
6682 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6683 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6684 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6686 b50cabdf 2019-07-12 stsp goto done;
6690 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6691 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6692 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6693 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6695 39cd0ff6 2019-07-12 stsp goto done;
6697 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6698 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6699 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6700 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6701 39cd0ff6 2019-07-12 stsp err = sync_err;
6703 39cd0ff6 2019-07-12 stsp if (fileindex)
6704 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6705 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6706 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6707 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6708 39cd0ff6 2019-07-12 stsp err = unlockerr;
6709 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6710 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6712 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6714 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6715 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6716 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6717 ef899790 2022-11-01 thomas free(diff_path);
6718 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6719 ef899790 2022-11-01 thomas err == NULL)
6720 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6721 c4296144 2019-05-09 stsp return err;
6724 8656d6c4 2019-05-20 stsp const char *
6725 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6727 8656d6c4 2019-05-20 stsp return ct->path;
6730 8656d6c4 2019-05-20 stsp unsigned int
6731 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6733 8656d6c4 2019-05-20 stsp return ct->status;
6736 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6737 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6738 818c7501 2019-07-11 stsp struct got_repository *repo;
6741 818c7501 2019-07-11 stsp static const struct got_error *
6742 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6744 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6745 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6746 818c7501 2019-07-11 stsp unsigned char status;
6747 818c7501 2019-07-11 stsp struct stat sb;
6748 818c7501 2019-07-11 stsp char *ondisk_path;
6750 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6751 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6752 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6753 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6755 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6757 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6759 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6760 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6761 818c7501 2019-07-11 stsp free(ondisk_path);
6763 818c7501 2019-07-11 stsp return err;
6765 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6766 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6767 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6768 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6770 818c7501 2019-07-11 stsp return NULL;
6773 818c7501 2019-07-11 stsp const struct got_error *
6774 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6775 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6776 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6777 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6779 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6780 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6781 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6782 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6783 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6784 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6785 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6787 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6788 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6789 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6791 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6793 818c7501 2019-07-11 stsp return err;
6795 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6797 818c7501 2019-07-11 stsp goto done;
6799 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6800 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6801 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6804 818c7501 2019-07-11 stsp goto done;
6806 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6808 818c7501 2019-07-11 stsp goto done;
6810 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6812 818c7501 2019-07-11 stsp goto done;
6814 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6816 818c7501 2019-07-11 stsp goto done;
6818 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6821 e51d7b55 2020-01-04 stsp goto done;
6823 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6825 818c7501 2019-07-11 stsp goto done;
6826 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6827 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6828 e51d7b55 2020-01-04 stsp goto done;
6831 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6832 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6834 818c7501 2019-07-11 stsp goto done;
6835 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6837 818c7501 2019-07-11 stsp goto done;
6839 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6841 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6843 818c7501 2019-07-11 stsp goto done;
6845 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6847 818c7501 2019-07-11 stsp goto done;
6849 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6850 818c7501 2019-07-11 stsp worktree->base_commit_id);
6852 818c7501 2019-07-11 stsp goto done;
6853 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6855 818c7501 2019-07-11 stsp goto done;
6857 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6859 818c7501 2019-07-11 stsp goto done;
6861 818c7501 2019-07-11 stsp free(fileindex_path);
6862 818c7501 2019-07-11 stsp free(tmp_branch_name);
6863 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6864 818c7501 2019-07-11 stsp free(branch_ref_name);
6865 818c7501 2019-07-11 stsp if (branch_ref)
6866 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6867 818c7501 2019-07-11 stsp if (wt_branch)
6868 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6869 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6870 818c7501 2019-07-11 stsp if (err) {
6871 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6872 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6873 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6875 818c7501 2019-07-11 stsp if (*tmp_branch) {
6876 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6877 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6879 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6880 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6881 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6883 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6885 818c7501 2019-07-11 stsp return err;
6888 818c7501 2019-07-11 stsp const struct got_error *
6889 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6890 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6891 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6892 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6894 818c7501 2019-07-11 stsp const struct got_error *err;
6895 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6896 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6897 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6898 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6899 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6901 818c7501 2019-07-11 stsp *commit_id = NULL;
6902 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6903 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6904 3e3a69f1 2019-07-25 stsp *branch = NULL;
6905 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6907 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6909 3e3a69f1 2019-07-25 stsp return err;
6911 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6913 f032f1f7 2019-08-04 stsp goto done;
6915 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6916 f032f1f7 2019-08-04 stsp &have_staged_files);
6917 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6918 f032f1f7 2019-08-04 stsp goto done;
6919 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6920 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6921 3e3a69f1 2019-07-25 stsp goto done;
6924 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6926 f032f1f7 2019-08-04 stsp goto done;
6928 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6930 818c7501 2019-07-11 stsp goto done;
6932 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6934 818c7501 2019-07-11 stsp goto done;
6936 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6938 818c7501 2019-07-11 stsp goto done;
6940 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6942 818c7501 2019-07-11 stsp goto done;
6944 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6945 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6947 818c7501 2019-07-11 stsp goto done;
6949 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6951 818c7501 2019-07-11 stsp goto done;
6953 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6955 818c7501 2019-07-11 stsp goto done;
6957 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6958 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6960 818c7501 2019-07-11 stsp goto done;
6962 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6964 818c7501 2019-07-11 stsp goto done;
6966 818c7501 2019-07-11 stsp free(commit_ref_name);
6967 818c7501 2019-07-11 stsp free(branch_ref_name);
6968 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6969 818c7501 2019-07-11 stsp if (commit_ref)
6970 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6971 818c7501 2019-07-11 stsp if (branch_ref)
6972 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6973 818c7501 2019-07-11 stsp if (err) {
6974 818c7501 2019-07-11 stsp free(*commit_id);
6975 818c7501 2019-07-11 stsp *commit_id = NULL;
6976 818c7501 2019-07-11 stsp if (*tmp_branch) {
6977 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6978 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6980 818c7501 2019-07-11 stsp if (*new_base_branch) {
6981 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6982 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6984 818c7501 2019-07-11 stsp if (*branch) {
6985 818c7501 2019-07-11 stsp got_ref_close(*branch);
6986 818c7501 2019-07-11 stsp *branch = NULL;
6988 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6989 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6990 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6992 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6994 818c7501 2019-07-11 stsp return err;
6997 818c7501 2019-07-11 stsp const struct got_error *
6998 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
7000 818c7501 2019-07-11 stsp const struct got_error *err;
7001 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
7003 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7005 818c7501 2019-07-11 stsp return err;
7007 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7008 818c7501 2019-07-11 stsp free(tmp_branch_name);
7009 818c7501 2019-07-11 stsp return NULL;
7012 818c7501 2019-07-11 stsp static const struct got_error *
7013 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7014 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
7016 0ebf8283 2019-07-24 stsp *logmsg = arg;
7017 818c7501 2019-07-11 stsp return NULL;
7020 818c7501 2019-07-11 stsp static const struct got_error *
7021 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7022 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7023 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7024 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7026 818c7501 2019-07-11 stsp return NULL;
7029 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
7030 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
7031 01757395 2019-07-12 stsp void *progress_arg;
7032 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
7035 01757395 2019-07-12 stsp static const struct got_error *
7036 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
7038 01757395 2019-07-12 stsp const struct got_error *err;
7039 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
7041 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
7043 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
7045 01757395 2019-07-12 stsp return err;
7047 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
7048 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
7049 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
7050 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
7051 01757395 2019-07-12 stsp return NULL;
7053 01757395 2019-07-12 stsp p = strdup(path);
7054 01757395 2019-07-12 stsp if (p == NULL)
7055 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
7057 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7058 01757395 2019-07-12 stsp if (err || new == NULL)
7060 01757395 2019-07-12 stsp return err;
7063 0ebf8283 2019-07-24 stsp static const struct got_error *
7064 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7065 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
7067 818c7501 2019-07-11 stsp const struct got_error *err;
7068 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
7070 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7071 818c7501 2019-07-11 stsp if (err) {
7072 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
7073 818c7501 2019-07-11 stsp goto done;
7074 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7076 818c7501 2019-07-11 stsp goto done;
7077 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
7079 818c7501 2019-07-11 stsp goto done;
7080 de05890f 2020-03-05 stsp } else if (is_rebase) {
7081 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
7084 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
7086 818c7501 2019-07-11 stsp goto done;
7087 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
7088 818c7501 2019-07-11 stsp free(stored_id);
7089 818c7501 2019-07-11 stsp if (cmp != 0) {
7090 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7091 818c7501 2019-07-11 stsp goto done;
7095 0ebf8283 2019-07-24 stsp if (commit_ref)
7096 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7097 0ebf8283 2019-07-24 stsp return err;
7100 0ebf8283 2019-07-24 stsp static const struct got_error *
7101 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
7102 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
7103 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7104 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
7105 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7106 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7108 0ebf8283 2019-07-24 stsp const struct got_error *err;
7109 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7110 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
7111 3e3a69f1 2019-07-25 stsp char *fileindex_path;
7113 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7115 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7117 0ebf8283 2019-07-24 stsp return err;
7119 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
7120 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
7121 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
7122 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
7123 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
7124 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
7125 818c7501 2019-07-11 stsp if (commit_ref)
7126 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
7127 818c7501 2019-07-11 stsp return err;
7130 818c7501 2019-07-11 stsp const struct got_error *
7131 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7132 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7133 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7134 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7135 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7136 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7138 0ebf8283 2019-07-24 stsp const struct got_error *err;
7139 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7141 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7143 0ebf8283 2019-07-24 stsp return err;
7145 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7147 0ebf8283 2019-07-24 stsp goto done;
7149 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7150 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7151 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7153 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7154 0ebf8283 2019-07-24 stsp return err;
7157 0ebf8283 2019-07-24 stsp const struct got_error *
7158 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7159 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7160 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7161 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7162 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7163 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7165 0ebf8283 2019-07-24 stsp const struct got_error *err;
7166 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7168 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7170 0ebf8283 2019-07-24 stsp return err;
7172 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7174 0ebf8283 2019-07-24 stsp goto done;
7176 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7177 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7178 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7180 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7181 0ebf8283 2019-07-24 stsp return err;
7184 0ebf8283 2019-07-24 stsp static const struct got_error *
7185 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
7186 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7187 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7188 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
7189 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
7190 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7192 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7193 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7194 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7195 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7196 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7197 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7198 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7200 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7201 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7202 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7204 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7206 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7208 3e3a69f1 2019-07-25 stsp return err;
7210 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7211 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7212 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7213 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7214 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
7216 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7217 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7219 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7220 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7221 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7222 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7223 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7224 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7226 01757395 2019-07-12 stsp if (merged_paths) {
7227 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7228 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7229 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7230 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7233 01757395 2019-07-12 stsp goto done;
7236 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7237 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7239 01757395 2019-07-12 stsp goto done;
7242 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7243 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7244 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7246 a0e95631 2019-07-12 stsp goto done;
7247 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7248 a0e95631 2019-07-12 stsp goto done;
7251 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7253 edd02c5e 2019-07-11 stsp goto done;
7255 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7257 818c7501 2019-07-11 stsp goto done;
7259 5943eee2 2019-08-13 stsp if (new_logmsg) {
7260 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7261 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7262 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7263 5943eee2 2019-08-13 stsp goto done;
7266 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7268 5943eee2 2019-08-13 stsp goto done;
7271 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7272 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7273 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
7274 8db00f97 2022-07-22 thomas committer ? committer :
7275 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
7276 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7278 a0e95631 2019-07-12 stsp goto done;
7280 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7282 a0e95631 2019-07-12 stsp goto done;
7284 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7286 a0e95631 2019-07-12 stsp goto done;
7288 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7289 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7290 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7291 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7292 a0e95631 2019-07-12 stsp err = sync_err;
7294 a0e95631 2019-07-12 stsp free(fileindex_path);
7295 a0e95631 2019-07-12 stsp free(head_commit_id);
7296 a0e95631 2019-07-12 stsp if (head_ref)
7297 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7298 818c7501 2019-07-11 stsp if (err) {
7299 818c7501 2019-07-11 stsp free(*new_commit_id);
7300 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7302 818c7501 2019-07-11 stsp return err;
7305 818c7501 2019-07-11 stsp const struct got_error *
7306 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7307 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7308 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7309 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7310 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
7311 be94c032 2023-02-20 thomas struct got_repository *repo)
7313 0ebf8283 2019-07-24 stsp const struct got_error *err;
7314 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7315 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7316 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7318 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7320 0ebf8283 2019-07-24 stsp return err;
7322 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7324 0ebf8283 2019-07-24 stsp goto done;
7325 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7327 0ebf8283 2019-07-24 stsp goto done;
7328 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7329 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7330 0ebf8283 2019-07-24 stsp goto done;
7333 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7334 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7335 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
7337 0ebf8283 2019-07-24 stsp if (commit_ref)
7338 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7339 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7340 0ebf8283 2019-07-24 stsp free(commit_id);
7341 0ebf8283 2019-07-24 stsp return err;
7344 0ebf8283 2019-07-24 stsp const struct got_error *
7345 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7346 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7347 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7348 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7349 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7350 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7352 0ebf8283 2019-07-24 stsp const struct got_error *err;
7353 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7354 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7356 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7358 0ebf8283 2019-07-24 stsp return err;
7360 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7362 0ebf8283 2019-07-24 stsp goto done;
7364 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7365 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7366 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
7368 0ebf8283 2019-07-24 stsp if (commit_ref)
7369 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7370 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7371 0ebf8283 2019-07-24 stsp return err;
7374 0ebf8283 2019-07-24 stsp const struct got_error *
7375 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7376 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7378 3e3a69f1 2019-07-25 stsp if (fileindex)
7379 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7380 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7383 69844fba 2019-07-11 stsp static const struct got_error *
7384 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7386 69844fba 2019-07-11 stsp const struct got_error *err;
7387 69844fba 2019-07-11 stsp struct got_reference *ref;
7389 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7390 69844fba 2019-07-11 stsp if (err) {
7391 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7392 69844fba 2019-07-11 stsp return NULL;
7393 69844fba 2019-07-11 stsp return err;
7396 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7397 69844fba 2019-07-11 stsp got_ref_close(ref);
7398 69844fba 2019-07-11 stsp return err;
7401 69844fba 2019-07-11 stsp static const struct got_error *
7402 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7404 69844fba 2019-07-11 stsp const struct got_error *err;
7405 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7406 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7408 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7410 69844fba 2019-07-11 stsp goto done;
7411 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7413 69844fba 2019-07-11 stsp goto done;
7415 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7417 69844fba 2019-07-11 stsp goto done;
7418 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7420 69844fba 2019-07-11 stsp goto done;
7422 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7424 69844fba 2019-07-11 stsp goto done;
7425 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7427 69844fba 2019-07-11 stsp goto done;
7429 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7431 69844fba 2019-07-11 stsp goto done;
7432 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7434 69844fba 2019-07-11 stsp goto done;
7437 69844fba 2019-07-11 stsp free(tmp_branch_name);
7438 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7439 69844fba 2019-07-11 stsp free(branch_ref_name);
7440 69844fba 2019-07-11 stsp free(commit_ref_name);
7441 e600f124 2021-03-21 stsp return err;
7444 ef20f542 2022-06-26 thomas static const struct got_error *
7445 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7446 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7448 e600f124 2021-03-21 stsp const struct got_error *err;
7449 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7450 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7451 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7452 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7453 e600f124 2021-03-21 stsp char *refname = NULL;
7455 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7456 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7457 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7458 e600f124 2021-03-21 stsp branch_name += 11;
7460 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7462 e600f124 2021-03-21 stsp return err;
7464 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7465 e600f124 2021-03-21 stsp new_id_str) == -1) {
7466 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7467 e600f124 2021-03-21 stsp goto done;
7470 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7472 e600f124 2021-03-21 stsp goto done;
7474 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7476 e600f124 2021-03-21 stsp goto done;
7478 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7480 e600f124 2021-03-21 stsp free(new_id_str);
7481 e600f124 2021-03-21 stsp free(refname);
7482 e600f124 2021-03-21 stsp free(old_commit_id);
7484 e600f124 2021-03-21 stsp got_ref_close(ref);
7485 69844fba 2019-07-11 stsp return err;
7488 818c7501 2019-07-11 stsp const struct got_error *
7489 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7490 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7491 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7492 f08f28be 2023-02-03 thomas int create_backup)
7494 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7495 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7496 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7498 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7500 818c7501 2019-07-11 stsp return err;
7502 e600f124 2021-03-21 stsp if (create_backup) {
7503 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7504 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7506 e600f124 2021-03-21 stsp goto done;
7509 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7511 818c7501 2019-07-11 stsp goto done;
7513 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7515 818c7501 2019-07-11 stsp goto done;
7517 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7519 818c7501 2019-07-11 stsp goto done;
7521 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7523 a615e0e7 2020-12-16 stsp goto done;
7525 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7527 a615e0e7 2020-12-16 stsp goto done;
7528 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7529 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7530 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7531 a615e0e7 2020-12-16 stsp err = sync_err;
7533 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7534 a615e0e7 2020-12-16 stsp free(fileindex_path);
7535 818c7501 2019-07-11 stsp free(new_head_commit_id);
7536 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7537 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7538 818c7501 2019-07-11 stsp err = unlockerr;
7539 818c7501 2019-07-11 stsp return err;
7542 8641a332 2023-04-14 thomas static const struct got_error *
7543 8641a332 2023-04-14 thomas get_paths_changed_between_commits(struct got_pathlist_head *paths,
7544 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7545 8641a332 2023-04-14 thomas struct got_repository *repo)
7547 8641a332 2023-04-14 thomas const struct got_error *err;
7548 8641a332 2023-04-14 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7549 8641a332 2023-04-14 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7551 8641a332 2023-04-14 thomas if (id1) {
7552 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit1, repo, id1);
7553 8641a332 2023-04-14 thomas if (err)
7554 8641a332 2023-04-14 thomas goto done;
7556 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree1, repo,
7557 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit1));
7558 8641a332 2023-04-14 thomas if (err)
7559 8641a332 2023-04-14 thomas goto done;
7562 8641a332 2023-04-14 thomas if (id2) {
7563 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit2, repo, id2);
7564 8641a332 2023-04-14 thomas if (err)
7565 8641a332 2023-04-14 thomas goto done;
7567 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree2, repo,
7568 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit2));
7569 8641a332 2023-04-14 thomas if (err)
7570 8641a332 2023-04-14 thomas goto done;
7573 8641a332 2023-04-14 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7574 8641a332 2023-04-14 thomas got_diff_tree_collect_changed_paths, paths, 0);
7575 8641a332 2023-04-14 thomas if (err)
7576 8641a332 2023-04-14 thomas goto done;
7578 8641a332 2023-04-14 thomas if (commit1)
7579 8641a332 2023-04-14 thomas got_object_commit_close(commit1);
7580 8641a332 2023-04-14 thomas if (commit2)
7581 8641a332 2023-04-14 thomas got_object_commit_close(commit2);
7582 8641a332 2023-04-14 thomas if (tree1)
7583 8641a332 2023-04-14 thomas got_object_tree_close(tree1);
7584 8641a332 2023-04-14 thomas if (tree2)
7585 8641a332 2023-04-14 thomas got_object_tree_close(tree2);
7586 8641a332 2023-04-14 thomas return err;
7589 8641a332 2023-04-14 thomas static const struct got_error *
7590 8641a332 2023-04-14 thomas get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7591 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7592 8641a332 2023-04-14 thomas const char *path_prefix, struct got_repository *repo)
7594 8641a332 2023-04-14 thomas const struct got_error *err;
7595 8641a332 2023-04-14 thomas struct got_pathlist_head merged_paths;
7596 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
7597 8641a332 2023-04-14 thomas char *abspath = NULL, *wt_path = NULL;
7599 8641a332 2023-04-14 thomas TAILQ_INIT(&merged_paths);
7601 8641a332 2023-04-14 thomas err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7602 8641a332 2023-04-14 thomas if (err)
7603 8641a332 2023-04-14 thomas goto done;
7605 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, &merged_paths, entry) {
7606 8641a332 2023-04-14 thomas struct got_diff_changed_path *change = pe->data;
7608 8641a332 2023-04-14 thomas if (change->status != GOT_STATUS_ADD)
7609 8641a332 2023-04-14 thomas continue;
7611 8641a332 2023-04-14 thomas if (got_path_is_root_dir(path_prefix)) {
7612 8641a332 2023-04-14 thomas wt_path = strdup(pe->path);
7613 8641a332 2023-04-14 thomas if (wt_path == NULL) {
7614 8641a332 2023-04-14 thomas err = got_error_from_errno("strdup");
7615 8641a332 2023-04-14 thomas goto done;
7617 8641a332 2023-04-14 thomas } else {
7618 8641a332 2023-04-14 thomas if (asprintf(&abspath, "/%s", pe->path) == -1) {
7619 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
7620 8641a332 2023-04-14 thomas goto done;
7623 8641a332 2023-04-14 thomas err = got_path_skip_common_ancestor(&wt_path,
7624 8641a332 2023-04-14 thomas path_prefix, abspath);
7625 8641a332 2023-04-14 thomas if (err)
7626 8641a332 2023-04-14 thomas goto done;
7627 8641a332 2023-04-14 thomas free(abspath);
7628 8641a332 2023-04-14 thomas abspath = NULL;
7631 8641a332 2023-04-14 thomas err = got_pathlist_append(added_paths, wt_path, NULL);
7632 8641a332 2023-04-14 thomas if (err)
7633 8641a332 2023-04-14 thomas goto done;
7634 8641a332 2023-04-14 thomas wt_path = NULL;
7638 8641a332 2023-04-14 thomas got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7639 8641a332 2023-04-14 thomas free(abspath);
7640 8641a332 2023-04-14 thomas free(wt_path);
7641 8641a332 2023-04-14 thomas return err;
7644 8641a332 2023-04-14 thomas static const struct got_error *
7645 8641a332 2023-04-14 thomas get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7646 8641a332 2023-04-14 thomas struct got_object_id *id, const char *path_prefix,
7647 8641a332 2023-04-14 thomas struct got_repository *repo)
7649 8641a332 2023-04-14 thomas const struct got_error *err;
7650 8641a332 2023-04-14 thomas struct got_commit_object *commit = NULL;
7651 8641a332 2023-04-14 thomas struct got_object_qid *pid;
7653 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo, id);
7654 8641a332 2023-04-14 thomas if (err)
7655 8641a332 2023-04-14 thomas goto done;
7657 8641a332 2023-04-14 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7659 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(added_paths,
7660 8641a332 2023-04-14 thomas pid ? &pid->id : NULL, id, path_prefix, repo);
7661 8641a332 2023-04-14 thomas if (err)
7662 8641a332 2023-04-14 thomas goto done;
7664 8641a332 2023-04-14 thomas if (commit)
7665 8641a332 2023-04-14 thomas got_object_commit_close(commit);
7666 8641a332 2023-04-14 thomas return err;
7669 818c7501 2019-07-11 stsp const struct got_error *
7670 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7671 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7672 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7673 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7675 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7676 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7677 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7678 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7679 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7680 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7681 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7682 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7683 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7684 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7685 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7687 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7689 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7691 818c7501 2019-07-11 stsp return err;
7693 8641a332 2023-04-14 thomas err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7694 8641a332 2023-04-14 thomas if (err)
7695 8641a332 2023-04-14 thomas goto done;
7697 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7698 8641a332 2023-04-14 thomas if (err)
7699 8641a332 2023-04-14 thomas goto done;
7701 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7702 8641a332 2023-04-14 thomas if (err)
7703 8641a332 2023-04-14 thomas goto done;
7706 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7707 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7708 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
7709 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
7711 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7712 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7713 8641a332 2023-04-14 thomas if (err)
7714 8641a332 2023-04-14 thomas goto done;
7716 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7717 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7719 818c7501 2019-07-11 stsp goto done;
7721 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7723 818c7501 2019-07-11 stsp goto done;
7726 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7727 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7728 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7730 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7732 818c7501 2019-07-11 stsp goto done;
7734 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7735 25a102ed 2023-04-14 thomas if (err)
7736 25a102ed 2023-04-14 thomas goto done;
7738 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7739 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7741 818c7501 2019-07-11 stsp goto done;
7743 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7744 945f9229 2022-04-16 thomas worktree->path_prefix);
7746 ca355955 2019-07-12 stsp goto done;
7748 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7750 ca355955 2019-07-12 stsp goto done;
7752 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7754 818c7501 2019-07-11 stsp goto done;
7756 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7757 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7758 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7759 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7760 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7761 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7762 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7763 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
7764 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
7765 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7766 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7768 1f1abb7e 2019-08-08 stsp goto sync;
7770 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7771 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7773 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7774 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7775 ca355955 2019-07-12 stsp err = sync_err;
7777 8641a332 2023-04-14 thomas got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7778 818c7501 2019-07-11 stsp got_ref_close(resolved);
7779 a3a2faf2 2019-07-12 stsp free(tree_id);
7780 818c7501 2019-07-11 stsp free(commit_id);
7781 8641a332 2023-04-14 thomas free(merged_commit_id);
7782 945f9229 2022-04-16 thomas if (commit)
7783 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7784 0ebf8283 2019-07-24 stsp if (fileindex)
7785 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7786 0ebf8283 2019-07-24 stsp free(fileindex_path);
7787 8641a332 2023-04-14 thomas free(commit_ref_name);
7788 8641a332 2023-04-14 thomas if (commit_ref)
7789 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
7791 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7792 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7793 0ebf8283 2019-07-24 stsp err = unlockerr;
7794 0ebf8283 2019-07-24 stsp return err;
7797 0ebf8283 2019-07-24 stsp const struct got_error *
7798 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7799 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7800 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7801 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7803 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7804 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7805 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7806 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7807 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7808 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7809 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7810 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7812 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7813 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7814 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7815 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7817 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7819 0ebf8283 2019-07-24 stsp return err;
7821 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7823 0ebf8283 2019-07-24 stsp goto done;
7825 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7826 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7827 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7830 0ebf8283 2019-07-24 stsp goto done;
7832 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7834 0ebf8283 2019-07-24 stsp goto done;
7836 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7838 0ebf8283 2019-07-24 stsp goto done;
7840 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7841 0ebf8283 2019-07-24 stsp worktree);
7843 0ebf8283 2019-07-24 stsp goto done;
7845 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7848 0ebf8283 2019-07-24 stsp goto done;
7850 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7852 0ebf8283 2019-07-24 stsp goto done;
7854 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7856 0ebf8283 2019-07-24 stsp goto done;
7858 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7859 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7861 0ebf8283 2019-07-24 stsp goto done;
7862 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7864 0ebf8283 2019-07-24 stsp goto done;
7865 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7866 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7867 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7868 0ebf8283 2019-07-24 stsp goto done;
7871 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7872 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7874 0ebf8283 2019-07-24 stsp goto done;
7875 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7877 0ebf8283 2019-07-24 stsp goto done;
7879 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7881 0ebf8283 2019-07-24 stsp goto done;
7883 0ebf8283 2019-07-24 stsp free(fileindex_path);
7884 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7885 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7886 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7887 0ebf8283 2019-07-24 stsp if (wt_branch)
7888 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7889 0ebf8283 2019-07-24 stsp if (err) {
7890 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7891 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7892 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7894 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7895 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7896 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7898 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7899 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7900 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7901 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7903 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7905 0ebf8283 2019-07-24 stsp return err;
7908 0ebf8283 2019-07-24 stsp const struct got_error *
7909 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7910 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7912 3e3a69f1 2019-07-25 stsp if (fileindex)
7913 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7914 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7917 0ebf8283 2019-07-24 stsp const struct got_error *
7918 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7919 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7921 0ebf8283 2019-07-24 stsp const struct got_error *err;
7922 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7924 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7926 0ebf8283 2019-07-24 stsp return err;
7928 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7929 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7930 0ebf8283 2019-07-24 stsp return NULL;
7933 0ebf8283 2019-07-24 stsp const struct got_error *
7934 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7935 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7936 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7937 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7939 0ebf8283 2019-07-24 stsp const struct got_error *err;
7940 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7941 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7942 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7943 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7944 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7945 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7947 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7948 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7949 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7950 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7952 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7954 3e3a69f1 2019-07-25 stsp return err;
7956 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7958 f032f1f7 2019-08-04 stsp goto done;
7960 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7961 f032f1f7 2019-08-04 stsp &have_staged_files);
7962 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7963 f032f1f7 2019-08-04 stsp goto done;
7964 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7965 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7966 3e3a69f1 2019-07-25 stsp goto done;
7969 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7971 f032f1f7 2019-08-04 stsp goto done;
7973 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7975 0ebf8283 2019-07-24 stsp goto done;
7977 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7979 0ebf8283 2019-07-24 stsp goto done;
7981 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7982 0ebf8283 2019-07-24 stsp worktree);
7984 0ebf8283 2019-07-24 stsp goto done;
7986 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7988 0ebf8283 2019-07-24 stsp goto done;
7990 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7992 0ebf8283 2019-07-24 stsp goto done;
7993 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7995 0ebf8283 2019-07-24 stsp goto done;
7997 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7999 0ebf8283 2019-07-24 stsp goto done;
8000 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
8002 0ebf8283 2019-07-24 stsp goto done;
8004 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
8006 0ebf8283 2019-07-24 stsp goto done;
8008 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8009 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8010 3e3a69f1 2019-07-25 stsp free(fileindex_path);
8011 0ebf8283 2019-07-24 stsp if (commit_ref)
8012 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
8013 0ebf8283 2019-07-24 stsp if (base_commit_ref)
8014 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
8015 0ebf8283 2019-07-24 stsp if (err) {
8016 0ebf8283 2019-07-24 stsp free(*commit_id);
8017 0ebf8283 2019-07-24 stsp *commit_id = NULL;
8018 0ebf8283 2019-07-24 stsp free(*base_commit_id);
8019 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
8020 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
8021 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
8022 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
8024 3e3a69f1 2019-07-25 stsp if (*fileindex) {
8025 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
8026 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
8028 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
8030 0ebf8283 2019-07-24 stsp return err;
8033 0ebf8283 2019-07-24 stsp static const struct got_error *
8034 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8036 0ebf8283 2019-07-24 stsp const struct got_error *err;
8037 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8038 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
8040 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8042 0ebf8283 2019-07-24 stsp goto done;
8043 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
8045 0ebf8283 2019-07-24 stsp goto done;
8047 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8048 0ebf8283 2019-07-24 stsp worktree);
8050 0ebf8283 2019-07-24 stsp goto done;
8051 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
8053 0ebf8283 2019-07-24 stsp goto done;
8055 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8057 0ebf8283 2019-07-24 stsp goto done;
8058 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
8060 0ebf8283 2019-07-24 stsp goto done;
8062 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8064 0ebf8283 2019-07-24 stsp goto done;
8065 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8067 0ebf8283 2019-07-24 stsp goto done;
8069 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
8070 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
8071 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8072 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8073 0ebf8283 2019-07-24 stsp return err;
8076 0ebf8283 2019-07-24 stsp const struct got_error *
8077 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
8078 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8079 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
8080 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8082 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8083 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8084 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
8085 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
8086 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8087 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
8088 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
8089 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
8090 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
8091 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
8093 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
8095 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
8097 0ebf8283 2019-07-24 stsp return err;
8099 8641a332 2023-04-14 thomas err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8100 8641a332 2023-04-14 thomas if (err)
8101 8641a332 2023-04-14 thomas goto done;
8103 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8104 8641a332 2023-04-14 thomas if (err) {
8105 8641a332 2023-04-14 thomas if (err->code != GOT_ERR_NOT_REF)
8106 8641a332 2023-04-14 thomas goto done;
8107 8641a332 2023-04-14 thomas /* Can happen on early abort due to invalid histedit script. */
8108 8641a332 2023-04-14 thomas commit_ref = NULL;
8111 8641a332 2023-04-14 thomas if (commit_ref) {
8112 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8113 8641a332 2023-04-14 thomas if (err)
8114 8641a332 2023-04-14 thomas goto done;
8117 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
8118 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
8119 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files added by the
8120 8641a332 2023-04-14 thomas * user during conflict resolution or during histedit -e.
8122 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8123 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
8124 8641a332 2023-04-14 thomas if (err)
8125 8641a332 2023-04-14 thomas goto done;
8128 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8129 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
8131 0ebf8283 2019-07-24 stsp goto done;
8133 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8135 0ebf8283 2019-07-24 stsp goto done;
8137 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8139 0ebf8283 2019-07-24 stsp goto done;
8141 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
8142 25a102ed 2023-04-14 thomas worktree->base_commit_id);
8143 25a102ed 2023-04-14 thomas if (err)
8144 25a102ed 2023-04-14 thomas goto done;
8146 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8147 0ebf8283 2019-07-24 stsp worktree->path_prefix);
8149 0ebf8283 2019-07-24 stsp goto done;
8151 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8153 0ebf8283 2019-07-24 stsp goto done;
8155 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
8157 0ebf8283 2019-07-24 stsp goto done;
8159 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
8160 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
8161 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
8162 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
8163 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
8164 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
8165 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
8166 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
8167 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8168 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8169 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8171 1f1abb7e 2019-08-08 stsp goto sync;
8173 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8174 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8176 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8177 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
8178 0ebf8283 2019-07-24 stsp err = sync_err;
8180 8641a332 2023-04-14 thomas if (resolved)
8181 8641a332 2023-04-14 thomas got_ref_close(resolved);
8182 8641a332 2023-04-14 thomas if (commit_ref)
8183 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8184 8641a332 2023-04-14 thomas free(merged_commit_id);
8185 0ebf8283 2019-07-24 stsp free(tree_id);
8186 818c7501 2019-07-11 stsp free(fileindex_path);
8187 8641a332 2023-04-14 thomas free(commit_ref_name);
8189 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8190 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
8191 818c7501 2019-07-11 stsp err = unlockerr;
8192 818c7501 2019-07-11 stsp return err;
8195 0ebf8283 2019-07-24 stsp const struct got_error *
8196 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8197 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8198 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8200 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8201 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8202 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8203 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8205 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8207 0ebf8283 2019-07-24 stsp return err;
8209 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8210 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8212 e600f124 2021-03-21 stsp goto done;
8214 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8215 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8217 0ebf8283 2019-07-24 stsp goto done;
8219 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8221 0ebf8283 2019-07-24 stsp goto done;
8223 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
8225 0ebf8283 2019-07-24 stsp goto done;
8227 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8229 0ebf8283 2019-07-24 stsp goto done;
8231 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8233 a615e0e7 2020-12-16 stsp goto done;
8235 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8237 a615e0e7 2020-12-16 stsp goto done;
8238 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8239 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8240 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8241 a615e0e7 2020-12-16 stsp err = sync_err;
8243 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8244 a615e0e7 2020-12-16 stsp free(fileindex_path);
8245 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8246 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8247 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8248 0ebf8283 2019-07-24 stsp err = unlockerr;
8249 0ebf8283 2019-07-24 stsp return err;
8252 0ebf8283 2019-07-24 stsp const struct got_error *
8253 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8254 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8256 0ebf8283 2019-07-24 stsp const struct got_error *err;
8257 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8259 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8261 0ebf8283 2019-07-24 stsp return err;
8263 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8265 0ebf8283 2019-07-24 stsp goto done;
8267 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8269 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8270 2822a352 2019-10-15 stsp return err;
8273 2822a352 2019-10-15 stsp const struct got_error *
8274 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8275 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8276 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8277 2822a352 2019-10-15 stsp struct got_repository *repo)
8279 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8280 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8281 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8283 2822a352 2019-10-15 stsp *fileindex = NULL;
8284 2822a352 2019-10-15 stsp *branch_ref = NULL;
8285 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8287 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8289 2822a352 2019-10-15 stsp return err;
8291 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8292 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8293 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8294 2822a352 2019-10-15 stsp "update -b or different branch name required");
8295 2822a352 2019-10-15 stsp goto done;
8298 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8300 2822a352 2019-10-15 stsp goto done;
8302 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8303 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8304 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8305 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8308 2822a352 2019-10-15 stsp goto done;
8310 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8312 2822a352 2019-10-15 stsp goto done;
8314 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8315 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8317 2822a352 2019-10-15 stsp if (err) {
8318 2822a352 2019-10-15 stsp if (*branch_ref) {
8319 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8320 2822a352 2019-10-15 stsp *branch_ref = NULL;
8322 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8323 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8324 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8326 2822a352 2019-10-15 stsp if (*fileindex) {
8327 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8328 2822a352 2019-10-15 stsp *fileindex = NULL;
8330 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8332 0cb83759 2019-08-03 stsp return err;
8335 2822a352 2019-10-15 stsp const struct got_error *
8336 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8337 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8338 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8339 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8340 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8342 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8343 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8344 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8345 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8347 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8349 2822a352 2019-10-15 stsp goto done;
8351 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8353 2822a352 2019-10-15 stsp goto done;
8355 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
8356 945f9229 2022-04-16 thomas if (err)
8357 945f9229 2022-04-16 thomas goto done;
8359 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8360 2822a352 2019-10-15 stsp worktree->path_prefix);
8362 2822a352 2019-10-15 stsp goto done;
8364 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8366 2822a352 2019-10-15 stsp goto done;
8368 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8369 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8371 2822a352 2019-10-15 stsp goto sync;
8373 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8375 2822a352 2019-10-15 stsp goto sync;
8377 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8379 6e210706 2021-01-22 stsp goto sync;
8381 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8383 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8384 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8385 2822a352 2019-10-15 stsp err = sync_err;
8388 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8389 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8390 2822a352 2019-10-15 stsp err = unlockerr;
8391 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8393 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8394 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8395 2822a352 2019-10-15 stsp err = unlockerr;
8396 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8398 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8399 2822a352 2019-10-15 stsp free(fileindex_path);
8400 2822a352 2019-10-15 stsp free(tree_id);
8401 945f9229 2022-04-16 thomas if (commit)
8402 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8404 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8405 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8406 2822a352 2019-10-15 stsp err = unlockerr;
8407 2822a352 2019-10-15 stsp return err;
8410 2822a352 2019-10-15 stsp const struct got_error *
8411 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8412 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8413 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8415 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8417 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8419 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8421 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8422 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8423 8b692cd0 2019-10-21 stsp err = unlockerr;
8424 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8426 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8427 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8428 8b692cd0 2019-10-21 stsp err = unlockerr;
8429 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8431 10604dce 2021-09-24 thomas return err;
8434 10604dce 2021-09-24 thomas const struct got_error *
8435 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
8436 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
8438 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
8439 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8441 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8442 10604dce 2021-09-24 thomas if (err)
8443 10604dce 2021-09-24 thomas goto done;
8445 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8447 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
8448 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8449 10604dce 2021-09-24 thomas err = sync_err;
8451 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8452 10604dce 2021-09-24 thomas free(fileindex_path);
8453 8b692cd0 2019-10-21 stsp return err;
8456 10604dce 2021-09-24 thomas static const struct got_error *
8457 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8459 10604dce 2021-09-24 thomas const struct got_error *err;
8460 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8462 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8463 10604dce 2021-09-24 thomas if (err)
8464 10604dce 2021-09-24 thomas goto done;
8465 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
8466 10604dce 2021-09-24 thomas if (err)
8467 10604dce 2021-09-24 thomas goto done;
8469 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8470 10604dce 2021-09-24 thomas if (err)
8471 10604dce 2021-09-24 thomas goto done;
8472 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
8473 10604dce 2021-09-24 thomas if (err)
8474 10604dce 2021-09-24 thomas goto done;
8477 10604dce 2021-09-24 thomas free(branch_refname);
8478 10604dce 2021-09-24 thomas free(commit_refname);
8479 10604dce 2021-09-24 thomas return err;
8482 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
8483 10604dce 2021-09-24 thomas struct got_worktree *worktree;
8484 10604dce 2021-09-24 thomas const char *branch_name;
8487 10604dce 2021-09-24 thomas static const struct got_error *
8488 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8489 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
8491 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
8493 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8494 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
8495 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
8497 10604dce 2021-09-24 thomas return NULL;
8501 10604dce 2021-09-24 thomas const struct got_error *
8502 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
8503 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
8504 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
8505 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
8506 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8507 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8509 10604dce 2021-09-24 thomas const struct got_error *err;
8510 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8511 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args cma;
8513 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8514 10604dce 2021-09-24 thomas if (err)
8515 10604dce 2021-09-24 thomas goto done;
8517 6ebb2263 2023-07-26 thomas cma.worktree = worktree;
8518 6ebb2263 2023-07-26 thomas cma.cancel_cb = cancel_cb;
8519 6ebb2263 2023-07-26 thomas cma.cancel_arg = cancel_arg;
8521 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8523 10604dce 2021-09-24 thomas if (err)
8524 10604dce 2021-09-24 thomas goto done;
8526 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8527 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
8528 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
8530 10604dce 2021-09-24 thomas free(fileindex_path);
8531 10604dce 2021-09-24 thomas return err;
8534 10604dce 2021-09-24 thomas const struct got_error *
8535 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
8536 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
8537 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
8538 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
8539 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
8540 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
8543 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
8544 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
8545 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
8546 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
8547 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
8548 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
8549 10604dce 2021-09-24 thomas int have_staged_files = 0;
8550 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
8551 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8553 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
8554 10604dce 2021-09-24 thomas *new_commit_id = NULL;
8556 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
8558 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8559 10604dce 2021-09-24 thomas if (err)
8560 10604dce 2021-09-24 thomas goto done;
8562 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8563 10604dce 2021-09-24 thomas if (err)
8564 10604dce 2021-09-24 thomas goto done;
8566 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
8567 10604dce 2021-09-24 thomas if (err)
8568 10604dce 2021-09-24 thomas goto done;
8570 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8571 10604dce 2021-09-24 thomas &have_staged_files);
8572 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8573 10604dce 2021-09-24 thomas goto done;
8574 10604dce 2021-09-24 thomas if (have_staged_files) {
8575 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8576 10604dce 2021-09-24 thomas goto done;
8579 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
8580 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
8581 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
8582 10604dce 2021-09-24 thomas cc_arg.repo = repo;
8583 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
8584 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8585 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
8586 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8587 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8588 10604dce 2021-09-24 thomas if (err)
8589 10604dce 2021-09-24 thomas goto done;
8591 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
8592 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
8593 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
8594 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
8595 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8596 10604dce 2021-09-24 thomas if (err)
8597 10604dce 2021-09-24 thomas goto done;
8599 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
8600 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
8601 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8602 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8603 10604dce 2021-09-24 thomas err = sync_err;
8605 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8606 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8608 10604dce 2021-09-24 thomas free_commitable(ct);
8610 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8611 10604dce 2021-09-24 thomas free(fileindex_path);
8612 10604dce 2021-09-24 thomas return err;
8615 10604dce 2021-09-24 thomas const struct got_error *
8616 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8617 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8619 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8620 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8622 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8623 10604dce 2021-09-24 thomas if (err)
8624 10604dce 2021-09-24 thomas goto done;
8626 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8627 10604dce 2021-09-24 thomas if (err)
8628 10604dce 2021-09-24 thomas goto done;
8629 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8630 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8631 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8632 10604dce 2021-09-24 thomas err = sync_err;
8634 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8635 10604dce 2021-09-24 thomas free(fileindex_path);
8636 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8637 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8638 10604dce 2021-09-24 thomas err = unlockerr;
8639 10604dce 2021-09-24 thomas return err;
8642 10604dce 2021-09-24 thomas const struct got_error *
8643 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8644 10604dce 2021-09-24 thomas struct got_repository *repo)
8646 10604dce 2021-09-24 thomas const struct got_error *err;
8647 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8648 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8650 10604dce 2021-09-24 thomas *in_progress = 0;
8652 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8653 10604dce 2021-09-24 thomas if (err)
8654 10604dce 2021-09-24 thomas return err;
8655 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8656 dfe86d1f 2021-09-24 thomas free(branch_refname);
8657 10604dce 2021-09-24 thomas if (err) {
8658 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8659 10604dce 2021-09-24 thomas return err;
8661 10604dce 2021-09-24 thomas *in_progress = 1;
8663 10604dce 2021-09-24 thomas return NULL;
8666 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8667 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8668 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8670 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8671 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8672 2b72f32d 2023-06-22 thomas struct got_reference *wt_branch = NULL;
8673 2b72f32d 2023-06-22 thomas struct got_object_id *wt_branch_tip = NULL;
8674 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8676 10604dce 2021-09-24 thomas *fileindex = NULL;
8678 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8679 10604dce 2021-09-24 thomas if (err)
8680 10604dce 2021-09-24 thomas return err;
8682 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8683 10604dce 2021-09-24 thomas if (err)
8684 10604dce 2021-09-24 thomas goto done;
8686 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8687 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8688 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8689 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8690 10604dce 2021-09-24 thomas &ok_arg);
8691 10604dce 2021-09-24 thomas if (err)
8692 10604dce 2021-09-24 thomas goto done;
8694 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8696 10604dce 2021-09-24 thomas if (err)
8697 10604dce 2021-09-24 thomas goto done;
8699 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8700 10604dce 2021-09-24 thomas if (err)
8701 10604dce 2021-09-24 thomas goto done;
8703 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8704 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8705 10604dce 2021-09-24 thomas goto done;
8709 2b72f32d 2023-06-22 thomas free(fileindex_path);
8710 2b72f32d 2023-06-22 thomas if (wt_branch)
8711 2b72f32d 2023-06-22 thomas got_ref_close(wt_branch);
8712 2b72f32d 2023-06-22 thomas free(wt_branch_tip);
8713 2b72f32d 2023-06-22 thomas if (err) {
8714 2b72f32d 2023-06-22 thomas if (*fileindex) {
8715 2b72f32d 2023-06-22 thomas got_fileindex_free(*fileindex);
8716 2b72f32d 2023-06-22 thomas *fileindex = NULL;
8718 2b72f32d 2023-06-22 thomas lock_worktree(worktree, LOCK_SH);
8720 2b72f32d 2023-06-22 thomas return err;
8723 2b72f32d 2023-06-22 thomas const struct got_error *got_worktree_merge_write_refs(
8724 2b72f32d 2023-06-22 thomas struct got_worktree *worktree, struct got_reference *branch,
8725 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8727 2b72f32d 2023-06-22 thomas const struct got_error *err = NULL;
8728 2b72f32d 2023-06-22 thomas char *branch_refname = NULL, *commit_refname = NULL;
8729 2b72f32d 2023-06-22 thomas struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8730 2b72f32d 2023-06-22 thomas struct got_object_id *branch_tip = NULL;
8732 2b72f32d 2023-06-22 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8733 2b72f32d 2023-06-22 thomas if (err)
8734 2b72f32d 2023-06-22 thomas return err;
8736 2b72f32d 2023-06-22 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8737 2b72f32d 2023-06-22 thomas if (err)
8738 2b72f32d 2023-06-22 thomas return err;
8740 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8741 10604dce 2021-09-24 thomas if (err)
8742 10604dce 2021-09-24 thomas goto done;
8744 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8745 10604dce 2021-09-24 thomas if (err)
8746 10604dce 2021-09-24 thomas goto done;
8747 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8748 10604dce 2021-09-24 thomas if (err)
8749 10604dce 2021-09-24 thomas goto done;
8751 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8752 10604dce 2021-09-24 thomas if (err)
8753 10604dce 2021-09-24 thomas goto done;
8754 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8755 10604dce 2021-09-24 thomas if (err)
8756 10604dce 2021-09-24 thomas goto done;
8759 10604dce 2021-09-24 thomas free(branch_refname);
8760 10604dce 2021-09-24 thomas free(commit_refname);
8761 10604dce 2021-09-24 thomas if (branch_ref)
8762 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8763 10604dce 2021-09-24 thomas if (commit_ref)
8764 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8765 2b72f32d 2023-06-22 thomas free(branch_tip);
8766 10604dce 2021-09-24 thomas return err;
8769 10604dce 2021-09-24 thomas const struct got_error *
8770 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8771 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8772 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8774 10604dce 2021-09-24 thomas const struct got_error *err;
8775 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8776 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8777 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8778 10604dce 2021-09-24 thomas int have_staged_files = 0;
8780 10604dce 2021-09-24 thomas *branch_name = NULL;
8781 10604dce 2021-09-24 thomas *branch_tip = NULL;
8782 10604dce 2021-09-24 thomas *fileindex = NULL;
8784 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8785 10604dce 2021-09-24 thomas if (err)
8786 10604dce 2021-09-24 thomas return err;
8788 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8789 10604dce 2021-09-24 thomas if (err)
8790 10604dce 2021-09-24 thomas goto done;
8792 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8793 10604dce 2021-09-24 thomas &have_staged_files);
8794 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8795 10604dce 2021-09-24 thomas goto done;
8796 10604dce 2021-09-24 thomas if (have_staged_files) {
8797 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8798 10604dce 2021-09-24 thomas goto done;
8801 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8802 10604dce 2021-09-24 thomas if (err)
8803 10604dce 2021-09-24 thomas goto done;
8805 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8806 10604dce 2021-09-24 thomas if (err)
8807 10604dce 2021-09-24 thomas goto done;
8809 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8810 10604dce 2021-09-24 thomas if (err)
8811 10604dce 2021-09-24 thomas goto done;
8813 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8814 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8815 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8816 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8817 10604dce 2021-09-24 thomas goto done;
8819 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8820 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8821 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8822 10604dce 2021-09-24 thomas goto done;
8825 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8826 10604dce 2021-09-24 thomas if (err)
8827 10604dce 2021-09-24 thomas goto done;
8829 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8830 10604dce 2021-09-24 thomas if (err)
8831 10604dce 2021-09-24 thomas goto done;
8833 10604dce 2021-09-24 thomas free(commit_refname);
8834 10604dce 2021-09-24 thomas free(branch_refname);
8835 10604dce 2021-09-24 thomas free(fileindex_path);
8836 10604dce 2021-09-24 thomas if (commit_ref)
8837 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8838 10604dce 2021-09-24 thomas if (branch_ref)
8839 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8840 10604dce 2021-09-24 thomas if (err) {
8841 10604dce 2021-09-24 thomas if (*branch_name) {
8842 10604dce 2021-09-24 thomas free(*branch_name);
8843 10604dce 2021-09-24 thomas *branch_name = NULL;
8845 10604dce 2021-09-24 thomas free(*branch_tip);
8846 10604dce 2021-09-24 thomas *branch_tip = NULL;
8847 10604dce 2021-09-24 thomas if (*fileindex) {
8848 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8849 10604dce 2021-09-24 thomas *fileindex = NULL;
8851 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8853 10604dce 2021-09-24 thomas return err;
8856 10604dce 2021-09-24 thomas const struct got_error *
8857 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8858 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8859 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8861 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8862 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8863 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8864 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8865 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
8866 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
8867 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
8868 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8869 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
8871 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
8873 8641a332 2023-04-14 thomas err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8874 8641a332 2023-04-14 thomas if (err)
8875 8641a332 2023-04-14 thomas goto done;
8877 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8878 8641a332 2023-04-14 thomas if (err)
8879 8641a332 2023-04-14 thomas goto done;
8881 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8882 8641a332 2023-04-14 thomas if (err)
8883 8641a332 2023-04-14 thomas goto done;
8886 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
8887 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
8888 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
8889 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
8891 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(&added_paths,
8892 8641a332 2023-04-14 thomas got_worktree_get_base_commit_id(worktree), merged_commit_id,
8893 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
8894 8641a332 2023-04-14 thomas if (err)
8895 8641a332 2023-04-14 thomas goto done;
8898 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8899 945f9229 2022-04-16 thomas worktree->base_commit_id);
8900 945f9229 2022-04-16 thomas if (err)
8901 945f9229 2022-04-16 thomas goto done;
8903 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8904 945f9229 2022-04-16 thomas worktree->path_prefix);
8905 10604dce 2021-09-24 thomas if (err)
8906 10604dce 2021-09-24 thomas goto done;
8908 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8909 10604dce 2021-09-24 thomas if (err)
8910 10604dce 2021-09-24 thomas goto done;
8912 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8913 10604dce 2021-09-24 thomas if (err)
8914 10604dce 2021-09-24 thomas goto done;
8916 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8917 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8918 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8919 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8920 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8921 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8922 10604dce 2021-09-24 thomas rfa.repo = repo;
8923 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8924 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8925 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8926 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8927 10604dce 2021-09-24 thomas if (err)
8928 10604dce 2021-09-24 thomas goto sync;
8930 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8931 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8933 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8934 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8935 10604dce 2021-09-24 thomas err = sync_err;
8937 10604dce 2021-09-24 thomas free(tree_id);
8938 8641a332 2023-04-14 thomas free(merged_commit_id);
8939 945f9229 2022-04-16 thomas if (commit)
8940 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8941 10604dce 2021-09-24 thomas if (fileindex)
8942 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8943 10604dce 2021-09-24 thomas free(fileindex_path);
8944 8641a332 2023-04-14 thomas if (commit_ref)
8945 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8946 8641a332 2023-04-14 thomas free(commit_ref_name);
8948 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8949 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8950 10604dce 2021-09-24 thomas err = unlockerr;
8951 10604dce 2021-09-24 thomas return err;
8954 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8955 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8956 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8957 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8958 2db2652d 2019-08-07 stsp struct got_repository *repo;
8959 2db2652d 2019-08-07 stsp int have_changes;
8962 ef20f542 2022-06-26 thomas static const struct got_error *
8963 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8964 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8965 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8966 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8968 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8969 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8970 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8971 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8972 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8973 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8975 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8976 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8977 8b13ce36 2019-08-08 stsp return NULL;
8978 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8979 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8981 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8982 735ef5ac 2019-08-03 stsp if (ie == NULL)
8983 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8985 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8986 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8987 735ef5ac 2019-08-03 stsp relpath) == -1)
8988 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8990 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8991 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
8992 43010591 2023-02-17 thomas &base_commit_id, ie);
8995 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8996 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8997 3aa5969e 2019-08-06 stsp goto done;
8998 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8999 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
9000 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
9001 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9002 735ef5ac 2019-08-03 stsp goto done;
9005 2db2652d 2019-08-07 stsp a->have_changes = 1;
9007 735ef5ac 2019-08-03 stsp p = in_repo_path;
9008 735ef5ac 2019-08-03 stsp while (p[0] == '/')
9010 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
9011 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
9012 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
9014 735ef5ac 2019-08-03 stsp free(in_repo_path);
9015 dc424a06 2019-08-07 stsp return err;
9018 2db2652d 2019-08-07 stsp struct stage_path_arg {
9019 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
9020 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
9021 2db2652d 2019-08-07 stsp struct got_repository *repo;
9022 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
9023 2db2652d 2019-08-07 stsp void *status_arg;
9024 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
9025 2db2652d 2019-08-07 stsp void *patch_arg;
9026 7b5dc508 2019-10-28 stsp int staged_something;
9027 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
9030 2db2652d 2019-08-07 stsp static const struct got_error *
9031 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
9032 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
9033 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9034 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9036 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
9037 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
9038 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
9039 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
9040 0cb83759 2019-08-03 stsp uint32_t stage;
9041 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
9042 0aeb8099 2020-07-23 stsp struct stat sb;
9044 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
9045 8b13ce36 2019-08-08 stsp return NULL;
9047 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9048 d3e7c587 2019-08-03 stsp if (ie == NULL)
9049 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9051 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9052 2db2652d 2019-08-07 stsp relpath)== -1)
9053 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
9055 0cb83759 2019-08-03 stsp switch (status) {
9056 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
9057 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
9058 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
9059 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
9060 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
9063 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9064 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
9065 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9066 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9067 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
9070 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
9073 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
9074 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
9075 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
9076 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
9077 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
9081 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
9082 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
9085 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9086 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
9087 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9088 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
9090 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
9091 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9092 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
9093 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
9094 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
9095 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
9096 35213c7c 2020-07-23 stsp ssize_t target_len;
9097 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
9098 35213c7c 2020-07-23 stsp sizeof(target_path));
9099 35213c7c 2020-07-23 stsp if (target_len == -1) {
9100 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
9101 35213c7c 2020-07-23 stsp ondisk_path);
9104 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
9105 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
9106 ad10f64e 2023-07-19 thomas a->worktree->root_path,
9107 ad10f64e 2023-07-19 thomas a->worktree->meta_dir);
9110 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
9111 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
9112 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
9116 35213c7c 2020-07-23 stsp if (is_bad_symlink)
9117 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9118 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
9120 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9121 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
9123 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9124 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
9126 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9127 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9129 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9130 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
9131 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
9132 f289c82c 2022-06-13 thomas if (err)
9135 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
9136 f289c82c 2022-06-13 thomas * implicitly unstage the file.
9138 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9139 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
9140 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
9141 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
9144 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
9145 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
9147 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9148 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9149 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
9150 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
9153 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9155 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9156 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9160 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
9161 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9162 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9163 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9165 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9166 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9169 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
9171 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
9172 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9174 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
9175 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
9178 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9182 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
9183 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
9184 dc424a06 2019-08-07 stsp free(path_content);
9185 2db2652d 2019-08-07 stsp free(ondisk_path);
9186 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
9187 0ebf8283 2019-07-24 stsp return err;
9190 0cb83759 2019-08-03 stsp const struct got_error *
9191 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
9192 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
9193 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
9194 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9195 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
9197 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9198 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9199 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9200 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9201 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9202 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9203 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9204 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9206 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9208 0cb83759 2019-08-03 stsp return err;
9210 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9211 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9213 735ef5ac 2019-08-03 stsp goto done;
9214 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9216 735ef5ac 2019-08-03 stsp goto done;
9217 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9219 0cb83759 2019-08-03 stsp goto done;
9221 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9222 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9223 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9224 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9225 2db2652d 2019-08-07 stsp oka.repo = repo;
9226 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9227 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9228 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9229 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
9231 735ef5ac 2019-08-03 stsp goto done;
9233 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9234 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9235 2db2652d 2019-08-07 stsp goto done;
9238 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9239 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9240 2db2652d 2019-08-07 stsp spa.repo = repo;
9241 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9242 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9243 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9244 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9245 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9246 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9247 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9248 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9249 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
9251 2db2652d 2019-08-07 stsp goto done;
9253 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9254 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9255 7b5dc508 2019-10-28 stsp goto done;
9258 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9259 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9260 0cb83759 2019-08-03 stsp err = sync_err;
9262 735ef5ac 2019-08-03 stsp if (head_ref)
9263 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9264 735ef5ac 2019-08-03 stsp free(head_commit_id);
9265 0cb83759 2019-08-03 stsp free(fileindex_path);
9266 0cb83759 2019-08-03 stsp if (fileindex)
9267 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9268 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9269 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9270 0cb83759 2019-08-03 stsp err = unlockerr;
9271 0cb83759 2019-08-03 stsp return err;
9274 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9275 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9276 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9277 ad493afc 2019-08-03 stsp struct got_repository *repo;
9278 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9279 ad493afc 2019-08-03 stsp void *progress_arg;
9280 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9281 2e1f37b0 2019-08-08 stsp void *patch_arg;
9284 2e1f37b0 2019-08-08 stsp static const struct got_error *
9285 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9286 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9287 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9288 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9289 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9291 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9292 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9293 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9294 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9295 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9296 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9297 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9298 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9300 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9301 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9303 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9305 2e1f37b0 2019-08-08 stsp return err;
9307 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9308 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9309 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9310 f4ae6ddb 2022-07-01 thomas goto done;
9312 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9313 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9314 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9315 f4ae6ddb 2022-07-01 thomas goto done;
9318 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9320 2e1f37b0 2019-08-08 stsp goto done;
9322 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9324 2e1f37b0 2019-08-08 stsp goto done;
9326 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9328 2e1f37b0 2019-08-08 stsp goto done;
9330 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9333 2e1f37b0 2019-08-08 stsp goto done;
9335 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9337 2e1f37b0 2019-08-08 stsp goto done;
9339 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9341 2e1f37b0 2019-08-08 stsp goto done;
9343 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9344 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9346 2e1f37b0 2019-08-08 stsp goto done;
9348 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9349 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
9351 2e1f37b0 2019-08-08 stsp goto done;
9352 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9353 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
9355 2e1f37b0 2019-08-08 stsp goto done;
9357 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9358 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9359 2e1f37b0 2019-08-08 stsp goto done;
9361 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9362 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9363 2e1f37b0 2019-08-08 stsp goto done;
9365 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9366 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9367 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
9368 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9369 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9370 fe621944 2020-11-10 stsp nchanges++;
9372 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9373 2e1f37b0 2019-08-08 stsp int choice;
9374 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9375 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9376 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9377 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9379 2e1f37b0 2019-08-08 stsp goto done;
9380 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9381 2e1f37b0 2019-08-08 stsp have_content = 1;
9383 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9384 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9387 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9388 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9389 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9391 2e1f37b0 2019-08-08 stsp free(label1);
9392 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9393 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9395 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9396 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9397 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9398 2e1f37b0 2019-08-08 stsp if (staged_blob)
9399 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9400 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9401 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9402 fe621944 2020-11-10 stsp err = free_err;
9403 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9404 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9405 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9406 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9407 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9408 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9409 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9410 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9411 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9412 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9413 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9414 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9415 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9416 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9417 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9418 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9419 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9420 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9421 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9423 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9424 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9425 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9426 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9427 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9428 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9429 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9431 2e1f37b0 2019-08-08 stsp free(path1);
9432 2e1f37b0 2019-08-08 stsp free(path2);
9433 fda8017d 2020-07-23 stsp return err;
9436 fda8017d 2020-07-23 stsp static const struct got_error *
9437 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9438 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9439 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9440 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9441 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9442 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9443 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9445 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9446 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9447 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9448 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9449 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9450 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9451 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9452 fda8017d 2020-07-23 stsp struct stat sb;
9454 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9455 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9456 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9458 fda8017d 2020-07-23 stsp return err;
9460 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9461 fda8017d 2020-07-23 stsp return NULL;
9463 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9464 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9465 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9467 fda8017d 2020-07-23 stsp goto done;
9470 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
9471 fda8017d 2020-07-23 stsp if (f == NULL) {
9472 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9473 fda8017d 2020-07-23 stsp path_unstaged_content);
9474 fda8017d 2020-07-23 stsp goto done;
9476 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9477 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9478 fda8017d 2020-07-23 stsp goto done;
9480 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9481 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9482 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9484 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9485 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9486 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9487 fda8017d 2020-07-23 stsp goto done;
9489 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9490 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9491 fda8017d 2020-07-23 stsp goto done;
9493 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9494 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9495 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9496 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9497 fda8017d 2020-07-23 stsp progress_arg);
9499 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9501 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9503 67a66647 2021-05-31 stsp return err;
9505 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9506 67a66647 2021-05-31 stsp parent) == -1) {
9507 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9508 67a66647 2021-05-31 stsp base_path = NULL;
9509 67a66647 2021-05-31 stsp goto done;
9512 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
9513 fc2a50f2 2022-11-01 thomas base_path, "");
9515 67a66647 2021-05-31 stsp goto done;
9516 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9517 67a66647 2021-05-31 stsp blob_base);
9519 67a66647 2021-05-31 stsp goto done;
9522 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9523 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9525 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9526 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9527 eec2f5a9 2021-06-03 stsp ondisk_path);
9529 eec2f5a9 2021-06-03 stsp goto done;
9532 06340621 2021-12-31 thomas fd = open(ondisk_path,
9533 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9534 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9535 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9536 eec2f5a9 2021-06-03 stsp goto done;
9538 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9539 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9540 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9541 eec2f5a9 2021-06-03 stsp close(fd);
9542 eec2f5a9 2021-06-03 stsp goto done;
9546 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9547 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9548 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9549 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9550 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9553 fda8017d 2020-07-23 stsp goto done;
9555 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9556 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9557 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9559 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9560 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9563 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9564 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9565 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9566 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9567 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9568 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9569 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9570 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9571 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9572 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9573 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9574 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9575 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9576 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9577 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9578 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9579 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9580 67a66647 2021-05-31 stsp free(blob_base_path);
9581 67a66647 2021-05-31 stsp free(parent);
9582 67a66647 2021-05-31 stsp free(base_path);
9583 2e1f37b0 2019-08-08 stsp return err;
9586 ad493afc 2019-08-03 stsp static const struct got_error *
9587 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9588 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9589 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9590 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9592 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9593 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9594 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9595 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9596 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9597 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9598 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9599 9bc94a15 2019-08-03 stsp struct stat sb;
9600 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9602 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9603 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9604 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9605 2e1f37b0 2019-08-08 stsp return NULL;
9607 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9608 ad493afc 2019-08-03 stsp if (ie == NULL)
9609 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9611 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9613 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9615 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9616 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9618 f69721c3 2019-10-21 stsp goto done;
9619 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9620 f69721c3 2019-10-21 stsp id_str) == -1) {
9621 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9622 f4ae6ddb 2022-07-01 thomas goto done;
9625 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9626 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9627 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9628 f4ae6ddb 2022-07-01 thomas goto done;
9630 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9631 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9632 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9633 f69721c3 2019-10-21 stsp goto done;
9636 ad493afc 2019-08-03 stsp switch (staged_status) {
9637 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9638 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9639 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
9642 ad493afc 2019-08-03 stsp /* fall through */
9643 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9644 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9645 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9646 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9647 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9648 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9651 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9654 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9655 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9656 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9657 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9658 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9659 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9662 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9663 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9666 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9667 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9668 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9669 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9670 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9671 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9672 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9673 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9674 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9676 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9677 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9678 36bf999c 2020-07-23 stsp char *staged_target;
9679 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9680 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9682 36bf999c 2020-07-23 stsp goto done;
9683 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9684 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9685 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9686 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9687 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9688 36bf999c 2020-07-23 stsp free(staged_target);
9690 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9691 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9692 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9693 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9694 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9695 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9696 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9700 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9703 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9704 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9705 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9706 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9709 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9710 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9711 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9712 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9713 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9716 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9718 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9719 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9723 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9724 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9725 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9726 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9729 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9733 ad493afc 2019-08-03 stsp free(ondisk_path);
9734 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9735 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9736 ad493afc 2019-08-03 stsp if (blob_base)
9737 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9738 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9739 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9740 ad493afc 2019-08-03 stsp if (blob_staged)
9741 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9742 f69721c3 2019-10-21 stsp free(id_str);
9743 f69721c3 2019-10-21 stsp free(label_orig);
9744 ad493afc 2019-08-03 stsp return err;
9747 ad493afc 2019-08-03 stsp const struct got_error *
9748 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9749 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9750 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9751 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9752 ad493afc 2019-08-03 stsp struct got_repository *repo)
9754 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9755 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9756 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9757 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9758 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9760 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9762 ad493afc 2019-08-03 stsp return err;
9764 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9766 ad493afc 2019-08-03 stsp goto done;
9768 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9769 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9770 ad493afc 2019-08-03 stsp upa.repo = repo;
9771 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9772 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9773 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9774 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9775 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9776 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9777 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9779 ad493afc 2019-08-03 stsp goto done;
9782 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9783 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9784 ad493afc 2019-08-03 stsp err = sync_err;
9786 ad493afc 2019-08-03 stsp free(fileindex_path);
9787 ad493afc 2019-08-03 stsp if (fileindex)
9788 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9789 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9790 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9791 ad493afc 2019-08-03 stsp err = unlockerr;
9792 ad493afc 2019-08-03 stsp return err;
9795 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9796 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9797 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9798 b2118c49 2020-07-28 stsp void *info_arg;
9799 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9800 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9801 b2118c49 2020-07-28 stsp void *cancel_arg;
9804 b2118c49 2020-07-28 stsp static const struct got_error *
9805 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9807 eb6fb9cd 2023-07-26 thomas const struct got_error *err;
9808 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9809 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9810 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9811 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9812 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9813 b2118c49 2020-07-28 stsp int stage;
9815 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
9816 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
9817 eb6fb9cd 2023-07-26 thomas if (err)
9818 eb6fb9cd 2023-07-26 thomas return err;
9821 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9822 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9823 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9826 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9827 b2118c49 2020-07-28 stsp return NULL;
9829 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9830 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9831 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9832 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9833 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9834 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9835 43010591 2023-02-17 thomas &staged_blob_id, ie);
9838 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9839 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9841 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9842 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9845 b2118c49 2020-07-28 stsp const struct got_error *
9846 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9847 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9848 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9849 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9852 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9853 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9854 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9855 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9857 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9859 b2118c49 2020-07-28 stsp return err;
9861 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9863 b2118c49 2020-07-28 stsp goto done;
9865 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9866 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9867 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9868 b2118c49 2020-07-28 stsp arg.paths = paths;
9869 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9870 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9871 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9874 b2118c49 2020-07-28 stsp free(fileindex_path);
9875 b2118c49 2020-07-28 stsp if (fileindex)
9876 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9877 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9878 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9879 b2118c49 2020-07-28 stsp err = unlockerr;
9880 b2118c49 2020-07-28 stsp return err;
9883 814624e7 2022-03-22 thomas static const struct got_error *
9884 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9885 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9886 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9888 814624e7 2022-03-22 thomas const struct got_error *err;
9889 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9890 814624e7 2022-03-22 thomas struct stat sb;
9891 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9893 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9894 814624e7 2022-03-22 thomas if (err)
9895 814624e7 2022-03-22 thomas return err;
9897 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9898 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9899 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9901 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9902 814624e7 2022-03-22 thomas if (ie) {
9903 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9904 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9906 814624e7 2022-03-22 thomas if (err)
9907 814624e7 2022-03-22 thomas goto done;
9908 814624e7 2022-03-22 thomas } else {
9909 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9910 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9911 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9912 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9913 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9914 814624e7 2022-03-22 thomas ondisk_path);
9915 814624e7 2022-03-22 thomas goto done;
9917 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9922 814624e7 2022-03-22 thomas free(ondisk_path);
9923 814624e7 2022-03-22 thomas return err;
9926 814624e7 2022-03-22 thomas static const struct got_error *
9927 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
9928 814624e7 2022-03-22 thomas unsigned char staged_status)
9930 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9931 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9932 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9933 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9934 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9935 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9936 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9937 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9938 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9939 814624e7 2022-03-22 thomas return NULL;
9942 814624e7 2022-03-22 thomas static const struct got_error *
9943 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9945 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9946 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9947 814624e7 2022-03-22 thomas return NULL;
9950 814624e7 2022-03-22 thomas static const struct got_error *
9951 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9952 814624e7 2022-03-22 thomas unsigned char staged_status)
9954 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9955 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9956 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9957 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9958 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9959 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9960 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9961 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9962 814624e7 2022-03-22 thomas return NULL;
9965 814624e7 2022-03-22 thomas const struct got_error *
9966 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9967 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9969 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9972 814624e7 2022-03-22 thomas const struct got_error *
9973 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9974 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9975 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9977 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9978 814624e7 2022-03-22 thomas int file_renamed = 0;
9979 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9980 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9982 814624e7 2022-03-22 thomas *oldpath = NULL;
9983 814624e7 2022-03-22 thomas *newpath = NULL;
9985 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9986 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9987 814624e7 2022-03-22 thomas if (err)
9988 814624e7 2022-03-22 thomas goto done;
9990 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9991 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9992 814624e7 2022-03-22 thomas if (err)
9993 814624e7 2022-03-22 thomas goto done;
9995 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9996 814624e7 2022-03-22 thomas file_renamed = 1;
9998 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9999 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
10000 814624e7 2022-03-22 thomas else if (file_renamed) {
10001 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
10002 814624e7 2022-03-22 thomas if (err == NULL)
10003 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
10004 814624e7 2022-03-22 thomas } else if (old == NULL)
10005 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
10007 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
10010 814624e7 2022-03-22 thomas if (err) {
10011 814624e7 2022-03-22 thomas free(*oldpath);
10012 814624e7 2022-03-22 thomas *oldpath = NULL;
10013 814624e7 2022-03-22 thomas free(*newpath);
10014 814624e7 2022-03-22 thomas *newpath = NULL;
10016 814624e7 2022-03-22 thomas return err;
10019 814624e7 2022-03-22 thomas const struct got_error *
10020 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10021 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
10022 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
10024 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
10026 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
10027 5e22b737 2022-05-31 thomas saa.worktree = worktree;
10028 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
10029 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
10030 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
10031 5e22b737 2022-05-31 thomas saa.repo = repo;
10033 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
10034 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
10037 5e22b737 2022-05-31 thomas const struct got_error *
10038 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10039 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
10040 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
10042 8ec7352a 2023-06-01 thomas const struct got_error *err;
10043 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
10044 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
10046 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
10047 5e22b737 2022-05-31 thomas sda.worktree = worktree;
10048 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
10049 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
10050 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
10051 5e22b737 2022-05-31 thomas sda.repo = repo;
10052 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
10053 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
10054 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
10055 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
10056 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s/%s",
10057 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree), path) == -1)
10058 8ec7352a 2023-06-01 thomas return got_error_from_errno("asprintf");
10059 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
10060 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
10062 8ec7352a 2023-06-01 thomas err = worktree_status(worktree, path, fileindex, repo,
10063 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10064 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
10065 8ec7352a 2023-06-01 thomas return err;
10068 5e22b737 2022-05-31 thomas const struct got_error *
10069 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
10070 36832a8e 2022-05-31 thomas const char *fileindex_path)
10072 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
10074 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
10075 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
10077 5e22b737 2022-05-31 thomas return err;