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 577ec78f 2018-03-11 stsp const char *prefix, 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 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
189 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_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 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_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 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_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 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
398 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
400 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
401 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
402 ddcd8544 2019-03-11 stsp struct stat sb;
403 ddcd8544 2019-03-11 stsp err = NULL;
404 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
405 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
406 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
407 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
408 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
411 4a1ddfc2 2019-01-12 stsp free(abspath);
412 68c76935 2019-02-19 stsp return err;
415 68c76935 2019-02-19 stsp static const struct got_error *
416 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
418 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
419 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
420 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
421 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
426 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
427 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
428 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
431 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
432 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
433 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
436 68c76935 2019-02-19 stsp if (flen1 == 0) {
437 68c76935 2019-02-19 stsp if (flen2 != 0)
440 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
441 68c76935 2019-02-19 stsp if (flen1 != 0)
444 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
445 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
455 68c76935 2019-02-19 stsp return err;
458 68c76935 2019-02-19 stsp static const struct got_error *
459 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
461 68c76935 2019-02-19 stsp struct stat sb;
462 68c76935 2019-02-19 stsp size_t size1, size2;
466 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
467 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
468 68c76935 2019-02-19 stsp size1 = sb.st_size;
470 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
471 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
472 68c76935 2019-02-19 stsp size2 = sb.st_size;
474 68c76935 2019-02-19 stsp if (size1 != size2) {
476 68c76935 2019-02-19 stsp return NULL;
479 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
480 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
481 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
482 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
484 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
487 24f136e0 2021-11-20 thomas static const struct got_error *
488 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
490 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
491 24f136e0 2021-11-20 thomas size_t flen;
492 24f136e0 2021-11-20 thomas ssize_t outlen;
494 24f136e0 2021-11-20 thomas *outsize = 0;
496 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
497 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
499 24f136e0 2021-11-20 thomas for (;;) {
500 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
501 24f136e0 2021-11-20 thomas if (flen == 0) {
502 24f136e0 2021-11-20 thomas if (ferror(f))
503 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
504 24f136e0 2021-11-20 thomas if (feof(f))
507 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
508 24f136e0 2021-11-20 thomas if (outlen == -1)
509 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
510 24f136e0 2021-11-20 thomas if (outlen != flen)
511 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
512 24f136e0 2021-11-20 thomas *outsize += outlen;
515 24f136e0 2021-11-20 thomas return NULL;
518 24f136e0 2021-11-20 thomas static const struct got_error *
519 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
520 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
521 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
522 24f136e0 2021-11-20 thomas const char *ondisk_path)
524 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
525 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
526 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
527 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
528 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
529 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
530 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
532 24f136e0 2021-11-20 thomas *overlapcnt = 0;
534 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
536 24f136e0 2021-11-20 thomas return err;
538 24f136e0 2021-11-20 thomas if (same_content)
539 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
541 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
543 24f136e0 2021-11-20 thomas return err;
544 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
545 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
547 24f136e0 2021-11-20 thomas return err;
548 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
550 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
551 24f136e0 2021-11-20 thomas *overlapcnt = 1;
552 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
553 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
554 24f136e0 2021-11-20 thomas goto done;
556 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
557 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
558 24f136e0 2021-11-20 thomas goto done;
560 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
561 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
562 24f136e0 2021-11-20 thomas goto done;
564 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
565 fc2a50f2 2022-11-01 thomas base_path_orig, "");
567 24f136e0 2021-11-20 thomas goto done;
568 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
569 fc2a50f2 2022-11-01 thomas base_path_deriv, "");
571 24f136e0 2021-11-20 thomas goto done;
572 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
573 fc2a50f2 2022-11-01 thomas base_path_deriv2, "");
575 24f136e0 2021-11-20 thomas goto done;
576 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
578 24f136e0 2021-11-20 thomas goto done;
579 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
581 24f136e0 2021-11-20 thomas goto done;
582 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
584 24f136e0 2021-11-20 thomas goto done;
585 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
586 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
587 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
588 24f136e0 2021-11-20 thomas goto done;
590 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
591 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
592 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
593 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
594 24f136e0 2021-11-20 thomas path_deriv) < 0) {
595 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
596 24f136e0 2021-11-20 thomas goto done;
598 24f136e0 2021-11-20 thomas if (size_orig > 0) {
599 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
600 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
601 24f136e0 2021-11-20 thomas label_orig ? " " : "",
602 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
603 24f136e0 2021-11-20 thomas path_orig) < 0) {
604 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
605 24f136e0 2021-11-20 thomas goto done;
608 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
609 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
610 24f136e0 2021-11-20 thomas path_deriv2,
611 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
612 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
613 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
614 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
615 24f136e0 2021-11-20 thomas goto done;
617 24f136e0 2021-11-20 thomas } else if (changed_deriv)
618 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
619 24f136e0 2021-11-20 thomas else if (changed_deriv2)
620 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
622 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
623 24f136e0 2021-11-20 thomas err == NULL)
624 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
625 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
626 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
627 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
628 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
629 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
630 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
631 24f136e0 2021-11-20 thomas free(path_orig);
632 24f136e0 2021-11-20 thomas free(path_deriv);
633 24f136e0 2021-11-20 thomas free(path_deriv2);
634 24f136e0 2021-11-20 thomas free(base_path_orig);
635 24f136e0 2021-11-20 thomas free(base_path_deriv);
636 24f136e0 2021-11-20 thomas free(base_path_deriv2);
637 24f136e0 2021-11-20 thomas return err;
641 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
642 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
643 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
644 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
645 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
647 6353ad76 2019-02-08 stsp static const struct got_error *
648 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
649 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
650 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
651 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
652 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
653 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
655 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
656 6353ad76 2019-02-08 stsp int merged_fd = -1;
657 db590691 2021-06-02 stsp FILE *f_merged = NULL;
658 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
659 234035bc 2019-06-01 stsp int overlapcnt = 0;
660 3524bbf9 2020-10-20 stsp char *parent = NULL;
662 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
664 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
666 3524bbf9 2020-10-20 stsp return err;
668 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
669 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
673 fc2a50f2 2022-11-01 thomas err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
677 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
678 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
679 24f136e0 2021-11-20 thomas if (err) {
680 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
681 24f136e0 2021-11-20 thomas goto done;
682 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
683 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
684 24f136e0 2021-11-20 thomas ondisk_path);
686 24f136e0 2021-11-20 thomas goto done;
689 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
690 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
694 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
695 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
699 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
700 db590691 2021-06-02 stsp if (f_merged == NULL) {
701 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
704 db590691 2021-06-02 stsp merged_fd = -1;
706 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
707 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
708 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
714 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
715 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
719 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
720 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
721 230a42bd 2019-05-11 jcs ondisk_path);
726 fdcb7daf 2019-12-15 stsp if (merged_path)
727 fdcb7daf 2019-12-15 stsp unlink(merged_path);
729 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
730 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
731 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
732 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
733 6353ad76 2019-02-08 stsp free(merged_path);
734 af54ae4a 2019-02-19 stsp free(base_path);
735 3524bbf9 2020-10-20 stsp free(parent);
736 af57b12a 2020-07-23 stsp return err;
739 af57b12a 2020-07-23 stsp static const struct got_error *
740 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
741 af57b12a 2020-07-23 stsp size_t target_len)
743 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
744 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
745 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
746 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
747 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
748 af57b12a 2020-07-23 stsp ondisk_path);
749 af57b12a 2020-07-23 stsp return NULL;
753 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
754 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
755 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
756 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
758 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
759 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
761 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
762 993e2a1b 2020-07-23 stsp * has deleted this symlink.
764 11cc08c1 2020-07-23 stsp static const struct got_error *
765 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
766 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
767 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
769 11cc08c1 2020-07-23 stsp const struct got_error *err;
770 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
771 11cc08c1 2020-07-23 stsp FILE *f = NULL;
773 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
775 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
777 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
778 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
779 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
783 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
785 3818e3c4 2020-11-01 naddy goto done;
787 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
788 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
792 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
793 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
794 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
795 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
796 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
797 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
798 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
799 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
800 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
801 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
805 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
806 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
809 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
810 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
814 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
815 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
816 11cc08c1 2020-07-23 stsp free(path);
817 11cc08c1 2020-07-23 stsp free(id_str);
818 11cc08c1 2020-07-23 stsp free(label_deriv);
819 11cc08c1 2020-07-23 stsp return err;
822 d219f183 2020-07-23 stsp /* forward declaration */
823 d219f183 2020-07-23 stsp static const struct got_error *
824 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
825 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
826 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
827 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
830 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
831 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
832 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
833 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
835 af57b12a 2020-07-23 stsp static const struct got_error *
836 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
837 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
838 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
839 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
840 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
842 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
843 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
844 af57b12a 2020-07-23 stsp struct stat sb;
845 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
846 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
847 11cc08c1 2020-07-23 stsp int have_local_change = 0;
848 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
850 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
851 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
853 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
854 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
855 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
856 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
857 af57b12a 2020-07-23 stsp ondisk_path);
860 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
862 526a746f 2020-07-23 stsp if (blob_orig) {
863 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
868 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
869 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
870 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
871 11cc08c1 2020-07-23 stsp have_local_change = 1;
873 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
874 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
875 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
876 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
877 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
879 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
880 11cc08c1 2020-07-23 stsp if (ancestor_target) {
881 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
882 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
884 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
885 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
886 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
887 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
890 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
891 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
892 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
893 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
896 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
899 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
900 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
901 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
902 af57b12a 2020-07-23 stsp strlen(deriv_target));
905 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
906 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
907 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
908 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
909 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
910 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
913 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
914 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
915 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
918 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
924 af57b12a 2020-07-23 stsp free(ancestor_target);
925 eec2f5a9 2021-06-03 stsp return err;
928 eec2f5a9 2021-06-03 stsp static const struct got_error *
929 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
931 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
932 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
933 eec2f5a9 2021-06-03 stsp ssize_t target_len;
937 eec2f5a9 2021-06-03 stsp *outfile = NULL;
939 eec2f5a9 2021-06-03 stsp f = got_opentemp();
940 eec2f5a9 2021-06-03 stsp if (f == NULL)
941 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
942 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
943 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
944 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
947 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
948 eec2f5a9 2021-06-03 stsp if (n != target_len) {
949 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
952 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
953 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
956 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
957 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
964 eec2f5a9 2021-06-03 stsp *outfile = f;
965 14c901f1 2019-08-08 stsp return err;
969 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
970 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
971 14c901f1 2019-08-08 stsp * acts as the second derived version.
973 14c901f1 2019-08-08 stsp static const struct got_error *
974 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
975 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
976 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
977 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
978 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
979 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
981 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
982 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
983 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
984 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
985 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
987 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
989 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
991 ed6b5030 2020-10-20 stsp return err;
993 67a66647 2021-05-31 stsp if (blob_orig) {
994 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
995 67a66647 2021-05-31 stsp parent) == -1) {
996 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
997 67a66647 2021-05-31 stsp base_path = NULL;
1001 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_orig_path, &f_orig,
1002 fc2a50f2 2022-11-01 thomas base_path, "");
1004 67a66647 2021-05-31 stsp goto done;
1005 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1006 67a66647 2021-05-31 stsp blob_orig);
1008 67a66647 2021-05-31 stsp goto done;
1009 67a66647 2021-05-31 stsp free(base_path);
1012 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1013 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1014 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1016 db590691 2021-06-02 stsp f_orig = got_opentemp();
1017 db590691 2021-06-02 stsp if (f_orig == NULL) {
1018 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1019 db590691 2021-06-02 stsp goto done;
1023 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1024 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1025 14c901f1 2019-08-08 stsp base_path = NULL;
1026 14c901f1 2019-08-08 stsp goto done;
1029 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1031 14c901f1 2019-08-08 stsp goto done;
1032 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1033 14c901f1 2019-08-08 stsp blob_deriv);
1035 14c901f1 2019-08-08 stsp goto done;
1037 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1039 14c901f1 2019-08-08 stsp goto done;
1040 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1041 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1042 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1043 14c901f1 2019-08-08 stsp goto done;
1047 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1048 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1050 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1051 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1053 eec2f5a9 2021-06-03 stsp goto done;
1056 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1057 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1058 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1059 eec2f5a9 2021-06-03 stsp goto done;
1061 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1062 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1063 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1064 eec2f5a9 2021-06-03 stsp close(fd);
1065 eec2f5a9 2021-06-03 stsp goto done;
1069 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1070 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1071 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1073 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1074 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1075 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1076 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1077 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1078 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1079 14c901f1 2019-08-08 stsp free(base_path);
1080 67a66647 2021-05-31 stsp if (blob_orig_path) {
1081 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1082 67a66647 2021-05-31 stsp free(blob_orig_path);
1084 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1085 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1086 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1088 6353ad76 2019-02-08 stsp free(id_str);
1089 818c7501 2019-07-11 stsp free(label_deriv);
1090 ed6b5030 2020-10-20 stsp free(parent);
1091 4a1ddfc2 2019-01-12 stsp return err;
1094 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1095 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1096 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1097 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1099 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1100 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1102 65b05cec 2020-07-23 stsp *new_iep = NULL;
1104 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1106 054041d0 2020-06-24 stsp return err;
1108 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1109 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1111 054041d0 2020-06-24 stsp goto done;
1113 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1116 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1118 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1119 13d9040b 2019-03-27 stsp return err;
1122 1ebedb77 2019-10-19 stsp static mode_t
1123 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1125 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1127 1ebedb77 2019-10-19 stsp if (executable) {
1128 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1129 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1130 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1131 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1132 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1133 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1136 a2c162eb 2022-10-30 thomas return st_mode;
1139 8ba819a3 2020-07-23 stsp /* forward declaration */
1140 13d9040b 2019-03-27 stsp static const struct got_error *
1141 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1142 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1143 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1144 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1145 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1146 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1149 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1150 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1152 8ba819a3 2020-07-23 stsp static const struct got_error *
1153 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1154 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1156 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1157 5a1fbc73 2020-07-23 stsp ssize_t elen;
1158 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1161 c6e8a826 2021-04-05 stsp *did_something = 0;
1164 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1165 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1166 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1167 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1168 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1169 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1171 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1172 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1173 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1174 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1176 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1177 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1178 5a1fbc73 2020-07-23 stsp if (elen == -1)
1179 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1181 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1182 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1183 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1186 c6e8a826 2021-04-05 stsp *did_something = 1;
1187 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1188 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1189 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1190 5a1fbc73 2020-07-23 stsp return err;
1193 3c1ec782 2020-07-23 stsp static const struct got_error *
1194 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1195 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1197 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1198 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1199 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1201 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1203 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1204 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1205 3c1ec782 2020-07-23 stsp return NULL;
1209 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1210 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1211 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1212 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1213 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1215 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1216 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1217 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1219 ce031e9e 2020-10-20 stsp return err;
1220 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1221 ce031e9e 2020-10-20 stsp free(parent);
1222 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1224 ce031e9e 2020-10-20 stsp free(parent);
1225 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1226 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1227 3c1ec782 2020-07-23 stsp free(abspath);
1228 3c1ec782 2020-07-23 stsp return err;
1230 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1231 3c1ec782 2020-07-23 stsp free(abspath);
1233 3c1ec782 2020-07-23 stsp return err;
1235 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1237 3c1ec782 2020-07-23 stsp return err;
1240 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1241 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1242 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1243 3c1ec782 2020-07-23 stsp return NULL;
1246 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1247 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1248 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1249 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1250 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1251 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1253 3c1ec782 2020-07-23 stsp free(path_got);
1254 3c1ec782 2020-07-23 stsp return NULL;
1257 5a1fbc73 2020-07-23 stsp static const struct got_error *
1258 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1259 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1260 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1261 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1262 ace90326 2021-09-27 thomas struct got_repository *repo,
1263 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1265 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1266 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1267 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1268 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1269 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1271 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1274 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1275 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1276 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1277 8ba819a3 2020-07-23 stsp * in the blob object.
1280 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1281 3521b466 2022-07-21 thomas if (err)
1282 3521b466 2022-07-21 thomas return err;
1284 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1285 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1286 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1287 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1288 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1289 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1290 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1291 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1292 3b9f0f87 2020-07-23 stsp progress_arg);
1294 8ba819a3 2020-07-23 stsp if (len > 0) {
1295 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1296 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1297 8ba819a3 2020-07-23 stsp len - hdrlen);
1298 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1299 8ba819a3 2020-07-23 stsp hdrlen = 0;
1301 8ba819a3 2020-07-23 stsp } while (len != 0);
1302 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1304 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1305 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1307 3c1ec782 2020-07-23 stsp return err;
1309 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1310 906c123b 2020-07-23 stsp /* install as a regular file */
1311 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1312 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1313 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1314 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1315 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1316 9e39ca77 2022-07-21 thomas return err;
1319 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1320 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1321 c6e8a826 2021-04-05 stsp int symlink_replaced;
1322 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1323 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1324 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1325 9e39ca77 2022-07-21 thomas return err;
1327 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1328 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1330 9e39ca77 2022-07-21 thomas return err;
1331 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1332 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1333 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1334 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1335 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1336 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1338 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1339 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1342 9e39ca77 2022-07-21 thomas return err; /* Nothing else to do. */
1345 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1346 f4994adc 2020-10-20 stsp char *parent;
1347 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1349 9e39ca77 2022-07-21 thomas return err;
1350 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1351 f4994adc 2020-10-20 stsp free(parent);
1353 9e39ca77 2022-07-21 thomas return err;
1355 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1356 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1358 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1359 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1360 9e39ca77 2022-07-21 thomas return err;
1364 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1365 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1366 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1367 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1368 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1369 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1370 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1371 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1372 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1373 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1374 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1375 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1376 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1378 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1379 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1381 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1382 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1383 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1384 8ba819a3 2020-07-23 stsp return err;
1387 8ba819a3 2020-07-23 stsp static const struct got_error *
1388 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1389 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1390 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1391 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1392 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1393 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1395 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1396 507dc3bb 2018-12-29 stsp int fd = -1;
1397 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1398 507dc3bb 2018-12-29 stsp int update = 0;
1399 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1400 a2c162eb 2022-10-30 thomas mode_t mode;
1402 a2c162eb 2022-10-30 thomas mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1403 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1404 a2c162eb 2022-10-30 thomas O_CLOEXEC, mode);
1405 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1406 9f323212 2023-03-10 thomas if (errno == ENOENT || errno == ENOTDIR) {
1407 f5375317 2020-10-20 stsp char *parent;
1408 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1410 f5375317 2020-10-20 stsp return err;
1411 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1412 9f323212 2023-03-10 thomas if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1413 9f323212 2023-03-10 thomas err = got_error_path(path, err->code);
1414 f5375317 2020-10-20 stsp free(parent);
1416 21908da4 2019-01-13 stsp return err;
1417 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1418 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1420 21908da4 2019-01-13 stsp if (fd == -1)
1421 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1422 230a42bd 2019-05-11 jcs ondisk_path);
1423 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1424 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1425 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1426 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1427 3b9f0f87 2020-07-23 stsp goto done;
1429 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1430 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1431 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1432 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1433 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1434 507dc3bb 2018-12-29 stsp goto done;
1436 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1437 fc2a50f2 2022-11-01 thomas ondisk_path, "");
1439 507dc3bb 2018-12-29 stsp goto done;
1440 507dc3bb 2018-12-29 stsp update = 1;
1442 a2c162eb 2022-10-30 thomas if (fchmod(fd, apply_umask(mode)) == -1) {
1443 a2c162eb 2022-10-30 thomas err = got_error_from_errno2("fchmod",
1444 a2c162eb 2022-10-30 thomas tmppath);
1445 a2c162eb 2022-10-30 thomas goto done;
1449 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1452 bd6aa359 2020-07-23 stsp if (progress_cb) {
1453 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1454 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1456 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1457 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1460 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1461 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1463 bd6aa359 2020-07-23 stsp goto done;
1466 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1468 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1469 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1472 9d31a1d8 2018-03-11 stsp if (len > 0) {
1473 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1474 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1475 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1476 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1477 b87c6f83 2018-12-24 stsp goto done;
1478 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1479 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1480 b87c6f83 2018-12-24 stsp goto done;
1482 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1484 9d31a1d8 2018-03-11 stsp } while (len != 0);
1486 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1487 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1488 816dc654 2019-02-16 stsp goto done;
1491 507dc3bb 2018-12-29 stsp if (update) {
1492 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1493 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1494 f6d8c0ac 2020-11-09 stsp goto done;
1496 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1497 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1498 230a42bd 2019-05-11 jcs ondisk_path);
1499 68ed9ba5 2019-02-10 stsp goto done;
1501 63df146d 2020-11-09 stsp free(tmppath);
1502 63df146d 2020-11-09 stsp tmppath = NULL;
1506 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1507 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1508 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1509 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1510 507dc3bb 2018-12-29 stsp free(tmppath);
1511 6353ad76 2019-02-08 stsp return err;
1515 be94c032 2023-02-20 thomas * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1516 be94c032 2023-02-20 thomas * conflict marker is found in newly added lines only.
1518 6353ad76 2019-02-08 stsp static const struct got_error *
1519 be94c032 2023-02-20 thomas get_modified_file_content_status(unsigned char *status,
1520 be94c032 2023-02-20 thomas struct got_blob_object *blob, const char *path, struct stat *sb,
1521 be94c032 2023-02-20 thomas FILE *ondisk_file)
1523 8de9d8ad 2023-02-20 thomas const struct got_error *err, *free_err;
1524 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1525 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1526 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1527 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1529 8de9d8ad 2023-02-20 thomas FILE *f1 = NULL;
1530 8de9d8ad 2023-02-20 thomas struct got_diffreg_result *diffreg_result = NULL;
1531 8de9d8ad 2023-02-20 thomas struct diff_result *r;
1532 8de9d8ad 2023-02-20 thomas int nchunks_parsed, n, i = 0, ln = 0;
1533 9bdd68dd 2020-01-02 naddy char *line = NULL;
1534 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1535 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1537 8de9d8ad 2023-02-20 thomas if (*status != GOT_STATUS_MODIFY)
1538 8de9d8ad 2023-02-20 thomas return NULL;
1540 8de9d8ad 2023-02-20 thomas f1 = got_opentemp();
1541 8de9d8ad 2023-02-20 thomas if (f1 == NULL)
1542 8de9d8ad 2023-02-20 thomas return got_error_from_errno("got_opentemp");
1544 8de9d8ad 2023-02-20 thomas if (blob) {
1545 8de9d8ad 2023-02-20 thomas got_object_blob_rewind(blob);
1546 8de9d8ad 2023-02-20 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1547 be94c032 2023-02-20 thomas if (err)
1548 be94c032 2023-02-20 thomas goto done;
1551 8de9d8ad 2023-02-20 thomas err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1552 8de9d8ad 2023-02-20 thomas 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1553 8de9d8ad 2023-02-20 thomas if (err)
1554 8de9d8ad 2023-02-20 thomas goto done;
1556 8de9d8ad 2023-02-20 thomas r = diffreg_result->result;
1558 8de9d8ad 2023-02-20 thomas for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1559 8de9d8ad 2023-02-20 thomas struct diff_chunk *c;
1560 8de9d8ad 2023-02-20 thomas struct diff_chunk_context cc = {};
1561 450d9f6b 2023-02-20 thomas off_t pos;
1564 8de9d8ad 2023-02-20 thomas * We can optimise a little by advancing straight
1565 8de9d8ad 2023-02-20 thomas * to the next chunk if this one has no added lines.
1567 8de9d8ad 2023-02-20 thomas c = diff_chunk_get(r, n);
1569 47c7ee21 2023-02-20 thomas if (diff_chunk_type(c) != CHUNK_PLUS) {
1570 8de9d8ad 2023-02-20 thomas nchunks_parsed = 1;
1571 450d9f6b 2023-02-20 thomas continue; /* removed or unchanged lines */
1574 450d9f6b 2023-02-20 thomas pos = diff_chunk_get_right_start_pos(c);
1575 450d9f6b 2023-02-20 thomas if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1576 450d9f6b 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1577 450d9f6b 2023-02-20 thomas goto done;
1580 450d9f6b 2023-02-20 thomas diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1581 450d9f6b 2023-02-20 thomas ln = cc.right.start;
1583 8de9d8ad 2023-02-20 thomas while (ln < cc.right.end) {
1584 8de9d8ad 2023-02-20 thomas linelen = getline(&line, &linesize, ondisk_file);
1585 8de9d8ad 2023-02-20 thomas if (linelen == -1) {
1586 8de9d8ad 2023-02-20 thomas if (feof(ondisk_file))
1588 8de9d8ad 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1592 8de9d8ad 2023-02-20 thomas if (line && strncmp(line, markers[i],
1593 8de9d8ad 2023-02-20 thomas strlen(markers[i])) == 0) {
1594 8de9d8ad 2023-02-20 thomas if (strcmp(markers[i],
1595 8de9d8ad 2023-02-20 thomas GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1596 8de9d8ad 2023-02-20 thomas *status = GOT_STATUS_CONFLICT;
1597 8de9d8ad 2023-02-20 thomas goto done;
1606 9bdd68dd 2020-01-02 naddy free(line);
1607 be94c032 2023-02-20 thomas if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1608 be94c032 2023-02-20 thomas err = got_error_from_errno("fclose");
1609 8de9d8ad 2023-02-20 thomas free_err = got_diffreg_result_free(diffreg_result);
1610 8de9d8ad 2023-02-20 thomas if (err == NULL)
1611 8de9d8ad 2023-02-20 thomas err = free_err;
1613 7154f6ce 2019-03-27 stsp return err;
1616 e2b1e152 2019-07-13 stsp static int
1617 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1619 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1620 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1623 1ebedb77 2019-10-19 stsp static int
1624 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1626 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1627 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1628 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1629 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1630 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1631 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1634 c363b2c1 2019-08-03 stsp static unsigned char
1635 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1637 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1638 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1639 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1640 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1641 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1642 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1643 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1645 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1649 a919d5c4 2020-07-23 stsp static const struct got_error *
1650 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1651 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1652 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1654 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1655 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1656 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1657 a919d5c4 2020-07-23 stsp ssize_t elen;
1658 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1659 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1660 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1662 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1664 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1666 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1668 a919d5c4 2020-07-23 stsp return err;
1669 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1671 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1672 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1674 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1676 a919d5c4 2020-07-23 stsp if (len > 0) {
1677 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1678 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1679 a919d5c4 2020-07-23 stsp len - hdrlen);
1680 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1681 a919d5c4 2020-07-23 stsp hdrlen = 0;
1683 a919d5c4 2020-07-23 stsp } while (len != 0);
1684 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1686 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1687 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1688 a919d5c4 2020-07-23 stsp if (elen == -1)
1689 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1691 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1692 a919d5c4 2020-07-23 stsp if (elen == -1)
1693 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1696 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1697 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1699 a919d5c4 2020-07-23 stsp return NULL;
1702 7154f6ce 2019-03-27 stsp static const struct got_error *
1703 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1704 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1705 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1707 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1708 6353ad76 2019-02-08 stsp struct got_object_id id;
1709 6353ad76 2019-02-08 stsp size_t hdrlen;
1710 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1711 6353ad76 2019-02-08 stsp FILE *f = NULL;
1712 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1713 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1714 6353ad76 2019-02-08 stsp size_t flen, blen;
1715 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
1717 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
1718 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1719 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1722 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1723 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1724 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1726 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1727 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1728 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1729 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1730 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1732 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1733 882ef1b9 2019-12-13 stsp goto done;
1735 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1736 3d35a492 2019-12-13 stsp goto done;
1739 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1740 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1741 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1742 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1743 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1744 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1745 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1746 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1747 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1748 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1749 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1751 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1752 3d35a492 2019-12-13 stsp goto done;
1754 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1755 1338848f 2019-12-13 stsp goto done;
1759 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1760 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1761 1338848f 2019-12-13 stsp goto done;
1764 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1765 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1766 1338848f 2019-12-13 stsp goto done;
1767 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1768 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1769 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1770 1338848f 2019-12-13 stsp goto done;
1773 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1774 f179e66d 2020-07-23 stsp goto done;
1776 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1777 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1778 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1779 1338848f 2019-12-13 stsp goto done;
1782 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1783 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1784 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
1786 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
1788 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1789 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1790 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1791 f4ae6ddb 2022-07-01 thomas goto done;
1793 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1795 1338848f 2019-12-13 stsp goto done;
1797 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1798 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1799 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1800 a919d5c4 2020-07-23 stsp goto done;
1803 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1804 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1805 ab0d4361 2019-12-13 stsp if (fd == -1) {
1806 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1807 ab0d4361 2019-12-13 stsp goto done;
1811 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1812 6353ad76 2019-02-08 stsp if (f == NULL) {
1813 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1814 6353ad76 2019-02-08 stsp goto done;
1817 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1819 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1820 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1822 7154f6ce 2019-03-27 stsp goto done;
1823 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1824 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1825 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1826 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1827 7154f6ce 2019-03-27 stsp goto done;
1829 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1830 6353ad76 2019-02-08 stsp if (flen != 0)
1831 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1833 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1834 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1835 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1837 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1838 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1839 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1840 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1844 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1847 6353ad76 2019-02-08 stsp hdrlen = 0;
1850 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1851 7154f6ce 2019-03-27 stsp rewind(f);
1852 be94c032 2023-02-20 thomas err = get_modified_file_content_status(status, blob, ie->path,
1854 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1855 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1857 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1858 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1860 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1861 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1862 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1863 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1864 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1865 9d31a1d8 2018-03-11 stsp return err;
1869 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1870 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1872 e2b1e152 2019-07-13 stsp static const struct got_error *
1873 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1874 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1876 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1877 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1878 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1880 e2b1e152 2019-07-13 stsp return NULL;
1883 9f323212 2023-03-10 thomas static const struct got_error *remove_ondisk_file(const char *, const char *);
1885 9d31a1d8 2018-03-11 stsp static const struct got_error *
1886 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1887 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1888 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1889 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1890 0584f854 2019-04-06 stsp void *progress_arg)
1892 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1893 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1894 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1895 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1896 b8f41171 2019-02-10 stsp struct stat sb;
1897 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1899 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1900 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1903 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1904 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1905 a76c42e6 2019-08-03 stsp goto done;
1907 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1910 abb4604f 2019-07-27 stsp goto done;
1911 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1912 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1914 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1915 9f323212 2023-03-10 thomas if (errno != ENOENT && errno != ENOTDIR) {
1916 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1917 e6f4ba31 2021-09-24 thomas ondisk_path);
1918 e6f4ba31 2021-09-24 thomas goto done;
1920 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1921 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1922 e6f4ba31 2021-09-24 thomas } else {
1923 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1924 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1926 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1930 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1932 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1933 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1934 b8f41171 2019-02-10 stsp goto done;
1936 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1938 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1939 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1941 5036ab18 2020-04-18 stsp goto done;
1944 9f323212 2023-03-10 thomas if (S_ISDIR(te->mode)) { /* file changing into a directory */
1945 9f323212 2023-03-10 thomas if (status == GOT_STATUS_UNVERSIONED) {
1946 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, status, path);
1947 9f323212 2023-03-10 thomas } else if (status != GOT_STATUS_NO_CHANGE &&
1948 9f323212 2023-03-10 thomas status != GOT_STATUS_DELETE &&
1949 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1950 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1951 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg,
1952 9f323212 2023-03-10 thomas GOT_STATUS_CANNOT_DELETE, path);
1953 9f323212 2023-03-10 thomas } else if (ie) {
1954 9f323212 2023-03-10 thomas if (status != GOT_STATUS_DELETE &&
1955 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1956 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1957 9f323212 2023-03-10 thomas err = remove_ondisk_file(worktree->root_path,
1958 9f323212 2023-03-10 thomas ie->path);
1959 9f323212 2023-03-10 thomas if (err && !(err->code == GOT_ERR_ERRNO &&
1960 9f323212 2023-03-10 thomas errno == ENOENT))
1961 9f323212 2023-03-10 thomas goto done;
1963 9f323212 2023-03-10 thomas got_fileindex_entry_remove(fileindex, ie);
1964 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1965 9f323212 2023-03-10 thomas ie->path);
1967 9f323212 2023-03-10 thomas goto done; /* nothing else to do */
1970 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1971 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1972 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1974 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1975 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1976 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1977 c6e8a826 2021-04-05 stsp * updating contents of this file.
1979 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1980 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1981 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1982 c6e8a826 2021-04-05 stsp /* Same commit. */
1983 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1984 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1986 e2b1e152 2019-07-13 stsp goto done;
1987 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1989 6353ad76 2019-02-08 stsp goto done;
1991 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1992 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1993 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1994 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1995 b36a9774 2023-04-14 thomas if (got_fileindex_entry_has_commit(ie)) {
1996 b36a9774 2023-04-14 thomas /* Update the base commit ID of this file. */
1997 b36a9774 2023-04-14 thomas memcpy(ie->commit_sha1,
1998 b36a9774 2023-04-14 thomas worktree->base_commit_id->sha1,
1999 b36a9774 2023-04-14 thomas sizeof(ie->commit_sha1));
2001 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2002 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2004 0f58026f 2021-04-05 stsp goto done;
2005 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2007 b8f41171 2019-02-10 stsp goto done;
2011 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
2012 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
2013 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2014 f4ae6ddb 2022-07-01 thomas goto done;
2016 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2018 6353ad76 2019-02-08 stsp goto done;
2020 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2021 234035bc 2019-06-01 stsp int update_timestamps;
2022 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2023 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2024 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2025 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
2026 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
2027 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2028 f4ae6ddb 2022-07-01 thomas goto done;
2030 234035bc 2019-06-01 stsp struct got_object_id id2;
2031 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id2, ie);
2032 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2035 f69721c3 2019-10-21 stsp goto done;
2037 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2038 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2039 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2040 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2041 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2042 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2043 f69721c3 2019-10-21 stsp goto done;
2045 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2046 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2047 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2048 234035bc 2019-06-01 stsp goto done;
2051 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2052 36bf999c 2020-07-23 stsp char *link_target;
2053 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2055 36bf999c 2020-07-23 stsp goto done;
2056 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2057 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2058 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2059 36bf999c 2020-07-23 stsp free(link_target);
2061 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2062 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2063 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2064 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2066 f69721c3 2019-10-21 stsp free(label_orig);
2067 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2068 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2069 f4ae6ddb 2022-07-01 thomas goto done;
2071 234035bc 2019-06-01 stsp if (blob2)
2072 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2074 909d120e 2019-09-22 stsp goto done;
2076 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2077 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2078 234035bc 2019-06-01 stsp * unmodified files again.
2080 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2081 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2082 234035bc 2019-06-01 stsp update_timestamps);
2083 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2084 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2085 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2086 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2087 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2089 1ee397ad 2019-07-12 stsp goto done;
2090 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2091 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2093 13d9040b 2019-03-27 stsp goto done;
2095 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2096 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2097 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2098 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2099 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2100 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2101 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2103 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2104 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2105 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2106 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2107 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2110 13d9040b 2019-03-27 stsp goto done;
2113 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2114 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2115 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2117 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2118 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2119 054041d0 2020-06-24 stsp &blob->id);
2122 13d9040b 2019-03-27 stsp goto done;
2124 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2125 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2126 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2130 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2131 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2132 f4ae6ddb 2022-07-01 thomas goto done;
2134 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2136 6353ad76 2019-02-08 stsp free(ondisk_path);
2137 8da9e5f4 2019-01-12 stsp return err;
2140 90285c3b 2019-01-08 stsp static const struct got_error *
2141 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2143 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2144 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2146 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2147 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2149 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2150 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2151 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2153 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2154 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2156 1c4cdd89 2021-06-20 stsp goto done;
2157 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2158 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2159 fddefe3b 2020-10-20 stsp free(ondisk_path);
2160 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2161 1c4cdd89 2021-06-20 stsp parent = NULL;
2162 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2163 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2164 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2165 fddefe3b 2020-10-20 stsp ondisk_path);
2168 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2174 90285c3b 2019-01-08 stsp free(ondisk_path);
2175 1c4cdd89 2021-06-20 stsp free(parent);
2176 90285c3b 2019-01-08 stsp return err;
2179 708d8e67 2019-03-27 stsp static const struct got_error *
2180 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2181 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2182 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2184 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2185 708d8e67 2019-03-27 stsp unsigned char status;
2186 708d8e67 2019-03-27 stsp struct stat sb;
2187 708d8e67 2019-03-27 stsp char *ondisk_path;
2189 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2190 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2192 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2194 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2196 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2198 5a58a424 2020-06-23 stsp goto done;
2200 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2201 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2202 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2203 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2204 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2205 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2206 993e2a1b 2020-07-23 stsp goto done;
2208 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2209 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2210 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2211 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2213 993e2a1b 2020-07-23 stsp goto done;
2214 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2215 993e2a1b 2020-07-23 stsp ie->path);
2216 993e2a1b 2020-07-23 stsp goto done;
2219 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2220 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2221 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2223 5a58a424 2020-06-23 stsp goto done;
2225 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2226 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2228 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2229 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2231 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2233 5a58a424 2020-06-23 stsp goto done;
2234 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2235 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2237 5a58a424 2020-06-23 stsp goto done;
2239 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2242 5a58a424 2020-06-23 stsp free(ondisk_path);
2243 fc6346c4 2019-03-27 stsp return err;
2246 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2247 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2248 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2249 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2250 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2251 8da9e5f4 2019-01-12 stsp void *progress_arg;
2252 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2253 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2256 512f0d0e 2019-01-02 stsp static const struct got_error *
2257 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2258 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2260 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2262 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2263 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2265 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2266 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2269 8da9e5f4 2019-01-12 stsp static const struct got_error *
2270 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2272 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2274 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2275 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2277 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2278 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2281 9d31a1d8 2018-03-11 stsp static const struct got_error *
2282 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2284 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2285 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2286 8da9e5f4 2019-01-12 stsp char *path;
2288 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2289 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2291 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2292 63c5ca5d 2019-08-24 stsp return NULL;
2294 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2295 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2297 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2299 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2300 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2302 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2303 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2305 8da9e5f4 2019-01-12 stsp free(path);
2306 0cd1c46a 2019-03-11 stsp return err;
2309 b2118c49 2020-07-28 stsp const struct got_error *
2310 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2312 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2314 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2315 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2316 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2317 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2320 b2118c49 2020-07-28 stsp return NULL;
2323 818c7501 2019-07-11 stsp static const struct got_error *
2324 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2326 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2327 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2329 517bab73 2019-03-11 stsp *refname = NULL;
2331 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2333 b2118c49 2020-07-28 stsp return err;
2335 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2336 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2337 0647c563 2019-03-11 stsp *refname = NULL;
2339 517bab73 2019-03-11 stsp free(uuidstr);
2340 517bab73 2019-03-11 stsp return err;
2343 818c7501 2019-07-11 stsp const struct got_error *
2344 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2345 f6cd0243 2023-01-28 thomas const char *prefix)
2347 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2350 fcfaa6d5 2023-07-05 thomas static const struct got_error *
2351 fcfaa6d5 2023-07-05 thomas get_base_ref_name(char **refname, struct got_worktree *worktree)
2353 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2356 818c7501 2019-07-11 stsp static const struct got_error *
2357 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2359 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2360 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2363 818c7501 2019-07-11 stsp static const struct got_error *
2364 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2366 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2369 818c7501 2019-07-11 stsp static const struct got_error *
2370 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2372 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2373 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2376 818c7501 2019-07-11 stsp static const struct got_error *
2377 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2379 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2380 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2383 0ebf8283 2019-07-24 stsp static const struct got_error *
2384 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2386 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2387 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2390 0ebf8283 2019-07-24 stsp static const struct got_error *
2391 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2393 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2394 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2397 0ebf8283 2019-07-24 stsp static const struct got_error *
2398 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2400 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2401 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2404 0ebf8283 2019-07-24 stsp static const struct got_error *
2405 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2407 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2408 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2411 0ebf8283 2019-07-24 stsp const struct got_error *
2412 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2413 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2415 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2416 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2417 0ebf8283 2019-07-24 stsp *path = NULL;
2418 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2420 0ebf8283 2019-07-24 stsp return NULL;
2423 10604dce 2021-09-24 thomas static const struct got_error *
2424 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2426 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2427 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2430 10604dce 2021-09-24 thomas static const struct got_error *
2431 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2433 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2434 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2438 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2439 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2441 517bab73 2019-03-11 stsp static const struct got_error *
2442 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2444 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2445 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2446 517bab73 2019-03-11 stsp char *refname;
2448 fcfaa6d5 2023-07-05 thomas err = get_base_ref_name(&refname, worktree);
2450 517bab73 2019-03-11 stsp return err;
2452 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2454 0cd1c46a 2019-03-11 stsp goto done;
2456 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2458 0cd1c46a 2019-03-11 stsp free(refname);
2460 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2461 3e3a69f1 2019-07-25 stsp return err;
2464 3e3a69f1 2019-07-25 stsp static const struct got_error *
2465 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2467 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2469 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2470 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2471 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2472 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2474 9d31a1d8 2018-03-11 stsp return err;
2478 ebf99748 2019-05-09 stsp static const struct got_error *
2479 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2480 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2482 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2483 ebf99748 2019-05-09 stsp FILE *index = NULL;
2485 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2486 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2487 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2488 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2490 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2492 ebf99748 2019-05-09 stsp goto done;
2494 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2495 ebf99748 2019-05-09 stsp if (index == NULL) {
2496 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2497 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2499 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2500 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2501 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2504 ebf99748 2019-05-09 stsp if (err) {
2505 ebf99748 2019-05-09 stsp free(*fileindex_path);
2506 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2507 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2508 ebf99748 2019-05-09 stsp *fileindex = NULL;
2510 ebf99748 2019-05-09 stsp return err;
2513 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2514 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2515 c932eeeb 2019-05-22 stsp const char *path;
2516 c932eeeb 2019-05-22 stsp size_t path_len;
2517 c932eeeb 2019-05-22 stsp const char *entry_name;
2518 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2519 a484d721 2019-06-10 stsp void *progress_arg;
2522 c932eeeb 2019-05-22 stsp static const struct got_error *
2523 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2525 1ee397ad 2019-07-12 stsp const struct got_error *err;
2526 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2528 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2529 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2530 c932eeeb 2019-05-22 stsp return NULL;
2531 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2532 102ff934 2019-06-11 stsp return NULL;
2534 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2535 a769b60b 2021-06-27 stsp return NULL;
2537 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2538 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2539 c932eeeb 2019-05-22 stsp return NULL;
2541 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2542 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2543 edd02c5e 2019-07-11 stsp ie->path);
2545 1ee397ad 2019-07-12 stsp return err;
2547 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2548 c932eeeb 2019-05-22 stsp return NULL;
2551 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2552 a615e0e7 2020-12-16 stsp static const struct got_error *
2553 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2554 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2555 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2557 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2559 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2560 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2561 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2562 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2563 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2564 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2566 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2567 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2570 9c6338c4 2019-06-02 stsp static const struct got_error *
2571 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2573 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2574 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2575 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2576 867630bb 2020-01-17 stsp struct timespec timeout;
2578 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2579 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2581 9c6338c4 2019-06-02 stsp goto done;
2583 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2585 9c6338c4 2019-06-02 stsp goto done;
2587 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2588 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2589 9c6338c4 2019-06-02 stsp fileindex_path);
2590 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2594 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2595 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2596 867630bb 2020-01-17 stsp * was recorded in the file index.
2598 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2599 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2600 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2602 9c6338c4 2019-06-02 stsp if (new_index)
2603 9c6338c4 2019-06-02 stsp fclose(new_index);
2604 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2605 9c6338c4 2019-06-02 stsp return err;
2608 6ced4176 2019-07-12 stsp static const struct got_error *
2609 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2610 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2611 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2612 945f9229 2022-04-16 thomas struct got_repository *repo)
2614 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2615 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2616 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2617 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2619 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2620 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2621 6ced4176 2019-07-12 stsp *tree_id = NULL;
2623 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2624 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2625 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2626 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2627 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2628 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2629 6ced4176 2019-07-12 stsp goto done;
2631 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2632 945f9229 2022-04-16 thomas worktree->path_prefix);
2634 6ced4176 2019-07-12 stsp goto done;
2635 6ced4176 2019-07-12 stsp return NULL;
2638 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2640 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2641 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2642 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2643 6ced4176 2019-07-12 stsp goto done;
2646 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2648 6ced4176 2019-07-12 stsp goto done;
2650 6ced4176 2019-07-12 stsp free(in_repo_path);
2651 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2653 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2655 6ced4176 2019-07-12 stsp goto done;
2657 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2658 6ced4176 2019-07-12 stsp /* Check out a single file. */
2659 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2660 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2661 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2662 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2663 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2664 6ced4176 2019-07-12 stsp goto done;
2666 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2667 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2668 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2669 6ced4176 2019-07-12 stsp goto done;
2672 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2673 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2675 6ced4176 2019-07-12 stsp return err;
2676 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2677 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2678 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2679 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2680 6ced4176 2019-07-12 stsp goto done;
2683 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2684 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2686 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2687 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2688 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2689 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2690 6ced4176 2019-07-12 stsp goto done;
2692 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2693 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2694 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2695 6ced4176 2019-07-12 stsp goto done;
2700 6ced4176 2019-07-12 stsp free(in_repo_path);
2701 6ced4176 2019-07-12 stsp if (err) {
2702 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2703 6ced4176 2019-07-12 stsp free(*tree_relpath);
2704 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2705 6ced4176 2019-07-12 stsp free(*tree_id);
2706 6ced4176 2019-07-12 stsp *tree_id = NULL;
2708 07ed472d 2019-07-12 stsp return err;
2711 07ed472d 2019-07-12 stsp static const struct got_error *
2712 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2713 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2714 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2715 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2717 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2718 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2719 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2720 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2721 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2723 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2724 7f47418f 2019-12-20 stsp if (err) {
2725 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2726 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2727 7f47418f 2019-12-20 stsp goto done;
2728 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2729 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2731 7f47418f 2019-12-20 stsp return err;
2734 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2735 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2737 07ed472d 2019-07-12 stsp goto done;
2739 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2741 07ed472d 2019-07-12 stsp goto done;
2743 07ed472d 2019-07-12 stsp if (entry_name &&
2744 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2745 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2746 07ed472d 2019-07-12 stsp goto done;
2749 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2750 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2751 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2752 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2753 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2754 07ed472d 2019-07-12 stsp arg.repo = repo;
2755 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2756 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2757 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2758 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2759 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2760 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2763 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2764 07ed472d 2019-07-12 stsp if (commit)
2765 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2766 6ced4176 2019-07-12 stsp return err;
2769 9d31a1d8 2018-03-11 stsp const struct got_error *
2770 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2771 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2772 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2773 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2775 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2776 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2777 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2778 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2779 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2780 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2781 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2782 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2783 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2784 f2ea84fa 2019-07-27 stsp int entry_type;
2785 f2ea84fa 2019-07-27 stsp char *relpath;
2786 f2ea84fa 2019-07-27 stsp char *entry_name;
2787 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2788 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2790 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2792 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2794 9d31a1d8 2018-03-11 stsp return err;
2796 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2797 945f9229 2022-04-16 thomas worktree->base_commit_id);
2798 945f9229 2022-04-16 thomas if (err)
2799 945f9229 2022-04-16 thomas goto done;
2801 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2802 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2803 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2804 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2805 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2806 f2ea84fa 2019-07-27 stsp goto done;
2809 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2810 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2811 945f9229 2022-04-16 thomas worktree, repo);
2812 f2ea84fa 2019-07-27 stsp if (err) {
2813 f2ea84fa 2019-07-27 stsp free(tpd);
2814 6ced4176 2019-07-12 stsp goto done;
2817 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2818 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2819 f2ea84fa 2019-07-27 stsp if (err) {
2820 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2821 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2822 f2ea84fa 2019-07-27 stsp free(tpd);
2823 f2ea84fa 2019-07-27 stsp goto done;
2826 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2828 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2832 51514078 2018-12-25 stsp * Read the file index.
2833 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2834 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2836 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2838 c4cdcb68 2019-04-03 stsp goto done;
2840 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2841 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2842 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2844 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2845 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2846 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2850 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2851 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2852 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2853 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2854 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2855 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2856 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2857 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2861 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2863 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2864 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2865 af12c6b9 2019-06-04 stsp err = sync_err;
2867 9c6338c4 2019-06-02 stsp free(fileindex_path);
2869 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2870 9d31a1d8 2018-03-11 stsp if (commit)
2871 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2872 5ade8233 2019-07-12 stsp if (fileindex)
2873 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2874 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2875 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2876 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2877 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2878 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2879 f2ea84fa 2019-07-27 stsp free(tpd);
2881 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2882 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2883 9d31a1d8 2018-03-11 stsp err = unlockerr;
2884 f8d1f275 2019-02-04 stsp return err;
2887 0529f8df 2023-03-10 thomas static const struct got_error *
2888 0529f8df 2023-03-10 thomas add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2889 0529f8df 2023-03-10 thomas struct got_fileindex_entry *ie, const char *ondisk_path,
2890 0529f8df 2023-03-10 thomas const char *path2, struct got_blob_object *blob2, mode_t mode2,
2891 0529f8df 2023-03-10 thomas int restoring_missing_file, int reverting_versioned_file,
2892 0529f8df 2023-03-10 thomas int path_is_unversioned, int allow_bad_symlinks,
2893 0529f8df 2023-03-10 thomas struct got_repository *repo,
2894 0529f8df 2023-03-10 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
2896 0529f8df 2023-03-10 thomas const struct got_error *err = NULL;
2897 0529f8df 2023-03-10 thomas int is_bad_symlink = 0;
2899 0529f8df 2023-03-10 thomas if (S_ISLNK(mode2)) {
2900 0529f8df 2023-03-10 thomas err = install_symlink(&is_bad_symlink,
2901 0529f8df 2023-03-10 thomas worktree, ondisk_path, path2, blob2,
2902 0529f8df 2023-03-10 thomas restoring_missing_file,
2903 0529f8df 2023-03-10 thomas reverting_versioned_file,
2904 0529f8df 2023-03-10 thomas path_is_unversioned, allow_bad_symlinks,
2905 0529f8df 2023-03-10 thomas repo, progress_cb, progress_arg);
2906 0529f8df 2023-03-10 thomas } else {
2907 0529f8df 2023-03-10 thomas err = install_blob(worktree, ondisk_path, path2,
2908 0529f8df 2023-03-10 thomas mode2, GOT_DEFAULT_FILE_MODE, blob2,
2909 0529f8df 2023-03-10 thomas restoring_missing_file, reverting_versioned_file, 0,
2910 0529f8df 2023-03-10 thomas path_is_unversioned, repo, progress_cb, progress_arg);
2912 0529f8df 2023-03-10 thomas if (err)
2913 0529f8df 2023-03-10 thomas return err;
2914 0529f8df 2023-03-10 thomas if (ie == NULL) {
2915 0529f8df 2023-03-10 thomas /* Adding an unversioned file. */
2916 0529f8df 2023-03-10 thomas err = got_fileindex_entry_alloc(&ie, path2);
2917 0529f8df 2023-03-10 thomas if (err)
2918 0529f8df 2023-03-10 thomas return err;
2919 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2920 0529f8df 2023-03-10 thomas worktree->root_fd, path2, NULL, NULL, 1);
2921 0529f8df 2023-03-10 thomas if (err) {
2922 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2923 0529f8df 2023-03-10 thomas return err;
2925 0529f8df 2023-03-10 thomas err = got_fileindex_entry_add(fileindex, ie);
2926 0529f8df 2023-03-10 thomas if (err) {
2927 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2928 0529f8df 2023-03-10 thomas return err;
2930 0529f8df 2023-03-10 thomas } else {
2931 0529f8df 2023-03-10 thomas /* Re-adding a locally deleted file. */
2932 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2933 0529f8df 2023-03-10 thomas worktree->root_fd, path2, ie->blob_sha1,
2934 0529f8df 2023-03-10 thomas worktree->base_commit_id->sha1, 0);
2935 0529f8df 2023-03-10 thomas if (err)
2936 0529f8df 2023-03-10 thomas return err;
2939 0529f8df 2023-03-10 thomas if (is_bad_symlink) {
2940 0529f8df 2023-03-10 thomas got_fileindex_entry_filetype_set(ie,
2941 0529f8df 2023-03-10 thomas GOT_FILEIDX_MODE_BAD_SYMLINK);
2944 0529f8df 2023-03-10 thomas return NULL;
2947 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2948 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2949 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2950 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2951 234035bc 2019-06-01 stsp void *progress_arg;
2952 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2953 234035bc 2019-06-01 stsp void *cancel_arg;
2954 f69721c3 2019-10-21 stsp const char *label_orig;
2955 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2956 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2959 234035bc 2019-06-01 stsp static const struct got_error *
2960 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2961 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2962 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2963 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2964 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2966 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2967 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2968 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2969 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2970 234035bc 2019-06-01 stsp struct stat sb;
2971 234035bc 2019-06-01 stsp unsigned char status;
2972 234035bc 2019-06-01 stsp int local_changes_subsumed;
2973 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2974 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2976 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2977 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2978 d6c87207 2019-08-02 stsp strlen(path2));
2979 1ee397ad 2019-07-12 stsp if (ie == NULL)
2980 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2981 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2983 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2984 234035bc 2019-06-01 stsp path2) == -1)
2985 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2987 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2990 234035bc 2019-06-01 stsp goto done;
2992 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2993 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2994 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2995 234035bc 2019-06-01 stsp goto done;
2997 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2998 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2999 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3000 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3001 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3002 234035bc 2019-06-01 stsp goto done;
3005 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3006 36bf999c 2020-07-23 stsp char *link_target2;
3007 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3009 36bf999c 2020-07-23 stsp goto done;
3010 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3011 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3012 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3013 36bf999c 2020-07-23 stsp free(link_target2);
3017 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3018 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3019 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3020 1af628f4 2021-06-03 stsp goto done;
3022 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3023 1af628f4 2021-06-03 stsp f_orig, blob1);
3025 1af628f4 2021-06-03 stsp goto done;
3027 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3028 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3029 1af628f4 2021-06-03 stsp goto done;
3030 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3031 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3033 1af628f4 2021-06-03 stsp goto done;
3035 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
3036 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3037 1af628f4 2021-06-03 stsp if (fd == -1) {
3038 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3039 1af628f4 2021-06-03 stsp ondisk_path);
3040 1af628f4 2021-06-03 stsp goto done;
3042 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3043 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3044 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3045 1af628f4 2021-06-03 stsp ondisk_path);
3046 1af628f4 2021-06-03 stsp close(fd);
3047 1af628f4 2021-06-03 stsp goto done;
3049 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3051 1af628f4 2021-06-03 stsp goto done;
3052 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3053 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3054 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3055 1af628f4 2021-06-03 stsp goto done;
3057 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3058 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3059 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
3060 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3061 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3063 234035bc 2019-06-01 stsp } else if (blob1) {
3064 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3065 d6c87207 2019-08-02 stsp strlen(path1));
3066 1ee397ad 2019-07-12 stsp if (ie == NULL)
3067 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3068 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3070 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3071 2b92fad7 2019-06-02 stsp path1) == -1)
3072 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3074 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3077 2b92fad7 2019-06-02 stsp goto done;
3079 2b92fad7 2019-06-02 stsp switch (status) {
3080 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3081 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3082 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3084 1ee397ad 2019-07-12 stsp goto done;
3085 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3087 2b92fad7 2019-06-02 stsp goto done;
3089 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3091 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3092 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3093 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3094 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3096 1ee397ad 2019-07-12 stsp goto done;
3098 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3100 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3101 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3102 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3103 e8f02263 2022-01-23 thomas off_t blob1_size;
3105 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3106 0a22ca1a 2020-09-23 stsp * exists in the repository.
3108 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
3109 e8f02263 2022-01-23 thomas &blob1_size, path1);
3111 0a22ca1a 2020-09-23 stsp goto done;
3112 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3113 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3114 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3116 0a22ca1a 2020-09-23 stsp goto done;
3117 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3120 0a22ca1a 2020-09-23 stsp goto done;
3122 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3125 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3126 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3128 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3129 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3132 0a22ca1a 2020-09-23 stsp goto done;
3135 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3136 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3137 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3138 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3140 1ee397ad 2019-07-12 stsp goto done;
3142 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3143 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3145 1ee397ad 2019-07-12 stsp goto done;
3150 234035bc 2019-06-01 stsp } else if (blob2) {
3151 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3152 234035bc 2019-06-01 stsp path2) == -1)
3153 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3154 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3155 d6c87207 2019-08-02 stsp strlen(path2));
3157 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3158 7f91a133 2019-12-13 stsp -1, NULL, repo);
3160 234035bc 2019-06-01 stsp goto done;
3161 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3162 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3163 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3164 0529f8df 2023-03-10 thomas status != GOT_STATUS_ADD &&
3165 0529f8df 2023-03-10 thomas status != GOT_STATUS_DELETE) {
3166 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3167 1ee397ad 2019-07-12 stsp status, path2);
3168 234035bc 2019-06-01 stsp goto done;
3170 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3171 36bf999c 2020-07-23 stsp char *link_target2;
3172 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3175 36bf999c 2020-07-23 stsp goto done;
3176 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3177 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3178 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3179 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3180 36bf999c 2020-07-23 stsp free(link_target2);
3181 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3182 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3183 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3184 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3185 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3186 526a746f 2020-07-23 stsp a->progress_arg);
3187 0529f8df 2023-03-10 thomas } else if (status != GOT_STATUS_DELETE) {
3188 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3189 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3192 dfe9fba0 2020-07-23 stsp goto done;
3193 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3194 0529f8df 2023-03-10 thomas /* Re-add file with content from new blob. */
3195 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, ie,
3196 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3197 0529f8df 2023-03-10 thomas 0, 0, 0, a->allow_bad_symlinks,
3198 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3200 234035bc 2019-06-01 stsp goto done;
3203 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, NULL,
3204 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3205 0529f8df 2023-03-10 thomas 0, 0, 1, a->allow_bad_symlinks,
3206 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3208 2b92fad7 2019-06-02 stsp goto done;
3212 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3213 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3214 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3215 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3216 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3217 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3218 1af628f4 2021-06-03 stsp free(id_str);
3219 54d5be07 2021-06-03 stsp free(label_deriv2);
3220 234035bc 2019-06-01 stsp free(ondisk_path);
3221 234035bc 2019-06-01 stsp return err;
3224 69de9dd4 2021-09-03 stsp static const struct got_error *
3225 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3227 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3229 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3230 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3231 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3232 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3233 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3235 69de9dd4 2021-09-03 stsp return NULL;
3238 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3239 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3240 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3241 234035bc 2019-06-01 stsp struct got_repository *repo;
3244 234035bc 2019-06-01 stsp static const struct got_error *
3245 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3246 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3247 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3248 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3249 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3251 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3252 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3253 234035bc 2019-06-01 stsp unsigned char status;
3254 234035bc 2019-06-01 stsp struct stat sb;
3255 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3256 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3257 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3258 234035bc 2019-06-01 stsp char *ondisk_path;
3260 69de9dd4 2021-09-03 stsp if (id == NULL)
3261 69de9dd4 2021-09-03 stsp return NULL;
3263 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3264 69de9dd4 2021-09-03 stsp if (ie == NULL)
3265 69de9dd4 2021-09-03 stsp return NULL;
3267 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3269 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3271 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3272 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3273 5546d466 2021-09-02 stsp free(ondisk_path);
3275 234035bc 2019-06-01 stsp return err;
3276 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3277 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3279 234035bc 2019-06-01 stsp return NULL;
3282 818c7501 2019-07-11 stsp static const struct got_error *
3283 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3284 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3285 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3286 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3287 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3289 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3290 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3291 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3292 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3293 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3294 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3295 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3296 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3297 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3299 03415a1a 2019-06-02 stsp if (commit_id1) {
3300 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3301 945f9229 2022-04-16 thomas if (err)
3302 945f9229 2022-04-16 thomas goto done;
3303 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3304 03415a1a 2019-06-02 stsp worktree->path_prefix);
3305 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3306 03415a1a 2019-06-02 stsp goto done;
3308 69d57f3d 2020-07-31 stsp if (tree_id1) {
3309 69d57f3d 2020-07-31 stsp char *id_str;
3311 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3313 03415a1a 2019-06-02 stsp goto done;
3315 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3317 f69721c3 2019-10-21 stsp goto done;
3319 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3320 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3321 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3322 f69721c3 2019-10-21 stsp free(id_str);
3323 f69721c3 2019-10-21 stsp goto done;
3325 f69721c3 2019-10-21 stsp free(id_str);
3327 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3328 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3329 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3330 a0f32f33 2022-06-13 thomas goto done;
3334 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3335 945f9229 2022-04-16 thomas if (err)
3336 945f9229 2022-04-16 thomas goto done;
3338 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3339 234035bc 2019-06-01 stsp worktree->path_prefix);
3341 234035bc 2019-06-01 stsp goto done;
3343 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3345 234035bc 2019-06-01 stsp goto done;
3347 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3348 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3349 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3350 19a6a6b5 2022-07-01 thomas goto done;
3353 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3354 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3355 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3356 a0f32f33 2022-06-13 thomas goto done;
3359 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3360 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3361 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3362 19a6a6b5 2022-07-01 thomas goto done;
3365 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3366 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3367 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3368 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3369 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3371 69de9dd4 2021-09-03 stsp goto done;
3373 234035bc 2019-06-01 stsp arg.worktree = worktree;
3374 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3375 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3376 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3377 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3378 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3379 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3380 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3381 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3382 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3383 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3384 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3385 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3386 af12c6b9 2019-06-04 stsp err = sync_err;
3388 945f9229 2022-04-16 thomas if (commit1)
3389 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3390 945f9229 2022-04-16 thomas if (commit2)
3391 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3392 234035bc 2019-06-01 stsp if (tree1)
3393 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3394 234035bc 2019-06-01 stsp if (tree2)
3395 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3396 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3397 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3398 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3399 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3400 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3401 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3402 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3403 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3404 f69721c3 2019-10-21 stsp free(label_orig);
3405 818c7501 2019-07-11 stsp return err;
3408 818c7501 2019-07-11 stsp const struct got_error *
3409 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3410 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3411 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3412 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3414 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3415 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3416 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3418 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3420 818c7501 2019-07-11 stsp return err;
3422 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3424 818c7501 2019-07-11 stsp goto done;
3426 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3427 69de9dd4 2021-09-03 stsp worktree);
3429 818c7501 2019-07-11 stsp goto done;
3431 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3432 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3433 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3435 818c7501 2019-07-11 stsp if (fileindex)
3436 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3437 818c7501 2019-07-11 stsp free(fileindex_path);
3438 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3439 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3440 234035bc 2019-06-01 stsp err = unlockerr;
3441 234035bc 2019-06-01 stsp return err;
3444 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3445 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3446 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3447 927df6b7 2019-02-10 stsp const char *status_path;
3448 927df6b7 2019-02-10 stsp size_t status_path_len;
3449 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3450 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3451 f8d1f275 2019-02-04 stsp void *status_arg;
3452 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3453 f8d1f275 2019-02-04 stsp void *cancel_arg;
3454 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3455 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3456 f2a9dc41 2019-12-13 tracey int report_unchanged;
3457 3143d852 2020-06-25 stsp int no_ignores;
3460 f8d1f275 2019-02-04 stsp static const struct got_error *
3461 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3462 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3463 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3464 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3466 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3467 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3468 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3469 927df6b7 2019-02-10 stsp struct stat sb;
3470 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3471 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3472 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3474 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3475 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3477 98eaaa12 2019-08-03 stsp return err;
3479 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3480 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3481 98eaaa12 2019-08-03 stsp return NULL;
3483 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3484 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3485 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3486 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3487 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3488 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3489 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3490 43010591 2023-02-17 thomas &staged_blob_id, ie);
3493 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3494 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3497 927df6b7 2019-02-10 stsp static const struct got_error *
3498 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3499 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3501 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3502 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3503 f8d1f275 2019-02-04 stsp char *abspath;
3505 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3506 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3508 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3509 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3510 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3511 927df6b7 2019-02-10 stsp return NULL;
3513 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3514 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3515 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3516 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3518 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3519 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3520 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3523 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3524 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3525 f8d1f275 2019-02-04 stsp free(abspath);
3526 9d31a1d8 2018-03-11 stsp return err;
3529 f8d1f275 2019-02-04 stsp static const struct got_error *
3530 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3532 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3533 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3534 2ec1f75b 2019-03-26 stsp unsigned char status;
3536 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3537 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3539 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3540 927df6b7 2019-02-10 stsp return NULL;
3542 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3543 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3544 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3545 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3547 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3548 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3549 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3552 ef20f542 2022-06-26 thomas static void
3553 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3555 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3557 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3558 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3560 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3562 21c2d8be 2023-01-10 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3565 6841da00 2019-08-08 stsp static const struct got_error *
3566 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3568 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3569 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3570 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3571 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3572 6841da00 2019-08-08 stsp size_t linesize = 0;
3573 6841da00 2019-08-08 stsp ssize_t linelen;
3575 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3576 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3577 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3578 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3580 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3581 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3582 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3584 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3585 bd8de430 2019-10-04 stsp if (line[0] == '#')
3588 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3589 bd8de430 2019-10-04 stsp if (line[0] == '!')
3592 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3593 6841da00 2019-08-08 stsp line) == -1) {
3594 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3595 6841da00 2019-08-08 stsp goto done;
3597 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3599 6841da00 2019-08-08 stsp goto done;
3601 6841da00 2019-08-08 stsp if (ferror(f)) {
3602 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3603 6841da00 2019-08-08 stsp goto done;
3606 6841da00 2019-08-08 stsp dirpath = strdup(path);
3607 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3608 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3609 6841da00 2019-08-08 stsp goto done;
3611 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3613 6841da00 2019-08-08 stsp free(line);
3614 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3615 6841da00 2019-08-08 stsp free(dirpath);
3616 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3618 6841da00 2019-08-08 stsp return err;
3621 1b5d300f 2023-02-20 thomas static int
3622 1b5d300f 2023-02-20 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
3623 1b5d300f 2023-02-20 thomas int flags)
3625 1b5d300f 2023-02-20 thomas char buf[PATH_MAX];
3628 1b5d300f 2023-02-20 thomas * Trailing slashes signify directories.
3629 1b5d300f 2023-02-20 thomas * Append a * to make such patterns conform to fnmatch rules.
3631 1b5d300f 2023-02-20 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3632 1b5d300f 2023-02-20 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3633 1b5d300f 2023-02-20 thomas return FNM_NOMATCH; /* XXX */
3635 1b5d300f 2023-02-20 thomas return fnmatch(buf, path, flags);
3638 1b5d300f 2023-02-20 thomas return fnmatch(pattern, path, flags);
3641 ef20f542 2022-06-26 thomas static int
3642 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3644 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3646 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3647 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3648 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3649 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3651 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3652 1b5d300f 2023-02-20 thomas const char *p;
3654 1b5d300f 2023-02-20 thomas if (pi->path_len < 3 ||
3655 1b5d300f 2023-02-20 thomas strncmp(pi->path, "**/", 3) != 0)
3658 bd8de430 2019-10-04 stsp while (*p) {
3659 1b5d300f 2023-02-20 thomas if (match_path(pi->path + 3,
3660 1b5d300f 2023-02-20 thomas pi->path_len - 3, p,
3661 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3662 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3663 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3665 bd8de430 2019-10-04 stsp while (*p == '/')
3675 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3676 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3677 6841da00 2019-08-08 stsp * ignores backwards.
3679 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3680 6841da00 2019-08-08 stsp while (pe) {
3681 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3682 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3683 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3684 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3685 1b5d300f 2023-02-20 thomas int flags = FNM_LEADING_DIR;
3686 1b5d300f 2023-02-20 thomas if (strstr(pi->path, "/**/") == NULL)
3687 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3688 1b5d300f 2023-02-20 thomas if (match_path(pi->path, pi->path_len,
3689 1b5d300f 2023-02-20 thomas path, flags))
3694 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3700 6841da00 2019-08-08 stsp static const struct got_error *
3701 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3702 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3704 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3705 6841da00 2019-08-08 stsp char *ignorespath;
3706 886cec17 2019-12-15 stsp int fd = -1;
3707 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3709 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3710 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3711 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3713 886cec17 2019-12-15 stsp if (dirfd != -1) {
3714 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3715 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3716 886cec17 2019-12-15 stsp if (fd == -1) {
3717 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3718 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3719 886cec17 2019-12-15 stsp ignorespath);
3721 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3722 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3723 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3724 886cec17 2019-12-15 stsp ignorespath);
3727 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3731 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3732 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3733 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3734 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3735 886cec17 2019-12-15 stsp ignorespath);
3737 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3740 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3741 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3742 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3743 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3744 6841da00 2019-08-08 stsp free(ignorespath);
3745 6841da00 2019-08-08 stsp return err;
3748 f8d1f275 2019-02-04 stsp static const struct got_error *
3749 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3750 6092c299 2021-10-04 thomas int dirfd)
3752 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3753 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3754 f8d1f275 2019-02-04 stsp char *path = NULL;
3756 6092c299 2021-10-04 thomas if (ignore != NULL)
3757 6092c299 2021-10-04 thomas *ignore = 0;
3759 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3760 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3762 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3763 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3764 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3766 f8d1f275 2019-02-04 stsp path = de->d_name;
3769 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3770 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3771 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3772 6092c299 2021-10-04 thomas *ignore = 1;
3773 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3774 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3775 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3776 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3777 f8d1f275 2019-02-04 stsp if (parent_path[0])
3778 f8d1f275 2019-02-04 stsp free(path);
3779 b72f483a 2019-02-05 stsp return err;
3782 347d1d3e 2019-07-12 stsp static const struct got_error *
3783 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3785 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3786 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3788 3143d852 2020-06-25 stsp if (a->no_ignores)
3789 3143d852 2020-06-25 stsp return NULL;
3791 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3792 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3794 3143d852 2020-06-25 stsp return err;
3796 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3797 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3799 3143d852 2020-06-25 stsp return err;
3802 3143d852 2020-06-25 stsp static const struct got_error *
3803 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3804 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3805 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3806 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3808 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3809 abb4604f 2019-07-27 stsp struct stat sb;
3811 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3813 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3814 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3816 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3817 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3818 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3819 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3820 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3823 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3824 fe1d8685 2022-02-12 thomas return NULL;
3826 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3827 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3828 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3830 abb4604f 2019-07-27 stsp return NULL;
3833 3143d852 2020-06-25 stsp static const struct got_error *
3834 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3835 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3837 3143d852 2020-06-25 stsp const struct got_error *err;
3838 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3840 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3841 3143d852 2020-06-25 stsp ".cvsignore");
3843 3143d852 2020-06-25 stsp return err;
3845 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3846 3143d852 2020-06-25 stsp ".gitignore");
3848 3143d852 2020-06-25 stsp return err;
3850 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3851 3143d852 2020-06-25 stsp if (err) {
3852 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3853 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3854 3143d852 2020-06-25 stsp return err;
3856 3143d852 2020-06-25 stsp for (;;) {
3857 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3858 3143d852 2020-06-25 stsp ".cvsignore");
3861 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3862 3143d852 2020-06-25 stsp ".gitignore");
3865 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3866 3143d852 2020-06-25 stsp if (err) {
3867 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3868 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3871 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3873 b737c85a 2020-06-26 stsp free(parent_path);
3874 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3875 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3878 b737c85a 2020-06-26 stsp free(parent_path);
3879 b737c85a 2020-06-26 stsp free(next_parent_path);
3880 3143d852 2020-06-25 stsp return err;
3883 f8836425 2023-04-14 thomas struct find_missing_children_args {
3884 f8836425 2023-04-14 thomas const char *parent_path;
3885 f8836425 2023-04-14 thomas size_t parent_len;
3886 f8836425 2023-04-14 thomas struct got_pathlist_head *children;
3887 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb;
3888 f8836425 2023-04-14 thomas void *cancel_arg;
3891 abb4604f 2019-07-27 stsp static const struct got_error *
3892 f8836425 2023-04-14 thomas find_missing_children(void *arg, struct got_fileindex_entry *ie)
3894 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
3895 f8836425 2023-04-14 thomas struct find_missing_children_args *a = arg;
3897 f8836425 2023-04-14 thomas if (a->cancel_cb) {
3898 f8836425 2023-04-14 thomas err = a->cancel_cb(a->cancel_arg);
3899 f8836425 2023-04-14 thomas if (err)
3900 f8836425 2023-04-14 thomas return err;
3903 f8836425 2023-04-14 thomas if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3904 f8836425 2023-04-14 thomas err = got_pathlist_append(a->children, ie->path, NULL);
3906 f8836425 2023-04-14 thomas return err;
3909 f8836425 2023-04-14 thomas static const struct got_error *
3910 f8836425 2023-04-14 thomas report_children(struct got_pathlist_head *children,
3911 f8836425 2023-04-14 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
3912 f8836425 2023-04-14 thomas struct got_repository *repo, int is_root_dir, int report_unchanged,
3913 f8836425 2023-04-14 thomas struct got_pathlist_head *ignores, int no_ignores,
3914 f8836425 2023-04-14 thomas got_worktree_status_cb status_cb, void *status_arg,
3915 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb, void *cancel_arg)
3917 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
3918 f8836425 2023-04-14 thomas struct got_pathlist_entry *pe;
3919 f8836425 2023-04-14 thomas char *ondisk_path = NULL;
3921 f8836425 2023-04-14 thomas TAILQ_FOREACH(pe, children, entry) {
3922 f8836425 2023-04-14 thomas if (cancel_cb) {
3923 f8836425 2023-04-14 thomas err = cancel_cb(cancel_arg);
3924 f8836425 2023-04-14 thomas if (err)
3928 f8836425 2023-04-14 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3929 f8836425 2023-04-14 thomas !is_root_dir ? "/" : "", pe->path) == -1) {
3930 f8836425 2023-04-14 thomas err = got_error_from_errno("asprintf");
3931 f8836425 2023-04-14 thomas ondisk_path = NULL;
3935 f8836425 2023-04-14 thomas err = report_single_file_status(pe->path, ondisk_path,
3936 f8836425 2023-04-14 thomas fileindex, status_cb, status_arg, repo, report_unchanged,
3937 f8836425 2023-04-14 thomas ignores, no_ignores);
3938 f8836425 2023-04-14 thomas if (err)
3941 f8836425 2023-04-14 thomas free(ondisk_path);
3942 f8836425 2023-04-14 thomas ondisk_path = NULL;
3945 f8836425 2023-04-14 thomas free(ondisk_path);
3946 f8836425 2023-04-14 thomas return err;
3949 f8836425 2023-04-14 thomas static const struct got_error *
3950 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3951 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3952 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3953 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3954 f2a9dc41 2019-12-13 tracey int report_unchanged)
3956 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3957 6fc93f37 2019-12-13 stsp int fd = -1;
3958 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3959 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3960 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3961 f8836425 2023-04-14 thomas struct got_pathlist_head ignores, missing_children;
3962 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3964 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3965 f8836425 2023-04-14 thomas TAILQ_INIT(&missing_children);
3967 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3968 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3969 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3971 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3972 a78810f8 2022-03-13 thomas if (ie) {
3973 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3974 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3975 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3976 a78810f8 2022-03-13 thomas goto done;
3977 f8836425 2023-04-14 thomas } else {
3978 f8836425 2023-04-14 thomas struct find_missing_children_args fmca;
3979 f8836425 2023-04-14 thomas fmca.parent_path = path;
3980 f8836425 2023-04-14 thomas fmca.parent_len = strlen(path);
3981 f8836425 2023-04-14 thomas fmca.children = &missing_children;
3982 f8836425 2023-04-14 thomas fmca.cancel_cb = cancel_cb;
3983 f8836425 2023-04-14 thomas fmca.cancel_arg = cancel_arg;
3984 f8836425 2023-04-14 thomas err = got_fileindex_for_each_entry_safe(fileindex,
3985 f8836425 2023-04-14 thomas find_missing_children, &fmca);
3986 f8836425 2023-04-14 thomas if (err)
3987 f8836425 2023-04-14 thomas goto done;
3990 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3991 6fc93f37 2019-12-13 stsp if (fd == -1) {
3992 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3993 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3994 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3996 ff56836b 2021-07-08 stsp if (!no_ignores) {
3997 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3998 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
4000 ff56836b 2021-07-08 stsp goto done;
4002 f8836425 2023-04-14 thomas if (TAILQ_EMPTY(&missing_children)) {
4003 f8836425 2023-04-14 thomas err = report_single_file_status(path,
4004 f8836425 2023-04-14 thomas ondisk_path, fileindex,
4005 f8836425 2023-04-14 thomas status_cb, status_arg, repo,
4006 f8836425 2023-04-14 thomas report_unchanged, &ignores, no_ignores);
4007 f8836425 2023-04-14 thomas if (err)
4008 f8836425 2023-04-14 thomas goto done;
4009 f8836425 2023-04-14 thomas } else {
4010 f8836425 2023-04-14 thomas err = report_children(&missing_children,
4011 f8836425 2023-04-14 thomas worktree, fileindex, repo,
4012 f8836425 2023-04-14 thomas (path[0] == '\0'), report_unchanged,
4013 f8836425 2023-04-14 thomas &ignores, no_ignores,
4014 f8836425 2023-04-14 thomas status_cb, status_arg,
4015 f8836425 2023-04-14 thomas cancel_cb, cancel_arg);
4016 6f97aa83 2023-04-14 thomas if (err)
4017 6f97aa83 2023-04-14 thomas goto done;
4021 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4022 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4023 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4024 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4025 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4026 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4027 abb4604f 2019-07-27 stsp arg.status_path = path;
4028 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4029 abb4604f 2019-07-27 stsp arg.repo = repo;
4030 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4031 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4032 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4033 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4034 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4035 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4036 022fae89 2019-12-06 tracey if (!no_ignores) {
4037 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4038 3143d852 2020-06-25 stsp worktree->root_path, path);
4040 3143d852 2020-06-25 stsp goto done;
4042 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4043 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4044 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4047 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4048 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4049 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4050 927df6b7 2019-02-10 stsp free(ondisk_path);
4051 347d1d3e 2019-07-12 stsp return err;
4054 347d1d3e 2019-07-12 stsp const struct got_error *
4055 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4056 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4057 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4058 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4060 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4061 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4062 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4063 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4065 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4067 347d1d3e 2019-07-12 stsp return err;
4069 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4070 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4071 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4072 f6343036 2021-06-22 stsp no_ignores, 0);
4076 f8d1f275 2019-02-04 stsp free(fileindex_path);
4077 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4078 f8d1f275 2019-02-04 stsp return err;
4081 6c7ab921 2019-03-18 stsp const struct got_error *
4082 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4083 6c7ab921 2019-03-18 stsp const char *arg)
4085 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4086 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4087 6c7ab921 2019-03-18 stsp size_t len;
4088 00bb5ea0 2020-07-23 stsp struct stat sb;
4089 01740607 2020-11-04 stsp char *abspath = NULL;
4090 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4092 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4094 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4095 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4096 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4098 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4099 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4100 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4101 00bb5ea0 2020-07-23 stsp goto done;
4103 727173c3 2020-11-06 stsp sb.st_mode = 0;
4105 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4107 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4108 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4109 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4110 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4112 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4113 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4114 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4115 00bb5ea0 2020-07-23 stsp goto done;
4119 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4120 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4122 d0710d08 2019-07-22 stsp goto done;
4123 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4124 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4125 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4126 00bb5ea0 2020-07-23 stsp goto done;
4129 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4130 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4131 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4132 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4133 00bb5ea0 2020-07-23 stsp goto done;
4135 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4136 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4137 01740607 2020-11-04 stsp goto done;
4139 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4140 01740607 2020-11-04 stsp sizeof(canonpath));
4142 00bb5ea0 2020-07-23 stsp goto done;
4143 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4144 01740607 2020-11-04 stsp if (resolved == NULL) {
4145 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4146 01740607 2020-11-04 stsp goto done;
4151 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4152 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4153 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4154 6c7ab921 2019-03-18 stsp goto done;
4157 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4158 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4159 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4161 f6d88e1a 2019-05-29 stsp goto done;
4163 f6d88e1a 2019-05-29 stsp path = strdup("");
4164 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4165 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4166 f6d88e1a 2019-05-29 stsp goto done;
4170 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4171 6c7ab921 2019-03-18 stsp len = strlen(path);
4172 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4173 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4177 01740607 2020-11-04 stsp free(abspath);
4178 6c7ab921 2019-03-18 stsp free(resolved);
4179 d0710d08 2019-07-22 stsp free(cwd);
4180 6c7ab921 2019-03-18 stsp if (err == NULL)
4181 6c7ab921 2019-03-18 stsp *wt_path = path;
4183 6c7ab921 2019-03-18 stsp free(path);
4184 d00136be 2019-03-26 stsp return err;
4187 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4188 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4189 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4190 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4191 4e68cba3 2019-11-23 stsp void *progress_arg;
4192 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4195 b21ebdb0 2023-06-22 thomas static int
4196 b21ebdb0 2023-06-22 thomas add_noop_status(unsigned char status)
4198 b21ebdb0 2023-06-22 thomas return (status == GOT_STATUS_ADD ||
4199 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODIFY ||
4200 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_CONFLICT ||
4201 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODE_CHANGE ||
4202 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_NO_CHANGE);
4205 4e68cba3 2019-11-23 stsp static const struct got_error *
4206 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4207 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4208 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4209 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4211 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4212 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4213 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4214 6d022e97 2019-08-04 stsp struct stat sb;
4215 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4217 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4218 dbb83fbd 2019-12-12 stsp relpath) == -1)
4219 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4221 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4223 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4224 12463d8b 2019-12-13 stsp de_name, a->repo);
4226 dbb83fbd 2019-12-12 stsp goto done;
4227 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4228 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE &&
4229 b21ebdb0 2023-06-22 thomas add_noop_status(status))
4230 dbb83fbd 2019-12-12 stsp goto done;
4231 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4233 dbb83fbd 2019-12-12 stsp goto done;
4236 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4237 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4238 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4240 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4241 dbb83fbd 2019-12-12 stsp goto done;
4244 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4246 4e68cba3 2019-11-23 stsp goto done;
4247 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4248 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4249 3969253a 2020-03-07 stsp if (err) {
4250 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4251 3969253a 2020-03-07 stsp goto done;
4253 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4254 07f5b47a 2019-06-02 stsp if (err) {
4255 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4256 4e68cba3 2019-11-23 stsp goto done;
4259 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4261 dbb83fbd 2019-12-12 stsp return err;
4262 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4263 dbb83fbd 2019-12-12 stsp return NULL;
4264 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4267 d00136be 2019-03-26 stsp const struct got_error *
4268 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4269 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4270 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4271 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4273 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4274 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4275 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4276 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4277 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4279 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4281 d00136be 2019-03-26 stsp return err;
4283 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4285 d00136be 2019-03-26 stsp goto done;
4287 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4288 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4289 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4290 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4291 4e68cba3 2019-11-23 stsp saa.repo = repo;
4293 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4294 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4295 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4299 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4300 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4301 af12c6b9 2019-06-04 stsp err = sync_err;
4303 fb399478 2019-07-12 stsp free(fileindex_path);
4304 d00136be 2019-03-26 stsp if (fileindex)
4305 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4306 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4307 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4308 d00136be 2019-03-26 stsp err = unlockerr;
4309 6c7ab921 2019-03-18 stsp return err;
4312 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4313 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4314 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4315 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4316 f2a9dc41 2019-12-13 tracey void *progress_arg;
4317 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4318 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4319 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4320 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4321 8ec7352a 2023-06-01 thomas const char *status_path;
4322 8ec7352a 2023-06-01 thomas size_t status_path_len;
4323 766841c2 2020-08-13 stsp const char *status_codes;
4326 f2a9dc41 2019-12-13 tracey static const struct got_error *
4327 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4328 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4329 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4330 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4332 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4333 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4334 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4335 17ed4618 2019-06-02 stsp struct stat sb;
4336 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4338 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4339 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4340 d64cc78a 2022-01-25 thomas return NULL;
4341 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4344 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4345 17ed4618 2019-06-02 stsp if (ie == NULL)
4346 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4348 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4349 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4350 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4351 9acbc4fa 2019-08-03 stsp return NULL;
4352 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4355 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4356 f2a9dc41 2019-12-13 tracey relpath) == -1)
4357 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4359 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4362 f2a9dc41 2019-12-13 tracey goto done;
4364 766841c2 2020-08-13 stsp if (a->status_codes) {
4365 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4367 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4368 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4371 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4372 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4373 766841c2 2020-08-13 stsp free(ondisk_path);
4374 766841c2 2020-08-13 stsp return NULL;
4376 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4377 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4378 766841c2 2020-08-13 stsp static char msg[64];
4379 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4380 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4381 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4382 766841c2 2020-08-13 stsp goto done;
4386 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4387 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4388 f2a9dc41 2019-12-13 tracey goto done;
4389 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4390 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4391 f2a9dc41 2019-12-13 tracey goto done;
4393 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4394 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4395 d64cc78a 2022-01-25 thomas goto done;
4397 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4398 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4399 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4400 f2a9dc41 2019-12-13 tracey goto done;
4404 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4405 20a2ad1c 2020-10-20 stsp size_t root_len;
4407 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4408 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4409 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4410 12463d8b 2019-12-13 stsp ondisk_path);
4411 12463d8b 2019-12-13 stsp goto done;
4413 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4414 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4415 12463d8b 2019-12-13 stsp goto done;
4418 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4420 20a2ad1c 2020-10-20 stsp char *parent;
4422 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4424 2513f20a 2020-10-20 stsp goto done;
4425 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4426 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4427 8ec7352a 2023-06-01 thomas if (got_path_cmp(ondisk_path, a->status_path,
4428 8ec7352a 2023-06-01 thomas strlen(ondisk_path), a->status_path_len) != 0 &&
4429 8ec7352a 2023-06-01 thomas !got_path_is_child(ondisk_path, a->status_path,
4430 8ec7352a 2023-06-01 thomas a->status_path_len))
4432 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4433 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4434 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4435 20a2ad1c 2020-10-20 stsp ondisk_path);
4438 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4439 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4442 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4444 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4445 f2a9dc41 2019-12-13 tracey if (err)
4446 f2a9dc41 2019-12-13 tracey return err;
4447 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4448 f2a9dc41 2019-12-13 tracey return NULL;
4449 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4450 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4453 2ec1f75b 2019-03-26 stsp const struct got_error *
4454 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4455 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4456 766841c2 2020-08-13 stsp const char *status_codes,
4457 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4458 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4460 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4461 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4462 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4463 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4464 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4466 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4468 2ec1f75b 2019-03-26 stsp return err;
4470 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4472 2ec1f75b 2019-03-26 stsp goto done;
4474 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4475 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4476 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4477 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4478 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4479 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4480 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4481 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4482 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4484 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4485 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
4487 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s%s%s",
4488 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree),
4489 8ec7352a 2023-06-01 thomas pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4490 8ec7352a 2023-06-01 thomas err = got_error_from_errno("asprintf");
4491 8ec7352a 2023-06-01 thomas goto done;
4493 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
4494 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
4495 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4496 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4497 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
4501 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4502 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4503 af12c6b9 2019-06-04 stsp err = sync_err;
4505 fb399478 2019-07-12 stsp free(fileindex_path);
4506 2ec1f75b 2019-03-26 stsp if (fileindex)
4507 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4508 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4509 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4510 2ec1f75b 2019-03-26 stsp err = unlockerr;
4511 33aa809d 2019-08-08 stsp return err;
4514 33aa809d 2019-08-08 stsp static const struct got_error *
4515 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4517 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4518 33aa809d 2019-08-08 stsp char *line = NULL;
4519 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4520 33aa809d 2019-08-08 stsp ssize_t linelen;
4522 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4523 33aa809d 2019-08-08 stsp if (linelen == -1) {
4524 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4525 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4526 33aa809d 2019-08-08 stsp goto done;
4528 33aa809d 2019-08-08 stsp return NULL;
4530 33aa809d 2019-08-08 stsp if (outfile) {
4531 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4532 33aa809d 2019-08-08 stsp if (n != linelen) {
4533 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4534 33aa809d 2019-08-08 stsp goto done;
4537 33aa809d 2019-08-08 stsp if (rejectfile) {
4538 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4539 33aa809d 2019-08-08 stsp if (n != linelen)
4540 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4543 33aa809d 2019-08-08 stsp free(line);
4544 2ec1f75b 2019-03-26 stsp return err;
4547 33aa809d 2019-08-08 stsp static const struct got_error *
4548 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4550 33aa809d 2019-08-08 stsp char *line = NULL;
4551 33aa809d 2019-08-08 stsp size_t linesize = 0;
4552 33aa809d 2019-08-08 stsp ssize_t linelen;
4554 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4555 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4556 500467ff 2019-09-25 hiltjo if (ferror(f))
4557 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4558 500467ff 2019-09-25 hiltjo return NULL;
4560 33aa809d 2019-08-08 stsp free(line);
4561 33aa809d 2019-08-08 stsp return NULL;
4564 33aa809d 2019-08-08 stsp static const struct got_error *
4565 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4566 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4567 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4569 33aa809d 2019-08-08 stsp const struct got_error *err;
4571 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4572 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4573 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4575 33aa809d 2019-08-08 stsp return err;
4576 33aa809d 2019-08-08 stsp (*line_cur1)++;
4578 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4579 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4580 33aa809d 2019-08-08 stsp if (rejectfile)
4581 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4583 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4585 33aa809d 2019-08-08 stsp return err;
4586 33aa809d 2019-08-08 stsp (*line_cur2)++;
4588 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4589 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4590 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4592 33aa809d 2019-08-08 stsp return err;
4593 33aa809d 2019-08-08 stsp (*line_cur2)++;
4595 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4596 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4597 33aa809d 2019-08-08 stsp if (rejectfile)
4598 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4600 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4602 33aa809d 2019-08-08 stsp return err;
4603 33aa809d 2019-08-08 stsp (*line_cur1)++;
4606 f1e81a05 2019-08-10 stsp return NULL;
4609 f1e81a05 2019-08-10 stsp static const struct got_error *
4610 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4611 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4613 f1e81a05 2019-08-10 stsp const struct got_error *err;
4615 f1e81a05 2019-08-10 stsp if (outfile) {
4616 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4617 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4618 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4620 f1e81a05 2019-08-10 stsp return err;
4621 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4624 f1e81a05 2019-08-10 stsp if (rejectfile) {
4625 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4626 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4627 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4629 f1e81a05 2019-08-10 stsp return err;
4630 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4634 33aa809d 2019-08-08 stsp return NULL;
4637 33aa809d 2019-08-08 stsp static const struct got_error *
4638 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4639 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4640 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4641 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4642 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4644 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4645 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4646 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4647 33aa809d 2019-08-08 stsp FILE *hunkfile;
4648 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4649 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4652 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4654 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4655 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4656 fe621944 2020-11-10 stsp start_old = cc.left.start;
4657 fe621944 2020-11-10 stsp end_old = cc.left.end;
4658 fe621944 2020-11-10 stsp start_new = cc.right.start;
4659 fe621944 2020-11-10 stsp end_new = cc.right.end;
4661 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4662 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4663 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4665 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4666 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4667 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4669 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4670 fe621944 2020-11-10 stsp if (diff_state == NULL)
4671 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4672 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4674 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4675 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4676 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4677 33aa809d 2019-08-08 stsp goto done;
4680 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4681 fe621944 2020-11-10 stsp diff_result, &cc);
4682 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4683 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4684 33aa809d 2019-08-08 stsp goto done;
4687 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4688 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4689 33aa809d 2019-08-08 stsp goto done;
4692 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4693 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4695 33aa809d 2019-08-08 stsp goto done;
4697 33aa809d 2019-08-08 stsp switch (*choice) {
4698 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4699 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4700 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4702 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4703 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4704 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4706 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4709 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4713 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4714 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4715 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4716 33aa809d 2019-08-08 stsp return err;
4719 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4720 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4721 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4722 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4723 1f1abb7e 2019-08-08 stsp void *progress_arg;
4724 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4725 33aa809d 2019-08-08 stsp void *patch_arg;
4726 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4727 10604dce 2021-09-24 thomas int unlink_added_files;
4728 8641a332 2023-04-14 thomas struct got_pathlist_head *added_files_to_unlink;
4731 e20a8b6f 2019-06-04 stsp static const struct got_error *
4732 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4733 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4734 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4735 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4736 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4738 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4739 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4740 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4741 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4742 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4743 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4744 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4745 fe621944 2020-11-10 stsp struct stat sb2;
4746 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4747 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4748 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4750 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4752 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4754 33aa809d 2019-08-08 stsp return err;
4756 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4757 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4758 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4759 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4760 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4761 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4762 fa3cef63 2020-07-23 stsp goto done;
4764 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4765 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4766 48b4f239 2021-12-31 thomas if (link_len == -1) {
4767 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4770 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4771 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4774 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4775 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4776 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4777 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4778 fa3cef63 2020-07-23 stsp goto done;
4780 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4781 fa3cef63 2020-07-23 stsp sizeof(link_target));
4782 fa3cef63 2020-07-23 stsp if (link_len == -1)
4783 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4784 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4785 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4788 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4789 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4790 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4791 fa3cef63 2020-07-23 stsp goto done;
4794 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4795 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4796 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4797 fa3cef63 2020-07-23 stsp goto done;
4802 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4803 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4804 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4805 fa3cef63 2020-07-23 stsp goto done;
4807 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4808 fa3cef63 2020-07-23 stsp if (n != link_len) {
4809 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4810 fa3cef63 2020-07-23 stsp goto done;
4812 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4813 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4814 fa3cef63 2020-07-23 stsp goto done;
4816 fa3cef63 2020-07-23 stsp rewind(f2);
4819 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4820 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4821 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4822 f4ae6ddb 2022-07-01 thomas goto done;
4825 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4827 33aa809d 2019-08-08 stsp goto done;
4829 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4831 33aa809d 2019-08-08 stsp goto done;
4833 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4835 33aa809d 2019-08-08 stsp goto done;
4837 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4838 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4840 33aa809d 2019-08-08 stsp goto done;
4842 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4845 33aa809d 2019-08-08 stsp goto done;
4847 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4848 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4849 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4850 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4852 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4853 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4854 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4855 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4856 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4857 fe621944 2020-11-10 stsp nchanges++;
4859 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4860 33aa809d 2019-08-08 stsp int choice;
4861 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4862 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4863 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4864 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4865 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4866 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4868 33aa809d 2019-08-08 stsp goto done;
4869 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4870 33aa809d 2019-08-08 stsp have_content = 1;
4871 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4874 1ebedb77 2019-10-19 stsp if (have_content) {
4875 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4876 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4877 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4879 1ebedb77 2019-10-19 stsp goto done;
4881 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4882 a2c162eb 2022-10-30 thomas mode_t mode;
4884 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
4885 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
4886 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4887 fa3cef63 2020-07-23 stsp goto done;
4892 33aa809d 2019-08-08 stsp free(id_str);
4893 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4894 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4896 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4897 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4898 fe621944 2020-11-10 stsp if (err == NULL)
4899 fe621944 2020-11-10 stsp err = free_err;
4900 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4901 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4902 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4903 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4904 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4905 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4906 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4907 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4908 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4909 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4910 33aa809d 2019-08-08 stsp if (err || !have_content) {
4911 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4912 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4913 33aa809d 2019-08-08 stsp free(*path_outfile);
4914 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4916 33aa809d 2019-08-08 stsp free(path1);
4917 33aa809d 2019-08-08 stsp return err;
4920 33aa809d 2019-08-08 stsp static const struct got_error *
4921 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4922 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4923 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4924 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4926 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4927 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4928 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4929 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4930 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4931 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4932 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4933 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4934 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4935 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4936 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4937 f4ae6ddb 2022-07-01 thomas int fd = -1;
4939 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4940 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4941 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4942 d3bcc3d1 2019-08-08 stsp return NULL;
4944 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4945 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4946 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4948 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4949 65084dad 2019-08-08 stsp if (ie == NULL)
4950 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4952 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4953 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4954 a129376b 2019-03-28 stsp if (err) {
4955 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4956 a129376b 2019-03-28 stsp goto done;
4957 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4958 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4959 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4960 e20a8b6f 2019-06-04 stsp goto done;
4963 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4964 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4965 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4966 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4967 a129376b 2019-03-28 stsp goto done;
4970 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4971 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4972 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4973 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4974 a129376b 2019-03-28 stsp goto done;
4977 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4978 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4979 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4980 a129376b 2019-03-28 stsp goto done;
4985 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4986 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
4987 945f9229 2022-04-16 thomas if (err)
4988 945f9229 2022-04-16 thomas goto done;
4990 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4991 a9fa2909 2019-07-27 stsp if (err) {
4992 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4993 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4994 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4995 a9fa2909 2019-07-27 stsp goto done;
4997 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4999 a9fa2909 2019-07-27 stsp goto done;
5001 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
5003 a9fa2909 2019-07-27 stsp goto done;
5005 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
5006 1233e6b6 2020-10-19 stsp free(te_name);
5007 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
5008 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
5009 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5010 a9fa2909 2019-07-27 stsp goto done;
5014 a129376b 2019-03-28 stsp switch (status) {
5015 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
5016 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5017 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5018 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5019 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5021 33aa809d 2019-08-08 stsp goto done;
5022 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5025 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5026 1f1abb7e 2019-08-08 stsp ie->path);
5028 1ee397ad 2019-07-12 stsp goto done;
5029 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5030 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
5031 8641a332 2023-04-14 thomas int do_unlink = a->added_files_to_unlink ? 0 : 1;
5033 8641a332 2023-04-14 thomas if (a->added_files_to_unlink) {
5034 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
5036 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, a->added_files_to_unlink,
5037 8641a332 2023-04-14 thomas entry) {
5038 8641a332 2023-04-14 thomas if (got_path_cmp(pe->path, relpath,
5039 8641a332 2023-04-14 thomas pe->path_len, strlen(relpath)))
5040 8641a332 2023-04-14 thomas continue;
5041 8641a332 2023-04-14 thomas do_unlink = 1;
5046 8641a332 2023-04-14 thomas if (do_unlink) {
5047 8641a332 2023-04-14 thomas if (asprintf(&ondisk_path, "%s/%s",
5048 8641a332 2023-04-14 thomas got_worktree_get_root_path(a->worktree),
5049 8641a332 2023-04-14 thomas relpath) == -1) {
5050 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
5051 8641a332 2023-04-14 thomas goto done;
5053 8641a332 2023-04-14 thomas if (unlink(ondisk_path) == -1) {
5054 8641a332 2023-04-14 thomas err = got_error_from_errno2("unlink",
5055 8641a332 2023-04-14 thomas ondisk_path);
5061 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5062 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5063 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5064 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5065 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5067 33aa809d 2019-08-08 stsp goto done;
5068 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5071 33aa809d 2019-08-08 stsp /* fall through */
5072 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5073 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5074 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5075 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5076 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5077 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5078 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
5079 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
5081 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
5082 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5083 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5084 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5085 f4ae6ddb 2022-07-01 thomas goto done;
5088 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5090 65084dad 2019-08-08 stsp goto done;
5092 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5093 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5094 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5095 a129376b 2019-03-28 stsp goto done;
5098 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5099 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5100 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5101 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5102 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5103 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5104 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5106 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5107 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5108 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5109 369fd7e5 2020-07-23 stsp path_content);
5112 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5113 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5114 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5115 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5117 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5118 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5119 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5120 369fd7e5 2020-07-23 stsp goto done;
5124 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5125 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5126 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5127 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5128 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5129 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5131 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5133 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5134 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5135 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
5136 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5139 a129376b 2019-03-28 stsp goto done;
5140 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5141 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5142 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5143 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5144 437adc9d 2020-12-10 yzhong blob->id.sha1,
5145 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5147 2e1fa222 2020-07-23 stsp goto done;
5149 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5150 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5151 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5160 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5161 33aa809d 2019-08-08 stsp free(path_content);
5162 e20a8b6f 2019-06-04 stsp free(parent_path);
5163 a129376b 2019-03-28 stsp free(tree_path);
5164 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5165 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5167 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5169 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5170 a129376b 2019-03-28 stsp free(tree_id);
5171 945f9229 2022-04-16 thomas if (base_commit)
5172 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
5173 e20a8b6f 2019-06-04 stsp return err;
5176 e20a8b6f 2019-06-04 stsp const struct got_error *
5177 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5178 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5179 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5180 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5181 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5183 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5184 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5185 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5186 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5187 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5188 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5190 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5192 e20a8b6f 2019-06-04 stsp return err;
5194 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5196 e20a8b6f 2019-06-04 stsp goto done;
5198 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5199 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5200 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5201 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5202 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5203 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5204 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5205 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
5206 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5207 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5208 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
5212 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5213 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5214 e20a8b6f 2019-06-04 stsp err = sync_err;
5216 fb399478 2019-07-12 stsp free(fileindex_path);
5217 a129376b 2019-03-28 stsp if (fileindex)
5218 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5219 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5220 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5221 a129376b 2019-03-28 stsp err = unlockerr;
5222 c4296144 2019-05-09 stsp return err;
5225 cf066bf8 2019-05-09 stsp static void
5226 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5228 24519714 2019-05-09 stsp free(ct->path);
5229 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5230 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5231 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5232 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5233 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5234 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5238 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5239 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5240 24519714 2019-05-09 stsp struct got_repository *repo;
5241 24519714 2019-05-09 stsp struct got_worktree *worktree;
5242 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5243 5f8a88c6 2019-08-03 stsp int have_staged_files;
5244 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5245 ef899790 2022-11-01 thomas int diff_header_shown;
5246 be94c032 2023-02-20 thomas int commit_conflicts;
5247 ef899790 2022-11-01 thomas FILE *diff_outfile;
5248 ef899790 2022-11-01 thomas FILE *f1;
5249 ef899790 2022-11-01 thomas FILE *f2;
5253 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5254 ef899790 2022-11-01 thomas * it as content to the diff engine.
5256 ef899790 2022-11-01 thomas static const struct got_error *
5257 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5258 ef899790 2022-11-01 thomas const char *abspath)
5260 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5261 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5262 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5264 ef899790 2022-11-01 thomas *fd = -1;
5266 ef899790 2022-11-01 thomas if (dirfd != -1) {
5267 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5268 ef899790 2022-11-01 thomas if (target_len == -1)
5269 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5270 ef899790 2022-11-01 thomas } else {
5271 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5272 ef899790 2022-11-01 thomas if (target_len == -1)
5273 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5276 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5277 ef899790 2022-11-01 thomas if (*fd == -1)
5278 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5280 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5281 ef899790 2022-11-01 thomas if (outlen == -1) {
5282 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5283 ef899790 2022-11-01 thomas goto done;
5286 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5287 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5288 ef899790 2022-11-01 thomas goto done;
5291 ef899790 2022-11-01 thomas if (err) {
5292 ef899790 2022-11-01 thomas close(*fd);
5293 ef899790 2022-11-01 thomas *fd = -1;
5295 ef899790 2022-11-01 thomas return err;
5298 ef899790 2022-11-01 thomas static const struct got_error *
5299 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5300 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5301 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5303 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5304 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5305 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5306 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5307 ef899790 2022-11-01 thomas char *label1 = NULL;
5308 ef899790 2022-11-01 thomas struct stat sb;
5309 ef899790 2022-11-01 thomas off_t size1 = 0;
5310 ef899790 2022-11-01 thomas int f2_exists = 0;
5311 ef899790 2022-11-01 thomas char *id_str = NULL;
5313 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5315 d259e491 2022-11-01 thomas if (diff_staged) {
5316 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5317 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5318 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5319 d259e491 2022-11-01 thomas return NULL;
5320 d259e491 2022-11-01 thomas } else {
5321 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5322 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5323 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5324 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5325 d259e491 2022-11-01 thomas return NULL;
5328 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5329 ef899790 2022-11-01 thomas if (err)
5330 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5331 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5332 ef899790 2022-11-01 thomas if (err)
5333 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5335 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5336 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5337 ef899790 2022-11-01 thomas if (err)
5338 ef899790 2022-11-01 thomas return err;
5339 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5340 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5341 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5342 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5343 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5344 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5345 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5348 ef899790 2022-11-01 thomas if (diff_staged) {
5349 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5350 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5351 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5352 ef899790 2022-11-01 thomas label1 = ct->path;
5353 ef899790 2022-11-01 thomas label2 = ct->path;
5355 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5356 ef899790 2022-11-01 thomas label2 = ct->path;
5358 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5359 ef899790 2022-11-01 thomas label1 = ct->path;
5361 ef899790 2022-11-01 thomas default:
5362 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5364 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5365 ef899790 2022-11-01 thomas if (fd1 == -1) {
5366 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5367 ef899790 2022-11-01 thomas goto done;
5369 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5370 ef899790 2022-11-01 thomas if (fd2 == -1) {
5371 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5372 ef899790 2022-11-01 thomas goto done;
5374 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5375 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5376 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5377 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5378 ef899790 2022-11-01 thomas goto done;
5381 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5382 ef899790 2022-11-01 thomas if (fd1 == -1) {
5383 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5384 ef899790 2022-11-01 thomas goto done;
5387 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5388 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5389 ef899790 2022-11-01 thomas 8192, fd1);
5390 ef899790 2022-11-01 thomas if (err)
5391 ef899790 2022-11-01 thomas goto done;
5394 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5395 ef899790 2022-11-01 thomas if (dirfd != -1) {
5396 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5397 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5398 ef899790 2022-11-01 thomas if (fd == -1) {
5399 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5400 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5401 ef899790 2022-11-01 thomas ct->ondisk_path);
5402 ef899790 2022-11-01 thomas goto done;
5404 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5405 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5406 ef899790 2022-11-01 thomas if (err)
5407 ef899790 2022-11-01 thomas goto done;
5409 ef899790 2022-11-01 thomas } else {
5410 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5411 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5412 ef899790 2022-11-01 thomas if (fd == -1) {
5413 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5414 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5415 ef899790 2022-11-01 thomas ct->ondisk_path);
5416 ef899790 2022-11-01 thomas goto done;
5418 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5419 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5420 ef899790 2022-11-01 thomas if (err)
5421 ef899790 2022-11-01 thomas goto done;
5424 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5425 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5426 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5427 ef899790 2022-11-01 thomas goto done;
5429 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5430 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5431 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5432 ef899790 2022-11-01 thomas goto done;
5434 ef899790 2022-11-01 thomas fd = -1;
5435 ef899790 2022-11-01 thomas f2_exists = 1;
5438 ef899790 2022-11-01 thomas if (blob1) {
5439 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5440 ef899790 2022-11-01 thomas f1, blob1);
5441 ef899790 2022-11-01 thomas if (err)
5442 ef899790 2022-11-01 thomas goto done;
5445 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5446 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5447 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5449 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5450 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5451 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5452 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5453 ef899790 2022-11-01 thomas if (blob1)
5454 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5455 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5456 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5457 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5458 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5459 ef899790 2022-11-01 thomas return err;
5462 c4296144 2019-05-09 stsp static const struct got_error *
5463 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5464 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5465 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5466 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5468 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5469 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5470 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5471 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5472 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5473 768aea60 2019-05-09 stsp struct stat sb;
5475 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5476 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5477 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5478 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5479 5f8a88c6 2019-08-03 stsp return NULL;
5481 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5482 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5483 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5486 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5487 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5488 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5489 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5490 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5491 5f8a88c6 2019-08-03 stsp return NULL;
5494 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5495 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5496 036813ee 2019-05-09 stsp goto done;
5498 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5499 036813ee 2019-05-09 stsp parent_path = strdup("");
5500 69960a46 2019-05-09 stsp if (parent_path == NULL)
5501 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5503 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5505 69960a46 2019-05-09 stsp return err;
5508 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5509 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5510 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5511 c4296144 2019-05-09 stsp goto done;
5514 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5515 768aea60 2019-05-09 stsp relpath) == -1) {
5516 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5517 768aea60 2019-05-09 stsp goto done;
5520 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5521 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5522 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5523 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5524 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5525 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5526 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5527 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5529 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5530 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5533 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5534 0aeb8099 2020-07-23 stsp goto done;
5536 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5537 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5538 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5539 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5540 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5541 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5542 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5543 12463d8b 2019-12-13 stsp ct->ondisk_path);
5544 12463d8b 2019-12-13 stsp goto done;
5546 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5547 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5548 768aea60 2019-05-09 stsp goto done;
5550 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5553 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5554 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5555 44d03001 2019-05-09 stsp relpath) == -1) {
5556 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5557 44d03001 2019-05-09 stsp goto done;
5560 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5561 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5562 35213c7c 2020-07-23 stsp int is_bad_symlink;
5563 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5564 35213c7c 2020-07-23 stsp ssize_t target_len;
5565 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5566 35213c7c 2020-07-23 stsp sizeof(target_path));
5567 35213c7c 2020-07-23 stsp if (target_len == -1) {
5568 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5569 35213c7c 2020-07-23 stsp ct->ondisk_path);
5570 35213c7c 2020-07-23 stsp goto done;
5572 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5573 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5575 35213c7c 2020-07-23 stsp goto done;
5576 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5577 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5578 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5579 35213c7c 2020-07-23 stsp goto done;
5584 cf066bf8 2019-05-09 stsp ct->status = status;
5585 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5586 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5587 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5588 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5589 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5590 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5591 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5592 b416585c 2019-05-13 stsp goto done;
5594 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5595 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5596 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5597 f0b75401 2019-08-03 stsp goto done;
5600 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5601 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5602 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5603 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5604 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5605 036813ee 2019-05-09 stsp goto done;
5608 24519714 2019-05-09 stsp ct->path = strdup(path);
5609 24519714 2019-05-09 stsp if (ct->path == NULL) {
5610 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5611 24519714 2019-05-09 stsp goto done;
5613 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5614 ef899790 2022-11-01 thomas if (err)
5615 ef899790 2022-11-01 thomas goto done;
5617 ef899790 2022-11-01 thomas if (a->diff_outfile && ct && new != NULL) {
5618 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5619 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5620 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5621 ef899790 2022-11-01 thomas if (err)
5622 ef899790 2022-11-01 thomas goto done;
5625 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5626 ed175427 2019-05-09 stsp free_commitable(ct);
5627 24519714 2019-05-09 stsp free(parent_path);
5628 036813ee 2019-05-09 stsp free(path);
5629 a129376b 2019-03-28 stsp return err;
5632 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5633 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5634 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5635 036813ee 2019-05-09 stsp struct got_repository *);
5637 ed175427 2019-05-09 stsp static const struct got_error *
5638 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5639 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5640 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5641 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5642 afa376bf 2019-05-09 stsp struct got_repository *repo)
5644 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5645 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5646 036813ee 2019-05-09 stsp char *subpath;
5648 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5649 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5650 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5652 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5654 ed175427 2019-05-09 stsp return err;
5656 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5657 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5658 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5659 036813ee 2019-05-09 stsp free(subpath);
5660 036813ee 2019-05-09 stsp return err;
5663 036813ee 2019-05-09 stsp static const struct got_error *
5664 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5666 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5667 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5669 036813ee 2019-05-09 stsp *match = 0;
5671 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5672 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5673 0f63689d 2019-05-10 stsp return NULL;
5676 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5678 0f63689d 2019-05-10 stsp return err;
5679 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5680 036813ee 2019-05-09 stsp free(ct_parent_path);
5681 036813ee 2019-05-09 stsp return err;
5684 768aea60 2019-05-09 stsp static mode_t
5685 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5687 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5688 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5690 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5693 036813ee 2019-05-09 stsp static const struct got_error *
5694 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5695 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5697 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5699 036813ee 2019-05-09 stsp *new_te = NULL;
5701 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5703 036813ee 2019-05-09 stsp goto done;
5705 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5707 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5708 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5709 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5711 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5713 036813ee 2019-05-09 stsp if (err && *new_te) {
5714 56e0773d 2019-11-28 stsp free(*new_te);
5715 036813ee 2019-05-09 stsp *new_te = NULL;
5717 036813ee 2019-05-09 stsp return err;
5720 036813ee 2019-05-09 stsp static const struct got_error *
5721 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5722 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5724 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5725 102b254e 2020-10-19 stsp char *ct_name = NULL;
5727 036813ee 2019-05-09 stsp *new_te = NULL;
5729 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5730 036813ee 2019-05-09 stsp if (*new_te == NULL)
5731 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5733 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5735 036813ee 2019-05-09 stsp goto done;
5736 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5737 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5738 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5739 036813ee 2019-05-09 stsp goto done;
5742 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5744 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5745 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5746 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5748 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5750 102b254e 2020-10-19 stsp free(ct_name);
5751 036813ee 2019-05-09 stsp if (err && *new_te) {
5752 56e0773d 2019-11-28 stsp free(*new_te);
5753 036813ee 2019-05-09 stsp *new_te = NULL;
5755 036813ee 2019-05-09 stsp return err;
5758 036813ee 2019-05-09 stsp static const struct got_error *
5759 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5760 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5762 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5763 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5765 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5767 036813ee 2019-05-09 stsp return err;
5768 036813ee 2019-05-09 stsp if (new_pe == NULL)
5769 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5770 036813ee 2019-05-09 stsp return NULL;
5773 afa376bf 2019-05-09 stsp static const struct got_error *
5774 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5775 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5777 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5778 5f8a88c6 2019-08-03 stsp unsigned char status;
5780 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5781 ae1e948a 2021-09-28 thomas return NULL;
5783 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5784 afa376bf 2019-05-09 stsp ct_path++;
5786 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5787 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5789 5f8a88c6 2019-08-03 stsp status = ct->status;
5791 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5792 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5795 036813ee 2019-05-09 stsp static const struct got_error *
5796 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5797 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5799 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5800 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5801 44d03001 2019-05-09 stsp char *te_path;
5803 44d03001 2019-05-09 stsp *modified = 0;
5805 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5806 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5807 44d03001 2019-05-09 stsp te->name) == -1)
5808 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5810 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5811 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5812 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5813 44d03001 2019-05-09 stsp strlen(te_path));
5814 62d463ca 2020-10-20 naddy if (*modified)
5818 44d03001 2019-05-09 stsp free(te_path);
5819 44d03001 2019-05-09 stsp return err;
5822 44d03001 2019-05-09 stsp static const struct got_error *
5823 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5824 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5825 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5827 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5828 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5830 036813ee 2019-05-09 stsp *ctp = NULL;
5832 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5833 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5834 036813ee 2019-05-09 stsp char *ct_name = NULL;
5835 036813ee 2019-05-09 stsp int path_matches;
5837 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5838 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5839 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5840 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5841 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5844 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5845 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5849 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5852 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5854 036813ee 2019-05-09 stsp return err;
5855 036813ee 2019-05-09 stsp if (!path_matches)
5858 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5860 d34b633e 2020-10-19 stsp return err;
5862 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5863 d34b633e 2020-10-19 stsp free(ct_name);
5866 d34b633e 2020-10-19 stsp free(ct_name);
5868 036813ee 2019-05-09 stsp *ctp = ct;
5872 2b496619 2019-07-10 stsp return err;
5875 2b496619 2019-07-10 stsp static const struct got_error *
5876 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5877 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5878 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5879 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5880 2b496619 2019-07-10 stsp struct got_repository *repo)
5882 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5883 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5884 2b496619 2019-07-10 stsp char *subtree_path;
5885 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5886 ba580f68 2020-03-22 stsp int nentries;
5888 2b496619 2019-07-10 stsp *new_tep = NULL;
5890 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5891 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5892 2b496619 2019-07-10 stsp child_path) == -1)
5893 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5895 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5896 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5897 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5898 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5900 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5901 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5902 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5903 2b496619 2019-07-10 stsp goto done;
5905 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5906 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5907 2b496619 2019-07-10 stsp if (err) {
5908 56e0773d 2019-11-28 stsp free(new_te);
5909 2b496619 2019-07-10 stsp goto done;
5911 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5914 2b496619 2019-07-10 stsp free(subtree_path);
5915 2b496619 2019-07-10 stsp if (err == NULL)
5916 2b496619 2019-07-10 stsp *new_tep = new_te;
5917 036813ee 2019-05-09 stsp return err;
5920 036813ee 2019-05-09 stsp static const struct got_error *
5921 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5922 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5923 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5924 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5925 036813ee 2019-05-09 stsp struct got_repository *repo)
5927 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5928 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5929 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5930 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5932 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5933 ba580f68 2020-03-22 stsp *nentries = 0;
5935 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5936 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5937 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5938 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5940 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5941 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5942 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5945 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5946 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5949 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5950 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5952 036813ee 2019-05-09 stsp goto done;
5954 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5955 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5956 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5958 036813ee 2019-05-09 stsp goto done;
5959 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5961 afa376bf 2019-05-09 stsp goto done;
5962 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5963 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5965 2f51b5b3 2019-05-09 stsp goto done;
5966 ba580f68 2020-03-22 stsp (*nentries)++;
5968 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5969 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5970 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5971 2b496619 2019-07-10 stsp == NULL) {
5972 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5973 2b496619 2019-07-10 stsp child_path, path_base_tree,
5974 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5977 2b496619 2019-07-10 stsp goto done;
5978 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5980 2b496619 2019-07-10 stsp goto done;
5981 ba580f68 2020-03-22 stsp (*nentries)++;
5986 2f51b5b3 2019-05-09 stsp if (base_tree) {
5987 56e0773d 2019-11-28 stsp int i, nbase_entries;
5988 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5989 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5990 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5991 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5993 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5994 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5995 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5996 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5998 63c5ca5d 2019-08-24 stsp goto done;
5999 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
6001 63c5ca5d 2019-08-24 stsp goto done;
6002 ba580f68 2020-03-22 stsp (*nentries)++;
6006 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
6007 44d03001 2019-05-09 stsp int modified;
6008 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6010 036813ee 2019-05-09 stsp goto done;
6011 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
6012 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
6014 2f51b5b3 2019-05-09 stsp goto done;
6015 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
6016 44d03001 2019-05-09 stsp if (modified) {
6017 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
6018 ba580f68 2020-03-22 stsp int nsubentries;
6019 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
6020 ba580f68 2020-03-22 stsp &nsubentries, te,
6021 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
6022 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
6024 44d03001 2019-05-09 stsp goto done;
6025 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6026 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6027 ba580f68 2020-03-22 stsp free(new_id);
6030 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6031 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6032 56e0773d 2019-11-28 stsp free(new_id);
6034 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6036 036813ee 2019-05-09 stsp goto done;
6037 ba580f68 2020-03-22 stsp (*nentries)++;
6041 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6042 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6044 f66c734c 2019-09-22 stsp goto done;
6046 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6047 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6048 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6049 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
6050 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6051 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6052 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6054 2f51b5b3 2019-05-09 stsp goto done;
6055 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6057 2f51b5b3 2019-05-09 stsp goto done;
6058 ba580f68 2020-03-22 stsp (*nentries)++;
6060 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6061 b416585c 2019-05-13 stsp status_arg);
6063 afa376bf 2019-05-09 stsp goto done;
6065 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6066 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6068 2f51b5b3 2019-05-09 stsp goto done;
6069 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6071 2f51b5b3 2019-05-09 stsp goto done;
6072 ba580f68 2020-03-22 stsp (*nentries)++;
6077 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6078 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6080 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6081 2e1fa222 2020-07-23 stsp return err;
6084 2e1fa222 2020-07-23 stsp static const struct got_error *
6085 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6086 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6087 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6088 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6090 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6091 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6092 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6094 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6095 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6096 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6098 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6100 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6101 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6102 437adc9d 2020-12-10 yzhong if (err)
6103 437adc9d 2020-12-10 yzhong goto done;
6106 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6107 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6108 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6109 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6110 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6111 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6112 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6113 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6115 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6116 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6117 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6118 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6119 72fd46fa 2019-09-06 stsp !have_staged_files);
6121 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6122 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6123 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6124 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6125 72fd46fa 2019-09-06 stsp !have_staged_files);
6127 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6129 437adc9d 2020-12-10 yzhong goto done;
6130 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6131 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6132 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6133 3969253a 2020-03-07 stsp if (err) {
6134 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6135 437adc9d 2020-12-10 yzhong goto done;
6137 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6138 3969253a 2020-03-07 stsp if (err) {
6139 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6140 437adc9d 2020-12-10 yzhong goto done;
6143 437adc9d 2020-12-10 yzhong free(relpath);
6144 437adc9d 2020-12-10 yzhong relpath = NULL;
6147 437adc9d 2020-12-10 yzhong free(relpath);
6148 d56d26ce 2019-05-10 stsp return err;
6152 d56d26ce 2019-05-10 stsp static const struct got_error *
6153 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6154 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6155 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6156 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6157 735ef5ac 2019-08-03 stsp int ood_errcode)
6159 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6160 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6161 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6163 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6164 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6165 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6166 1a36436d 2019-06-10 stsp return NULL;
6168 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6169 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6171 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6172 945f9229 2022-04-16 thomas if (err)
6173 945f9229 2022-04-16 thomas goto done;
6174 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6175 9bead371 2019-07-28 stsp if (err) {
6176 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6177 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6178 1a36436d 2019-06-10 stsp goto done;
6179 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6180 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6182 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6183 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6184 945f9229 2022-04-16 thomas if (err)
6185 945f9229 2022-04-16 thomas goto done;
6186 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6187 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6188 1a36436d 2019-06-10 stsp goto done;
6189 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6193 945f9229 2022-04-16 thomas if (commit)
6194 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6195 1a36436d 2019-06-10 stsp return err;
6198 ef20f542 2022-06-26 thomas static const struct got_error *
6199 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6200 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6201 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
6202 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
6203 10604dce 2021-09-24 thomas struct got_worktree *worktree,
6204 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
6205 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6206 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6207 afa376bf 2019-05-09 stsp struct got_repository *repo)
6209 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6210 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6211 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6212 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6213 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6214 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6215 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6216 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6217 10604dce 2021-09-24 thomas int nentries, nparents = 0;
6218 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6219 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6220 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6221 892eab0a 2022-07-22 thomas time_t timestamp;
6223 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6225 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6227 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6229 588edf97 2019-05-10 stsp goto done;
6231 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6233 588edf97 2019-05-10 stsp goto done;
6235 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6236 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
6237 ef899790 2022-11-01 thomas &logmsg, commit_arg);
6242 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6243 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6247 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6248 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6249 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6250 cf066bf8 2019-05-09 stsp char *ondisk_path;
6252 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6253 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6254 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6257 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6258 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6259 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6260 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6263 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6264 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6265 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6266 cf066bf8 2019-05-09 stsp goto done;
6268 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6269 2e1fa222 2020-07-23 stsp free(ondisk_path);
6271 cf066bf8 2019-05-09 stsp goto done;
6274 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6275 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6276 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6278 036813ee 2019-05-09 stsp goto done;
6280 538aebb5 2023-04-22 thomas err = got_object_qid_alloc(&pid, head_commit_id);
6282 de18fc63 2019-05-09 stsp goto done;
6283 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6284 10604dce 2021-09-24 thomas nparents++;
6285 10604dce 2021-09-24 thomas if (parent_id2) {
6286 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6287 10604dce 2021-09-24 thomas if (err)
6288 10604dce 2021-09-24 thomas goto done;
6289 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6290 10604dce 2021-09-24 thomas nparents++;
6292 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6293 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6294 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6295 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6296 33ad4cbe 2019-05-12 jcs free(logmsg);
6298 9d40349a 2019-05-09 stsp goto done;
6300 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6301 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6302 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6303 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6304 09f5bd90 2019-05-09 stsp goto done;
6306 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6307 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6309 09f5bd90 2019-05-09 stsp goto done;
6310 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6312 ebf99748 2019-05-09 stsp goto done;
6313 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6314 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6315 09f5bd90 2019-05-09 stsp goto done;
6317 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6318 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6320 f2c16586 2019-05-09 stsp goto done;
6321 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6323 f2c16586 2019-05-09 stsp goto done;
6325 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6327 09f5bd90 2019-05-09 stsp goto done;
6329 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6331 ebf99748 2019-05-09 stsp goto done;
6333 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6334 588edf97 2019-05-10 stsp if (head_tree)
6335 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6336 588edf97 2019-05-10 stsp if (head_commit)
6337 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6338 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6339 2f17228e 2019-05-12 stsp if (head_ref2) {
6340 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6341 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6342 2f17228e 2019-05-12 stsp err = unlockerr;
6343 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6345 39cd0ff6 2019-07-12 stsp return err;
6348 5c1e53bc 2019-07-28 stsp static const struct got_error *
6349 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6350 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6352 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6353 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6355 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6356 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6357 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6359 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6360 5c1e53bc 2019-07-28 stsp ct_path++;
6362 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6363 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6367 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6368 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6370 5c1e53bc 2019-07-28 stsp return NULL;
6373 f0b75401 2019-08-03 stsp static const struct got_error *
6374 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6376 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6378 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6379 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6380 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6383 f0b75401 2019-08-03 stsp return NULL;
6386 5f8a88c6 2019-08-03 stsp static const struct got_error *
6387 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6388 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6390 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6391 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6393 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6394 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6396 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6397 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6398 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6399 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6400 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6401 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6404 5f8a88c6 2019-08-03 stsp return NULL;
6407 39cd0ff6 2019-07-12 stsp const struct got_error *
6408 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6409 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6410 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6411 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6412 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6413 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6414 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6416 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6417 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6418 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6419 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6420 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6421 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6422 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6423 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6424 ef899790 2022-11-01 thomas char *diff_path = NULL;
6425 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6427 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6429 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6430 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6432 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6434 39cd0ff6 2019-07-12 stsp goto done;
6436 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6438 39cd0ff6 2019-07-12 stsp goto done;
6440 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6442 39cd0ff6 2019-07-12 stsp goto done;
6444 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6446 39cd0ff6 2019-07-12 stsp goto done;
6448 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6449 5f8a88c6 2019-08-03 stsp &have_staged_files);
6450 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6451 5f8a88c6 2019-08-03 stsp goto done;
6452 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6453 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6455 5f8a88c6 2019-08-03 stsp goto done;
6458 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6459 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6460 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6461 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6462 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6463 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6464 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6465 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6466 ef899790 2022-11-01 thomas if (show_diff) {
6467 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6468 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6469 ef899790 2022-11-01 thomas if (err)
6470 ef899790 2022-11-01 thomas goto done;
6471 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6472 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6473 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6474 ef899790 2022-11-01 thomas goto done;
6476 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6477 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6478 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6479 ef899790 2022-11-01 thomas goto done;
6483 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6484 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6485 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6487 5c1e53bc 2019-07-28 stsp goto done;
6490 ef899790 2022-11-01 thomas if (show_diff) {
6491 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6492 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6493 ef899790 2022-11-01 thomas goto done;
6497 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6498 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6499 39cd0ff6 2019-07-12 stsp goto done;
6502 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6503 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6505 5c1e53bc 2019-07-28 stsp goto done;
6508 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6509 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6510 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6512 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6513 f0b75401 2019-08-03 stsp ct_path++;
6514 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6515 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6516 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6518 b50cabdf 2019-07-12 stsp goto done;
6522 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6523 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6524 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6525 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6527 39cd0ff6 2019-07-12 stsp goto done;
6529 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6530 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6531 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6532 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6533 39cd0ff6 2019-07-12 stsp err = sync_err;
6535 39cd0ff6 2019-07-12 stsp if (fileindex)
6536 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6537 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6538 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6539 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6540 39cd0ff6 2019-07-12 stsp err = unlockerr;
6541 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6542 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6544 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6546 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6547 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6548 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6549 ef899790 2022-11-01 thomas free(diff_path);
6550 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6551 ef899790 2022-11-01 thomas err == NULL)
6552 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6553 c4296144 2019-05-09 stsp return err;
6556 8656d6c4 2019-05-20 stsp const char *
6557 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6559 8656d6c4 2019-05-20 stsp return ct->path;
6562 8656d6c4 2019-05-20 stsp unsigned int
6563 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6565 8656d6c4 2019-05-20 stsp return ct->status;
6568 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6569 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6570 818c7501 2019-07-11 stsp struct got_repository *repo;
6573 818c7501 2019-07-11 stsp static const struct got_error *
6574 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6576 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6577 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6578 818c7501 2019-07-11 stsp unsigned char status;
6579 818c7501 2019-07-11 stsp struct stat sb;
6580 818c7501 2019-07-11 stsp char *ondisk_path;
6582 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6583 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6584 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6585 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6587 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6589 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6591 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6592 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6593 818c7501 2019-07-11 stsp free(ondisk_path);
6595 818c7501 2019-07-11 stsp return err;
6597 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6598 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6599 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6600 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6602 818c7501 2019-07-11 stsp return NULL;
6605 818c7501 2019-07-11 stsp const struct got_error *
6606 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6607 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6608 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6609 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6611 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6612 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6613 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6614 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6615 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6616 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6617 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6619 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6620 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6621 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6623 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6625 818c7501 2019-07-11 stsp return err;
6627 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6629 818c7501 2019-07-11 stsp goto done;
6631 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6632 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6633 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6636 818c7501 2019-07-11 stsp goto done;
6638 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6640 818c7501 2019-07-11 stsp goto done;
6642 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6644 818c7501 2019-07-11 stsp goto done;
6646 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6648 818c7501 2019-07-11 stsp goto done;
6650 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6653 e51d7b55 2020-01-04 stsp goto done;
6655 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6657 818c7501 2019-07-11 stsp goto done;
6658 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6659 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6660 e51d7b55 2020-01-04 stsp goto done;
6663 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6664 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6666 818c7501 2019-07-11 stsp goto done;
6667 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6669 818c7501 2019-07-11 stsp goto done;
6671 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6673 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6675 818c7501 2019-07-11 stsp goto done;
6677 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6679 818c7501 2019-07-11 stsp goto done;
6681 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6682 818c7501 2019-07-11 stsp worktree->base_commit_id);
6684 818c7501 2019-07-11 stsp goto done;
6685 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6687 818c7501 2019-07-11 stsp goto done;
6689 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6691 818c7501 2019-07-11 stsp goto done;
6693 818c7501 2019-07-11 stsp free(fileindex_path);
6694 818c7501 2019-07-11 stsp free(tmp_branch_name);
6695 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6696 818c7501 2019-07-11 stsp free(branch_ref_name);
6697 818c7501 2019-07-11 stsp if (branch_ref)
6698 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6699 818c7501 2019-07-11 stsp if (wt_branch)
6700 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6701 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6702 818c7501 2019-07-11 stsp if (err) {
6703 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6704 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6705 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6707 818c7501 2019-07-11 stsp if (*tmp_branch) {
6708 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6709 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6711 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6712 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6713 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6715 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6717 818c7501 2019-07-11 stsp return err;
6720 818c7501 2019-07-11 stsp const struct got_error *
6721 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6722 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6723 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6724 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6726 818c7501 2019-07-11 stsp const struct got_error *err;
6727 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6728 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6729 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6730 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6731 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6733 818c7501 2019-07-11 stsp *commit_id = NULL;
6734 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6735 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6736 3e3a69f1 2019-07-25 stsp *branch = NULL;
6737 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6739 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6741 3e3a69f1 2019-07-25 stsp return err;
6743 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6745 f032f1f7 2019-08-04 stsp goto done;
6747 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6748 f032f1f7 2019-08-04 stsp &have_staged_files);
6749 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6750 f032f1f7 2019-08-04 stsp goto done;
6751 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6752 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6753 3e3a69f1 2019-07-25 stsp goto done;
6756 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6758 f032f1f7 2019-08-04 stsp goto done;
6760 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6762 818c7501 2019-07-11 stsp goto done;
6764 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6766 818c7501 2019-07-11 stsp goto done;
6768 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6770 818c7501 2019-07-11 stsp goto done;
6772 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6774 818c7501 2019-07-11 stsp goto done;
6776 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6777 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6779 818c7501 2019-07-11 stsp goto done;
6781 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6783 818c7501 2019-07-11 stsp goto done;
6785 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6787 818c7501 2019-07-11 stsp goto done;
6789 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6790 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6792 818c7501 2019-07-11 stsp goto done;
6794 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6796 818c7501 2019-07-11 stsp goto done;
6798 818c7501 2019-07-11 stsp free(commit_ref_name);
6799 818c7501 2019-07-11 stsp free(branch_ref_name);
6800 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6801 818c7501 2019-07-11 stsp if (commit_ref)
6802 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6803 818c7501 2019-07-11 stsp if (branch_ref)
6804 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6805 818c7501 2019-07-11 stsp if (err) {
6806 818c7501 2019-07-11 stsp free(*commit_id);
6807 818c7501 2019-07-11 stsp *commit_id = NULL;
6808 818c7501 2019-07-11 stsp if (*tmp_branch) {
6809 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6810 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6812 818c7501 2019-07-11 stsp if (*new_base_branch) {
6813 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6814 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6816 818c7501 2019-07-11 stsp if (*branch) {
6817 818c7501 2019-07-11 stsp got_ref_close(*branch);
6818 818c7501 2019-07-11 stsp *branch = NULL;
6820 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6821 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6822 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6824 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6826 818c7501 2019-07-11 stsp return err;
6829 818c7501 2019-07-11 stsp const struct got_error *
6830 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6832 818c7501 2019-07-11 stsp const struct got_error *err;
6833 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6835 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6837 818c7501 2019-07-11 stsp return err;
6839 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6840 818c7501 2019-07-11 stsp free(tmp_branch_name);
6841 818c7501 2019-07-11 stsp return NULL;
6844 818c7501 2019-07-11 stsp static const struct got_error *
6845 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6846 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
6848 0ebf8283 2019-07-24 stsp *logmsg = arg;
6849 818c7501 2019-07-11 stsp return NULL;
6852 818c7501 2019-07-11 stsp static const struct got_error *
6853 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6854 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6855 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6856 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6858 818c7501 2019-07-11 stsp return NULL;
6861 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6862 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6863 01757395 2019-07-12 stsp void *progress_arg;
6864 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6867 01757395 2019-07-12 stsp static const struct got_error *
6868 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6870 01757395 2019-07-12 stsp const struct got_error *err;
6871 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6873 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6875 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6877 01757395 2019-07-12 stsp return err;
6879 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6880 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6881 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6882 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6883 01757395 2019-07-12 stsp return NULL;
6885 01757395 2019-07-12 stsp p = strdup(path);
6886 01757395 2019-07-12 stsp if (p == NULL)
6887 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6889 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6890 01757395 2019-07-12 stsp if (err || new == NULL)
6892 01757395 2019-07-12 stsp return err;
6895 0ebf8283 2019-07-24 stsp static const struct got_error *
6896 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6897 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6899 818c7501 2019-07-11 stsp const struct got_error *err;
6900 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6902 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6903 818c7501 2019-07-11 stsp if (err) {
6904 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6905 818c7501 2019-07-11 stsp goto done;
6906 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6908 818c7501 2019-07-11 stsp goto done;
6909 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6911 818c7501 2019-07-11 stsp goto done;
6912 de05890f 2020-03-05 stsp } else if (is_rebase) {
6913 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6916 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6918 818c7501 2019-07-11 stsp goto done;
6919 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6920 818c7501 2019-07-11 stsp free(stored_id);
6921 818c7501 2019-07-11 stsp if (cmp != 0) {
6922 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6923 818c7501 2019-07-11 stsp goto done;
6927 0ebf8283 2019-07-24 stsp if (commit_ref)
6928 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6929 0ebf8283 2019-07-24 stsp return err;
6932 0ebf8283 2019-07-24 stsp static const struct got_error *
6933 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6934 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6935 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6936 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6937 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6938 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6940 0ebf8283 2019-07-24 stsp const struct got_error *err;
6941 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6942 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6943 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6945 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6947 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6949 0ebf8283 2019-07-24 stsp return err;
6951 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6952 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6953 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6954 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6955 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6956 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6957 818c7501 2019-07-11 stsp if (commit_ref)
6958 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6959 818c7501 2019-07-11 stsp return err;
6962 818c7501 2019-07-11 stsp const struct got_error *
6963 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6964 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6965 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6966 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6967 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6968 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6970 0ebf8283 2019-07-24 stsp const struct got_error *err;
6971 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6973 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6975 0ebf8283 2019-07-24 stsp return err;
6977 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6979 0ebf8283 2019-07-24 stsp goto done;
6981 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6982 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6983 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6985 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6986 0ebf8283 2019-07-24 stsp return err;
6989 0ebf8283 2019-07-24 stsp const struct got_error *
6990 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6991 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6992 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6993 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6994 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6995 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6997 0ebf8283 2019-07-24 stsp const struct got_error *err;
6998 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7000 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7002 0ebf8283 2019-07-24 stsp return err;
7004 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7006 0ebf8283 2019-07-24 stsp goto done;
7008 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7009 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7010 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7012 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7013 0ebf8283 2019-07-24 stsp return err;
7016 0ebf8283 2019-07-24 stsp static const struct got_error *
7017 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
7018 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7019 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7020 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
7021 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
7022 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7024 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7025 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7026 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7027 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7028 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7029 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7030 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7032 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7033 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7034 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7036 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7038 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7040 3e3a69f1 2019-07-25 stsp return err;
7042 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7043 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7044 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7045 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7046 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
7048 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7049 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7051 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7052 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7053 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7054 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7055 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7056 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7058 01757395 2019-07-12 stsp if (merged_paths) {
7059 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7060 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7061 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7062 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7065 01757395 2019-07-12 stsp goto done;
7068 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7069 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7071 01757395 2019-07-12 stsp goto done;
7074 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7075 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7076 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7078 a0e95631 2019-07-12 stsp goto done;
7079 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7080 a0e95631 2019-07-12 stsp goto done;
7083 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7085 edd02c5e 2019-07-11 stsp goto done;
7087 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7089 818c7501 2019-07-11 stsp goto done;
7091 5943eee2 2019-08-13 stsp if (new_logmsg) {
7092 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7093 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7094 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7095 5943eee2 2019-08-13 stsp goto done;
7098 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7100 5943eee2 2019-08-13 stsp goto done;
7103 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7104 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7105 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
7106 8db00f97 2022-07-22 thomas committer ? committer :
7107 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
7108 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7110 a0e95631 2019-07-12 stsp goto done;
7112 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7114 a0e95631 2019-07-12 stsp goto done;
7116 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7118 a0e95631 2019-07-12 stsp goto done;
7120 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7121 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7122 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7123 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7124 a0e95631 2019-07-12 stsp err = sync_err;
7126 a0e95631 2019-07-12 stsp free(fileindex_path);
7127 a0e95631 2019-07-12 stsp free(head_commit_id);
7128 a0e95631 2019-07-12 stsp if (head_ref)
7129 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7130 818c7501 2019-07-11 stsp if (err) {
7131 818c7501 2019-07-11 stsp free(*new_commit_id);
7132 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7134 818c7501 2019-07-11 stsp return err;
7137 818c7501 2019-07-11 stsp const struct got_error *
7138 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7139 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7140 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7141 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7142 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
7143 be94c032 2023-02-20 thomas struct got_repository *repo)
7145 0ebf8283 2019-07-24 stsp const struct got_error *err;
7146 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7147 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7148 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7150 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7152 0ebf8283 2019-07-24 stsp return err;
7154 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7156 0ebf8283 2019-07-24 stsp goto done;
7157 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7159 0ebf8283 2019-07-24 stsp goto done;
7160 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7161 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7162 0ebf8283 2019-07-24 stsp goto done;
7165 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7166 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7167 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
7169 0ebf8283 2019-07-24 stsp if (commit_ref)
7170 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7171 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7172 0ebf8283 2019-07-24 stsp free(commit_id);
7173 0ebf8283 2019-07-24 stsp return err;
7176 0ebf8283 2019-07-24 stsp const struct got_error *
7177 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7178 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7179 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7180 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7181 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7182 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7184 0ebf8283 2019-07-24 stsp const struct got_error *err;
7185 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7186 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7188 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7190 0ebf8283 2019-07-24 stsp return err;
7192 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7194 0ebf8283 2019-07-24 stsp goto done;
7196 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7197 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7198 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
7200 0ebf8283 2019-07-24 stsp if (commit_ref)
7201 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7202 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7203 0ebf8283 2019-07-24 stsp return err;
7206 0ebf8283 2019-07-24 stsp const struct got_error *
7207 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7208 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7210 3e3a69f1 2019-07-25 stsp if (fileindex)
7211 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7212 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7215 69844fba 2019-07-11 stsp static const struct got_error *
7216 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7218 69844fba 2019-07-11 stsp const struct got_error *err;
7219 69844fba 2019-07-11 stsp struct got_reference *ref;
7221 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7222 69844fba 2019-07-11 stsp if (err) {
7223 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7224 69844fba 2019-07-11 stsp return NULL;
7225 69844fba 2019-07-11 stsp return err;
7228 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7229 69844fba 2019-07-11 stsp got_ref_close(ref);
7230 69844fba 2019-07-11 stsp return err;
7233 69844fba 2019-07-11 stsp static const struct got_error *
7234 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7236 69844fba 2019-07-11 stsp const struct got_error *err;
7237 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7238 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7240 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7242 69844fba 2019-07-11 stsp goto done;
7243 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7245 69844fba 2019-07-11 stsp goto done;
7247 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7249 69844fba 2019-07-11 stsp goto done;
7250 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7252 69844fba 2019-07-11 stsp goto done;
7254 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7256 69844fba 2019-07-11 stsp goto done;
7257 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7259 69844fba 2019-07-11 stsp goto done;
7261 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7263 69844fba 2019-07-11 stsp goto done;
7264 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7266 69844fba 2019-07-11 stsp goto done;
7269 69844fba 2019-07-11 stsp free(tmp_branch_name);
7270 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7271 69844fba 2019-07-11 stsp free(branch_ref_name);
7272 69844fba 2019-07-11 stsp free(commit_ref_name);
7273 e600f124 2021-03-21 stsp return err;
7276 ef20f542 2022-06-26 thomas static const struct got_error *
7277 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7278 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7280 e600f124 2021-03-21 stsp const struct got_error *err;
7281 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7282 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7283 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7284 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7285 e600f124 2021-03-21 stsp char *refname = NULL;
7287 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7288 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7289 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7290 e600f124 2021-03-21 stsp branch_name += 11;
7292 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7294 e600f124 2021-03-21 stsp return err;
7296 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7297 e600f124 2021-03-21 stsp new_id_str) == -1) {
7298 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7299 e600f124 2021-03-21 stsp goto done;
7302 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7304 e600f124 2021-03-21 stsp goto done;
7306 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7308 e600f124 2021-03-21 stsp goto done;
7310 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7312 e600f124 2021-03-21 stsp free(new_id_str);
7313 e600f124 2021-03-21 stsp free(refname);
7314 e600f124 2021-03-21 stsp free(old_commit_id);
7316 e600f124 2021-03-21 stsp got_ref_close(ref);
7317 69844fba 2019-07-11 stsp return err;
7320 818c7501 2019-07-11 stsp const struct got_error *
7321 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7322 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7323 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7324 f08f28be 2023-02-03 thomas int create_backup)
7326 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7327 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7328 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7330 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7332 818c7501 2019-07-11 stsp return err;
7334 e600f124 2021-03-21 stsp if (create_backup) {
7335 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7336 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7338 e600f124 2021-03-21 stsp goto done;
7341 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7343 818c7501 2019-07-11 stsp goto done;
7345 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7347 818c7501 2019-07-11 stsp goto done;
7349 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7351 818c7501 2019-07-11 stsp goto done;
7353 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7355 a615e0e7 2020-12-16 stsp goto done;
7357 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7359 a615e0e7 2020-12-16 stsp goto done;
7360 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7361 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7362 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7363 a615e0e7 2020-12-16 stsp err = sync_err;
7365 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7366 a615e0e7 2020-12-16 stsp free(fileindex_path);
7367 818c7501 2019-07-11 stsp free(new_head_commit_id);
7368 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7369 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7370 818c7501 2019-07-11 stsp err = unlockerr;
7371 818c7501 2019-07-11 stsp return err;
7374 8641a332 2023-04-14 thomas static const struct got_error *
7375 8641a332 2023-04-14 thomas get_paths_changed_between_commits(struct got_pathlist_head *paths,
7376 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7377 8641a332 2023-04-14 thomas struct got_repository *repo)
7379 8641a332 2023-04-14 thomas const struct got_error *err;
7380 8641a332 2023-04-14 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7381 8641a332 2023-04-14 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7383 8641a332 2023-04-14 thomas if (id1) {
7384 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit1, repo, id1);
7385 8641a332 2023-04-14 thomas if (err)
7386 8641a332 2023-04-14 thomas goto done;
7388 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree1, repo,
7389 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit1));
7390 8641a332 2023-04-14 thomas if (err)
7391 8641a332 2023-04-14 thomas goto done;
7394 8641a332 2023-04-14 thomas if (id2) {
7395 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit2, repo, id2);
7396 8641a332 2023-04-14 thomas if (err)
7397 8641a332 2023-04-14 thomas goto done;
7399 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree2, repo,
7400 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit2));
7401 8641a332 2023-04-14 thomas if (err)
7402 8641a332 2023-04-14 thomas goto done;
7405 8641a332 2023-04-14 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7406 8641a332 2023-04-14 thomas got_diff_tree_collect_changed_paths, paths, 0);
7407 8641a332 2023-04-14 thomas if (err)
7408 8641a332 2023-04-14 thomas goto done;
7410 8641a332 2023-04-14 thomas if (commit1)
7411 8641a332 2023-04-14 thomas got_object_commit_close(commit1);
7412 8641a332 2023-04-14 thomas if (commit2)
7413 8641a332 2023-04-14 thomas got_object_commit_close(commit2);
7414 8641a332 2023-04-14 thomas if (tree1)
7415 8641a332 2023-04-14 thomas got_object_tree_close(tree1);
7416 8641a332 2023-04-14 thomas if (tree2)
7417 8641a332 2023-04-14 thomas got_object_tree_close(tree2);
7418 8641a332 2023-04-14 thomas return err;
7421 8641a332 2023-04-14 thomas static const struct got_error *
7422 8641a332 2023-04-14 thomas get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7423 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7424 8641a332 2023-04-14 thomas const char *path_prefix, struct got_repository *repo)
7426 8641a332 2023-04-14 thomas const struct got_error *err;
7427 8641a332 2023-04-14 thomas struct got_pathlist_head merged_paths;
7428 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
7429 8641a332 2023-04-14 thomas char *abspath = NULL, *wt_path = NULL;
7431 8641a332 2023-04-14 thomas TAILQ_INIT(&merged_paths);
7433 8641a332 2023-04-14 thomas err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7434 8641a332 2023-04-14 thomas if (err)
7435 8641a332 2023-04-14 thomas goto done;
7437 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, &merged_paths, entry) {
7438 8641a332 2023-04-14 thomas struct got_diff_changed_path *change = pe->data;
7440 8641a332 2023-04-14 thomas if (change->status != GOT_STATUS_ADD)
7441 8641a332 2023-04-14 thomas continue;
7443 8641a332 2023-04-14 thomas if (got_path_is_root_dir(path_prefix)) {
7444 8641a332 2023-04-14 thomas wt_path = strdup(pe->path);
7445 8641a332 2023-04-14 thomas if (wt_path == NULL) {
7446 8641a332 2023-04-14 thomas err = got_error_from_errno("strdup");
7447 8641a332 2023-04-14 thomas goto done;
7449 8641a332 2023-04-14 thomas } else {
7450 8641a332 2023-04-14 thomas if (asprintf(&abspath, "/%s", pe->path) == -1) {
7451 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
7452 8641a332 2023-04-14 thomas goto done;
7455 8641a332 2023-04-14 thomas err = got_path_skip_common_ancestor(&wt_path,
7456 8641a332 2023-04-14 thomas path_prefix, abspath);
7457 8641a332 2023-04-14 thomas if (err)
7458 8641a332 2023-04-14 thomas goto done;
7459 8641a332 2023-04-14 thomas free(abspath);
7460 8641a332 2023-04-14 thomas abspath = NULL;
7463 8641a332 2023-04-14 thomas err = got_pathlist_append(added_paths, wt_path, NULL);
7464 8641a332 2023-04-14 thomas if (err)
7465 8641a332 2023-04-14 thomas goto done;
7466 8641a332 2023-04-14 thomas wt_path = NULL;
7470 8641a332 2023-04-14 thomas got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7471 8641a332 2023-04-14 thomas free(abspath);
7472 8641a332 2023-04-14 thomas free(wt_path);
7473 8641a332 2023-04-14 thomas return err;
7476 8641a332 2023-04-14 thomas static const struct got_error *
7477 8641a332 2023-04-14 thomas get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7478 8641a332 2023-04-14 thomas struct got_object_id *id, const char *path_prefix,
7479 8641a332 2023-04-14 thomas struct got_repository *repo)
7481 8641a332 2023-04-14 thomas const struct got_error *err;
7482 8641a332 2023-04-14 thomas struct got_commit_object *commit = NULL;
7483 8641a332 2023-04-14 thomas struct got_object_qid *pid;
7485 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo, id);
7486 8641a332 2023-04-14 thomas if (err)
7487 8641a332 2023-04-14 thomas goto done;
7489 8641a332 2023-04-14 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7491 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(added_paths,
7492 8641a332 2023-04-14 thomas pid ? &pid->id : NULL, id, path_prefix, repo);
7493 8641a332 2023-04-14 thomas if (err)
7494 8641a332 2023-04-14 thomas goto done;
7496 8641a332 2023-04-14 thomas if (commit)
7497 8641a332 2023-04-14 thomas got_object_commit_close(commit);
7498 8641a332 2023-04-14 thomas return err;
7501 818c7501 2019-07-11 stsp const struct got_error *
7502 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7503 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7504 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7505 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7507 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7508 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7509 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7510 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7511 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7512 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7513 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7514 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7515 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7516 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7517 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7519 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7521 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7523 818c7501 2019-07-11 stsp return err;
7525 8641a332 2023-04-14 thomas err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7526 8641a332 2023-04-14 thomas if (err)
7527 8641a332 2023-04-14 thomas goto done;
7529 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7530 8641a332 2023-04-14 thomas if (err)
7531 8641a332 2023-04-14 thomas goto done;
7533 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7534 8641a332 2023-04-14 thomas if (err)
7535 8641a332 2023-04-14 thomas goto done;
7538 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7539 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7540 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
7541 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
7543 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7544 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7545 8641a332 2023-04-14 thomas if (err)
7546 8641a332 2023-04-14 thomas goto done;
7548 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7549 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7551 818c7501 2019-07-11 stsp goto done;
7553 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7555 818c7501 2019-07-11 stsp goto done;
7558 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7559 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7560 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7562 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7564 818c7501 2019-07-11 stsp goto done;
7566 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7567 25a102ed 2023-04-14 thomas if (err)
7568 25a102ed 2023-04-14 thomas goto done;
7570 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7571 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7573 818c7501 2019-07-11 stsp goto done;
7575 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7576 945f9229 2022-04-16 thomas worktree->path_prefix);
7578 ca355955 2019-07-12 stsp goto done;
7580 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7582 ca355955 2019-07-12 stsp goto done;
7584 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7586 818c7501 2019-07-11 stsp goto done;
7588 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7589 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7590 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7591 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7592 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7593 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7594 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7595 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
7596 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
7597 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7598 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7600 1f1abb7e 2019-08-08 stsp goto sync;
7602 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7603 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7605 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7606 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7607 ca355955 2019-07-12 stsp err = sync_err;
7609 8641a332 2023-04-14 thomas got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7610 818c7501 2019-07-11 stsp got_ref_close(resolved);
7611 a3a2faf2 2019-07-12 stsp free(tree_id);
7612 818c7501 2019-07-11 stsp free(commit_id);
7613 8641a332 2023-04-14 thomas free(merged_commit_id);
7614 945f9229 2022-04-16 thomas if (commit)
7615 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7616 0ebf8283 2019-07-24 stsp if (fileindex)
7617 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7618 0ebf8283 2019-07-24 stsp free(fileindex_path);
7619 8641a332 2023-04-14 thomas free(commit_ref_name);
7620 8641a332 2023-04-14 thomas if (commit_ref)
7621 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
7623 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7624 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7625 0ebf8283 2019-07-24 stsp err = unlockerr;
7626 0ebf8283 2019-07-24 stsp return err;
7629 0ebf8283 2019-07-24 stsp const struct got_error *
7630 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7631 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7632 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7633 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7635 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7636 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7637 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7638 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7639 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7640 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7641 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7642 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7644 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7645 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7646 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7647 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7649 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7651 0ebf8283 2019-07-24 stsp return err;
7653 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7655 0ebf8283 2019-07-24 stsp goto done;
7657 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7658 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7659 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7662 0ebf8283 2019-07-24 stsp goto done;
7664 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7666 0ebf8283 2019-07-24 stsp goto done;
7668 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7670 0ebf8283 2019-07-24 stsp goto done;
7672 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7673 0ebf8283 2019-07-24 stsp worktree);
7675 0ebf8283 2019-07-24 stsp goto done;
7677 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7680 0ebf8283 2019-07-24 stsp goto done;
7682 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7684 0ebf8283 2019-07-24 stsp goto done;
7686 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7688 0ebf8283 2019-07-24 stsp goto done;
7690 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7691 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7693 0ebf8283 2019-07-24 stsp goto done;
7694 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7696 0ebf8283 2019-07-24 stsp goto done;
7697 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7698 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7699 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7700 0ebf8283 2019-07-24 stsp goto done;
7703 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7704 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7706 0ebf8283 2019-07-24 stsp goto done;
7707 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7709 0ebf8283 2019-07-24 stsp goto done;
7711 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7713 0ebf8283 2019-07-24 stsp goto done;
7715 0ebf8283 2019-07-24 stsp free(fileindex_path);
7716 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7717 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7718 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7719 0ebf8283 2019-07-24 stsp if (wt_branch)
7720 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7721 0ebf8283 2019-07-24 stsp if (err) {
7722 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7723 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7724 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7726 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7727 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7728 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7730 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7731 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7732 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7733 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7735 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7737 0ebf8283 2019-07-24 stsp return err;
7740 0ebf8283 2019-07-24 stsp const struct got_error *
7741 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7742 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7744 3e3a69f1 2019-07-25 stsp if (fileindex)
7745 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7746 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7749 0ebf8283 2019-07-24 stsp const struct got_error *
7750 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7751 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7753 0ebf8283 2019-07-24 stsp const struct got_error *err;
7754 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7756 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7758 0ebf8283 2019-07-24 stsp return err;
7760 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7761 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7762 0ebf8283 2019-07-24 stsp return NULL;
7765 0ebf8283 2019-07-24 stsp const struct got_error *
7766 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7767 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7768 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7769 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7771 0ebf8283 2019-07-24 stsp const struct got_error *err;
7772 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7773 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7774 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7775 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7776 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7777 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7779 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7780 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7781 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7782 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7784 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7786 3e3a69f1 2019-07-25 stsp return err;
7788 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7790 f032f1f7 2019-08-04 stsp goto done;
7792 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7793 f032f1f7 2019-08-04 stsp &have_staged_files);
7794 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7795 f032f1f7 2019-08-04 stsp goto done;
7796 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7797 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7798 3e3a69f1 2019-07-25 stsp goto done;
7801 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7803 f032f1f7 2019-08-04 stsp goto done;
7805 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7807 0ebf8283 2019-07-24 stsp goto done;
7809 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7811 0ebf8283 2019-07-24 stsp goto done;
7813 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7814 0ebf8283 2019-07-24 stsp worktree);
7816 0ebf8283 2019-07-24 stsp goto done;
7818 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7820 0ebf8283 2019-07-24 stsp goto done;
7822 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7824 0ebf8283 2019-07-24 stsp goto done;
7825 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7827 0ebf8283 2019-07-24 stsp goto done;
7829 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7831 0ebf8283 2019-07-24 stsp goto done;
7832 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7834 0ebf8283 2019-07-24 stsp goto done;
7836 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7838 0ebf8283 2019-07-24 stsp goto done;
7840 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7841 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7842 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7843 0ebf8283 2019-07-24 stsp if (commit_ref)
7844 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7845 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7846 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7847 0ebf8283 2019-07-24 stsp if (err) {
7848 0ebf8283 2019-07-24 stsp free(*commit_id);
7849 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7850 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7851 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7852 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7853 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7854 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7856 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7857 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7858 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7860 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7862 0ebf8283 2019-07-24 stsp return err;
7865 0ebf8283 2019-07-24 stsp static const struct got_error *
7866 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7868 0ebf8283 2019-07-24 stsp const struct got_error *err;
7869 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7870 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7872 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7874 0ebf8283 2019-07-24 stsp goto done;
7875 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7877 0ebf8283 2019-07-24 stsp goto done;
7879 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7880 0ebf8283 2019-07-24 stsp worktree);
7882 0ebf8283 2019-07-24 stsp goto done;
7883 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7885 0ebf8283 2019-07-24 stsp goto done;
7887 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7889 0ebf8283 2019-07-24 stsp goto done;
7890 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7892 0ebf8283 2019-07-24 stsp goto done;
7894 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7896 0ebf8283 2019-07-24 stsp goto done;
7897 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7899 0ebf8283 2019-07-24 stsp goto done;
7901 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7902 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7903 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7904 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7905 0ebf8283 2019-07-24 stsp return err;
7908 0ebf8283 2019-07-24 stsp const struct got_error *
7909 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7910 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7911 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7912 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7914 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7915 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7916 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7917 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7918 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7919 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7920 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7921 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7922 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7923 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7925 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7927 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7929 0ebf8283 2019-07-24 stsp return err;
7931 8641a332 2023-04-14 thomas err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7932 8641a332 2023-04-14 thomas if (err)
7933 8641a332 2023-04-14 thomas goto done;
7935 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7936 8641a332 2023-04-14 thomas if (err) {
7937 8641a332 2023-04-14 thomas if (err->code != GOT_ERR_NOT_REF)
7938 8641a332 2023-04-14 thomas goto done;
7939 8641a332 2023-04-14 thomas /* Can happen on early abort due to invalid histedit script. */
7940 8641a332 2023-04-14 thomas commit_ref = NULL;
7943 8641a332 2023-04-14 thomas if (commit_ref) {
7944 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7945 8641a332 2023-04-14 thomas if (err)
7946 8641a332 2023-04-14 thomas goto done;
7949 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7950 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7951 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files added by the
7952 8641a332 2023-04-14 thomas * user during conflict resolution or during histedit -e.
7954 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7955 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7956 8641a332 2023-04-14 thomas if (err)
7957 8641a332 2023-04-14 thomas goto done;
7960 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7961 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7963 0ebf8283 2019-07-24 stsp goto done;
7965 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7967 0ebf8283 2019-07-24 stsp goto done;
7969 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7971 0ebf8283 2019-07-24 stsp goto done;
7973 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7974 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7975 25a102ed 2023-04-14 thomas if (err)
7976 25a102ed 2023-04-14 thomas goto done;
7978 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7979 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7981 0ebf8283 2019-07-24 stsp goto done;
7983 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7985 0ebf8283 2019-07-24 stsp goto done;
7987 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7989 0ebf8283 2019-07-24 stsp goto done;
7991 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7992 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7993 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7994 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7995 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7996 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7997 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7998 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
7999 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8000 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8001 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8003 1f1abb7e 2019-08-08 stsp goto sync;
8005 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8006 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8008 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8009 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
8010 0ebf8283 2019-07-24 stsp err = sync_err;
8012 8641a332 2023-04-14 thomas if (resolved)
8013 8641a332 2023-04-14 thomas got_ref_close(resolved);
8014 8641a332 2023-04-14 thomas if (commit_ref)
8015 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8016 8641a332 2023-04-14 thomas free(merged_commit_id);
8017 0ebf8283 2019-07-24 stsp free(tree_id);
8018 818c7501 2019-07-11 stsp free(fileindex_path);
8019 8641a332 2023-04-14 thomas free(commit_ref_name);
8021 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8022 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
8023 818c7501 2019-07-11 stsp err = unlockerr;
8024 818c7501 2019-07-11 stsp return err;
8027 0ebf8283 2019-07-24 stsp const struct got_error *
8028 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8029 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8030 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8032 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8033 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8034 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8035 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8037 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8039 0ebf8283 2019-07-24 stsp return err;
8041 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8042 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8044 e600f124 2021-03-21 stsp goto done;
8046 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8047 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8049 0ebf8283 2019-07-24 stsp goto done;
8051 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8053 0ebf8283 2019-07-24 stsp goto done;
8055 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
8057 0ebf8283 2019-07-24 stsp goto done;
8059 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8061 0ebf8283 2019-07-24 stsp goto done;
8063 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8065 a615e0e7 2020-12-16 stsp goto done;
8067 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8069 a615e0e7 2020-12-16 stsp goto done;
8070 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8071 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8072 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8073 a615e0e7 2020-12-16 stsp err = sync_err;
8075 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8076 a615e0e7 2020-12-16 stsp free(fileindex_path);
8077 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8078 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8079 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8080 0ebf8283 2019-07-24 stsp err = unlockerr;
8081 0ebf8283 2019-07-24 stsp return err;
8084 0ebf8283 2019-07-24 stsp const struct got_error *
8085 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8086 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8088 0ebf8283 2019-07-24 stsp const struct got_error *err;
8089 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8091 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8093 0ebf8283 2019-07-24 stsp return err;
8095 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8097 0ebf8283 2019-07-24 stsp goto done;
8099 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8101 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8102 2822a352 2019-10-15 stsp return err;
8105 2822a352 2019-10-15 stsp const struct got_error *
8106 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8107 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8108 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8109 2822a352 2019-10-15 stsp struct got_repository *repo)
8111 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8112 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8113 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8115 2822a352 2019-10-15 stsp *fileindex = NULL;
8116 2822a352 2019-10-15 stsp *branch_ref = NULL;
8117 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8119 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8121 2822a352 2019-10-15 stsp return err;
8123 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8124 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8125 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8126 2822a352 2019-10-15 stsp "update -b or different branch name required");
8127 2822a352 2019-10-15 stsp goto done;
8130 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8132 2822a352 2019-10-15 stsp goto done;
8134 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8135 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8136 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8137 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8140 2822a352 2019-10-15 stsp goto done;
8142 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8144 2822a352 2019-10-15 stsp goto done;
8146 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8147 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8149 2822a352 2019-10-15 stsp if (err) {
8150 2822a352 2019-10-15 stsp if (*branch_ref) {
8151 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8152 2822a352 2019-10-15 stsp *branch_ref = NULL;
8154 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8155 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8156 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8158 2822a352 2019-10-15 stsp if (*fileindex) {
8159 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8160 2822a352 2019-10-15 stsp *fileindex = NULL;
8162 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8164 0cb83759 2019-08-03 stsp return err;
8167 2822a352 2019-10-15 stsp const struct got_error *
8168 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8169 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8170 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8171 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8172 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8174 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8175 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8176 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8177 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8179 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8181 2822a352 2019-10-15 stsp goto done;
8183 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8185 2822a352 2019-10-15 stsp goto done;
8187 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
8188 945f9229 2022-04-16 thomas if (err)
8189 945f9229 2022-04-16 thomas goto done;
8191 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8192 2822a352 2019-10-15 stsp worktree->path_prefix);
8194 2822a352 2019-10-15 stsp goto done;
8196 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8198 2822a352 2019-10-15 stsp goto done;
8200 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8201 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8203 2822a352 2019-10-15 stsp goto sync;
8205 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8207 2822a352 2019-10-15 stsp goto sync;
8209 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8211 6e210706 2021-01-22 stsp goto sync;
8213 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8215 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8216 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8217 2822a352 2019-10-15 stsp err = sync_err;
8220 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8221 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8222 2822a352 2019-10-15 stsp err = unlockerr;
8223 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8225 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8226 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8227 2822a352 2019-10-15 stsp err = unlockerr;
8228 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8230 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8231 2822a352 2019-10-15 stsp free(fileindex_path);
8232 2822a352 2019-10-15 stsp free(tree_id);
8233 945f9229 2022-04-16 thomas if (commit)
8234 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8236 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8237 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8238 2822a352 2019-10-15 stsp err = unlockerr;
8239 2822a352 2019-10-15 stsp return err;
8242 2822a352 2019-10-15 stsp const struct got_error *
8243 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8244 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8245 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8247 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8249 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8251 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8253 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8254 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8255 8b692cd0 2019-10-21 stsp err = unlockerr;
8256 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8258 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8259 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8260 8b692cd0 2019-10-21 stsp err = unlockerr;
8261 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8263 10604dce 2021-09-24 thomas return err;
8266 10604dce 2021-09-24 thomas const struct got_error *
8267 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
8268 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
8270 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
8271 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8273 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8274 10604dce 2021-09-24 thomas if (err)
8275 10604dce 2021-09-24 thomas goto done;
8277 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8279 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
8280 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8281 10604dce 2021-09-24 thomas err = sync_err;
8283 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8284 10604dce 2021-09-24 thomas free(fileindex_path);
8285 8b692cd0 2019-10-21 stsp return err;
8288 10604dce 2021-09-24 thomas static const struct got_error *
8289 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8291 10604dce 2021-09-24 thomas const struct got_error *err;
8292 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8294 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8295 10604dce 2021-09-24 thomas if (err)
8296 10604dce 2021-09-24 thomas goto done;
8297 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
8298 10604dce 2021-09-24 thomas if (err)
8299 10604dce 2021-09-24 thomas goto done;
8301 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8302 10604dce 2021-09-24 thomas if (err)
8303 10604dce 2021-09-24 thomas goto done;
8304 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
8305 10604dce 2021-09-24 thomas if (err)
8306 10604dce 2021-09-24 thomas goto done;
8309 10604dce 2021-09-24 thomas free(branch_refname);
8310 10604dce 2021-09-24 thomas free(commit_refname);
8311 10604dce 2021-09-24 thomas return err;
8314 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
8315 10604dce 2021-09-24 thomas struct got_worktree *worktree;
8316 10604dce 2021-09-24 thomas const char *branch_name;
8319 10604dce 2021-09-24 thomas static const struct got_error *
8320 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8321 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
8323 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
8325 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8326 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
8327 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
8329 10604dce 2021-09-24 thomas return NULL;
8333 10604dce 2021-09-24 thomas const struct got_error *
8334 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
8335 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
8336 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
8337 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
8338 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8339 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8341 10604dce 2021-09-24 thomas const struct got_error *err;
8342 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8344 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8345 10604dce 2021-09-24 thomas if (err)
8346 10604dce 2021-09-24 thomas goto done;
8348 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8349 10604dce 2021-09-24 thomas worktree);
8350 10604dce 2021-09-24 thomas if (err)
8351 10604dce 2021-09-24 thomas goto done;
8353 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8354 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
8355 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
8357 10604dce 2021-09-24 thomas free(fileindex_path);
8358 10604dce 2021-09-24 thomas return err;
8361 10604dce 2021-09-24 thomas const struct got_error *
8362 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
8363 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
8364 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
8365 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
8366 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
8367 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
8370 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
8371 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
8372 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
8373 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
8374 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
8375 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
8376 10604dce 2021-09-24 thomas int have_staged_files = 0;
8377 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
8378 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8380 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
8381 10604dce 2021-09-24 thomas *new_commit_id = NULL;
8383 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
8385 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8386 10604dce 2021-09-24 thomas if (err)
8387 10604dce 2021-09-24 thomas goto done;
8389 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8390 10604dce 2021-09-24 thomas if (err)
8391 10604dce 2021-09-24 thomas goto done;
8393 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
8394 10604dce 2021-09-24 thomas if (err)
8395 10604dce 2021-09-24 thomas goto done;
8397 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8398 10604dce 2021-09-24 thomas &have_staged_files);
8399 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8400 10604dce 2021-09-24 thomas goto done;
8401 10604dce 2021-09-24 thomas if (have_staged_files) {
8402 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8403 10604dce 2021-09-24 thomas goto done;
8406 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
8407 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
8408 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
8409 10604dce 2021-09-24 thomas cc_arg.repo = repo;
8410 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
8411 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8412 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
8413 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8414 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8415 10604dce 2021-09-24 thomas if (err)
8416 10604dce 2021-09-24 thomas goto done;
8418 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
8419 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
8420 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
8421 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
8422 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8423 10604dce 2021-09-24 thomas if (err)
8424 10604dce 2021-09-24 thomas goto done;
8426 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
8427 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
8428 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8429 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8430 10604dce 2021-09-24 thomas err = sync_err;
8432 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8433 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8435 10604dce 2021-09-24 thomas free_commitable(ct);
8437 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8438 10604dce 2021-09-24 thomas free(fileindex_path);
8439 10604dce 2021-09-24 thomas return err;
8442 10604dce 2021-09-24 thomas const struct got_error *
8443 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8444 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8446 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8447 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8449 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8450 10604dce 2021-09-24 thomas if (err)
8451 10604dce 2021-09-24 thomas goto done;
8453 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8454 10604dce 2021-09-24 thomas if (err)
8455 10604dce 2021-09-24 thomas goto done;
8456 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8457 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8458 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8459 10604dce 2021-09-24 thomas err = sync_err;
8461 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8462 10604dce 2021-09-24 thomas free(fileindex_path);
8463 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8464 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8465 10604dce 2021-09-24 thomas err = unlockerr;
8466 10604dce 2021-09-24 thomas return err;
8469 10604dce 2021-09-24 thomas const struct got_error *
8470 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8471 10604dce 2021-09-24 thomas struct got_repository *repo)
8473 10604dce 2021-09-24 thomas const struct got_error *err;
8474 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8475 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8477 10604dce 2021-09-24 thomas *in_progress = 0;
8479 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8480 10604dce 2021-09-24 thomas if (err)
8481 10604dce 2021-09-24 thomas return err;
8482 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8483 dfe86d1f 2021-09-24 thomas free(branch_refname);
8484 10604dce 2021-09-24 thomas if (err) {
8485 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8486 10604dce 2021-09-24 thomas return err;
8488 10604dce 2021-09-24 thomas *in_progress = 1;
8490 10604dce 2021-09-24 thomas return NULL;
8493 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8494 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8495 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8497 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8498 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8499 2b72f32d 2023-06-22 thomas struct got_reference *wt_branch = NULL;
8500 2b72f32d 2023-06-22 thomas struct got_object_id *wt_branch_tip = NULL;
8501 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8503 10604dce 2021-09-24 thomas *fileindex = NULL;
8505 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8506 10604dce 2021-09-24 thomas if (err)
8507 10604dce 2021-09-24 thomas return err;
8509 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8510 10604dce 2021-09-24 thomas if (err)
8511 10604dce 2021-09-24 thomas goto done;
8513 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8514 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8515 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8516 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8517 10604dce 2021-09-24 thomas &ok_arg);
8518 10604dce 2021-09-24 thomas if (err)
8519 10604dce 2021-09-24 thomas goto done;
8521 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8523 10604dce 2021-09-24 thomas if (err)
8524 10604dce 2021-09-24 thomas goto done;
8526 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8527 10604dce 2021-09-24 thomas if (err)
8528 10604dce 2021-09-24 thomas goto done;
8530 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8531 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8532 10604dce 2021-09-24 thomas goto done;
8536 2b72f32d 2023-06-22 thomas free(fileindex_path);
8537 2b72f32d 2023-06-22 thomas if (wt_branch)
8538 2b72f32d 2023-06-22 thomas got_ref_close(wt_branch);
8539 2b72f32d 2023-06-22 thomas free(wt_branch_tip);
8540 2b72f32d 2023-06-22 thomas if (err) {
8541 2b72f32d 2023-06-22 thomas if (*fileindex) {
8542 2b72f32d 2023-06-22 thomas got_fileindex_free(*fileindex);
8543 2b72f32d 2023-06-22 thomas *fileindex = NULL;
8545 2b72f32d 2023-06-22 thomas lock_worktree(worktree, LOCK_SH);
8547 2b72f32d 2023-06-22 thomas return err;
8550 2b72f32d 2023-06-22 thomas const struct got_error *got_worktree_merge_write_refs(
8551 2b72f32d 2023-06-22 thomas struct got_worktree *worktree, struct got_reference *branch,
8552 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8554 2b72f32d 2023-06-22 thomas const struct got_error *err = NULL;
8555 2b72f32d 2023-06-22 thomas char *branch_refname = NULL, *commit_refname = NULL;
8556 2b72f32d 2023-06-22 thomas struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8557 2b72f32d 2023-06-22 thomas struct got_object_id *branch_tip = NULL;
8559 2b72f32d 2023-06-22 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8560 2b72f32d 2023-06-22 thomas if (err)
8561 2b72f32d 2023-06-22 thomas return err;
8563 2b72f32d 2023-06-22 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8564 2b72f32d 2023-06-22 thomas if (err)
8565 2b72f32d 2023-06-22 thomas return err;
8567 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8568 10604dce 2021-09-24 thomas if (err)
8569 10604dce 2021-09-24 thomas goto done;
8571 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8572 10604dce 2021-09-24 thomas if (err)
8573 10604dce 2021-09-24 thomas goto done;
8574 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8575 10604dce 2021-09-24 thomas if (err)
8576 10604dce 2021-09-24 thomas goto done;
8578 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8579 10604dce 2021-09-24 thomas if (err)
8580 10604dce 2021-09-24 thomas goto done;
8581 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8582 10604dce 2021-09-24 thomas if (err)
8583 10604dce 2021-09-24 thomas goto done;
8586 10604dce 2021-09-24 thomas free(branch_refname);
8587 10604dce 2021-09-24 thomas free(commit_refname);
8588 10604dce 2021-09-24 thomas if (branch_ref)
8589 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8590 10604dce 2021-09-24 thomas if (commit_ref)
8591 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8592 2b72f32d 2023-06-22 thomas free(branch_tip);
8593 10604dce 2021-09-24 thomas return err;
8596 10604dce 2021-09-24 thomas const struct got_error *
8597 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8598 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8599 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8601 10604dce 2021-09-24 thomas const struct got_error *err;
8602 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8603 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8604 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8605 10604dce 2021-09-24 thomas int have_staged_files = 0;
8607 10604dce 2021-09-24 thomas *branch_name = NULL;
8608 10604dce 2021-09-24 thomas *branch_tip = NULL;
8609 10604dce 2021-09-24 thomas *fileindex = NULL;
8611 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8612 10604dce 2021-09-24 thomas if (err)
8613 10604dce 2021-09-24 thomas return err;
8615 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8616 10604dce 2021-09-24 thomas if (err)
8617 10604dce 2021-09-24 thomas goto done;
8619 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8620 10604dce 2021-09-24 thomas &have_staged_files);
8621 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8622 10604dce 2021-09-24 thomas goto done;
8623 10604dce 2021-09-24 thomas if (have_staged_files) {
8624 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8625 10604dce 2021-09-24 thomas goto done;
8628 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8629 10604dce 2021-09-24 thomas if (err)
8630 10604dce 2021-09-24 thomas goto done;
8632 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8633 10604dce 2021-09-24 thomas if (err)
8634 10604dce 2021-09-24 thomas goto done;
8636 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8637 10604dce 2021-09-24 thomas if (err)
8638 10604dce 2021-09-24 thomas goto done;
8640 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8641 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8642 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8643 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8644 10604dce 2021-09-24 thomas goto done;
8646 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8647 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8648 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8649 10604dce 2021-09-24 thomas goto done;
8652 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8653 10604dce 2021-09-24 thomas if (err)
8654 10604dce 2021-09-24 thomas goto done;
8656 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8657 10604dce 2021-09-24 thomas if (err)
8658 10604dce 2021-09-24 thomas goto done;
8660 10604dce 2021-09-24 thomas free(commit_refname);
8661 10604dce 2021-09-24 thomas free(branch_refname);
8662 10604dce 2021-09-24 thomas free(fileindex_path);
8663 10604dce 2021-09-24 thomas if (commit_ref)
8664 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8665 10604dce 2021-09-24 thomas if (branch_ref)
8666 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8667 10604dce 2021-09-24 thomas if (err) {
8668 10604dce 2021-09-24 thomas if (*branch_name) {
8669 10604dce 2021-09-24 thomas free(*branch_name);
8670 10604dce 2021-09-24 thomas *branch_name = NULL;
8672 10604dce 2021-09-24 thomas free(*branch_tip);
8673 10604dce 2021-09-24 thomas *branch_tip = NULL;
8674 10604dce 2021-09-24 thomas if (*fileindex) {
8675 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8676 10604dce 2021-09-24 thomas *fileindex = NULL;
8678 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8680 10604dce 2021-09-24 thomas return err;
8683 10604dce 2021-09-24 thomas const struct got_error *
8684 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8685 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8686 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8688 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8689 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8690 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8691 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8692 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
8693 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
8694 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
8695 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8696 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
8698 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
8700 8641a332 2023-04-14 thomas err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8701 8641a332 2023-04-14 thomas if (err)
8702 8641a332 2023-04-14 thomas goto done;
8704 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8705 8641a332 2023-04-14 thomas if (err)
8706 8641a332 2023-04-14 thomas goto done;
8708 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8709 8641a332 2023-04-14 thomas if (err)
8710 8641a332 2023-04-14 thomas goto done;
8713 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
8714 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
8715 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
8716 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
8718 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(&added_paths,
8719 8641a332 2023-04-14 thomas got_worktree_get_base_commit_id(worktree), merged_commit_id,
8720 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
8721 8641a332 2023-04-14 thomas if (err)
8722 8641a332 2023-04-14 thomas goto done;
8725 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8726 945f9229 2022-04-16 thomas worktree->base_commit_id);
8727 945f9229 2022-04-16 thomas if (err)
8728 945f9229 2022-04-16 thomas goto done;
8730 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8731 945f9229 2022-04-16 thomas worktree->path_prefix);
8732 10604dce 2021-09-24 thomas if (err)
8733 10604dce 2021-09-24 thomas goto done;
8735 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8736 10604dce 2021-09-24 thomas if (err)
8737 10604dce 2021-09-24 thomas goto done;
8739 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8740 10604dce 2021-09-24 thomas if (err)
8741 10604dce 2021-09-24 thomas goto done;
8743 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8744 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8745 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8746 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8747 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8748 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8749 10604dce 2021-09-24 thomas rfa.repo = repo;
8750 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8751 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8752 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8753 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8754 10604dce 2021-09-24 thomas if (err)
8755 10604dce 2021-09-24 thomas goto sync;
8757 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8758 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8760 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8761 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8762 10604dce 2021-09-24 thomas err = sync_err;
8764 10604dce 2021-09-24 thomas free(tree_id);
8765 8641a332 2023-04-14 thomas free(merged_commit_id);
8766 945f9229 2022-04-16 thomas if (commit)
8767 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8768 10604dce 2021-09-24 thomas if (fileindex)
8769 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8770 10604dce 2021-09-24 thomas free(fileindex_path);
8771 8641a332 2023-04-14 thomas if (commit_ref)
8772 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8773 8641a332 2023-04-14 thomas free(commit_ref_name);
8775 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8776 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8777 10604dce 2021-09-24 thomas err = unlockerr;
8778 10604dce 2021-09-24 thomas return err;
8781 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8782 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8783 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8784 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8785 2db2652d 2019-08-07 stsp struct got_repository *repo;
8786 2db2652d 2019-08-07 stsp int have_changes;
8789 ef20f542 2022-06-26 thomas static const struct got_error *
8790 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8791 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8792 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8793 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8795 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8796 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8797 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8798 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8799 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8800 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8802 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8803 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8804 8b13ce36 2019-08-08 stsp return NULL;
8805 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8806 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8808 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8809 735ef5ac 2019-08-03 stsp if (ie == NULL)
8810 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8812 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8813 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8814 735ef5ac 2019-08-03 stsp relpath) == -1)
8815 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8817 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8818 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
8819 43010591 2023-02-17 thomas &base_commit_id, ie);
8822 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8823 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8824 3aa5969e 2019-08-06 stsp goto done;
8825 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8826 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8827 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8828 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8829 735ef5ac 2019-08-03 stsp goto done;
8832 2db2652d 2019-08-07 stsp a->have_changes = 1;
8834 735ef5ac 2019-08-03 stsp p = in_repo_path;
8835 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8837 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8838 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8839 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8841 735ef5ac 2019-08-03 stsp free(in_repo_path);
8842 dc424a06 2019-08-07 stsp return err;
8845 2db2652d 2019-08-07 stsp struct stage_path_arg {
8846 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8847 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8848 2db2652d 2019-08-07 stsp struct got_repository *repo;
8849 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8850 2db2652d 2019-08-07 stsp void *status_arg;
8851 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8852 2db2652d 2019-08-07 stsp void *patch_arg;
8853 7b5dc508 2019-10-28 stsp int staged_something;
8854 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8857 2db2652d 2019-08-07 stsp static const struct got_error *
8858 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8859 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8860 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8861 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8863 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8864 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8865 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8866 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8867 0cb83759 2019-08-03 stsp uint32_t stage;
8868 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8869 0aeb8099 2020-07-23 stsp struct stat sb;
8871 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8872 8b13ce36 2019-08-08 stsp return NULL;
8874 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8875 d3e7c587 2019-08-03 stsp if (ie == NULL)
8876 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8878 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8879 2db2652d 2019-08-07 stsp relpath)== -1)
8880 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8882 0cb83759 2019-08-03 stsp switch (status) {
8883 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8884 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8885 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8886 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8887 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8890 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8891 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8892 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8893 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8894 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8897 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8900 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8901 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8902 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8903 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8904 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8908 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8909 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8912 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8913 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8914 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8915 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8917 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8918 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8919 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8920 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8921 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8922 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8923 35213c7c 2020-07-23 stsp ssize_t target_len;
8924 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8925 35213c7c 2020-07-23 stsp sizeof(target_path));
8926 35213c7c 2020-07-23 stsp if (target_len == -1) {
8927 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8928 35213c7c 2020-07-23 stsp ondisk_path);
8931 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8932 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8933 35213c7c 2020-07-23 stsp a->worktree->root_path);
8936 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8937 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8938 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8942 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8943 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8944 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8946 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8947 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8949 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8950 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8952 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8953 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8955 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8956 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8957 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8958 f289c82c 2022-06-13 thomas if (err)
8961 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8962 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8964 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8965 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8966 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8967 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8970 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8971 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8973 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8974 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8975 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8976 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8979 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8981 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8982 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8986 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8987 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8988 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8989 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8991 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8992 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8995 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8997 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8998 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9000 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
9001 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
9004 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9008 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
9009 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
9010 dc424a06 2019-08-07 stsp free(path_content);
9011 2db2652d 2019-08-07 stsp free(ondisk_path);
9012 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
9013 0ebf8283 2019-07-24 stsp return err;
9016 0cb83759 2019-08-03 stsp const struct got_error *
9017 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
9018 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
9019 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
9020 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9021 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
9023 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9024 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9025 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9026 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9027 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9028 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9029 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9030 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9032 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9034 0cb83759 2019-08-03 stsp return err;
9036 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9037 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9039 735ef5ac 2019-08-03 stsp goto done;
9040 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9042 735ef5ac 2019-08-03 stsp goto done;
9043 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9045 0cb83759 2019-08-03 stsp goto done;
9047 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9048 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9049 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9050 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9051 2db2652d 2019-08-07 stsp oka.repo = repo;
9052 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9053 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9054 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9055 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
9057 735ef5ac 2019-08-03 stsp goto done;
9059 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9060 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9061 2db2652d 2019-08-07 stsp goto done;
9064 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9065 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9066 2db2652d 2019-08-07 stsp spa.repo = repo;
9067 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9068 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9069 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9070 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9071 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9072 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9073 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9074 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9075 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
9077 2db2652d 2019-08-07 stsp goto done;
9079 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9080 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9081 7b5dc508 2019-10-28 stsp goto done;
9084 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9085 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9086 0cb83759 2019-08-03 stsp err = sync_err;
9088 735ef5ac 2019-08-03 stsp if (head_ref)
9089 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9090 735ef5ac 2019-08-03 stsp free(head_commit_id);
9091 0cb83759 2019-08-03 stsp free(fileindex_path);
9092 0cb83759 2019-08-03 stsp if (fileindex)
9093 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9094 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9095 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9096 0cb83759 2019-08-03 stsp err = unlockerr;
9097 0cb83759 2019-08-03 stsp return err;
9100 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9101 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9102 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9103 ad493afc 2019-08-03 stsp struct got_repository *repo;
9104 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9105 ad493afc 2019-08-03 stsp void *progress_arg;
9106 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9107 2e1f37b0 2019-08-08 stsp void *patch_arg;
9110 2e1f37b0 2019-08-08 stsp static const struct got_error *
9111 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9112 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9113 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9114 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9115 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9117 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9118 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9119 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9120 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9121 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9122 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9123 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9124 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9126 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9127 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9129 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9131 2e1f37b0 2019-08-08 stsp return err;
9133 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9134 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9135 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9136 f4ae6ddb 2022-07-01 thomas goto done;
9138 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9139 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9140 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9141 f4ae6ddb 2022-07-01 thomas goto done;
9144 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9146 2e1f37b0 2019-08-08 stsp goto done;
9148 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9150 2e1f37b0 2019-08-08 stsp goto done;
9152 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9154 2e1f37b0 2019-08-08 stsp goto done;
9156 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9159 2e1f37b0 2019-08-08 stsp goto done;
9161 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9163 2e1f37b0 2019-08-08 stsp goto done;
9165 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9167 2e1f37b0 2019-08-08 stsp goto done;
9169 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9170 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9172 2e1f37b0 2019-08-08 stsp goto done;
9174 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9175 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
9177 2e1f37b0 2019-08-08 stsp goto done;
9178 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9179 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
9181 2e1f37b0 2019-08-08 stsp goto done;
9183 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9184 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9185 2e1f37b0 2019-08-08 stsp goto done;
9187 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9188 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9189 2e1f37b0 2019-08-08 stsp goto done;
9191 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9192 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9193 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
9194 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9195 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9196 fe621944 2020-11-10 stsp nchanges++;
9198 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9199 2e1f37b0 2019-08-08 stsp int choice;
9200 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9201 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9202 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9203 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9205 2e1f37b0 2019-08-08 stsp goto done;
9206 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9207 2e1f37b0 2019-08-08 stsp have_content = 1;
9209 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9210 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9213 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9214 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9215 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9217 2e1f37b0 2019-08-08 stsp free(label1);
9218 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9219 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9221 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9222 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9223 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9224 2e1f37b0 2019-08-08 stsp if (staged_blob)
9225 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9226 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9227 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9228 fe621944 2020-11-10 stsp err = free_err;
9229 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9230 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9231 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9232 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9233 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9234 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9235 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9236 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9237 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9238 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9239 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9240 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9241 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9242 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9243 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9244 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9245 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9246 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9247 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9249 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9250 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9251 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9252 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9253 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9254 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9255 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9257 2e1f37b0 2019-08-08 stsp free(path1);
9258 2e1f37b0 2019-08-08 stsp free(path2);
9259 fda8017d 2020-07-23 stsp return err;
9262 fda8017d 2020-07-23 stsp static const struct got_error *
9263 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9264 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9265 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9266 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9267 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9268 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9269 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9271 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9272 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9273 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9274 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9275 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9276 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9277 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9278 fda8017d 2020-07-23 stsp struct stat sb;
9280 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9281 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9282 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9284 fda8017d 2020-07-23 stsp return err;
9286 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9287 fda8017d 2020-07-23 stsp return NULL;
9289 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9290 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9291 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9293 fda8017d 2020-07-23 stsp goto done;
9296 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
9297 fda8017d 2020-07-23 stsp if (f == NULL) {
9298 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9299 fda8017d 2020-07-23 stsp path_unstaged_content);
9300 fda8017d 2020-07-23 stsp goto done;
9302 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9303 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9304 fda8017d 2020-07-23 stsp goto done;
9306 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9307 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9308 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9310 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9311 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9312 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9313 fda8017d 2020-07-23 stsp goto done;
9315 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9316 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9317 fda8017d 2020-07-23 stsp goto done;
9319 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9320 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9321 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9322 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9323 fda8017d 2020-07-23 stsp progress_arg);
9325 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9327 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9329 67a66647 2021-05-31 stsp return err;
9331 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9332 67a66647 2021-05-31 stsp parent) == -1) {
9333 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9334 67a66647 2021-05-31 stsp base_path = NULL;
9335 67a66647 2021-05-31 stsp goto done;
9338 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
9339 fc2a50f2 2022-11-01 thomas base_path, "");
9341 67a66647 2021-05-31 stsp goto done;
9342 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9343 67a66647 2021-05-31 stsp blob_base);
9345 67a66647 2021-05-31 stsp goto done;
9348 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9349 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9351 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9352 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9353 eec2f5a9 2021-06-03 stsp ondisk_path);
9355 eec2f5a9 2021-06-03 stsp goto done;
9358 06340621 2021-12-31 thomas fd = open(ondisk_path,
9359 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9360 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9361 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9362 eec2f5a9 2021-06-03 stsp goto done;
9364 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9365 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9366 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9367 eec2f5a9 2021-06-03 stsp close(fd);
9368 eec2f5a9 2021-06-03 stsp goto done;
9372 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9373 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9374 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9375 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9376 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9379 fda8017d 2020-07-23 stsp goto done;
9381 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9382 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9383 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9385 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9386 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9389 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9390 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9391 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9392 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9393 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9394 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9395 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9396 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9397 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9398 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9399 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9400 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9401 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9402 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9403 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9404 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9405 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9406 67a66647 2021-05-31 stsp free(blob_base_path);
9407 67a66647 2021-05-31 stsp free(parent);
9408 67a66647 2021-05-31 stsp free(base_path);
9409 2e1f37b0 2019-08-08 stsp return err;
9412 ad493afc 2019-08-03 stsp static const struct got_error *
9413 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9414 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9415 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9416 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9418 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9419 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9420 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9421 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9422 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9423 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9424 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9425 9bc94a15 2019-08-03 stsp struct stat sb;
9426 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9428 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9429 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9430 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9431 2e1f37b0 2019-08-08 stsp return NULL;
9433 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9434 ad493afc 2019-08-03 stsp if (ie == NULL)
9435 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9437 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9439 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9441 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9442 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9444 f69721c3 2019-10-21 stsp goto done;
9445 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9446 f69721c3 2019-10-21 stsp id_str) == -1) {
9447 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9448 f4ae6ddb 2022-07-01 thomas goto done;
9451 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9452 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9453 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9454 f4ae6ddb 2022-07-01 thomas goto done;
9456 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9457 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9458 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9459 f69721c3 2019-10-21 stsp goto done;
9462 ad493afc 2019-08-03 stsp switch (staged_status) {
9463 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9464 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9465 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
9468 ad493afc 2019-08-03 stsp /* fall through */
9469 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9470 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9471 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9472 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9473 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9474 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9477 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9480 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9481 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9482 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9483 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9484 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9485 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9488 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9489 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9492 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9493 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9494 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9495 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9496 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9497 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9498 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9499 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9500 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9502 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9503 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9504 36bf999c 2020-07-23 stsp char *staged_target;
9505 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9506 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9508 36bf999c 2020-07-23 stsp goto done;
9509 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9510 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9511 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9512 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9513 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9514 36bf999c 2020-07-23 stsp free(staged_target);
9516 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9517 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9518 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9519 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9520 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9521 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9522 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9526 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9529 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9530 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9531 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9532 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9535 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9536 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9537 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9538 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9539 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9542 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9544 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9545 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9549 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9550 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9551 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9552 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9555 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9559 ad493afc 2019-08-03 stsp free(ondisk_path);
9560 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9561 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9562 ad493afc 2019-08-03 stsp if (blob_base)
9563 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9564 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9565 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9566 ad493afc 2019-08-03 stsp if (blob_staged)
9567 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9568 f69721c3 2019-10-21 stsp free(id_str);
9569 f69721c3 2019-10-21 stsp free(label_orig);
9570 ad493afc 2019-08-03 stsp return err;
9573 ad493afc 2019-08-03 stsp const struct got_error *
9574 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9575 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9576 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9577 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9578 ad493afc 2019-08-03 stsp struct got_repository *repo)
9580 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9581 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9582 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9583 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9584 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9586 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9588 ad493afc 2019-08-03 stsp return err;
9590 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9592 ad493afc 2019-08-03 stsp goto done;
9594 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9595 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9596 ad493afc 2019-08-03 stsp upa.repo = repo;
9597 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9598 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9599 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9600 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9601 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9602 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9603 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9605 ad493afc 2019-08-03 stsp goto done;
9608 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9609 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9610 ad493afc 2019-08-03 stsp err = sync_err;
9612 ad493afc 2019-08-03 stsp free(fileindex_path);
9613 ad493afc 2019-08-03 stsp if (fileindex)
9614 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9615 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9616 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9617 ad493afc 2019-08-03 stsp err = unlockerr;
9618 ad493afc 2019-08-03 stsp return err;
9621 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9622 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9623 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9624 b2118c49 2020-07-28 stsp void *info_arg;
9625 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9626 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9627 b2118c49 2020-07-28 stsp void *cancel_arg;
9630 b2118c49 2020-07-28 stsp static const struct got_error *
9631 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9633 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9634 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9635 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9636 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9637 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9638 b2118c49 2020-07-28 stsp int stage;
9640 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9641 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9643 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9644 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9645 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9648 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9649 b2118c49 2020-07-28 stsp return NULL;
9651 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9652 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9653 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9654 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9655 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9656 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9657 43010591 2023-02-17 thomas &staged_blob_id, ie);
9660 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9661 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9663 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9664 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9667 b2118c49 2020-07-28 stsp const struct got_error *
9668 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9669 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9670 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9671 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9674 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9675 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9676 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9677 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9679 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9681 b2118c49 2020-07-28 stsp return err;
9683 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9685 b2118c49 2020-07-28 stsp goto done;
9687 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9688 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9689 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9690 b2118c49 2020-07-28 stsp arg.paths = paths;
9691 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9692 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9693 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9696 b2118c49 2020-07-28 stsp free(fileindex_path);
9697 b2118c49 2020-07-28 stsp if (fileindex)
9698 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9699 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9700 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9701 b2118c49 2020-07-28 stsp err = unlockerr;
9702 b2118c49 2020-07-28 stsp return err;
9705 814624e7 2022-03-22 thomas static const struct got_error *
9706 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9707 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9708 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9710 814624e7 2022-03-22 thomas const struct got_error *err;
9711 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9712 814624e7 2022-03-22 thomas struct stat sb;
9713 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9715 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9716 814624e7 2022-03-22 thomas if (err)
9717 814624e7 2022-03-22 thomas return err;
9719 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9720 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9721 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9723 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9724 814624e7 2022-03-22 thomas if (ie) {
9725 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9726 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9728 814624e7 2022-03-22 thomas if (err)
9729 814624e7 2022-03-22 thomas goto done;
9730 814624e7 2022-03-22 thomas } else {
9731 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9732 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9733 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9734 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9735 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9736 814624e7 2022-03-22 thomas ondisk_path);
9737 814624e7 2022-03-22 thomas goto done;
9739 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9744 814624e7 2022-03-22 thomas free(ondisk_path);
9745 814624e7 2022-03-22 thomas return err;
9748 814624e7 2022-03-22 thomas static const struct got_error *
9749 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
9750 814624e7 2022-03-22 thomas unsigned char staged_status)
9752 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9753 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9754 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9755 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9756 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9757 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9758 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9759 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9760 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9761 814624e7 2022-03-22 thomas return NULL;
9764 814624e7 2022-03-22 thomas static const struct got_error *
9765 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9767 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9768 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9769 814624e7 2022-03-22 thomas return NULL;
9772 814624e7 2022-03-22 thomas static const struct got_error *
9773 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9774 814624e7 2022-03-22 thomas unsigned char staged_status)
9776 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9777 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9778 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9779 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9780 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9781 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9782 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9783 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9784 814624e7 2022-03-22 thomas return NULL;
9787 814624e7 2022-03-22 thomas const struct got_error *
9788 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9789 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9791 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9794 814624e7 2022-03-22 thomas const struct got_error *
9795 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9796 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9797 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9799 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9800 814624e7 2022-03-22 thomas int file_renamed = 0;
9801 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9802 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9804 814624e7 2022-03-22 thomas *oldpath = NULL;
9805 814624e7 2022-03-22 thomas *newpath = NULL;
9807 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9808 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9809 814624e7 2022-03-22 thomas if (err)
9810 814624e7 2022-03-22 thomas goto done;
9812 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9813 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9814 814624e7 2022-03-22 thomas if (err)
9815 814624e7 2022-03-22 thomas goto done;
9817 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9818 814624e7 2022-03-22 thomas file_renamed = 1;
9820 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9821 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9822 814624e7 2022-03-22 thomas else if (file_renamed) {
9823 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9824 814624e7 2022-03-22 thomas if (err == NULL)
9825 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9826 814624e7 2022-03-22 thomas } else if (old == NULL)
9827 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9829 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9832 814624e7 2022-03-22 thomas if (err) {
9833 814624e7 2022-03-22 thomas free(*oldpath);
9834 814624e7 2022-03-22 thomas *oldpath = NULL;
9835 814624e7 2022-03-22 thomas free(*newpath);
9836 814624e7 2022-03-22 thomas *newpath = NULL;
9838 814624e7 2022-03-22 thomas return err;
9841 814624e7 2022-03-22 thomas const struct got_error *
9842 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9843 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9844 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9846 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9848 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9849 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9850 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9851 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9852 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9853 5e22b737 2022-05-31 thomas saa.repo = repo;
9855 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9856 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9859 5e22b737 2022-05-31 thomas const struct got_error *
9860 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9861 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9862 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9864 8ec7352a 2023-06-01 thomas const struct got_error *err;
9865 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9866 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
9868 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9869 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9870 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9871 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9872 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9873 5e22b737 2022-05-31 thomas sda.repo = repo;
9874 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9875 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9876 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9877 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9878 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s/%s",
9879 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree), path) == -1)
9880 8ec7352a 2023-06-01 thomas return got_error_from_errno("asprintf");
9881 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
9882 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
9884 8ec7352a 2023-06-01 thomas err = worktree_status(worktree, path, fileindex, repo,
9885 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9886 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
9887 8ec7352a 2023-06-01 thomas return err;
9890 5e22b737 2022-05-31 thomas const struct got_error *
9891 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9892 36832a8e 2022-05-31 thomas const char *fileindex_path)
9894 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9896 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9897 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9899 5e22b737 2022-05-31 thomas return err;