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 59530ea7 2023-07-17 thomas /* We only accept worktree-relative paths here. */
398 59530ea7 2023-07-17 thomas if (got_path_is_absolute(path)) {
399 59530ea7 2023-07-17 thomas return got_error_fmt(GOT_ERR_BAD_PATH,
400 59530ea7 2023-07-17 thomas "%s does not accept absolute paths: %s",
401 59530ea7 2023-07-17 thomas __func__, path);
404 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
405 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
407 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
408 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
409 ddcd8544 2019-03-11 stsp struct stat sb;
410 ddcd8544 2019-03-11 stsp err = NULL;
411 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
412 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
413 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
414 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
415 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
418 4a1ddfc2 2019-01-12 stsp free(abspath);
419 68c76935 2019-02-19 stsp return err;
422 68c76935 2019-02-19 stsp static const struct got_error *
423 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
425 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
426 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
427 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
428 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
433 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
434 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
435 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
438 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
439 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
440 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
443 68c76935 2019-02-19 stsp if (flen1 == 0) {
444 68c76935 2019-02-19 stsp if (flen2 != 0)
447 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
448 68c76935 2019-02-19 stsp if (flen1 != 0)
451 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
452 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
462 68c76935 2019-02-19 stsp return err;
465 68c76935 2019-02-19 stsp static const struct got_error *
466 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
468 68c76935 2019-02-19 stsp struct stat sb;
469 68c76935 2019-02-19 stsp size_t size1, size2;
473 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
474 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
475 68c76935 2019-02-19 stsp size1 = sb.st_size;
477 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
478 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
479 68c76935 2019-02-19 stsp size2 = sb.st_size;
481 68c76935 2019-02-19 stsp if (size1 != size2) {
483 68c76935 2019-02-19 stsp return NULL;
486 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
487 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
488 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
489 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
491 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
494 24f136e0 2021-11-20 thomas static const struct got_error *
495 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
497 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
498 24f136e0 2021-11-20 thomas size_t flen;
499 24f136e0 2021-11-20 thomas ssize_t outlen;
501 24f136e0 2021-11-20 thomas *outsize = 0;
503 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
504 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
506 24f136e0 2021-11-20 thomas for (;;) {
507 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
508 24f136e0 2021-11-20 thomas if (flen == 0) {
509 24f136e0 2021-11-20 thomas if (ferror(f))
510 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
511 24f136e0 2021-11-20 thomas if (feof(f))
514 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
515 24f136e0 2021-11-20 thomas if (outlen == -1)
516 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
517 24f136e0 2021-11-20 thomas if (outlen != flen)
518 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
519 24f136e0 2021-11-20 thomas *outsize += outlen;
522 24f136e0 2021-11-20 thomas return NULL;
525 24f136e0 2021-11-20 thomas static const struct got_error *
526 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
527 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
528 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
529 24f136e0 2021-11-20 thomas const char *ondisk_path)
531 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
532 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
533 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
534 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
535 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
536 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
537 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
539 24f136e0 2021-11-20 thomas *overlapcnt = 0;
541 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
543 24f136e0 2021-11-20 thomas return err;
545 24f136e0 2021-11-20 thomas if (same_content)
546 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
548 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
550 24f136e0 2021-11-20 thomas return err;
551 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
552 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
554 24f136e0 2021-11-20 thomas return err;
555 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
557 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
558 24f136e0 2021-11-20 thomas *overlapcnt = 1;
559 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
560 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
561 24f136e0 2021-11-20 thomas goto done;
563 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
564 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
565 24f136e0 2021-11-20 thomas goto done;
567 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
568 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
569 24f136e0 2021-11-20 thomas goto done;
571 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
572 fc2a50f2 2022-11-01 thomas base_path_orig, "");
574 24f136e0 2021-11-20 thomas goto done;
575 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
576 fc2a50f2 2022-11-01 thomas base_path_deriv, "");
578 24f136e0 2021-11-20 thomas goto done;
579 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
580 fc2a50f2 2022-11-01 thomas base_path_deriv2, "");
582 24f136e0 2021-11-20 thomas goto done;
583 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
585 24f136e0 2021-11-20 thomas goto done;
586 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
588 24f136e0 2021-11-20 thomas goto done;
589 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
591 24f136e0 2021-11-20 thomas goto done;
592 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
593 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
594 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
595 24f136e0 2021-11-20 thomas goto done;
597 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
598 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
599 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
600 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
601 24f136e0 2021-11-20 thomas path_deriv) < 0) {
602 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
603 24f136e0 2021-11-20 thomas goto done;
605 24f136e0 2021-11-20 thomas if (size_orig > 0) {
606 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
607 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
608 24f136e0 2021-11-20 thomas label_orig ? " " : "",
609 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
610 24f136e0 2021-11-20 thomas path_orig) < 0) {
611 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
612 24f136e0 2021-11-20 thomas goto done;
615 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
616 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
617 24f136e0 2021-11-20 thomas path_deriv2,
618 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
619 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
620 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
621 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
622 24f136e0 2021-11-20 thomas goto done;
624 24f136e0 2021-11-20 thomas } else if (changed_deriv)
625 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
626 24f136e0 2021-11-20 thomas else if (changed_deriv2)
627 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
629 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
630 24f136e0 2021-11-20 thomas err == NULL)
631 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
632 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
633 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
634 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
635 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
636 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
637 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
638 24f136e0 2021-11-20 thomas free(path_orig);
639 24f136e0 2021-11-20 thomas free(path_deriv);
640 24f136e0 2021-11-20 thomas free(path_deriv2);
641 24f136e0 2021-11-20 thomas free(base_path_orig);
642 24f136e0 2021-11-20 thomas free(base_path_deriv);
643 24f136e0 2021-11-20 thomas free(base_path_deriv2);
644 24f136e0 2021-11-20 thomas return err;
648 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
649 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
650 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
651 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
652 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
654 6353ad76 2019-02-08 stsp static const struct got_error *
655 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
656 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
657 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
658 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
659 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
660 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
662 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
663 6353ad76 2019-02-08 stsp int merged_fd = -1;
664 db590691 2021-06-02 stsp FILE *f_merged = NULL;
665 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
666 234035bc 2019-06-01 stsp int overlapcnt = 0;
667 3524bbf9 2020-10-20 stsp char *parent = NULL;
669 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
671 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
673 3524bbf9 2020-10-20 stsp return err;
675 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
676 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
680 fc2a50f2 2022-11-01 thomas err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
684 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
685 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
686 24f136e0 2021-11-20 thomas if (err) {
687 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
688 24f136e0 2021-11-20 thomas goto done;
689 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
690 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
691 24f136e0 2021-11-20 thomas ondisk_path);
693 24f136e0 2021-11-20 thomas goto done;
696 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
697 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
701 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
702 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
706 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
707 db590691 2021-06-02 stsp if (f_merged == NULL) {
708 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
711 db590691 2021-06-02 stsp merged_fd = -1;
713 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
714 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
715 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
721 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
722 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
726 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
727 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
728 230a42bd 2019-05-11 jcs ondisk_path);
733 fdcb7daf 2019-12-15 stsp if (merged_path)
734 fdcb7daf 2019-12-15 stsp unlink(merged_path);
736 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
737 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
738 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
739 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
740 6353ad76 2019-02-08 stsp free(merged_path);
741 af54ae4a 2019-02-19 stsp free(base_path);
742 3524bbf9 2020-10-20 stsp free(parent);
743 af57b12a 2020-07-23 stsp return err;
746 af57b12a 2020-07-23 stsp static const struct got_error *
747 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
748 af57b12a 2020-07-23 stsp size_t target_len)
750 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
751 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
752 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
753 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
754 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
755 af57b12a 2020-07-23 stsp ondisk_path);
756 af57b12a 2020-07-23 stsp return NULL;
760 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
761 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
762 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
763 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
765 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
766 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
768 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
769 993e2a1b 2020-07-23 stsp * has deleted this symlink.
771 11cc08c1 2020-07-23 stsp static const struct got_error *
772 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
773 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
774 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
776 11cc08c1 2020-07-23 stsp const struct got_error *err;
777 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
778 11cc08c1 2020-07-23 stsp FILE *f = NULL;
780 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
782 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
784 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
785 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
786 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
790 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
792 3818e3c4 2020-11-01 naddy goto done;
794 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
795 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
799 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
800 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
801 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
802 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
803 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
804 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
805 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
806 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
807 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
808 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
812 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
813 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
816 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
817 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
821 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
822 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
823 11cc08c1 2020-07-23 stsp free(path);
824 11cc08c1 2020-07-23 stsp free(id_str);
825 11cc08c1 2020-07-23 stsp free(label_deriv);
826 11cc08c1 2020-07-23 stsp return err;
829 d219f183 2020-07-23 stsp /* forward declaration */
830 d219f183 2020-07-23 stsp static const struct got_error *
831 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
832 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
833 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
834 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
837 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
838 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
839 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
840 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
842 af57b12a 2020-07-23 stsp static const struct got_error *
843 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
844 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
845 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
846 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
847 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
849 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
850 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
851 af57b12a 2020-07-23 stsp struct stat sb;
852 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
853 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
854 11cc08c1 2020-07-23 stsp int have_local_change = 0;
855 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
857 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
858 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
860 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
861 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
862 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
863 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
864 af57b12a 2020-07-23 stsp ondisk_path);
867 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
869 526a746f 2020-07-23 stsp if (blob_orig) {
870 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
875 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
876 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
877 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
878 11cc08c1 2020-07-23 stsp have_local_change = 1;
880 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
881 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
882 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
883 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
884 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
886 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
887 11cc08c1 2020-07-23 stsp if (ancestor_target) {
888 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
889 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
891 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
892 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
893 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
894 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
897 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
898 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
899 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
900 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
903 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
906 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
907 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
908 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
909 af57b12a 2020-07-23 stsp strlen(deriv_target));
912 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
913 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
914 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
915 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
916 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
917 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
920 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
921 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
922 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
925 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
931 af57b12a 2020-07-23 stsp free(ancestor_target);
932 eec2f5a9 2021-06-03 stsp return err;
935 eec2f5a9 2021-06-03 stsp static const struct got_error *
936 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
938 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
939 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
940 eec2f5a9 2021-06-03 stsp ssize_t target_len;
944 eec2f5a9 2021-06-03 stsp *outfile = NULL;
946 eec2f5a9 2021-06-03 stsp f = got_opentemp();
947 eec2f5a9 2021-06-03 stsp if (f == NULL)
948 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
949 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
950 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
951 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
954 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
955 eec2f5a9 2021-06-03 stsp if (n != target_len) {
956 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
959 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
960 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
963 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
964 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
971 eec2f5a9 2021-06-03 stsp *outfile = f;
972 14c901f1 2019-08-08 stsp return err;
976 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
977 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
978 14c901f1 2019-08-08 stsp * acts as the second derived version.
980 14c901f1 2019-08-08 stsp static const struct got_error *
981 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
982 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
983 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
984 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
985 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
986 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
988 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
989 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
990 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
991 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
992 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
994 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
996 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
998 ed6b5030 2020-10-20 stsp return err;
1000 67a66647 2021-05-31 stsp if (blob_orig) {
1001 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
1002 67a66647 2021-05-31 stsp parent) == -1) {
1003 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
1004 67a66647 2021-05-31 stsp base_path = NULL;
1005 67a66647 2021-05-31 stsp goto done;
1008 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_orig_path, &f_orig,
1009 fc2a50f2 2022-11-01 thomas base_path, "");
1011 67a66647 2021-05-31 stsp goto done;
1012 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1013 67a66647 2021-05-31 stsp blob_orig);
1015 67a66647 2021-05-31 stsp goto done;
1016 67a66647 2021-05-31 stsp free(base_path);
1019 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1020 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1021 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1023 db590691 2021-06-02 stsp f_orig = got_opentemp();
1024 db590691 2021-06-02 stsp if (f_orig == NULL) {
1025 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1026 db590691 2021-06-02 stsp goto done;
1030 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1031 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1032 14c901f1 2019-08-08 stsp base_path = NULL;
1033 14c901f1 2019-08-08 stsp goto done;
1036 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1038 14c901f1 2019-08-08 stsp goto done;
1039 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1040 14c901f1 2019-08-08 stsp blob_deriv);
1042 14c901f1 2019-08-08 stsp goto done;
1044 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1046 14c901f1 2019-08-08 stsp goto done;
1047 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1048 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1049 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1050 14c901f1 2019-08-08 stsp goto done;
1054 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1055 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1057 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1058 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1060 eec2f5a9 2021-06-03 stsp goto done;
1063 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1064 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1065 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1066 eec2f5a9 2021-06-03 stsp goto done;
1068 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1069 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1070 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1071 eec2f5a9 2021-06-03 stsp close(fd);
1072 eec2f5a9 2021-06-03 stsp goto done;
1076 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1077 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1078 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1080 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1081 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1082 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1083 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1084 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1085 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1086 14c901f1 2019-08-08 stsp free(base_path);
1087 67a66647 2021-05-31 stsp if (blob_orig_path) {
1088 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1089 67a66647 2021-05-31 stsp free(blob_orig_path);
1091 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1092 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1093 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1095 6353ad76 2019-02-08 stsp free(id_str);
1096 818c7501 2019-07-11 stsp free(label_deriv);
1097 ed6b5030 2020-10-20 stsp free(parent);
1098 4a1ddfc2 2019-01-12 stsp return err;
1101 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1102 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1103 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1104 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1106 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1107 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1109 65b05cec 2020-07-23 stsp *new_iep = NULL;
1111 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1113 054041d0 2020-06-24 stsp return err;
1115 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1116 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1118 054041d0 2020-06-24 stsp goto done;
1120 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1123 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1125 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1126 13d9040b 2019-03-27 stsp return err;
1129 1ebedb77 2019-10-19 stsp static mode_t
1130 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1132 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1134 1ebedb77 2019-10-19 stsp if (executable) {
1135 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1136 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1137 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1138 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1139 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1140 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1143 a2c162eb 2022-10-30 thomas return st_mode;
1146 8ba819a3 2020-07-23 stsp /* forward declaration */
1147 13d9040b 2019-03-27 stsp static const struct got_error *
1148 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1149 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1150 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1151 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1152 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1153 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1156 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1157 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1159 8ba819a3 2020-07-23 stsp static const struct got_error *
1160 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1161 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1163 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1164 5a1fbc73 2020-07-23 stsp ssize_t elen;
1165 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1168 c6e8a826 2021-04-05 stsp *did_something = 0;
1171 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1172 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1173 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1174 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1175 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1176 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1178 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1179 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1180 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1181 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1183 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1184 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1185 5a1fbc73 2020-07-23 stsp if (elen == -1)
1186 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1188 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1189 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1190 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1193 c6e8a826 2021-04-05 stsp *did_something = 1;
1194 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1195 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1196 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1197 5a1fbc73 2020-07-23 stsp return err;
1200 3c1ec782 2020-07-23 stsp static const struct got_error *
1201 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1202 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1204 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1205 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1206 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1208 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1210 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1211 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1212 3c1ec782 2020-07-23 stsp return NULL;
1216 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1217 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1218 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1219 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1220 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1222 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1223 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1224 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1226 ce031e9e 2020-10-20 stsp return err;
1227 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1228 ce031e9e 2020-10-20 stsp free(parent);
1229 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1231 ce031e9e 2020-10-20 stsp free(parent);
1232 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1233 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1234 3c1ec782 2020-07-23 stsp free(abspath);
1235 3c1ec782 2020-07-23 stsp return err;
1237 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1238 3c1ec782 2020-07-23 stsp free(abspath);
1240 3c1ec782 2020-07-23 stsp return err;
1242 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1244 3c1ec782 2020-07-23 stsp return err;
1247 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1248 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1249 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1250 3c1ec782 2020-07-23 stsp return NULL;
1253 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1254 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1255 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1256 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1257 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1258 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1260 3c1ec782 2020-07-23 stsp free(path_got);
1261 3c1ec782 2020-07-23 stsp return NULL;
1264 5a1fbc73 2020-07-23 stsp static const struct got_error *
1265 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1266 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1267 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1268 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1269 ace90326 2021-09-27 thomas struct got_repository *repo,
1270 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1272 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1273 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1274 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1275 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1276 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1278 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1281 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1282 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1283 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1284 8ba819a3 2020-07-23 stsp * in the blob object.
1287 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1288 3521b466 2022-07-21 thomas if (err)
1289 3521b466 2022-07-21 thomas return err;
1291 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1292 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1293 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1294 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1295 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1296 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1297 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1298 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1299 3b9f0f87 2020-07-23 stsp progress_arg);
1301 8ba819a3 2020-07-23 stsp if (len > 0) {
1302 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1303 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1304 8ba819a3 2020-07-23 stsp len - hdrlen);
1305 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1306 8ba819a3 2020-07-23 stsp hdrlen = 0;
1308 8ba819a3 2020-07-23 stsp } while (len != 0);
1309 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1311 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1312 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1314 3c1ec782 2020-07-23 stsp return err;
1316 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1317 906c123b 2020-07-23 stsp /* install as a regular file */
1318 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1319 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1320 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1321 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1322 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1323 9e39ca77 2022-07-21 thomas return err;
1326 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1327 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1328 c6e8a826 2021-04-05 stsp int symlink_replaced;
1329 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1330 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1331 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1332 9e39ca77 2022-07-21 thomas return err;
1334 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1335 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1337 9e39ca77 2022-07-21 thomas return err;
1338 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1339 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1340 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1341 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1342 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1343 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1345 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1346 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1349 9e39ca77 2022-07-21 thomas return err; /* Nothing else to do. */
1352 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1353 f4994adc 2020-10-20 stsp char *parent;
1354 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1356 9e39ca77 2022-07-21 thomas return err;
1357 59530ea7 2023-07-17 thomas err = got_path_mkdir(parent);
1358 f4994adc 2020-10-20 stsp free(parent);
1360 9e39ca77 2022-07-21 thomas return err;
1362 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1363 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1365 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1366 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1367 59530ea7 2023-07-17 thomas if (progress_cb) {
1368 59530ea7 2023-07-17 thomas err = (*progress_cb)(progress_arg,
1369 59530ea7 2023-07-17 thomas reverting_versioned_file ?
1370 59530ea7 2023-07-17 thomas GOT_STATUS_REVERT : GOT_STATUS_ADD,
1373 9e39ca77 2022-07-21 thomas return err;
1377 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1378 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1379 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1380 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1381 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1382 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1383 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1384 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1385 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1386 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1387 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1388 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1389 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1391 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1392 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1394 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1395 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1396 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1397 8ba819a3 2020-07-23 stsp return err;
1400 8ba819a3 2020-07-23 stsp static const struct got_error *
1401 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1402 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1403 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1404 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1405 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1406 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1408 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1409 507dc3bb 2018-12-29 stsp int fd = -1;
1410 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1411 507dc3bb 2018-12-29 stsp int update = 0;
1412 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1413 a2c162eb 2022-10-30 thomas mode_t mode;
1415 a2c162eb 2022-10-30 thomas mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1416 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1417 a2c162eb 2022-10-30 thomas O_CLOEXEC, mode);
1418 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1419 9f323212 2023-03-10 thomas if (errno == ENOENT || errno == ENOTDIR) {
1420 f5375317 2020-10-20 stsp char *parent;
1421 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1423 f5375317 2020-10-20 stsp return err;
1424 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1425 9f323212 2023-03-10 thomas if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1426 9f323212 2023-03-10 thomas err = got_error_path(path, err->code);
1427 f5375317 2020-10-20 stsp free(parent);
1429 21908da4 2019-01-13 stsp return err;
1430 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1431 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1433 21908da4 2019-01-13 stsp if (fd == -1)
1434 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1435 230a42bd 2019-05-11 jcs ondisk_path);
1436 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1437 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1438 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1439 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1440 3b9f0f87 2020-07-23 stsp goto done;
1442 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1443 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1444 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1445 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1446 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1447 507dc3bb 2018-12-29 stsp goto done;
1449 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1450 fc2a50f2 2022-11-01 thomas ondisk_path, "");
1452 507dc3bb 2018-12-29 stsp goto done;
1453 507dc3bb 2018-12-29 stsp update = 1;
1455 a2c162eb 2022-10-30 thomas if (fchmod(fd, apply_umask(mode)) == -1) {
1456 a2c162eb 2022-10-30 thomas err = got_error_from_errno2("fchmod",
1457 a2c162eb 2022-10-30 thomas tmppath);
1458 a2c162eb 2022-10-30 thomas goto done;
1462 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1465 bd6aa359 2020-07-23 stsp if (progress_cb) {
1466 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1467 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1469 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1470 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1473 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1474 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1476 bd6aa359 2020-07-23 stsp goto done;
1479 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1481 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1482 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1485 9d31a1d8 2018-03-11 stsp if (len > 0) {
1486 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1487 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1488 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1489 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1490 b87c6f83 2018-12-24 stsp goto done;
1491 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1492 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1493 b87c6f83 2018-12-24 stsp goto done;
1495 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1497 9d31a1d8 2018-03-11 stsp } while (len != 0);
1499 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1500 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1501 816dc654 2019-02-16 stsp goto done;
1504 507dc3bb 2018-12-29 stsp if (update) {
1505 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1506 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1507 f6d8c0ac 2020-11-09 stsp goto done;
1509 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1510 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1511 230a42bd 2019-05-11 jcs ondisk_path);
1512 68ed9ba5 2019-02-10 stsp goto done;
1514 63df146d 2020-11-09 stsp free(tmppath);
1515 63df146d 2020-11-09 stsp tmppath = NULL;
1519 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1520 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1521 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1522 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1523 507dc3bb 2018-12-29 stsp free(tmppath);
1524 6353ad76 2019-02-08 stsp return err;
1528 be94c032 2023-02-20 thomas * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1529 be94c032 2023-02-20 thomas * conflict marker is found in newly added lines only.
1531 6353ad76 2019-02-08 stsp static const struct got_error *
1532 be94c032 2023-02-20 thomas get_modified_file_content_status(unsigned char *status,
1533 be94c032 2023-02-20 thomas struct got_blob_object *blob, const char *path, struct stat *sb,
1534 be94c032 2023-02-20 thomas FILE *ondisk_file)
1536 8de9d8ad 2023-02-20 thomas const struct got_error *err, *free_err;
1537 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1538 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1539 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1540 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1542 8de9d8ad 2023-02-20 thomas FILE *f1 = NULL;
1543 8de9d8ad 2023-02-20 thomas struct got_diffreg_result *diffreg_result = NULL;
1544 8de9d8ad 2023-02-20 thomas struct diff_result *r;
1545 8de9d8ad 2023-02-20 thomas int nchunks_parsed, n, i = 0, ln = 0;
1546 9bdd68dd 2020-01-02 naddy char *line = NULL;
1547 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1548 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1550 8de9d8ad 2023-02-20 thomas if (*status != GOT_STATUS_MODIFY)
1551 8de9d8ad 2023-02-20 thomas return NULL;
1553 8de9d8ad 2023-02-20 thomas f1 = got_opentemp();
1554 8de9d8ad 2023-02-20 thomas if (f1 == NULL)
1555 8de9d8ad 2023-02-20 thomas return got_error_from_errno("got_opentemp");
1557 8de9d8ad 2023-02-20 thomas if (blob) {
1558 8de9d8ad 2023-02-20 thomas got_object_blob_rewind(blob);
1559 8de9d8ad 2023-02-20 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1560 be94c032 2023-02-20 thomas if (err)
1561 be94c032 2023-02-20 thomas goto done;
1564 8de9d8ad 2023-02-20 thomas err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1565 8de9d8ad 2023-02-20 thomas 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1566 8de9d8ad 2023-02-20 thomas if (err)
1567 8de9d8ad 2023-02-20 thomas goto done;
1569 8de9d8ad 2023-02-20 thomas r = diffreg_result->result;
1571 8de9d8ad 2023-02-20 thomas for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1572 8de9d8ad 2023-02-20 thomas struct diff_chunk *c;
1573 8de9d8ad 2023-02-20 thomas struct diff_chunk_context cc = {};
1574 450d9f6b 2023-02-20 thomas off_t pos;
1577 8de9d8ad 2023-02-20 thomas * We can optimise a little by advancing straight
1578 8de9d8ad 2023-02-20 thomas * to the next chunk if this one has no added lines.
1580 8de9d8ad 2023-02-20 thomas c = diff_chunk_get(r, n);
1582 47c7ee21 2023-02-20 thomas if (diff_chunk_type(c) != CHUNK_PLUS) {
1583 8de9d8ad 2023-02-20 thomas nchunks_parsed = 1;
1584 450d9f6b 2023-02-20 thomas continue; /* removed or unchanged lines */
1587 450d9f6b 2023-02-20 thomas pos = diff_chunk_get_right_start_pos(c);
1588 450d9f6b 2023-02-20 thomas if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1589 450d9f6b 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1590 450d9f6b 2023-02-20 thomas goto done;
1593 450d9f6b 2023-02-20 thomas diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1594 450d9f6b 2023-02-20 thomas ln = cc.right.start;
1596 8de9d8ad 2023-02-20 thomas while (ln < cc.right.end) {
1597 8de9d8ad 2023-02-20 thomas linelen = getline(&line, &linesize, ondisk_file);
1598 8de9d8ad 2023-02-20 thomas if (linelen == -1) {
1599 8de9d8ad 2023-02-20 thomas if (feof(ondisk_file))
1601 8de9d8ad 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1605 8de9d8ad 2023-02-20 thomas if (line && strncmp(line, markers[i],
1606 8de9d8ad 2023-02-20 thomas strlen(markers[i])) == 0) {
1607 8de9d8ad 2023-02-20 thomas if (strcmp(markers[i],
1608 8de9d8ad 2023-02-20 thomas GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1609 8de9d8ad 2023-02-20 thomas *status = GOT_STATUS_CONFLICT;
1610 8de9d8ad 2023-02-20 thomas goto done;
1619 9bdd68dd 2020-01-02 naddy free(line);
1620 be94c032 2023-02-20 thomas if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1621 be94c032 2023-02-20 thomas err = got_error_from_errno("fclose");
1622 8de9d8ad 2023-02-20 thomas free_err = got_diffreg_result_free(diffreg_result);
1623 8de9d8ad 2023-02-20 thomas if (err == NULL)
1624 8de9d8ad 2023-02-20 thomas err = free_err;
1626 7154f6ce 2019-03-27 stsp return err;
1629 e2b1e152 2019-07-13 stsp static int
1630 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1632 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1633 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1636 1ebedb77 2019-10-19 stsp static int
1637 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1639 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1640 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1641 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1642 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1643 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1644 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1647 c363b2c1 2019-08-03 stsp static unsigned char
1648 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1650 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1651 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1652 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1653 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1654 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1655 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1656 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1658 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1662 a919d5c4 2020-07-23 stsp static const struct got_error *
1663 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1664 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1665 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1667 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1668 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1669 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1670 a919d5c4 2020-07-23 stsp ssize_t elen;
1671 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1672 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1673 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1675 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1677 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1679 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1681 a919d5c4 2020-07-23 stsp return err;
1682 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1684 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1685 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1687 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1689 a919d5c4 2020-07-23 stsp if (len > 0) {
1690 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1691 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1692 a919d5c4 2020-07-23 stsp len - hdrlen);
1693 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1694 a919d5c4 2020-07-23 stsp hdrlen = 0;
1696 a919d5c4 2020-07-23 stsp } while (len != 0);
1697 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1699 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1700 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1701 a919d5c4 2020-07-23 stsp if (elen == -1)
1702 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1704 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1705 a919d5c4 2020-07-23 stsp if (elen == -1)
1706 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1709 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1710 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1712 a919d5c4 2020-07-23 stsp return NULL;
1715 7154f6ce 2019-03-27 stsp static const struct got_error *
1716 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1717 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1718 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1720 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1721 6353ad76 2019-02-08 stsp struct got_object_id id;
1722 6353ad76 2019-02-08 stsp size_t hdrlen;
1723 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1724 6353ad76 2019-02-08 stsp FILE *f = NULL;
1725 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1726 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1727 6353ad76 2019-02-08 stsp size_t flen, blen;
1728 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
1730 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
1731 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1732 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1735 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1736 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1737 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1739 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1740 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1741 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1742 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1743 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1745 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1746 882ef1b9 2019-12-13 stsp goto done;
1748 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1749 3d35a492 2019-12-13 stsp goto done;
1752 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1753 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1754 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1755 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1756 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1757 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1758 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1759 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1760 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1761 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1762 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1764 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1765 3d35a492 2019-12-13 stsp goto done;
1767 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1768 1338848f 2019-12-13 stsp goto done;
1772 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1773 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1774 1338848f 2019-12-13 stsp goto done;
1777 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1778 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1779 1338848f 2019-12-13 stsp goto done;
1780 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1781 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1782 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1783 1338848f 2019-12-13 stsp goto done;
1786 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1787 f179e66d 2020-07-23 stsp goto done;
1789 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1790 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1791 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1792 1338848f 2019-12-13 stsp goto done;
1795 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1796 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1797 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
1799 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
1801 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1802 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1803 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1804 f4ae6ddb 2022-07-01 thomas goto done;
1806 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1808 1338848f 2019-12-13 stsp goto done;
1810 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1811 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1812 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1813 a919d5c4 2020-07-23 stsp goto done;
1816 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1817 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1818 ab0d4361 2019-12-13 stsp if (fd == -1) {
1819 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1820 ab0d4361 2019-12-13 stsp goto done;
1824 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1825 6353ad76 2019-02-08 stsp if (f == NULL) {
1826 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1827 6353ad76 2019-02-08 stsp goto done;
1830 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1832 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1833 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1835 7154f6ce 2019-03-27 stsp goto done;
1836 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1837 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1838 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1839 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1840 7154f6ce 2019-03-27 stsp goto done;
1842 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1843 6353ad76 2019-02-08 stsp if (flen != 0)
1844 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1846 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1847 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1848 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1850 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1851 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1852 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1853 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1857 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1860 6353ad76 2019-02-08 stsp hdrlen = 0;
1863 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1864 7154f6ce 2019-03-27 stsp rewind(f);
1865 be94c032 2023-02-20 thomas err = get_modified_file_content_status(status, blob, ie->path,
1867 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1868 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1870 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1871 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1873 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1874 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1875 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1876 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1877 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1878 9d31a1d8 2018-03-11 stsp return err;
1882 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1883 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1885 e2b1e152 2019-07-13 stsp static const struct got_error *
1886 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1887 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1889 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1890 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1891 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1893 e2b1e152 2019-07-13 stsp return NULL;
1896 9f323212 2023-03-10 thomas static const struct got_error *remove_ondisk_file(const char *, const char *);
1898 9d31a1d8 2018-03-11 stsp static const struct got_error *
1899 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1900 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1901 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1902 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1903 0584f854 2019-04-06 stsp void *progress_arg)
1905 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1906 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1907 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1908 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1909 b8f41171 2019-02-10 stsp struct stat sb;
1910 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1912 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1913 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1916 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1917 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1918 a76c42e6 2019-08-03 stsp goto done;
1920 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1923 abb4604f 2019-07-27 stsp goto done;
1924 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1925 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1927 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1928 9f323212 2023-03-10 thomas if (errno != ENOENT && errno != ENOTDIR) {
1929 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1930 e6f4ba31 2021-09-24 thomas ondisk_path);
1931 e6f4ba31 2021-09-24 thomas goto done;
1933 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1934 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1935 e6f4ba31 2021-09-24 thomas } else {
1936 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1937 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1939 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1943 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1945 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1946 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1947 b8f41171 2019-02-10 stsp goto done;
1949 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1951 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1952 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1954 5036ab18 2020-04-18 stsp goto done;
1957 9f323212 2023-03-10 thomas if (S_ISDIR(te->mode)) { /* file changing into a directory */
1958 9f323212 2023-03-10 thomas if (status == GOT_STATUS_UNVERSIONED) {
1959 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, status, path);
1960 9f323212 2023-03-10 thomas } else if (status != GOT_STATUS_NO_CHANGE &&
1961 9f323212 2023-03-10 thomas status != GOT_STATUS_DELETE &&
1962 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1963 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1964 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg,
1965 9f323212 2023-03-10 thomas GOT_STATUS_CANNOT_DELETE, path);
1966 9f323212 2023-03-10 thomas } else if (ie) {
1967 9f323212 2023-03-10 thomas if (status != GOT_STATUS_DELETE &&
1968 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1969 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1970 9f323212 2023-03-10 thomas err = remove_ondisk_file(worktree->root_path,
1971 9f323212 2023-03-10 thomas ie->path);
1972 9f323212 2023-03-10 thomas if (err && !(err->code == GOT_ERR_ERRNO &&
1973 9f323212 2023-03-10 thomas errno == ENOENT))
1974 9f323212 2023-03-10 thomas goto done;
1976 9f323212 2023-03-10 thomas got_fileindex_entry_remove(fileindex, ie);
1977 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1978 9f323212 2023-03-10 thomas ie->path);
1980 9f323212 2023-03-10 thomas goto done; /* nothing else to do */
1983 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1984 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1985 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1987 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1988 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1989 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1990 c6e8a826 2021-04-05 stsp * updating contents of this file.
1992 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1993 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1994 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1995 c6e8a826 2021-04-05 stsp /* Same commit. */
1996 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1997 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1999 e2b1e152 2019-07-13 stsp goto done;
2000 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2002 6353ad76 2019-02-08 stsp goto done;
2004 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
2005 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
2006 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
2007 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
2008 b36a9774 2023-04-14 thomas if (got_fileindex_entry_has_commit(ie)) {
2009 b36a9774 2023-04-14 thomas /* Update the base commit ID of this file. */
2010 b36a9774 2023-04-14 thomas memcpy(ie->commit_sha1,
2011 b36a9774 2023-04-14 thomas worktree->base_commit_id->sha1,
2012 b36a9774 2023-04-14 thomas sizeof(ie->commit_sha1));
2014 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2015 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2017 0f58026f 2021-04-05 stsp goto done;
2018 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2020 b8f41171 2019-02-10 stsp goto done;
2024 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
2025 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
2026 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2027 f4ae6ddb 2022-07-01 thomas goto done;
2029 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2031 6353ad76 2019-02-08 stsp goto done;
2033 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2034 234035bc 2019-06-01 stsp int update_timestamps;
2035 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2036 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2037 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2038 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
2039 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
2040 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2041 f4ae6ddb 2022-07-01 thomas goto done;
2043 234035bc 2019-06-01 stsp struct got_object_id id2;
2044 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id2, ie);
2045 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2048 f69721c3 2019-10-21 stsp goto done;
2050 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2051 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2052 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2053 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2054 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2055 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2056 f69721c3 2019-10-21 stsp goto done;
2058 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2059 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2060 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2061 234035bc 2019-06-01 stsp goto done;
2064 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2065 36bf999c 2020-07-23 stsp char *link_target;
2066 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2068 36bf999c 2020-07-23 stsp goto done;
2069 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2070 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2071 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2072 36bf999c 2020-07-23 stsp free(link_target);
2074 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2075 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2076 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2077 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2079 f69721c3 2019-10-21 stsp free(label_orig);
2080 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2081 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2082 f4ae6ddb 2022-07-01 thomas goto done;
2084 234035bc 2019-06-01 stsp if (blob2)
2085 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2087 909d120e 2019-09-22 stsp goto done;
2089 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2090 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2091 234035bc 2019-06-01 stsp * unmodified files again.
2093 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2094 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2095 234035bc 2019-06-01 stsp update_timestamps);
2096 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2097 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2098 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2099 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2100 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2102 1ee397ad 2019-07-12 stsp goto done;
2103 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2104 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2106 13d9040b 2019-03-27 stsp goto done;
2108 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2109 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2110 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2111 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2112 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2113 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2114 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2116 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2117 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2118 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2119 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2120 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2123 13d9040b 2019-03-27 stsp goto done;
2126 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2127 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2128 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2130 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2131 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2132 054041d0 2020-06-24 stsp &blob->id);
2135 13d9040b 2019-03-27 stsp goto done;
2137 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2138 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2139 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2143 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2144 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2145 f4ae6ddb 2022-07-01 thomas goto done;
2147 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2149 6353ad76 2019-02-08 stsp free(ondisk_path);
2150 8da9e5f4 2019-01-12 stsp return err;
2153 90285c3b 2019-01-08 stsp static const struct got_error *
2154 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2156 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2157 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2159 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2160 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2162 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2163 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2164 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2166 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2167 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2169 1c4cdd89 2021-06-20 stsp goto done;
2170 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2171 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2172 fddefe3b 2020-10-20 stsp free(ondisk_path);
2173 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2174 1c4cdd89 2021-06-20 stsp parent = NULL;
2175 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2176 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2177 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2178 fddefe3b 2020-10-20 stsp ondisk_path);
2181 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2187 90285c3b 2019-01-08 stsp free(ondisk_path);
2188 1c4cdd89 2021-06-20 stsp free(parent);
2189 90285c3b 2019-01-08 stsp return err;
2192 708d8e67 2019-03-27 stsp static const struct got_error *
2193 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2194 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2195 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2197 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2198 708d8e67 2019-03-27 stsp unsigned char status;
2199 708d8e67 2019-03-27 stsp struct stat sb;
2200 708d8e67 2019-03-27 stsp char *ondisk_path;
2202 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2203 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2205 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2207 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2209 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2211 5a58a424 2020-06-23 stsp goto done;
2213 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2214 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2215 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2216 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2217 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2218 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2219 993e2a1b 2020-07-23 stsp goto done;
2221 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2222 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2223 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2224 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2226 993e2a1b 2020-07-23 stsp goto done;
2227 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2228 993e2a1b 2020-07-23 stsp ie->path);
2229 993e2a1b 2020-07-23 stsp goto done;
2232 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2233 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2234 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2236 5a58a424 2020-06-23 stsp goto done;
2238 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2239 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2241 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2242 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2244 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2246 5a58a424 2020-06-23 stsp goto done;
2247 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2248 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2250 5a58a424 2020-06-23 stsp goto done;
2252 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2255 5a58a424 2020-06-23 stsp free(ondisk_path);
2256 fc6346c4 2019-03-27 stsp return err;
2259 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2260 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2261 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2262 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2263 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2264 8da9e5f4 2019-01-12 stsp void *progress_arg;
2265 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2266 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2269 512f0d0e 2019-01-02 stsp static const struct got_error *
2270 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2271 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2273 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2275 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2276 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2278 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2279 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2282 8da9e5f4 2019-01-12 stsp static const struct got_error *
2283 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2285 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2287 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2288 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2290 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2291 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2294 9d31a1d8 2018-03-11 stsp static const struct got_error *
2295 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2297 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2298 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2299 8da9e5f4 2019-01-12 stsp char *path;
2301 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2302 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2304 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2305 59530ea7 2023-07-17 thomas return NULL;
2307 59530ea7 2023-07-17 thomas if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2308 63c5ca5d 2019-08-24 stsp return NULL;
2310 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2311 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2313 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2315 59530ea7 2023-07-17 thomas err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2316 59530ea7 2023-07-17 thomas a->repo, a->progress_cb, a->progress_arg);
2318 8da9e5f4 2019-01-12 stsp free(path);
2319 0cd1c46a 2019-03-11 stsp return err;
2322 b2118c49 2020-07-28 stsp const struct got_error *
2323 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2325 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2327 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2328 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2329 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2330 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2333 b2118c49 2020-07-28 stsp return NULL;
2336 818c7501 2019-07-11 stsp static const struct got_error *
2337 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2339 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2340 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2342 517bab73 2019-03-11 stsp *refname = NULL;
2344 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2346 b2118c49 2020-07-28 stsp return err;
2348 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2349 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2350 0647c563 2019-03-11 stsp *refname = NULL;
2352 517bab73 2019-03-11 stsp free(uuidstr);
2353 517bab73 2019-03-11 stsp return err;
2356 818c7501 2019-07-11 stsp const struct got_error *
2357 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2358 f6cd0243 2023-01-28 thomas const char *prefix)
2360 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2363 fcfaa6d5 2023-07-05 thomas static const struct got_error *
2364 fcfaa6d5 2023-07-05 thomas get_base_ref_name(char **refname, struct got_worktree *worktree)
2366 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2369 818c7501 2019-07-11 stsp static const struct got_error *
2370 818c7501 2019-07-11 stsp get_rebase_tmp_ref_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_TMP_REF_PREFIX);
2376 818c7501 2019-07-11 stsp static const struct got_error *
2377 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2379 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2382 818c7501 2019-07-11 stsp static const struct got_error *
2383 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2385 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2386 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2389 818c7501 2019-07-11 stsp static const struct got_error *
2390 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2392 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2393 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2396 0ebf8283 2019-07-24 stsp static const struct got_error *
2397 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2399 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2400 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2403 0ebf8283 2019-07-24 stsp static const struct got_error *
2404 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2406 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2407 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2410 0ebf8283 2019-07-24 stsp static const struct got_error *
2411 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2413 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2414 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2417 0ebf8283 2019-07-24 stsp static const struct got_error *
2418 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2420 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2421 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2424 0ebf8283 2019-07-24 stsp const struct got_error *
2425 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2426 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2428 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2429 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2430 0ebf8283 2019-07-24 stsp *path = NULL;
2431 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2433 0ebf8283 2019-07-24 stsp return NULL;
2436 10604dce 2021-09-24 thomas static const struct got_error *
2437 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2439 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2440 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2443 10604dce 2021-09-24 thomas static const struct got_error *
2444 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2446 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2447 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2451 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2452 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2454 517bab73 2019-03-11 stsp static const struct got_error *
2455 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2457 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2458 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2459 517bab73 2019-03-11 stsp char *refname;
2461 fcfaa6d5 2023-07-05 thomas err = get_base_ref_name(&refname, worktree);
2463 517bab73 2019-03-11 stsp return err;
2465 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2467 0cd1c46a 2019-03-11 stsp goto done;
2469 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2471 0cd1c46a 2019-03-11 stsp free(refname);
2473 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2474 3e3a69f1 2019-07-25 stsp return err;
2477 3e3a69f1 2019-07-25 stsp static const struct got_error *
2478 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2480 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2482 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2483 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2484 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2485 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2487 9d31a1d8 2018-03-11 stsp return err;
2491 ebf99748 2019-05-09 stsp static const struct got_error *
2492 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2493 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2495 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2496 ebf99748 2019-05-09 stsp FILE *index = NULL;
2498 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2499 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2500 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2501 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2503 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2505 ebf99748 2019-05-09 stsp goto done;
2507 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2508 ebf99748 2019-05-09 stsp if (index == NULL) {
2509 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2510 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2512 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2513 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2514 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2517 ebf99748 2019-05-09 stsp if (err) {
2518 ebf99748 2019-05-09 stsp free(*fileindex_path);
2519 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2520 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2521 ebf99748 2019-05-09 stsp *fileindex = NULL;
2523 ebf99748 2019-05-09 stsp return err;
2526 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2527 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2528 c932eeeb 2019-05-22 stsp const char *path;
2529 c932eeeb 2019-05-22 stsp size_t path_len;
2530 c932eeeb 2019-05-22 stsp const char *entry_name;
2531 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2532 a484d721 2019-06-10 stsp void *progress_arg;
2535 c932eeeb 2019-05-22 stsp static const struct got_error *
2536 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2538 1ee397ad 2019-07-12 stsp const struct got_error *err;
2539 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2541 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2542 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2543 c932eeeb 2019-05-22 stsp return NULL;
2544 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2545 102ff934 2019-06-11 stsp return NULL;
2547 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2548 a769b60b 2021-06-27 stsp return NULL;
2550 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2551 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2552 c932eeeb 2019-05-22 stsp return NULL;
2554 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2555 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2556 edd02c5e 2019-07-11 stsp ie->path);
2558 1ee397ad 2019-07-12 stsp return err;
2560 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2561 c932eeeb 2019-05-22 stsp return NULL;
2564 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2565 a615e0e7 2020-12-16 stsp static const struct got_error *
2566 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2567 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2568 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2570 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2572 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2573 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2574 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2575 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2576 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2577 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2579 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2580 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2583 9c6338c4 2019-06-02 stsp static const struct got_error *
2584 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2586 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2587 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2588 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2589 867630bb 2020-01-17 stsp struct timespec timeout;
2591 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2592 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2594 9c6338c4 2019-06-02 stsp goto done;
2596 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2598 9c6338c4 2019-06-02 stsp goto done;
2600 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2601 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2602 9c6338c4 2019-06-02 stsp fileindex_path);
2603 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2607 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2608 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2609 867630bb 2020-01-17 stsp * was recorded in the file index.
2611 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2612 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2613 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2615 9c6338c4 2019-06-02 stsp if (new_index)
2616 9c6338c4 2019-06-02 stsp fclose(new_index);
2617 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2618 9c6338c4 2019-06-02 stsp return err;
2621 6ced4176 2019-07-12 stsp static const struct got_error *
2622 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2623 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2624 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2625 945f9229 2022-04-16 thomas struct got_repository *repo)
2627 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2628 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2629 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2630 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2632 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2633 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2634 6ced4176 2019-07-12 stsp *tree_id = NULL;
2636 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2637 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2638 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2639 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2640 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2641 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2642 6ced4176 2019-07-12 stsp goto done;
2644 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2645 945f9229 2022-04-16 thomas worktree->path_prefix);
2647 6ced4176 2019-07-12 stsp goto done;
2648 6ced4176 2019-07-12 stsp return NULL;
2651 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2653 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2654 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2655 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2656 6ced4176 2019-07-12 stsp goto done;
2659 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2661 6ced4176 2019-07-12 stsp goto done;
2663 6ced4176 2019-07-12 stsp free(in_repo_path);
2664 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2666 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2668 6ced4176 2019-07-12 stsp goto done;
2670 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2671 6ced4176 2019-07-12 stsp /* Check out a single file. */
2672 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2673 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2674 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2675 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2676 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2677 6ced4176 2019-07-12 stsp goto done;
2679 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2680 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2681 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2682 6ced4176 2019-07-12 stsp goto done;
2685 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2686 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2688 6ced4176 2019-07-12 stsp return err;
2689 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2690 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2691 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2692 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2693 6ced4176 2019-07-12 stsp goto done;
2696 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2697 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2699 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2700 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2701 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2702 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2703 6ced4176 2019-07-12 stsp goto done;
2705 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2706 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2707 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2708 6ced4176 2019-07-12 stsp goto done;
2713 6ced4176 2019-07-12 stsp free(in_repo_path);
2714 6ced4176 2019-07-12 stsp if (err) {
2715 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2716 6ced4176 2019-07-12 stsp free(*tree_relpath);
2717 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2718 6ced4176 2019-07-12 stsp free(*tree_id);
2719 6ced4176 2019-07-12 stsp *tree_id = NULL;
2721 07ed472d 2019-07-12 stsp return err;
2724 07ed472d 2019-07-12 stsp static const struct got_error *
2725 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2726 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2727 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2728 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2730 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2731 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2732 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2733 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2734 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2736 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2737 7f47418f 2019-12-20 stsp if (err) {
2738 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2739 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2740 7f47418f 2019-12-20 stsp goto done;
2741 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2742 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2744 7f47418f 2019-12-20 stsp return err;
2747 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2748 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2750 07ed472d 2019-07-12 stsp goto done;
2752 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2754 07ed472d 2019-07-12 stsp goto done;
2756 07ed472d 2019-07-12 stsp if (entry_name &&
2757 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2758 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2759 07ed472d 2019-07-12 stsp goto done;
2762 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2763 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2764 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2765 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2766 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2767 07ed472d 2019-07-12 stsp arg.repo = repo;
2768 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2769 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2770 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2771 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2772 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2773 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2776 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2777 07ed472d 2019-07-12 stsp if (commit)
2778 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2779 6ced4176 2019-07-12 stsp return err;
2782 9d31a1d8 2018-03-11 stsp const struct got_error *
2783 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2784 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2785 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2786 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2788 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2789 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2790 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2791 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2792 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2793 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2794 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2795 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2796 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2797 f2ea84fa 2019-07-27 stsp int entry_type;
2798 f2ea84fa 2019-07-27 stsp char *relpath;
2799 f2ea84fa 2019-07-27 stsp char *entry_name;
2800 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2801 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2803 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2805 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2807 9d31a1d8 2018-03-11 stsp return err;
2809 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2810 945f9229 2022-04-16 thomas worktree->base_commit_id);
2811 945f9229 2022-04-16 thomas if (err)
2812 945f9229 2022-04-16 thomas goto done;
2814 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2815 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2816 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2817 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2818 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2819 f2ea84fa 2019-07-27 stsp goto done;
2822 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2823 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2824 945f9229 2022-04-16 thomas worktree, repo);
2825 f2ea84fa 2019-07-27 stsp if (err) {
2826 f2ea84fa 2019-07-27 stsp free(tpd);
2827 6ced4176 2019-07-12 stsp goto done;
2830 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2831 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2832 f2ea84fa 2019-07-27 stsp if (err) {
2833 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2834 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2835 f2ea84fa 2019-07-27 stsp free(tpd);
2836 f2ea84fa 2019-07-27 stsp goto done;
2839 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2841 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2845 51514078 2018-12-25 stsp * Read the file index.
2846 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2847 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2849 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2851 c4cdcb68 2019-04-03 stsp goto done;
2853 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2854 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2855 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2857 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2858 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2859 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2863 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2864 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2865 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2866 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2867 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2868 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2869 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2870 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2874 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2876 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2877 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2878 af12c6b9 2019-06-04 stsp err = sync_err;
2880 9c6338c4 2019-06-02 stsp free(fileindex_path);
2882 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2883 9d31a1d8 2018-03-11 stsp if (commit)
2884 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2885 5ade8233 2019-07-12 stsp if (fileindex)
2886 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2887 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2888 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2889 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2890 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2891 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2892 f2ea84fa 2019-07-27 stsp free(tpd);
2894 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2895 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2896 9d31a1d8 2018-03-11 stsp err = unlockerr;
2897 f8d1f275 2019-02-04 stsp return err;
2900 0529f8df 2023-03-10 thomas static const struct got_error *
2901 0529f8df 2023-03-10 thomas add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2902 0529f8df 2023-03-10 thomas struct got_fileindex_entry *ie, const char *ondisk_path,
2903 0529f8df 2023-03-10 thomas const char *path2, struct got_blob_object *blob2, mode_t mode2,
2904 0529f8df 2023-03-10 thomas int restoring_missing_file, int reverting_versioned_file,
2905 0529f8df 2023-03-10 thomas int path_is_unversioned, int allow_bad_symlinks,
2906 0529f8df 2023-03-10 thomas struct got_repository *repo,
2907 0529f8df 2023-03-10 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
2909 0529f8df 2023-03-10 thomas const struct got_error *err = NULL;
2910 0529f8df 2023-03-10 thomas int is_bad_symlink = 0;
2912 0529f8df 2023-03-10 thomas if (S_ISLNK(mode2)) {
2913 0529f8df 2023-03-10 thomas err = install_symlink(&is_bad_symlink,
2914 0529f8df 2023-03-10 thomas worktree, ondisk_path, path2, blob2,
2915 0529f8df 2023-03-10 thomas restoring_missing_file,
2916 0529f8df 2023-03-10 thomas reverting_versioned_file,
2917 0529f8df 2023-03-10 thomas path_is_unversioned, allow_bad_symlinks,
2918 0529f8df 2023-03-10 thomas repo, progress_cb, progress_arg);
2919 0529f8df 2023-03-10 thomas } else {
2920 0529f8df 2023-03-10 thomas err = install_blob(worktree, ondisk_path, path2,
2921 0529f8df 2023-03-10 thomas mode2, GOT_DEFAULT_FILE_MODE, blob2,
2922 0529f8df 2023-03-10 thomas restoring_missing_file, reverting_versioned_file, 0,
2923 0529f8df 2023-03-10 thomas path_is_unversioned, repo, progress_cb, progress_arg);
2925 0529f8df 2023-03-10 thomas if (err)
2926 0529f8df 2023-03-10 thomas return err;
2927 0529f8df 2023-03-10 thomas if (ie == NULL) {
2928 0529f8df 2023-03-10 thomas /* Adding an unversioned file. */
2929 0529f8df 2023-03-10 thomas err = got_fileindex_entry_alloc(&ie, path2);
2930 0529f8df 2023-03-10 thomas if (err)
2931 0529f8df 2023-03-10 thomas return err;
2932 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2933 0529f8df 2023-03-10 thomas worktree->root_fd, path2, NULL, NULL, 1);
2934 0529f8df 2023-03-10 thomas if (err) {
2935 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2936 0529f8df 2023-03-10 thomas return err;
2938 0529f8df 2023-03-10 thomas err = got_fileindex_entry_add(fileindex, ie);
2939 0529f8df 2023-03-10 thomas if (err) {
2940 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2941 0529f8df 2023-03-10 thomas return err;
2943 0529f8df 2023-03-10 thomas } else {
2944 0529f8df 2023-03-10 thomas /* Re-adding a locally deleted file. */
2945 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2946 0529f8df 2023-03-10 thomas worktree->root_fd, path2, ie->blob_sha1,
2947 0529f8df 2023-03-10 thomas worktree->base_commit_id->sha1, 0);
2948 0529f8df 2023-03-10 thomas if (err)
2949 0529f8df 2023-03-10 thomas return err;
2952 0529f8df 2023-03-10 thomas if (is_bad_symlink) {
2953 0529f8df 2023-03-10 thomas got_fileindex_entry_filetype_set(ie,
2954 0529f8df 2023-03-10 thomas GOT_FILEIDX_MODE_BAD_SYMLINK);
2957 0529f8df 2023-03-10 thomas return NULL;
2960 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2961 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2962 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2963 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2964 234035bc 2019-06-01 stsp void *progress_arg;
2965 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2966 234035bc 2019-06-01 stsp void *cancel_arg;
2967 f69721c3 2019-10-21 stsp const char *label_orig;
2968 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2969 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2972 234035bc 2019-06-01 stsp static const struct got_error *
2973 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2974 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2975 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2976 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2977 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2979 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2980 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2981 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2982 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2983 234035bc 2019-06-01 stsp struct stat sb;
2984 234035bc 2019-06-01 stsp unsigned char status;
2985 234035bc 2019-06-01 stsp int local_changes_subsumed;
2986 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2987 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2989 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2990 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2991 d6c87207 2019-08-02 stsp strlen(path2));
2992 1ee397ad 2019-07-12 stsp if (ie == NULL)
2993 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2994 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2996 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2997 234035bc 2019-06-01 stsp path2) == -1)
2998 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3000 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3003 234035bc 2019-06-01 stsp goto done;
3005 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3006 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3007 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
3008 234035bc 2019-06-01 stsp goto done;
3010 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3011 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3012 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3013 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3014 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3015 234035bc 2019-06-01 stsp goto done;
3018 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3019 36bf999c 2020-07-23 stsp char *link_target2;
3020 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3022 36bf999c 2020-07-23 stsp goto done;
3023 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3024 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3025 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3026 36bf999c 2020-07-23 stsp free(link_target2);
3030 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3031 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3032 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3033 1af628f4 2021-06-03 stsp goto done;
3035 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3036 1af628f4 2021-06-03 stsp f_orig, blob1);
3038 1af628f4 2021-06-03 stsp goto done;
3040 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3041 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3042 1af628f4 2021-06-03 stsp goto done;
3043 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3044 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3046 1af628f4 2021-06-03 stsp goto done;
3048 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
3049 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3050 1af628f4 2021-06-03 stsp if (fd == -1) {
3051 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3052 1af628f4 2021-06-03 stsp ondisk_path);
3053 1af628f4 2021-06-03 stsp goto done;
3055 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3056 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3057 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3058 1af628f4 2021-06-03 stsp ondisk_path);
3059 1af628f4 2021-06-03 stsp close(fd);
3060 1af628f4 2021-06-03 stsp goto done;
3062 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3064 1af628f4 2021-06-03 stsp goto done;
3065 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3066 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3067 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3068 1af628f4 2021-06-03 stsp goto done;
3070 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3071 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3072 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
3073 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3074 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3076 234035bc 2019-06-01 stsp } else if (blob1) {
3077 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3078 d6c87207 2019-08-02 stsp strlen(path1));
3079 1ee397ad 2019-07-12 stsp if (ie == NULL)
3080 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3081 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3083 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3084 2b92fad7 2019-06-02 stsp path1) == -1)
3085 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3087 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3090 2b92fad7 2019-06-02 stsp goto done;
3092 2b92fad7 2019-06-02 stsp switch (status) {
3093 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3094 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3095 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3097 1ee397ad 2019-07-12 stsp goto done;
3098 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3100 2b92fad7 2019-06-02 stsp goto done;
3102 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3104 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3105 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3106 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3107 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3109 1ee397ad 2019-07-12 stsp goto done;
3111 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3113 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3114 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3115 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3116 e8f02263 2022-01-23 thomas off_t blob1_size;
3118 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3119 0a22ca1a 2020-09-23 stsp * exists in the repository.
3121 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
3122 e8f02263 2022-01-23 thomas &blob1_size, path1);
3124 0a22ca1a 2020-09-23 stsp goto done;
3125 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3126 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3127 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3129 0a22ca1a 2020-09-23 stsp goto done;
3130 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3133 0a22ca1a 2020-09-23 stsp goto done;
3135 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3138 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3139 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3141 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3142 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3145 0a22ca1a 2020-09-23 stsp goto done;
3148 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3149 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3150 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3151 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3153 1ee397ad 2019-07-12 stsp goto done;
3155 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3156 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3158 1ee397ad 2019-07-12 stsp goto done;
3163 234035bc 2019-06-01 stsp } else if (blob2) {
3164 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3165 234035bc 2019-06-01 stsp path2) == -1)
3166 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3167 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3168 d6c87207 2019-08-02 stsp strlen(path2));
3170 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3171 7f91a133 2019-12-13 stsp -1, NULL, repo);
3173 234035bc 2019-06-01 stsp goto done;
3174 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3175 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3176 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3177 0529f8df 2023-03-10 thomas status != GOT_STATUS_ADD &&
3178 0529f8df 2023-03-10 thomas status != GOT_STATUS_DELETE) {
3179 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3180 1ee397ad 2019-07-12 stsp status, path2);
3181 234035bc 2019-06-01 stsp goto done;
3183 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3184 36bf999c 2020-07-23 stsp char *link_target2;
3185 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3188 36bf999c 2020-07-23 stsp goto done;
3189 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3190 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3191 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3192 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3193 36bf999c 2020-07-23 stsp free(link_target2);
3194 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3195 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3196 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3197 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3198 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3199 526a746f 2020-07-23 stsp a->progress_arg);
3200 0529f8df 2023-03-10 thomas } else if (status != GOT_STATUS_DELETE) {
3201 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3202 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3205 dfe9fba0 2020-07-23 stsp goto done;
3206 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3207 0529f8df 2023-03-10 thomas /* Re-add file with content from new blob. */
3208 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, ie,
3209 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3210 0529f8df 2023-03-10 thomas 0, 0, 0, a->allow_bad_symlinks,
3211 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3213 234035bc 2019-06-01 stsp goto done;
3216 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, NULL,
3217 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3218 0529f8df 2023-03-10 thomas 0, 0, 1, a->allow_bad_symlinks,
3219 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3221 2b92fad7 2019-06-02 stsp goto done;
3225 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3226 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3227 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3228 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3229 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3230 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3231 1af628f4 2021-06-03 stsp free(id_str);
3232 54d5be07 2021-06-03 stsp free(label_deriv2);
3233 234035bc 2019-06-01 stsp free(ondisk_path);
3234 234035bc 2019-06-01 stsp return err;
3237 69de9dd4 2021-09-03 stsp static const struct got_error *
3238 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3240 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3242 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3243 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3244 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3245 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3246 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3248 69de9dd4 2021-09-03 stsp return NULL;
3251 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3252 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3253 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3254 234035bc 2019-06-01 stsp struct got_repository *repo;
3257 234035bc 2019-06-01 stsp static const struct got_error *
3258 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3259 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3260 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3261 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3262 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3264 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3265 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3266 234035bc 2019-06-01 stsp unsigned char status;
3267 234035bc 2019-06-01 stsp struct stat sb;
3268 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3269 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3270 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3271 234035bc 2019-06-01 stsp char *ondisk_path;
3273 69de9dd4 2021-09-03 stsp if (id == NULL)
3274 69de9dd4 2021-09-03 stsp return NULL;
3276 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3277 69de9dd4 2021-09-03 stsp if (ie == NULL)
3278 69de9dd4 2021-09-03 stsp return NULL;
3280 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3282 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3284 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3285 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3286 5546d466 2021-09-02 stsp free(ondisk_path);
3288 234035bc 2019-06-01 stsp return err;
3289 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3290 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3292 234035bc 2019-06-01 stsp return NULL;
3295 818c7501 2019-07-11 stsp static const struct got_error *
3296 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3297 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3298 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3299 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3300 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3302 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3303 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3304 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3305 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3306 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3307 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3308 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3309 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3310 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3312 03415a1a 2019-06-02 stsp if (commit_id1) {
3313 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3314 945f9229 2022-04-16 thomas if (err)
3315 945f9229 2022-04-16 thomas goto done;
3316 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3317 03415a1a 2019-06-02 stsp worktree->path_prefix);
3318 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3319 03415a1a 2019-06-02 stsp goto done;
3321 69d57f3d 2020-07-31 stsp if (tree_id1) {
3322 69d57f3d 2020-07-31 stsp char *id_str;
3324 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3326 03415a1a 2019-06-02 stsp goto done;
3328 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3330 f69721c3 2019-10-21 stsp goto done;
3332 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3333 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3334 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3335 f69721c3 2019-10-21 stsp free(id_str);
3336 f69721c3 2019-10-21 stsp goto done;
3338 f69721c3 2019-10-21 stsp free(id_str);
3340 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3341 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3342 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3343 a0f32f33 2022-06-13 thomas goto done;
3347 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3348 945f9229 2022-04-16 thomas if (err)
3349 945f9229 2022-04-16 thomas goto done;
3351 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3352 234035bc 2019-06-01 stsp worktree->path_prefix);
3354 234035bc 2019-06-01 stsp goto done;
3356 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3358 234035bc 2019-06-01 stsp goto done;
3360 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3361 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3362 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3363 19a6a6b5 2022-07-01 thomas goto done;
3366 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3367 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3368 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3369 a0f32f33 2022-06-13 thomas goto done;
3372 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3373 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3374 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3375 19a6a6b5 2022-07-01 thomas goto done;
3378 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3379 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3380 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3381 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3382 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3384 69de9dd4 2021-09-03 stsp goto done;
3386 234035bc 2019-06-01 stsp arg.worktree = worktree;
3387 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3388 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3389 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3390 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3391 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3392 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3393 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3394 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3395 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3396 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3397 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3398 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3399 af12c6b9 2019-06-04 stsp err = sync_err;
3401 945f9229 2022-04-16 thomas if (commit1)
3402 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3403 945f9229 2022-04-16 thomas if (commit2)
3404 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3405 234035bc 2019-06-01 stsp if (tree1)
3406 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3407 234035bc 2019-06-01 stsp if (tree2)
3408 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3409 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3410 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3411 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3412 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3413 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3414 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3415 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3416 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3417 f69721c3 2019-10-21 stsp free(label_orig);
3418 818c7501 2019-07-11 stsp return err;
3421 818c7501 2019-07-11 stsp const struct got_error *
3422 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3423 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3424 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3425 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3427 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3428 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3429 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3431 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3433 818c7501 2019-07-11 stsp return err;
3435 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3437 818c7501 2019-07-11 stsp goto done;
3439 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3440 69de9dd4 2021-09-03 stsp worktree);
3442 818c7501 2019-07-11 stsp goto done;
3444 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3445 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3446 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3448 818c7501 2019-07-11 stsp if (fileindex)
3449 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3450 818c7501 2019-07-11 stsp free(fileindex_path);
3451 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3452 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3453 234035bc 2019-06-01 stsp err = unlockerr;
3454 234035bc 2019-06-01 stsp return err;
3457 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3458 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3459 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3460 927df6b7 2019-02-10 stsp const char *status_path;
3461 927df6b7 2019-02-10 stsp size_t status_path_len;
3462 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3463 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3464 f8d1f275 2019-02-04 stsp void *status_arg;
3465 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3466 f8d1f275 2019-02-04 stsp void *cancel_arg;
3467 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3468 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3469 f2a9dc41 2019-12-13 tracey int report_unchanged;
3470 3143d852 2020-06-25 stsp int no_ignores;
3473 f8d1f275 2019-02-04 stsp static const struct got_error *
3474 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3475 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3476 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3477 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3479 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3480 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3481 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3482 927df6b7 2019-02-10 stsp struct stat sb;
3483 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3484 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3485 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3487 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3488 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3490 98eaaa12 2019-08-03 stsp return err;
3492 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3493 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3494 98eaaa12 2019-08-03 stsp return NULL;
3496 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3497 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3498 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3499 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3500 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3501 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3502 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3503 43010591 2023-02-17 thomas &staged_blob_id, ie);
3506 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3507 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3510 927df6b7 2019-02-10 stsp static const struct got_error *
3511 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3512 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3514 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3515 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3516 f8d1f275 2019-02-04 stsp char *abspath;
3518 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3519 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3521 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3522 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3523 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3524 927df6b7 2019-02-10 stsp return NULL;
3526 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3527 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3528 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3529 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3531 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3532 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3533 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3536 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3537 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3538 f8d1f275 2019-02-04 stsp free(abspath);
3539 9d31a1d8 2018-03-11 stsp return err;
3542 f8d1f275 2019-02-04 stsp static const struct got_error *
3543 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3545 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3546 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3547 2ec1f75b 2019-03-26 stsp unsigned char status;
3549 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3550 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3552 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3553 927df6b7 2019-02-10 stsp return NULL;
3555 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3556 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3557 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3558 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3560 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3561 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3562 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3565 ef20f542 2022-06-26 thomas static void
3566 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3568 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3570 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3571 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3573 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3575 21c2d8be 2023-01-10 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3578 6841da00 2019-08-08 stsp static const struct got_error *
3579 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3581 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3582 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3583 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3584 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3585 6841da00 2019-08-08 stsp size_t linesize = 0;
3586 6841da00 2019-08-08 stsp ssize_t linelen;
3588 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3589 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3590 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3591 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3593 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3594 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3595 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3597 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3598 bd8de430 2019-10-04 stsp if (line[0] == '#')
3601 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3602 bd8de430 2019-10-04 stsp if (line[0] == '!')
3605 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3606 6841da00 2019-08-08 stsp line) == -1) {
3607 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3608 6841da00 2019-08-08 stsp goto done;
3610 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3612 6841da00 2019-08-08 stsp goto done;
3614 6841da00 2019-08-08 stsp if (ferror(f)) {
3615 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3616 6841da00 2019-08-08 stsp goto done;
3619 6841da00 2019-08-08 stsp dirpath = strdup(path);
3620 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3621 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3622 6841da00 2019-08-08 stsp goto done;
3624 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3626 6841da00 2019-08-08 stsp free(line);
3627 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3628 6841da00 2019-08-08 stsp free(dirpath);
3629 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3631 6841da00 2019-08-08 stsp return err;
3634 1b5d300f 2023-02-20 thomas static int
3635 1b5d300f 2023-02-20 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
3636 1b5d300f 2023-02-20 thomas int flags)
3638 1b5d300f 2023-02-20 thomas char buf[PATH_MAX];
3641 1b5d300f 2023-02-20 thomas * Trailing slashes signify directories.
3642 1b5d300f 2023-02-20 thomas * Append a * to make such patterns conform to fnmatch rules.
3644 1b5d300f 2023-02-20 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3645 1b5d300f 2023-02-20 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3646 1b5d300f 2023-02-20 thomas return FNM_NOMATCH; /* XXX */
3648 1b5d300f 2023-02-20 thomas return fnmatch(buf, path, flags);
3651 1b5d300f 2023-02-20 thomas return fnmatch(pattern, path, flags);
3654 ef20f542 2022-06-26 thomas static int
3655 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3657 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3659 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3660 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3661 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3662 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3664 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3665 1b5d300f 2023-02-20 thomas const char *p;
3667 1b5d300f 2023-02-20 thomas if (pi->path_len < 3 ||
3668 1b5d300f 2023-02-20 thomas strncmp(pi->path, "**/", 3) != 0)
3671 bd8de430 2019-10-04 stsp while (*p) {
3672 1b5d300f 2023-02-20 thomas if (match_path(pi->path + 3,
3673 1b5d300f 2023-02-20 thomas pi->path_len - 3, p,
3674 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3675 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3676 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3678 bd8de430 2019-10-04 stsp while (*p == '/')
3688 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3689 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3690 6841da00 2019-08-08 stsp * ignores backwards.
3692 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3693 6841da00 2019-08-08 stsp while (pe) {
3694 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3695 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3696 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3697 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3698 1b5d300f 2023-02-20 thomas int flags = FNM_LEADING_DIR;
3699 1b5d300f 2023-02-20 thomas if (strstr(pi->path, "/**/") == NULL)
3700 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3701 1b5d300f 2023-02-20 thomas if (match_path(pi->path, pi->path_len,
3702 1b5d300f 2023-02-20 thomas path, flags))
3707 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3713 6841da00 2019-08-08 stsp static const struct got_error *
3714 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3715 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3717 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3718 6841da00 2019-08-08 stsp char *ignorespath;
3719 886cec17 2019-12-15 stsp int fd = -1;
3720 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3722 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3723 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3724 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3726 886cec17 2019-12-15 stsp if (dirfd != -1) {
3727 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3728 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3729 886cec17 2019-12-15 stsp if (fd == -1) {
3730 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3731 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3732 886cec17 2019-12-15 stsp ignorespath);
3734 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3735 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3736 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3737 886cec17 2019-12-15 stsp ignorespath);
3740 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3744 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3745 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3746 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3747 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3748 886cec17 2019-12-15 stsp ignorespath);
3750 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3753 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3754 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3755 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3756 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3757 6841da00 2019-08-08 stsp free(ignorespath);
3758 6841da00 2019-08-08 stsp return err;
3761 f8d1f275 2019-02-04 stsp static const struct got_error *
3762 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3763 6092c299 2021-10-04 thomas int dirfd)
3765 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3766 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3767 f8d1f275 2019-02-04 stsp char *path = NULL;
3769 6092c299 2021-10-04 thomas if (ignore != NULL)
3770 6092c299 2021-10-04 thomas *ignore = 0;
3772 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3773 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3775 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3776 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3777 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3779 f8d1f275 2019-02-04 stsp path = de->d_name;
3782 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3783 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3784 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3785 6092c299 2021-10-04 thomas *ignore = 1;
3786 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3787 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3788 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3789 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3790 f8d1f275 2019-02-04 stsp if (parent_path[0])
3791 f8d1f275 2019-02-04 stsp free(path);
3792 b72f483a 2019-02-05 stsp return err;
3795 347d1d3e 2019-07-12 stsp static const struct got_error *
3796 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3798 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3799 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3801 3143d852 2020-06-25 stsp if (a->no_ignores)
3802 3143d852 2020-06-25 stsp return NULL;
3804 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3805 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3807 3143d852 2020-06-25 stsp return err;
3809 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3810 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3812 3143d852 2020-06-25 stsp return err;
3815 3143d852 2020-06-25 stsp static const struct got_error *
3816 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3817 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3818 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3819 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3821 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3822 abb4604f 2019-07-27 stsp struct stat sb;
3824 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3826 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3827 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3829 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3830 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3831 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3832 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3833 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3836 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3837 fe1d8685 2022-02-12 thomas return NULL;
3839 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3840 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3841 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3843 abb4604f 2019-07-27 stsp return NULL;
3846 3143d852 2020-06-25 stsp static const struct got_error *
3847 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3848 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3850 3143d852 2020-06-25 stsp const struct got_error *err;
3851 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3853 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3854 3143d852 2020-06-25 stsp ".cvsignore");
3856 3143d852 2020-06-25 stsp return err;
3858 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3859 3143d852 2020-06-25 stsp ".gitignore");
3861 3143d852 2020-06-25 stsp return err;
3863 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3864 3143d852 2020-06-25 stsp if (err) {
3865 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3866 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3867 3143d852 2020-06-25 stsp return err;
3869 3143d852 2020-06-25 stsp for (;;) {
3870 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3871 3143d852 2020-06-25 stsp ".cvsignore");
3874 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3875 3143d852 2020-06-25 stsp ".gitignore");
3878 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3879 3143d852 2020-06-25 stsp if (err) {
3880 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3881 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3884 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3886 b737c85a 2020-06-26 stsp free(parent_path);
3887 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3888 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3891 b737c85a 2020-06-26 stsp free(parent_path);
3892 b737c85a 2020-06-26 stsp free(next_parent_path);
3893 3143d852 2020-06-25 stsp return err;
3896 f8836425 2023-04-14 thomas struct find_missing_children_args {
3897 f8836425 2023-04-14 thomas const char *parent_path;
3898 f8836425 2023-04-14 thomas size_t parent_len;
3899 f8836425 2023-04-14 thomas struct got_pathlist_head *children;
3900 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb;
3901 f8836425 2023-04-14 thomas void *cancel_arg;
3904 abb4604f 2019-07-27 stsp static const struct got_error *
3905 f8836425 2023-04-14 thomas find_missing_children(void *arg, struct got_fileindex_entry *ie)
3907 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
3908 f8836425 2023-04-14 thomas struct find_missing_children_args *a = arg;
3910 f8836425 2023-04-14 thomas if (a->cancel_cb) {
3911 f8836425 2023-04-14 thomas err = a->cancel_cb(a->cancel_arg);
3912 f8836425 2023-04-14 thomas if (err)
3913 f8836425 2023-04-14 thomas return err;
3916 f8836425 2023-04-14 thomas if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3917 f8836425 2023-04-14 thomas err = got_pathlist_append(a->children, ie->path, NULL);
3919 f8836425 2023-04-14 thomas return err;
3922 f8836425 2023-04-14 thomas static const struct got_error *
3923 f8836425 2023-04-14 thomas report_children(struct got_pathlist_head *children,
3924 f8836425 2023-04-14 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
3925 f8836425 2023-04-14 thomas struct got_repository *repo, int is_root_dir, int report_unchanged,
3926 f8836425 2023-04-14 thomas struct got_pathlist_head *ignores, int no_ignores,
3927 f8836425 2023-04-14 thomas got_worktree_status_cb status_cb, void *status_arg,
3928 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb, void *cancel_arg)
3930 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
3931 f8836425 2023-04-14 thomas struct got_pathlist_entry *pe;
3932 f8836425 2023-04-14 thomas char *ondisk_path = NULL;
3934 f8836425 2023-04-14 thomas TAILQ_FOREACH(pe, children, entry) {
3935 f8836425 2023-04-14 thomas if (cancel_cb) {
3936 f8836425 2023-04-14 thomas err = cancel_cb(cancel_arg);
3937 f8836425 2023-04-14 thomas if (err)
3941 f8836425 2023-04-14 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3942 f8836425 2023-04-14 thomas !is_root_dir ? "/" : "", pe->path) == -1) {
3943 f8836425 2023-04-14 thomas err = got_error_from_errno("asprintf");
3944 f8836425 2023-04-14 thomas ondisk_path = NULL;
3948 f8836425 2023-04-14 thomas err = report_single_file_status(pe->path, ondisk_path,
3949 f8836425 2023-04-14 thomas fileindex, status_cb, status_arg, repo, report_unchanged,
3950 f8836425 2023-04-14 thomas ignores, no_ignores);
3951 f8836425 2023-04-14 thomas if (err)
3954 f8836425 2023-04-14 thomas free(ondisk_path);
3955 f8836425 2023-04-14 thomas ondisk_path = NULL;
3958 f8836425 2023-04-14 thomas free(ondisk_path);
3959 f8836425 2023-04-14 thomas return err;
3962 f8836425 2023-04-14 thomas static const struct got_error *
3963 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3964 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3965 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3966 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3967 f2a9dc41 2019-12-13 tracey int report_unchanged)
3969 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3970 6fc93f37 2019-12-13 stsp int fd = -1;
3971 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3972 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3973 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3974 f8836425 2023-04-14 thomas struct got_pathlist_head ignores, missing_children;
3975 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3977 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3978 f8836425 2023-04-14 thomas TAILQ_INIT(&missing_children);
3980 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3981 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3982 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3984 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3985 a78810f8 2022-03-13 thomas if (ie) {
3986 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3987 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3988 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3989 a78810f8 2022-03-13 thomas goto done;
3990 f8836425 2023-04-14 thomas } else {
3991 f8836425 2023-04-14 thomas struct find_missing_children_args fmca;
3992 f8836425 2023-04-14 thomas fmca.parent_path = path;
3993 f8836425 2023-04-14 thomas fmca.parent_len = strlen(path);
3994 f8836425 2023-04-14 thomas fmca.children = &missing_children;
3995 f8836425 2023-04-14 thomas fmca.cancel_cb = cancel_cb;
3996 f8836425 2023-04-14 thomas fmca.cancel_arg = cancel_arg;
3997 f8836425 2023-04-14 thomas err = got_fileindex_for_each_entry_safe(fileindex,
3998 f8836425 2023-04-14 thomas find_missing_children, &fmca);
3999 f8836425 2023-04-14 thomas if (err)
4000 f8836425 2023-04-14 thomas goto done;
4003 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4004 6fc93f37 2019-12-13 stsp if (fd == -1) {
4005 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4006 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
4007 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
4009 ff56836b 2021-07-08 stsp if (!no_ignores) {
4010 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4011 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
4013 ff56836b 2021-07-08 stsp goto done;
4015 f8836425 2023-04-14 thomas if (TAILQ_EMPTY(&missing_children)) {
4016 f8836425 2023-04-14 thomas err = report_single_file_status(path,
4017 f8836425 2023-04-14 thomas ondisk_path, fileindex,
4018 f8836425 2023-04-14 thomas status_cb, status_arg, repo,
4019 f8836425 2023-04-14 thomas report_unchanged, &ignores, no_ignores);
4020 f8836425 2023-04-14 thomas if (err)
4021 f8836425 2023-04-14 thomas goto done;
4022 f8836425 2023-04-14 thomas } else {
4023 f8836425 2023-04-14 thomas err = report_children(&missing_children,
4024 f8836425 2023-04-14 thomas worktree, fileindex, repo,
4025 f8836425 2023-04-14 thomas (path[0] == '\0'), report_unchanged,
4026 f8836425 2023-04-14 thomas &ignores, no_ignores,
4027 f8836425 2023-04-14 thomas status_cb, status_arg,
4028 f8836425 2023-04-14 thomas cancel_cb, cancel_arg);
4029 6f97aa83 2023-04-14 thomas if (err)
4030 6f97aa83 2023-04-14 thomas goto done;
4034 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4035 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4036 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4037 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4038 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4039 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4040 abb4604f 2019-07-27 stsp arg.status_path = path;
4041 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4042 abb4604f 2019-07-27 stsp arg.repo = repo;
4043 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4044 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4045 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4046 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4047 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4048 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4049 022fae89 2019-12-06 tracey if (!no_ignores) {
4050 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4051 3143d852 2020-06-25 stsp worktree->root_path, path);
4053 3143d852 2020-06-25 stsp goto done;
4055 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4056 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4057 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4060 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4061 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4062 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4063 927df6b7 2019-02-10 stsp free(ondisk_path);
4064 347d1d3e 2019-07-12 stsp return err;
4067 347d1d3e 2019-07-12 stsp const struct got_error *
4068 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4069 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4070 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4071 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4073 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4074 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4075 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4076 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4078 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4080 347d1d3e 2019-07-12 stsp return err;
4082 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4083 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4084 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4085 f6343036 2021-06-22 stsp no_ignores, 0);
4089 f8d1f275 2019-02-04 stsp free(fileindex_path);
4090 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4091 f8d1f275 2019-02-04 stsp return err;
4094 6c7ab921 2019-03-18 stsp const struct got_error *
4095 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4096 6c7ab921 2019-03-18 stsp const char *arg)
4098 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4099 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4100 6c7ab921 2019-03-18 stsp size_t len;
4101 00bb5ea0 2020-07-23 stsp struct stat sb;
4102 01740607 2020-11-04 stsp char *abspath = NULL;
4103 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4105 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4107 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4108 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4109 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4111 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4112 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4113 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4114 00bb5ea0 2020-07-23 stsp goto done;
4116 727173c3 2020-11-06 stsp sb.st_mode = 0;
4118 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4120 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4121 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4122 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4123 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4125 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4126 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4127 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4128 00bb5ea0 2020-07-23 stsp goto done;
4132 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4133 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4135 d0710d08 2019-07-22 stsp goto done;
4136 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4137 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4138 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4139 00bb5ea0 2020-07-23 stsp goto done;
4142 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4143 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4144 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4145 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4146 00bb5ea0 2020-07-23 stsp goto done;
4148 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4149 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4150 01740607 2020-11-04 stsp goto done;
4152 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4153 01740607 2020-11-04 stsp sizeof(canonpath));
4155 00bb5ea0 2020-07-23 stsp goto done;
4156 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4157 01740607 2020-11-04 stsp if (resolved == NULL) {
4158 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4159 01740607 2020-11-04 stsp goto done;
4164 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4165 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4166 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4167 6c7ab921 2019-03-18 stsp goto done;
4170 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4171 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4172 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4174 f6d88e1a 2019-05-29 stsp goto done;
4176 f6d88e1a 2019-05-29 stsp path = strdup("");
4177 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4178 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4179 f6d88e1a 2019-05-29 stsp goto done;
4183 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4184 6c7ab921 2019-03-18 stsp len = strlen(path);
4185 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4186 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4190 01740607 2020-11-04 stsp free(abspath);
4191 6c7ab921 2019-03-18 stsp free(resolved);
4192 d0710d08 2019-07-22 stsp free(cwd);
4193 6c7ab921 2019-03-18 stsp if (err == NULL)
4194 6c7ab921 2019-03-18 stsp *wt_path = path;
4196 6c7ab921 2019-03-18 stsp free(path);
4197 d00136be 2019-03-26 stsp return err;
4200 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4201 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4202 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4203 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4204 4e68cba3 2019-11-23 stsp void *progress_arg;
4205 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4208 b21ebdb0 2023-06-22 thomas static int
4209 b21ebdb0 2023-06-22 thomas add_noop_status(unsigned char status)
4211 b21ebdb0 2023-06-22 thomas return (status == GOT_STATUS_ADD ||
4212 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODIFY ||
4213 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_CONFLICT ||
4214 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODE_CHANGE ||
4215 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_NO_CHANGE);
4218 4e68cba3 2019-11-23 stsp static const struct got_error *
4219 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4220 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4221 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4222 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4224 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4225 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4226 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4227 6d022e97 2019-08-04 stsp struct stat sb;
4228 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4230 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4231 dbb83fbd 2019-12-12 stsp relpath) == -1)
4232 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4234 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4236 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4237 12463d8b 2019-12-13 stsp de_name, a->repo);
4239 dbb83fbd 2019-12-12 stsp goto done;
4240 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4241 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE &&
4242 b21ebdb0 2023-06-22 thomas add_noop_status(status))
4243 dbb83fbd 2019-12-12 stsp goto done;
4244 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4246 dbb83fbd 2019-12-12 stsp goto done;
4249 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4250 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4251 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4253 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4254 dbb83fbd 2019-12-12 stsp goto done;
4257 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4259 4e68cba3 2019-11-23 stsp goto done;
4260 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4261 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4262 3969253a 2020-03-07 stsp if (err) {
4263 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4264 3969253a 2020-03-07 stsp goto done;
4266 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4267 07f5b47a 2019-06-02 stsp if (err) {
4268 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4269 4e68cba3 2019-11-23 stsp goto done;
4272 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4274 dbb83fbd 2019-12-12 stsp return err;
4275 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4276 dbb83fbd 2019-12-12 stsp return NULL;
4277 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4280 d00136be 2019-03-26 stsp const struct got_error *
4281 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4282 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4283 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4284 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4286 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4287 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4288 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4289 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4290 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4292 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4294 d00136be 2019-03-26 stsp return err;
4296 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4298 d00136be 2019-03-26 stsp goto done;
4300 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4301 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4302 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4303 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4304 4e68cba3 2019-11-23 stsp saa.repo = repo;
4306 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4307 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4308 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4312 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4313 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4314 af12c6b9 2019-06-04 stsp err = sync_err;
4316 fb399478 2019-07-12 stsp free(fileindex_path);
4317 d00136be 2019-03-26 stsp if (fileindex)
4318 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4319 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4320 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4321 d00136be 2019-03-26 stsp err = unlockerr;
4322 6c7ab921 2019-03-18 stsp return err;
4325 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4326 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4327 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4328 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4329 f2a9dc41 2019-12-13 tracey void *progress_arg;
4330 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4331 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4332 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4333 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4334 8ec7352a 2023-06-01 thomas const char *status_path;
4335 8ec7352a 2023-06-01 thomas size_t status_path_len;
4336 766841c2 2020-08-13 stsp const char *status_codes;
4339 f2a9dc41 2019-12-13 tracey static const struct got_error *
4340 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4341 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4342 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4343 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4345 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4346 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4347 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4348 17ed4618 2019-06-02 stsp struct stat sb;
4349 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4351 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4352 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4353 d64cc78a 2022-01-25 thomas return NULL;
4354 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4357 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4358 17ed4618 2019-06-02 stsp if (ie == NULL)
4359 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4361 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4362 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4363 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4364 9acbc4fa 2019-08-03 stsp return NULL;
4365 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4368 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4369 f2a9dc41 2019-12-13 tracey relpath) == -1)
4370 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4372 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4375 f2a9dc41 2019-12-13 tracey goto done;
4377 766841c2 2020-08-13 stsp if (a->status_codes) {
4378 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4380 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4381 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4384 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4385 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4386 766841c2 2020-08-13 stsp free(ondisk_path);
4387 766841c2 2020-08-13 stsp return NULL;
4389 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4390 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4391 766841c2 2020-08-13 stsp static char msg[64];
4392 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4393 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4394 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4395 766841c2 2020-08-13 stsp goto done;
4399 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4400 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4401 f2a9dc41 2019-12-13 tracey goto done;
4402 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4403 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4404 f2a9dc41 2019-12-13 tracey goto done;
4406 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4407 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4408 d64cc78a 2022-01-25 thomas goto done;
4410 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4411 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4412 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4413 f2a9dc41 2019-12-13 tracey goto done;
4417 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4418 20a2ad1c 2020-10-20 stsp size_t root_len;
4420 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4421 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4422 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4423 12463d8b 2019-12-13 stsp ondisk_path);
4424 12463d8b 2019-12-13 stsp goto done;
4426 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4427 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4428 12463d8b 2019-12-13 stsp goto done;
4431 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4433 20a2ad1c 2020-10-20 stsp char *parent;
4435 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4437 2513f20a 2020-10-20 stsp goto done;
4438 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4439 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4440 8ec7352a 2023-06-01 thomas if (got_path_cmp(ondisk_path, a->status_path,
4441 8ec7352a 2023-06-01 thomas strlen(ondisk_path), a->status_path_len) != 0 &&
4442 8ec7352a 2023-06-01 thomas !got_path_is_child(ondisk_path, a->status_path,
4443 8ec7352a 2023-06-01 thomas a->status_path_len))
4445 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4446 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4447 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4448 20a2ad1c 2020-10-20 stsp ondisk_path);
4451 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4452 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4455 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4457 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4458 f2a9dc41 2019-12-13 tracey if (err)
4459 f2a9dc41 2019-12-13 tracey return err;
4460 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4461 f2a9dc41 2019-12-13 tracey return NULL;
4462 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4463 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4466 2ec1f75b 2019-03-26 stsp const struct got_error *
4467 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4468 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4469 766841c2 2020-08-13 stsp const char *status_codes,
4470 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4471 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4473 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4474 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4475 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4476 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4477 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4479 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4481 2ec1f75b 2019-03-26 stsp return err;
4483 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4485 2ec1f75b 2019-03-26 stsp goto done;
4487 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4488 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4489 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4490 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4491 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4492 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4493 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4494 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4495 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4497 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4498 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
4500 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s%s%s",
4501 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree),
4502 8ec7352a 2023-06-01 thomas pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4503 8ec7352a 2023-06-01 thomas err = got_error_from_errno("asprintf");
4504 8ec7352a 2023-06-01 thomas goto done;
4506 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
4507 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
4508 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4509 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4510 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
4514 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4515 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4516 af12c6b9 2019-06-04 stsp err = sync_err;
4518 fb399478 2019-07-12 stsp free(fileindex_path);
4519 2ec1f75b 2019-03-26 stsp if (fileindex)
4520 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4521 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4522 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4523 2ec1f75b 2019-03-26 stsp err = unlockerr;
4524 33aa809d 2019-08-08 stsp return err;
4527 33aa809d 2019-08-08 stsp static const struct got_error *
4528 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4530 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4531 33aa809d 2019-08-08 stsp char *line = NULL;
4532 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4533 33aa809d 2019-08-08 stsp ssize_t linelen;
4535 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4536 33aa809d 2019-08-08 stsp if (linelen == -1) {
4537 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4538 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4539 33aa809d 2019-08-08 stsp goto done;
4541 33aa809d 2019-08-08 stsp return NULL;
4543 33aa809d 2019-08-08 stsp if (outfile) {
4544 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4545 33aa809d 2019-08-08 stsp if (n != linelen) {
4546 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4547 33aa809d 2019-08-08 stsp goto done;
4550 33aa809d 2019-08-08 stsp if (rejectfile) {
4551 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4552 33aa809d 2019-08-08 stsp if (n != linelen)
4553 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4556 33aa809d 2019-08-08 stsp free(line);
4557 2ec1f75b 2019-03-26 stsp return err;
4560 33aa809d 2019-08-08 stsp static const struct got_error *
4561 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4563 33aa809d 2019-08-08 stsp char *line = NULL;
4564 33aa809d 2019-08-08 stsp size_t linesize = 0;
4565 33aa809d 2019-08-08 stsp ssize_t linelen;
4567 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4568 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4569 500467ff 2019-09-25 hiltjo if (ferror(f))
4570 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4571 500467ff 2019-09-25 hiltjo return NULL;
4573 33aa809d 2019-08-08 stsp free(line);
4574 33aa809d 2019-08-08 stsp return NULL;
4577 33aa809d 2019-08-08 stsp static const struct got_error *
4578 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4579 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4580 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4582 33aa809d 2019-08-08 stsp const struct got_error *err;
4584 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4585 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4586 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4588 33aa809d 2019-08-08 stsp return err;
4589 33aa809d 2019-08-08 stsp (*line_cur1)++;
4591 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4592 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4593 33aa809d 2019-08-08 stsp if (rejectfile)
4594 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4596 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4598 33aa809d 2019-08-08 stsp return err;
4599 33aa809d 2019-08-08 stsp (*line_cur2)++;
4601 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4602 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4603 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4605 33aa809d 2019-08-08 stsp return err;
4606 33aa809d 2019-08-08 stsp (*line_cur2)++;
4608 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4609 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4610 33aa809d 2019-08-08 stsp if (rejectfile)
4611 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4613 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4615 33aa809d 2019-08-08 stsp return err;
4616 33aa809d 2019-08-08 stsp (*line_cur1)++;
4619 f1e81a05 2019-08-10 stsp return NULL;
4622 f1e81a05 2019-08-10 stsp static const struct got_error *
4623 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4624 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4626 f1e81a05 2019-08-10 stsp const struct got_error *err;
4628 f1e81a05 2019-08-10 stsp if (outfile) {
4629 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4630 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4631 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4633 f1e81a05 2019-08-10 stsp return err;
4634 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4637 f1e81a05 2019-08-10 stsp if (rejectfile) {
4638 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4639 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4640 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4642 f1e81a05 2019-08-10 stsp return err;
4643 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4647 33aa809d 2019-08-08 stsp return NULL;
4650 33aa809d 2019-08-08 stsp static const struct got_error *
4651 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4652 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4653 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4654 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4655 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4657 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4658 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4659 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4660 33aa809d 2019-08-08 stsp FILE *hunkfile;
4661 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4662 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4665 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4667 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4668 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4669 fe621944 2020-11-10 stsp start_old = cc.left.start;
4670 fe621944 2020-11-10 stsp end_old = cc.left.end;
4671 fe621944 2020-11-10 stsp start_new = cc.right.start;
4672 fe621944 2020-11-10 stsp end_new = cc.right.end;
4674 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4675 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4676 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4678 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4679 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4680 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4682 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4683 fe621944 2020-11-10 stsp if (diff_state == NULL)
4684 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4685 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4687 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4688 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4689 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4690 33aa809d 2019-08-08 stsp goto done;
4693 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4694 fe621944 2020-11-10 stsp diff_result, &cc);
4695 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4696 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4697 33aa809d 2019-08-08 stsp goto done;
4700 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4701 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4702 33aa809d 2019-08-08 stsp goto done;
4705 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4706 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4708 33aa809d 2019-08-08 stsp goto done;
4710 33aa809d 2019-08-08 stsp switch (*choice) {
4711 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4712 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4713 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4715 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4716 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4717 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4719 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4722 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4726 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4727 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4728 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4729 33aa809d 2019-08-08 stsp return err;
4732 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4733 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4734 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4735 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4736 1f1abb7e 2019-08-08 stsp void *progress_arg;
4737 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4738 33aa809d 2019-08-08 stsp void *patch_arg;
4739 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4740 10604dce 2021-09-24 thomas int unlink_added_files;
4741 8641a332 2023-04-14 thomas struct got_pathlist_head *added_files_to_unlink;
4744 e20a8b6f 2019-06-04 stsp static const struct got_error *
4745 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4746 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4747 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4748 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4749 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4751 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4752 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4753 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4754 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4755 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4756 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4757 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4758 fe621944 2020-11-10 stsp struct stat sb2;
4759 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4760 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4761 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4763 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4765 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4767 33aa809d 2019-08-08 stsp return err;
4769 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4770 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4771 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4772 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4773 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4774 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4775 fa3cef63 2020-07-23 stsp goto done;
4777 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4778 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4779 48b4f239 2021-12-31 thomas if (link_len == -1) {
4780 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4783 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4784 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4787 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4788 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4789 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4790 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4791 fa3cef63 2020-07-23 stsp goto done;
4793 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4794 fa3cef63 2020-07-23 stsp sizeof(link_target));
4795 fa3cef63 2020-07-23 stsp if (link_len == -1)
4796 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4797 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4798 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4801 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4802 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4803 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4804 fa3cef63 2020-07-23 stsp goto done;
4807 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4808 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4809 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4810 fa3cef63 2020-07-23 stsp goto done;
4815 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4816 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4817 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4818 fa3cef63 2020-07-23 stsp goto done;
4820 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4821 fa3cef63 2020-07-23 stsp if (n != link_len) {
4822 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4823 fa3cef63 2020-07-23 stsp goto done;
4825 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4826 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4827 fa3cef63 2020-07-23 stsp goto done;
4829 fa3cef63 2020-07-23 stsp rewind(f2);
4832 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4833 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4834 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4835 f4ae6ddb 2022-07-01 thomas goto done;
4838 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4840 33aa809d 2019-08-08 stsp goto done;
4842 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4844 33aa809d 2019-08-08 stsp goto done;
4846 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4848 33aa809d 2019-08-08 stsp goto done;
4850 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4851 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4853 33aa809d 2019-08-08 stsp goto done;
4855 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4858 33aa809d 2019-08-08 stsp goto done;
4860 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4861 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4862 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4863 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4865 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4866 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4867 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4868 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4869 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4870 fe621944 2020-11-10 stsp nchanges++;
4872 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4873 33aa809d 2019-08-08 stsp int choice;
4874 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4875 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4876 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4877 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4878 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4879 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4881 33aa809d 2019-08-08 stsp goto done;
4882 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4883 33aa809d 2019-08-08 stsp have_content = 1;
4884 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4887 1ebedb77 2019-10-19 stsp if (have_content) {
4888 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4889 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4890 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4892 1ebedb77 2019-10-19 stsp goto done;
4894 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4895 a2c162eb 2022-10-30 thomas mode_t mode;
4897 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
4898 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
4899 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4900 fa3cef63 2020-07-23 stsp goto done;
4905 33aa809d 2019-08-08 stsp free(id_str);
4906 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4907 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4909 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4910 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4911 fe621944 2020-11-10 stsp if (err == NULL)
4912 fe621944 2020-11-10 stsp err = free_err;
4913 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4914 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4915 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4916 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4917 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4918 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4919 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4920 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4921 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4922 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4923 33aa809d 2019-08-08 stsp if (err || !have_content) {
4924 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4925 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4926 33aa809d 2019-08-08 stsp free(*path_outfile);
4927 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4929 33aa809d 2019-08-08 stsp free(path1);
4930 33aa809d 2019-08-08 stsp return err;
4933 33aa809d 2019-08-08 stsp static const struct got_error *
4934 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4935 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4936 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4937 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4939 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4940 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4941 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4942 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4943 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4944 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4945 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4946 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4947 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4948 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4949 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4950 f4ae6ddb 2022-07-01 thomas int fd = -1;
4952 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4953 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4954 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4955 d3bcc3d1 2019-08-08 stsp return NULL;
4957 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4958 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4959 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4961 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4962 65084dad 2019-08-08 stsp if (ie == NULL)
4963 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4965 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4966 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4967 a129376b 2019-03-28 stsp if (err) {
4968 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4969 a129376b 2019-03-28 stsp goto done;
4970 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4971 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4972 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4973 e20a8b6f 2019-06-04 stsp goto done;
4976 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4977 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4978 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4979 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4980 a129376b 2019-03-28 stsp goto done;
4983 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4984 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4985 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4986 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4987 a129376b 2019-03-28 stsp goto done;
4990 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4991 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4992 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4993 a129376b 2019-03-28 stsp goto done;
4998 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4999 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
5000 945f9229 2022-04-16 thomas if (err)
5001 945f9229 2022-04-16 thomas goto done;
5003 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5004 a9fa2909 2019-07-27 stsp if (err) {
5005 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5006 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
5007 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
5008 a9fa2909 2019-07-27 stsp goto done;
5010 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
5012 a9fa2909 2019-07-27 stsp goto done;
5014 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
5016 a9fa2909 2019-07-27 stsp goto done;
5018 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
5019 1233e6b6 2020-10-19 stsp free(te_name);
5020 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
5021 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
5022 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5023 a9fa2909 2019-07-27 stsp goto done;
5027 a129376b 2019-03-28 stsp switch (status) {
5028 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
5029 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5030 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5031 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5032 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5034 33aa809d 2019-08-08 stsp goto done;
5035 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5038 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5039 1f1abb7e 2019-08-08 stsp ie->path);
5041 1ee397ad 2019-07-12 stsp goto done;
5042 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5043 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
5044 8641a332 2023-04-14 thomas int do_unlink = a->added_files_to_unlink ? 0 : 1;
5046 8641a332 2023-04-14 thomas if (a->added_files_to_unlink) {
5047 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
5049 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, a->added_files_to_unlink,
5050 8641a332 2023-04-14 thomas entry) {
5051 8641a332 2023-04-14 thomas if (got_path_cmp(pe->path, relpath,
5052 8641a332 2023-04-14 thomas pe->path_len, strlen(relpath)))
5053 8641a332 2023-04-14 thomas continue;
5054 8641a332 2023-04-14 thomas do_unlink = 1;
5059 8641a332 2023-04-14 thomas if (do_unlink) {
5060 8641a332 2023-04-14 thomas if (asprintf(&ondisk_path, "%s/%s",
5061 8641a332 2023-04-14 thomas got_worktree_get_root_path(a->worktree),
5062 8641a332 2023-04-14 thomas relpath) == -1) {
5063 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
5064 8641a332 2023-04-14 thomas goto done;
5066 8641a332 2023-04-14 thomas if (unlink(ondisk_path) == -1) {
5067 8641a332 2023-04-14 thomas err = got_error_from_errno2("unlink",
5068 8641a332 2023-04-14 thomas ondisk_path);
5074 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5075 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5076 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5077 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5078 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5080 33aa809d 2019-08-08 stsp goto done;
5081 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5084 33aa809d 2019-08-08 stsp /* fall through */
5085 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5086 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5087 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5088 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5089 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5090 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5091 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
5092 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
5094 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
5095 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5096 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5097 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5098 f4ae6ddb 2022-07-01 thomas goto done;
5101 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5103 65084dad 2019-08-08 stsp goto done;
5105 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5106 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5107 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5108 a129376b 2019-03-28 stsp goto done;
5111 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5112 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5113 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5114 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5115 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5116 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5117 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5119 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5120 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5121 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5122 369fd7e5 2020-07-23 stsp path_content);
5125 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5126 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5127 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5128 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5130 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5131 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5132 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5133 369fd7e5 2020-07-23 stsp goto done;
5137 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5138 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5139 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5140 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5141 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5142 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5144 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5146 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5147 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5148 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
5149 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5152 a129376b 2019-03-28 stsp goto done;
5153 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5154 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5155 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5156 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5157 437adc9d 2020-12-10 yzhong blob->id.sha1,
5158 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5160 2e1fa222 2020-07-23 stsp goto done;
5162 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5163 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5164 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5173 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5174 33aa809d 2019-08-08 stsp free(path_content);
5175 e20a8b6f 2019-06-04 stsp free(parent_path);
5176 a129376b 2019-03-28 stsp free(tree_path);
5177 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5178 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5180 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5182 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5183 a129376b 2019-03-28 stsp free(tree_id);
5184 945f9229 2022-04-16 thomas if (base_commit)
5185 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
5186 e20a8b6f 2019-06-04 stsp return err;
5189 e20a8b6f 2019-06-04 stsp const struct got_error *
5190 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5191 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5192 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5193 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5194 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5196 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5197 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5198 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5199 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5200 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5201 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5203 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5205 e20a8b6f 2019-06-04 stsp return err;
5207 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5209 e20a8b6f 2019-06-04 stsp goto done;
5211 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5212 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5213 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5214 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5215 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5216 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5217 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5218 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
5219 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5220 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5221 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
5225 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5226 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5227 e20a8b6f 2019-06-04 stsp err = sync_err;
5229 fb399478 2019-07-12 stsp free(fileindex_path);
5230 a129376b 2019-03-28 stsp if (fileindex)
5231 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5232 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5233 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5234 a129376b 2019-03-28 stsp err = unlockerr;
5235 c4296144 2019-05-09 stsp return err;
5238 cf066bf8 2019-05-09 stsp static void
5239 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5241 24519714 2019-05-09 stsp free(ct->path);
5242 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5243 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5244 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5245 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5246 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5247 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5251 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5252 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5253 24519714 2019-05-09 stsp struct got_repository *repo;
5254 24519714 2019-05-09 stsp struct got_worktree *worktree;
5255 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5256 5f8a88c6 2019-08-03 stsp int have_staged_files;
5257 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5258 ef899790 2022-11-01 thomas int diff_header_shown;
5259 be94c032 2023-02-20 thomas int commit_conflicts;
5260 ef899790 2022-11-01 thomas FILE *diff_outfile;
5261 ef899790 2022-11-01 thomas FILE *f1;
5262 ef899790 2022-11-01 thomas FILE *f2;
5266 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5267 ef899790 2022-11-01 thomas * it as content to the diff engine.
5269 ef899790 2022-11-01 thomas static const struct got_error *
5270 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5271 ef899790 2022-11-01 thomas const char *abspath)
5273 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5274 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5275 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5277 ef899790 2022-11-01 thomas *fd = -1;
5279 ef899790 2022-11-01 thomas if (dirfd != -1) {
5280 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5281 ef899790 2022-11-01 thomas if (target_len == -1)
5282 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5283 ef899790 2022-11-01 thomas } else {
5284 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5285 ef899790 2022-11-01 thomas if (target_len == -1)
5286 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5289 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5290 ef899790 2022-11-01 thomas if (*fd == -1)
5291 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5293 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5294 ef899790 2022-11-01 thomas if (outlen == -1) {
5295 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5296 ef899790 2022-11-01 thomas goto done;
5299 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5300 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5301 ef899790 2022-11-01 thomas goto done;
5304 ef899790 2022-11-01 thomas if (err) {
5305 ef899790 2022-11-01 thomas close(*fd);
5306 ef899790 2022-11-01 thomas *fd = -1;
5308 ef899790 2022-11-01 thomas return err;
5311 ef899790 2022-11-01 thomas static const struct got_error *
5312 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5313 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5314 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5316 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5317 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5318 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5319 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5320 ef899790 2022-11-01 thomas char *label1 = NULL;
5321 ef899790 2022-11-01 thomas struct stat sb;
5322 ef899790 2022-11-01 thomas off_t size1 = 0;
5323 ef899790 2022-11-01 thomas int f2_exists = 0;
5324 ef899790 2022-11-01 thomas char *id_str = NULL;
5326 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5328 d259e491 2022-11-01 thomas if (diff_staged) {
5329 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5330 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5331 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5332 d259e491 2022-11-01 thomas return NULL;
5333 d259e491 2022-11-01 thomas } else {
5334 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5335 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5336 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5337 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5338 d259e491 2022-11-01 thomas return NULL;
5341 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5342 ef899790 2022-11-01 thomas if (err)
5343 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5344 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5345 ef899790 2022-11-01 thomas if (err)
5346 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5348 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5349 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5350 ef899790 2022-11-01 thomas if (err)
5351 ef899790 2022-11-01 thomas return err;
5352 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5353 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5354 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5355 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5356 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5357 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5358 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5361 ef899790 2022-11-01 thomas if (diff_staged) {
5362 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5363 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5364 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5365 ef899790 2022-11-01 thomas label1 = ct->path;
5366 ef899790 2022-11-01 thomas label2 = ct->path;
5368 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5369 ef899790 2022-11-01 thomas label2 = ct->path;
5371 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5372 ef899790 2022-11-01 thomas label1 = ct->path;
5374 ef899790 2022-11-01 thomas default:
5375 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5377 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5378 ef899790 2022-11-01 thomas if (fd1 == -1) {
5379 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5380 ef899790 2022-11-01 thomas goto done;
5382 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5383 ef899790 2022-11-01 thomas if (fd2 == -1) {
5384 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5385 ef899790 2022-11-01 thomas goto done;
5387 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5388 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5389 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5390 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5391 ef899790 2022-11-01 thomas goto done;
5394 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5395 ef899790 2022-11-01 thomas if (fd1 == -1) {
5396 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5397 ef899790 2022-11-01 thomas goto done;
5400 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5401 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5402 ef899790 2022-11-01 thomas 8192, fd1);
5403 ef899790 2022-11-01 thomas if (err)
5404 ef899790 2022-11-01 thomas goto done;
5407 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5408 ef899790 2022-11-01 thomas if (dirfd != -1) {
5409 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5410 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5411 ef899790 2022-11-01 thomas if (fd == -1) {
5412 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5413 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5414 ef899790 2022-11-01 thomas ct->ondisk_path);
5415 ef899790 2022-11-01 thomas goto done;
5417 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5418 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5419 ef899790 2022-11-01 thomas if (err)
5420 ef899790 2022-11-01 thomas goto done;
5422 ef899790 2022-11-01 thomas } else {
5423 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5424 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5425 ef899790 2022-11-01 thomas if (fd == -1) {
5426 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5427 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5428 ef899790 2022-11-01 thomas ct->ondisk_path);
5429 ef899790 2022-11-01 thomas goto done;
5431 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5432 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5433 ef899790 2022-11-01 thomas if (err)
5434 ef899790 2022-11-01 thomas goto done;
5437 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5438 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5439 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5440 ef899790 2022-11-01 thomas goto done;
5442 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5443 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5444 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5445 ef899790 2022-11-01 thomas goto done;
5447 ef899790 2022-11-01 thomas fd = -1;
5448 ef899790 2022-11-01 thomas f2_exists = 1;
5451 ef899790 2022-11-01 thomas if (blob1) {
5452 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5453 ef899790 2022-11-01 thomas f1, blob1);
5454 ef899790 2022-11-01 thomas if (err)
5455 ef899790 2022-11-01 thomas goto done;
5458 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5459 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5460 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5462 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5463 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5464 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5465 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5466 ef899790 2022-11-01 thomas if (blob1)
5467 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5468 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5469 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5470 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5471 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5472 ef899790 2022-11-01 thomas return err;
5475 c4296144 2019-05-09 stsp static const struct got_error *
5476 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5477 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5478 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5479 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5481 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5482 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5483 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5484 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5485 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5486 768aea60 2019-05-09 stsp struct stat sb;
5488 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5489 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5490 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5491 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5492 5f8a88c6 2019-08-03 stsp return NULL;
5494 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5495 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5496 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5499 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5500 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5501 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5502 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5503 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5504 5f8a88c6 2019-08-03 stsp return NULL;
5507 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5508 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5509 036813ee 2019-05-09 stsp goto done;
5511 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5512 036813ee 2019-05-09 stsp parent_path = strdup("");
5513 69960a46 2019-05-09 stsp if (parent_path == NULL)
5514 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5516 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5518 69960a46 2019-05-09 stsp return err;
5521 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5522 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5523 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5524 c4296144 2019-05-09 stsp goto done;
5527 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5528 768aea60 2019-05-09 stsp relpath) == -1) {
5529 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5530 768aea60 2019-05-09 stsp goto done;
5533 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5534 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5535 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5536 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5537 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5538 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5539 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5540 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5542 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5543 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5546 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5547 0aeb8099 2020-07-23 stsp goto done;
5549 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5550 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5551 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5552 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5553 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5554 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5555 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5556 12463d8b 2019-12-13 stsp ct->ondisk_path);
5557 12463d8b 2019-12-13 stsp goto done;
5559 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5560 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5561 768aea60 2019-05-09 stsp goto done;
5563 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5566 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5567 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5568 44d03001 2019-05-09 stsp relpath) == -1) {
5569 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5570 44d03001 2019-05-09 stsp goto done;
5573 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5574 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5575 35213c7c 2020-07-23 stsp int is_bad_symlink;
5576 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5577 35213c7c 2020-07-23 stsp ssize_t target_len;
5578 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5579 35213c7c 2020-07-23 stsp sizeof(target_path));
5580 35213c7c 2020-07-23 stsp if (target_len == -1) {
5581 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5582 35213c7c 2020-07-23 stsp ct->ondisk_path);
5583 35213c7c 2020-07-23 stsp goto done;
5585 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5586 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5588 35213c7c 2020-07-23 stsp goto done;
5589 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5590 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5591 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5592 35213c7c 2020-07-23 stsp goto done;
5597 cf066bf8 2019-05-09 stsp ct->status = status;
5598 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5599 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5600 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5601 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5602 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5603 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5604 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5605 b416585c 2019-05-13 stsp goto done;
5607 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5608 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5609 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5610 f0b75401 2019-08-03 stsp goto done;
5613 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5614 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5615 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5616 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5617 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5618 036813ee 2019-05-09 stsp goto done;
5621 24519714 2019-05-09 stsp ct->path = strdup(path);
5622 24519714 2019-05-09 stsp if (ct->path == NULL) {
5623 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5624 24519714 2019-05-09 stsp goto done;
5626 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5627 ef899790 2022-11-01 thomas if (err)
5628 ef899790 2022-11-01 thomas goto done;
5630 ef899790 2022-11-01 thomas if (a->diff_outfile && ct && new != NULL) {
5631 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5632 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5633 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5634 ef899790 2022-11-01 thomas if (err)
5635 ef899790 2022-11-01 thomas goto done;
5638 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5639 ed175427 2019-05-09 stsp free_commitable(ct);
5640 24519714 2019-05-09 stsp free(parent_path);
5641 036813ee 2019-05-09 stsp free(path);
5642 a129376b 2019-03-28 stsp return err;
5645 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5646 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5647 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5648 036813ee 2019-05-09 stsp struct got_repository *);
5650 ed175427 2019-05-09 stsp static const struct got_error *
5651 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5652 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5653 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5654 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5655 afa376bf 2019-05-09 stsp struct got_repository *repo)
5657 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5658 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5659 036813ee 2019-05-09 stsp char *subpath;
5661 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5662 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5663 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5665 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5667 ed175427 2019-05-09 stsp return err;
5669 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5670 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5671 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5672 036813ee 2019-05-09 stsp free(subpath);
5673 036813ee 2019-05-09 stsp return err;
5676 036813ee 2019-05-09 stsp static const struct got_error *
5677 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5679 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5680 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5682 036813ee 2019-05-09 stsp *match = 0;
5684 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5685 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5686 0f63689d 2019-05-10 stsp return NULL;
5689 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5691 0f63689d 2019-05-10 stsp return err;
5692 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5693 036813ee 2019-05-09 stsp free(ct_parent_path);
5694 036813ee 2019-05-09 stsp return err;
5697 768aea60 2019-05-09 stsp static mode_t
5698 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5700 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5701 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5703 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5706 036813ee 2019-05-09 stsp static const struct got_error *
5707 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5708 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5710 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5712 036813ee 2019-05-09 stsp *new_te = NULL;
5714 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5716 036813ee 2019-05-09 stsp goto done;
5718 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5720 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5721 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5722 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5724 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5726 036813ee 2019-05-09 stsp if (err && *new_te) {
5727 56e0773d 2019-11-28 stsp free(*new_te);
5728 036813ee 2019-05-09 stsp *new_te = NULL;
5730 036813ee 2019-05-09 stsp return err;
5733 036813ee 2019-05-09 stsp static const struct got_error *
5734 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5735 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5737 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5738 102b254e 2020-10-19 stsp char *ct_name = NULL;
5740 036813ee 2019-05-09 stsp *new_te = NULL;
5742 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5743 036813ee 2019-05-09 stsp if (*new_te == NULL)
5744 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5746 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5748 036813ee 2019-05-09 stsp goto done;
5749 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5750 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5751 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5752 036813ee 2019-05-09 stsp goto done;
5755 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5757 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5758 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5759 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5761 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5763 102b254e 2020-10-19 stsp free(ct_name);
5764 036813ee 2019-05-09 stsp if (err && *new_te) {
5765 56e0773d 2019-11-28 stsp free(*new_te);
5766 036813ee 2019-05-09 stsp *new_te = NULL;
5768 036813ee 2019-05-09 stsp return err;
5771 036813ee 2019-05-09 stsp static const struct got_error *
5772 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5773 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5775 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5776 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5778 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5780 036813ee 2019-05-09 stsp return err;
5781 036813ee 2019-05-09 stsp if (new_pe == NULL)
5782 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5783 036813ee 2019-05-09 stsp return NULL;
5786 afa376bf 2019-05-09 stsp static const struct got_error *
5787 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5788 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5790 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5791 5f8a88c6 2019-08-03 stsp unsigned char status;
5793 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5794 ae1e948a 2021-09-28 thomas return NULL;
5796 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5797 afa376bf 2019-05-09 stsp ct_path++;
5799 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5800 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5802 5f8a88c6 2019-08-03 stsp status = ct->status;
5804 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5805 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5808 036813ee 2019-05-09 stsp static const struct got_error *
5809 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5810 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5812 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5813 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5814 44d03001 2019-05-09 stsp char *te_path;
5816 44d03001 2019-05-09 stsp *modified = 0;
5818 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5819 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5820 44d03001 2019-05-09 stsp te->name) == -1)
5821 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5823 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5824 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5825 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5826 44d03001 2019-05-09 stsp strlen(te_path));
5827 62d463ca 2020-10-20 naddy if (*modified)
5831 44d03001 2019-05-09 stsp free(te_path);
5832 44d03001 2019-05-09 stsp return err;
5835 44d03001 2019-05-09 stsp static const struct got_error *
5836 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5837 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5838 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5840 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5841 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5843 036813ee 2019-05-09 stsp *ctp = NULL;
5845 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5846 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5847 036813ee 2019-05-09 stsp char *ct_name = NULL;
5848 036813ee 2019-05-09 stsp int path_matches;
5850 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5851 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5852 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5853 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5854 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5857 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5858 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5862 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5865 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5867 036813ee 2019-05-09 stsp return err;
5868 036813ee 2019-05-09 stsp if (!path_matches)
5871 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5873 d34b633e 2020-10-19 stsp return err;
5875 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5876 d34b633e 2020-10-19 stsp free(ct_name);
5879 d34b633e 2020-10-19 stsp free(ct_name);
5881 036813ee 2019-05-09 stsp *ctp = ct;
5885 2b496619 2019-07-10 stsp return err;
5888 2b496619 2019-07-10 stsp static const struct got_error *
5889 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5890 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5891 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5892 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5893 2b496619 2019-07-10 stsp struct got_repository *repo)
5895 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5896 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5897 2b496619 2019-07-10 stsp char *subtree_path;
5898 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5899 ba580f68 2020-03-22 stsp int nentries;
5901 2b496619 2019-07-10 stsp *new_tep = NULL;
5903 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5904 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5905 2b496619 2019-07-10 stsp child_path) == -1)
5906 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5908 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5909 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5910 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5911 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5913 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5914 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5915 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5916 2b496619 2019-07-10 stsp goto done;
5918 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5919 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5920 2b496619 2019-07-10 stsp if (err) {
5921 56e0773d 2019-11-28 stsp free(new_te);
5922 2b496619 2019-07-10 stsp goto done;
5924 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5927 2b496619 2019-07-10 stsp free(subtree_path);
5928 2b496619 2019-07-10 stsp if (err == NULL)
5929 2b496619 2019-07-10 stsp *new_tep = new_te;
5930 036813ee 2019-05-09 stsp return err;
5933 036813ee 2019-05-09 stsp static const struct got_error *
5934 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5935 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5936 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5937 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5938 036813ee 2019-05-09 stsp struct got_repository *repo)
5940 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5941 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5942 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5943 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5945 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5946 ba580f68 2020-03-22 stsp *nentries = 0;
5948 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5949 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5950 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5951 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5953 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5954 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5955 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5958 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5959 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5962 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5963 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5965 036813ee 2019-05-09 stsp goto done;
5967 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5968 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5969 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5971 036813ee 2019-05-09 stsp goto done;
5972 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5974 afa376bf 2019-05-09 stsp goto done;
5975 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5976 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5978 2f51b5b3 2019-05-09 stsp goto done;
5979 ba580f68 2020-03-22 stsp (*nentries)++;
5981 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5982 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5983 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5984 2b496619 2019-07-10 stsp == NULL) {
5985 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5986 2b496619 2019-07-10 stsp child_path, path_base_tree,
5987 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5990 2b496619 2019-07-10 stsp goto done;
5991 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5993 2b496619 2019-07-10 stsp goto done;
5994 ba580f68 2020-03-22 stsp (*nentries)++;
5999 2f51b5b3 2019-05-09 stsp if (base_tree) {
6000 56e0773d 2019-11-28 stsp int i, nbase_entries;
6001 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
6002 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
6003 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
6004 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
6006 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
6007 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
6008 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
6009 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
6011 63c5ca5d 2019-08-24 stsp goto done;
6012 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
6014 63c5ca5d 2019-08-24 stsp goto done;
6015 ba580f68 2020-03-22 stsp (*nentries)++;
6019 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
6020 44d03001 2019-05-09 stsp int modified;
6021 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6023 036813ee 2019-05-09 stsp goto done;
6024 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
6025 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
6027 2f51b5b3 2019-05-09 stsp goto done;
6028 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
6029 44d03001 2019-05-09 stsp if (modified) {
6030 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
6031 ba580f68 2020-03-22 stsp int nsubentries;
6032 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
6033 ba580f68 2020-03-22 stsp &nsubentries, te,
6034 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
6035 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
6037 44d03001 2019-05-09 stsp goto done;
6038 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6039 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6040 ba580f68 2020-03-22 stsp free(new_id);
6043 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6044 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6045 56e0773d 2019-11-28 stsp free(new_id);
6047 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6049 036813ee 2019-05-09 stsp goto done;
6050 ba580f68 2020-03-22 stsp (*nentries)++;
6054 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6055 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6057 f66c734c 2019-09-22 stsp goto done;
6059 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6060 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6061 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6062 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
6063 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6064 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6065 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6067 2f51b5b3 2019-05-09 stsp goto done;
6068 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6070 2f51b5b3 2019-05-09 stsp goto done;
6071 ba580f68 2020-03-22 stsp (*nentries)++;
6073 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6074 b416585c 2019-05-13 stsp status_arg);
6076 afa376bf 2019-05-09 stsp goto done;
6078 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6079 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6081 2f51b5b3 2019-05-09 stsp goto done;
6082 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6084 2f51b5b3 2019-05-09 stsp goto done;
6085 ba580f68 2020-03-22 stsp (*nentries)++;
6090 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6091 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6093 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6094 2e1fa222 2020-07-23 stsp return err;
6097 2e1fa222 2020-07-23 stsp static const struct got_error *
6098 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6099 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6100 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6101 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6103 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6104 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6105 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6107 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6108 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6109 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6111 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6113 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6114 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6115 437adc9d 2020-12-10 yzhong if (err)
6116 437adc9d 2020-12-10 yzhong goto done;
6119 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6120 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6121 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6122 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6123 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6124 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6125 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6126 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6128 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6129 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6130 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6131 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6132 72fd46fa 2019-09-06 stsp !have_staged_files);
6134 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6135 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6136 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6137 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6138 72fd46fa 2019-09-06 stsp !have_staged_files);
6140 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6142 437adc9d 2020-12-10 yzhong goto done;
6143 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6144 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6145 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6146 3969253a 2020-03-07 stsp if (err) {
6147 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6148 437adc9d 2020-12-10 yzhong goto done;
6150 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6151 3969253a 2020-03-07 stsp if (err) {
6152 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6153 437adc9d 2020-12-10 yzhong goto done;
6156 437adc9d 2020-12-10 yzhong free(relpath);
6157 437adc9d 2020-12-10 yzhong relpath = NULL;
6160 437adc9d 2020-12-10 yzhong free(relpath);
6161 d56d26ce 2019-05-10 stsp return err;
6165 d56d26ce 2019-05-10 stsp static const struct got_error *
6166 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6167 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6168 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6169 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6170 735ef5ac 2019-08-03 stsp int ood_errcode)
6172 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6173 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6174 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6176 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6177 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6178 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6179 1a36436d 2019-06-10 stsp return NULL;
6181 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6182 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6184 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6185 945f9229 2022-04-16 thomas if (err)
6186 945f9229 2022-04-16 thomas goto done;
6187 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6188 9bead371 2019-07-28 stsp if (err) {
6189 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6190 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6191 1a36436d 2019-06-10 stsp goto done;
6192 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6193 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6195 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6196 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6197 945f9229 2022-04-16 thomas if (err)
6198 945f9229 2022-04-16 thomas goto done;
6199 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6200 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6201 1a36436d 2019-06-10 stsp goto done;
6202 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6206 945f9229 2022-04-16 thomas if (commit)
6207 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6208 1a36436d 2019-06-10 stsp return err;
6211 ef20f542 2022-06-26 thomas static const struct got_error *
6212 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6213 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6214 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
6215 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
6216 10604dce 2021-09-24 thomas struct got_worktree *worktree,
6217 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
6218 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6219 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6220 afa376bf 2019-05-09 stsp struct got_repository *repo)
6222 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6223 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6224 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6225 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6226 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6227 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6228 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6229 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6230 10604dce 2021-09-24 thomas int nentries, nparents = 0;
6231 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6232 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6233 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6234 892eab0a 2022-07-22 thomas time_t timestamp;
6236 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6238 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6240 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6242 588edf97 2019-05-10 stsp goto done;
6244 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6246 588edf97 2019-05-10 stsp goto done;
6248 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6249 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
6250 ef899790 2022-11-01 thomas &logmsg, commit_arg);
6255 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6256 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6260 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6261 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6262 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6263 cf066bf8 2019-05-09 stsp char *ondisk_path;
6265 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6266 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6267 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6270 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6271 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6272 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6273 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6276 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6277 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6278 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6279 cf066bf8 2019-05-09 stsp goto done;
6281 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6282 2e1fa222 2020-07-23 stsp free(ondisk_path);
6284 cf066bf8 2019-05-09 stsp goto done;
6287 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6288 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6289 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6291 036813ee 2019-05-09 stsp goto done;
6293 538aebb5 2023-04-22 thomas err = got_object_qid_alloc(&pid, head_commit_id);
6295 de18fc63 2019-05-09 stsp goto done;
6296 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6297 10604dce 2021-09-24 thomas nparents++;
6298 10604dce 2021-09-24 thomas if (parent_id2) {
6299 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6300 10604dce 2021-09-24 thomas if (err)
6301 10604dce 2021-09-24 thomas goto done;
6302 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6303 10604dce 2021-09-24 thomas nparents++;
6305 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6306 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6307 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6308 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6309 33ad4cbe 2019-05-12 jcs free(logmsg);
6311 9d40349a 2019-05-09 stsp goto done;
6313 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6314 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6315 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6316 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6317 09f5bd90 2019-05-09 stsp goto done;
6319 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6320 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6322 09f5bd90 2019-05-09 stsp goto done;
6323 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6325 ebf99748 2019-05-09 stsp goto done;
6326 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6327 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6328 09f5bd90 2019-05-09 stsp goto done;
6330 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6331 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6333 f2c16586 2019-05-09 stsp goto done;
6334 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6336 f2c16586 2019-05-09 stsp goto done;
6338 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6340 09f5bd90 2019-05-09 stsp goto done;
6342 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6344 ebf99748 2019-05-09 stsp goto done;
6346 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6347 588edf97 2019-05-10 stsp if (head_tree)
6348 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6349 588edf97 2019-05-10 stsp if (head_commit)
6350 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6351 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6352 2f17228e 2019-05-12 stsp if (head_ref2) {
6353 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6354 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6355 2f17228e 2019-05-12 stsp err = unlockerr;
6356 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6358 39cd0ff6 2019-07-12 stsp return err;
6361 5c1e53bc 2019-07-28 stsp static const struct got_error *
6362 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6363 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6365 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6366 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6368 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6369 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6370 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6372 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6373 5c1e53bc 2019-07-28 stsp ct_path++;
6375 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6376 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6380 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6381 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6383 5c1e53bc 2019-07-28 stsp return NULL;
6386 f0b75401 2019-08-03 stsp static const struct got_error *
6387 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6389 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6391 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6392 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6393 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6396 f0b75401 2019-08-03 stsp return NULL;
6399 5f8a88c6 2019-08-03 stsp static const struct got_error *
6400 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6401 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6403 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6404 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6406 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6407 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6409 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6410 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6411 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6412 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6413 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6414 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6417 5f8a88c6 2019-08-03 stsp return NULL;
6420 39cd0ff6 2019-07-12 stsp const struct got_error *
6421 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6422 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6423 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6424 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6425 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6426 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6427 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6429 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6430 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6431 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6432 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6433 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6434 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6435 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6436 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6437 ef899790 2022-11-01 thomas char *diff_path = NULL;
6438 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6440 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6442 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6443 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6445 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6447 39cd0ff6 2019-07-12 stsp goto done;
6449 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6451 39cd0ff6 2019-07-12 stsp goto done;
6453 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6455 39cd0ff6 2019-07-12 stsp goto done;
6457 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6459 39cd0ff6 2019-07-12 stsp goto done;
6461 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6462 5f8a88c6 2019-08-03 stsp &have_staged_files);
6463 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6464 5f8a88c6 2019-08-03 stsp goto done;
6465 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6466 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6468 5f8a88c6 2019-08-03 stsp goto done;
6471 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6472 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6473 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6474 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6475 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6476 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6477 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6478 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6479 ef899790 2022-11-01 thomas if (show_diff) {
6480 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6481 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6482 ef899790 2022-11-01 thomas if (err)
6483 ef899790 2022-11-01 thomas goto done;
6484 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6485 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6486 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6487 ef899790 2022-11-01 thomas goto done;
6489 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6490 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6491 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6492 ef899790 2022-11-01 thomas goto done;
6496 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6497 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6498 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6500 5c1e53bc 2019-07-28 stsp goto done;
6503 ef899790 2022-11-01 thomas if (show_diff) {
6504 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6505 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6506 ef899790 2022-11-01 thomas goto done;
6510 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6511 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6512 39cd0ff6 2019-07-12 stsp goto done;
6515 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6516 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6518 5c1e53bc 2019-07-28 stsp goto done;
6521 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6522 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6523 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6525 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6526 f0b75401 2019-08-03 stsp ct_path++;
6527 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6528 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6529 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6531 b50cabdf 2019-07-12 stsp goto done;
6535 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6536 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6537 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6538 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6540 39cd0ff6 2019-07-12 stsp goto done;
6542 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6543 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6544 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6545 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6546 39cd0ff6 2019-07-12 stsp err = sync_err;
6548 39cd0ff6 2019-07-12 stsp if (fileindex)
6549 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6550 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6551 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6552 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6553 39cd0ff6 2019-07-12 stsp err = unlockerr;
6554 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6555 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6557 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6559 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6560 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6561 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6562 ef899790 2022-11-01 thomas free(diff_path);
6563 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6564 ef899790 2022-11-01 thomas err == NULL)
6565 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6566 c4296144 2019-05-09 stsp return err;
6569 8656d6c4 2019-05-20 stsp const char *
6570 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6572 8656d6c4 2019-05-20 stsp return ct->path;
6575 8656d6c4 2019-05-20 stsp unsigned int
6576 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6578 8656d6c4 2019-05-20 stsp return ct->status;
6581 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6582 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6583 818c7501 2019-07-11 stsp struct got_repository *repo;
6586 818c7501 2019-07-11 stsp static const struct got_error *
6587 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6589 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6590 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6591 818c7501 2019-07-11 stsp unsigned char status;
6592 818c7501 2019-07-11 stsp struct stat sb;
6593 818c7501 2019-07-11 stsp char *ondisk_path;
6595 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6596 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6597 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6598 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6600 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6602 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6604 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6605 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6606 818c7501 2019-07-11 stsp free(ondisk_path);
6608 818c7501 2019-07-11 stsp return err;
6610 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6611 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6612 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6613 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6615 818c7501 2019-07-11 stsp return NULL;
6618 818c7501 2019-07-11 stsp const struct got_error *
6619 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6620 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6621 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6622 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6624 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6625 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6626 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6627 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6628 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6629 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6630 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6632 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6633 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6634 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6636 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6638 818c7501 2019-07-11 stsp return err;
6640 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6642 818c7501 2019-07-11 stsp goto done;
6644 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6645 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6646 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6649 818c7501 2019-07-11 stsp goto done;
6651 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6653 818c7501 2019-07-11 stsp goto done;
6655 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6657 818c7501 2019-07-11 stsp goto done;
6659 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6661 818c7501 2019-07-11 stsp goto done;
6663 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6666 e51d7b55 2020-01-04 stsp goto done;
6668 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6670 818c7501 2019-07-11 stsp goto done;
6671 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6672 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6673 e51d7b55 2020-01-04 stsp goto done;
6676 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6677 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6679 818c7501 2019-07-11 stsp goto done;
6680 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6682 818c7501 2019-07-11 stsp goto done;
6684 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6686 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6688 818c7501 2019-07-11 stsp goto done;
6690 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6692 818c7501 2019-07-11 stsp goto done;
6694 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6695 818c7501 2019-07-11 stsp worktree->base_commit_id);
6697 818c7501 2019-07-11 stsp goto done;
6698 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6700 818c7501 2019-07-11 stsp goto done;
6702 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6704 818c7501 2019-07-11 stsp goto done;
6706 818c7501 2019-07-11 stsp free(fileindex_path);
6707 818c7501 2019-07-11 stsp free(tmp_branch_name);
6708 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6709 818c7501 2019-07-11 stsp free(branch_ref_name);
6710 818c7501 2019-07-11 stsp if (branch_ref)
6711 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6712 818c7501 2019-07-11 stsp if (wt_branch)
6713 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6714 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6715 818c7501 2019-07-11 stsp if (err) {
6716 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6717 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6718 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6720 818c7501 2019-07-11 stsp if (*tmp_branch) {
6721 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6722 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6724 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6725 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6726 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6728 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6730 818c7501 2019-07-11 stsp return err;
6733 818c7501 2019-07-11 stsp const struct got_error *
6734 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6735 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6736 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6737 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6739 818c7501 2019-07-11 stsp const struct got_error *err;
6740 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6741 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6742 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6743 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6744 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6746 818c7501 2019-07-11 stsp *commit_id = NULL;
6747 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6748 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6749 3e3a69f1 2019-07-25 stsp *branch = NULL;
6750 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6752 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6754 3e3a69f1 2019-07-25 stsp return err;
6756 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6758 f032f1f7 2019-08-04 stsp goto done;
6760 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6761 f032f1f7 2019-08-04 stsp &have_staged_files);
6762 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6763 f032f1f7 2019-08-04 stsp goto done;
6764 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6765 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6766 3e3a69f1 2019-07-25 stsp goto done;
6769 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6771 f032f1f7 2019-08-04 stsp goto done;
6773 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6775 818c7501 2019-07-11 stsp goto done;
6777 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6779 818c7501 2019-07-11 stsp goto done;
6781 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6783 818c7501 2019-07-11 stsp goto done;
6785 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6787 818c7501 2019-07-11 stsp goto done;
6789 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6790 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6792 818c7501 2019-07-11 stsp goto done;
6794 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6796 818c7501 2019-07-11 stsp goto done;
6798 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6800 818c7501 2019-07-11 stsp goto done;
6802 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6803 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6805 818c7501 2019-07-11 stsp goto done;
6807 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6809 818c7501 2019-07-11 stsp goto done;
6811 818c7501 2019-07-11 stsp free(commit_ref_name);
6812 818c7501 2019-07-11 stsp free(branch_ref_name);
6813 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6814 818c7501 2019-07-11 stsp if (commit_ref)
6815 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6816 818c7501 2019-07-11 stsp if (branch_ref)
6817 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6818 818c7501 2019-07-11 stsp if (err) {
6819 818c7501 2019-07-11 stsp free(*commit_id);
6820 818c7501 2019-07-11 stsp *commit_id = NULL;
6821 818c7501 2019-07-11 stsp if (*tmp_branch) {
6822 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6823 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6825 818c7501 2019-07-11 stsp if (*new_base_branch) {
6826 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6827 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6829 818c7501 2019-07-11 stsp if (*branch) {
6830 818c7501 2019-07-11 stsp got_ref_close(*branch);
6831 818c7501 2019-07-11 stsp *branch = NULL;
6833 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6834 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6835 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6837 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6839 818c7501 2019-07-11 stsp return err;
6842 818c7501 2019-07-11 stsp const struct got_error *
6843 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6845 818c7501 2019-07-11 stsp const struct got_error *err;
6846 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6848 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6850 818c7501 2019-07-11 stsp return err;
6852 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6853 818c7501 2019-07-11 stsp free(tmp_branch_name);
6854 818c7501 2019-07-11 stsp return NULL;
6857 818c7501 2019-07-11 stsp static const struct got_error *
6858 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6859 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
6861 0ebf8283 2019-07-24 stsp *logmsg = arg;
6862 818c7501 2019-07-11 stsp return NULL;
6865 818c7501 2019-07-11 stsp static const struct got_error *
6866 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6867 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6868 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6869 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6871 818c7501 2019-07-11 stsp return NULL;
6874 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6875 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6876 01757395 2019-07-12 stsp void *progress_arg;
6877 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6880 01757395 2019-07-12 stsp static const struct got_error *
6881 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6883 01757395 2019-07-12 stsp const struct got_error *err;
6884 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6886 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6888 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6890 01757395 2019-07-12 stsp return err;
6892 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6893 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6894 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6895 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6896 01757395 2019-07-12 stsp return NULL;
6898 01757395 2019-07-12 stsp p = strdup(path);
6899 01757395 2019-07-12 stsp if (p == NULL)
6900 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6902 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6903 01757395 2019-07-12 stsp if (err || new == NULL)
6905 01757395 2019-07-12 stsp return err;
6908 0ebf8283 2019-07-24 stsp static const struct got_error *
6909 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6910 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6912 818c7501 2019-07-11 stsp const struct got_error *err;
6913 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6915 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6916 818c7501 2019-07-11 stsp if (err) {
6917 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6918 818c7501 2019-07-11 stsp goto done;
6919 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6921 818c7501 2019-07-11 stsp goto done;
6922 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6924 818c7501 2019-07-11 stsp goto done;
6925 de05890f 2020-03-05 stsp } else if (is_rebase) {
6926 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6929 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6931 818c7501 2019-07-11 stsp goto done;
6932 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6933 818c7501 2019-07-11 stsp free(stored_id);
6934 818c7501 2019-07-11 stsp if (cmp != 0) {
6935 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6936 818c7501 2019-07-11 stsp goto done;
6940 0ebf8283 2019-07-24 stsp if (commit_ref)
6941 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6942 0ebf8283 2019-07-24 stsp return err;
6945 0ebf8283 2019-07-24 stsp static const struct got_error *
6946 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6947 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6948 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6949 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6950 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6951 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6953 0ebf8283 2019-07-24 stsp const struct got_error *err;
6954 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6955 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6956 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6958 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6960 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6962 0ebf8283 2019-07-24 stsp return err;
6964 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6965 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6966 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6967 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6968 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6969 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6970 818c7501 2019-07-11 stsp if (commit_ref)
6971 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6972 818c7501 2019-07-11 stsp return err;
6975 818c7501 2019-07-11 stsp const struct got_error *
6976 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6977 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6978 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6979 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6980 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6981 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6983 0ebf8283 2019-07-24 stsp const struct got_error *err;
6984 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6986 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6988 0ebf8283 2019-07-24 stsp return err;
6990 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6992 0ebf8283 2019-07-24 stsp goto done;
6994 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6995 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6996 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6998 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6999 0ebf8283 2019-07-24 stsp return err;
7002 0ebf8283 2019-07-24 stsp const struct got_error *
7003 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7004 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7005 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7006 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7007 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7008 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7010 0ebf8283 2019-07-24 stsp const struct got_error *err;
7011 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7013 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7015 0ebf8283 2019-07-24 stsp return err;
7017 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7019 0ebf8283 2019-07-24 stsp goto done;
7021 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7022 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7023 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7025 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7026 0ebf8283 2019-07-24 stsp return err;
7029 0ebf8283 2019-07-24 stsp static const struct got_error *
7030 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
7031 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7032 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7033 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
7034 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
7035 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7037 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7038 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7039 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7040 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7041 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7042 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7043 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7045 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7046 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7047 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7049 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7051 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7053 3e3a69f1 2019-07-25 stsp return err;
7055 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7056 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7057 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7058 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7059 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
7061 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7062 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7064 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7065 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7066 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7067 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7068 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7069 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7071 01757395 2019-07-12 stsp if (merged_paths) {
7072 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7073 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7074 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7075 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7078 01757395 2019-07-12 stsp goto done;
7081 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7082 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7084 01757395 2019-07-12 stsp goto done;
7087 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7088 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7089 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7091 a0e95631 2019-07-12 stsp goto done;
7092 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7093 a0e95631 2019-07-12 stsp goto done;
7096 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7098 edd02c5e 2019-07-11 stsp goto done;
7100 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7102 818c7501 2019-07-11 stsp goto done;
7104 5943eee2 2019-08-13 stsp if (new_logmsg) {
7105 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7106 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7107 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7108 5943eee2 2019-08-13 stsp goto done;
7111 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7113 5943eee2 2019-08-13 stsp goto done;
7116 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7117 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7118 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
7119 8db00f97 2022-07-22 thomas committer ? committer :
7120 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
7121 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7123 a0e95631 2019-07-12 stsp goto done;
7125 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7127 a0e95631 2019-07-12 stsp goto done;
7129 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7131 a0e95631 2019-07-12 stsp goto done;
7133 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7134 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7135 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7136 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7137 a0e95631 2019-07-12 stsp err = sync_err;
7139 a0e95631 2019-07-12 stsp free(fileindex_path);
7140 a0e95631 2019-07-12 stsp free(head_commit_id);
7141 a0e95631 2019-07-12 stsp if (head_ref)
7142 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7143 818c7501 2019-07-11 stsp if (err) {
7144 818c7501 2019-07-11 stsp free(*new_commit_id);
7145 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7147 818c7501 2019-07-11 stsp return err;
7150 818c7501 2019-07-11 stsp const struct got_error *
7151 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7152 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7153 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7154 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7155 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
7156 be94c032 2023-02-20 thomas struct got_repository *repo)
7158 0ebf8283 2019-07-24 stsp const struct got_error *err;
7159 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7160 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7161 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7163 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7165 0ebf8283 2019-07-24 stsp return err;
7167 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7169 0ebf8283 2019-07-24 stsp goto done;
7170 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7172 0ebf8283 2019-07-24 stsp goto done;
7173 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7174 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7175 0ebf8283 2019-07-24 stsp goto done;
7178 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7179 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7180 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
7182 0ebf8283 2019-07-24 stsp if (commit_ref)
7183 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7184 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7185 0ebf8283 2019-07-24 stsp free(commit_id);
7186 0ebf8283 2019-07-24 stsp return err;
7189 0ebf8283 2019-07-24 stsp const struct got_error *
7190 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7191 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7192 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7193 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7194 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7195 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7197 0ebf8283 2019-07-24 stsp const struct got_error *err;
7198 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7199 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7201 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7203 0ebf8283 2019-07-24 stsp return err;
7205 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7207 0ebf8283 2019-07-24 stsp goto done;
7209 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7210 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7211 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
7213 0ebf8283 2019-07-24 stsp if (commit_ref)
7214 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7215 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7216 0ebf8283 2019-07-24 stsp return err;
7219 0ebf8283 2019-07-24 stsp const struct got_error *
7220 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7221 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7223 3e3a69f1 2019-07-25 stsp if (fileindex)
7224 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7225 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7228 69844fba 2019-07-11 stsp static const struct got_error *
7229 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7231 69844fba 2019-07-11 stsp const struct got_error *err;
7232 69844fba 2019-07-11 stsp struct got_reference *ref;
7234 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7235 69844fba 2019-07-11 stsp if (err) {
7236 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7237 69844fba 2019-07-11 stsp return NULL;
7238 69844fba 2019-07-11 stsp return err;
7241 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7242 69844fba 2019-07-11 stsp got_ref_close(ref);
7243 69844fba 2019-07-11 stsp return err;
7246 69844fba 2019-07-11 stsp static const struct got_error *
7247 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7249 69844fba 2019-07-11 stsp const struct got_error *err;
7250 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7251 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7253 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7255 69844fba 2019-07-11 stsp goto done;
7256 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7258 69844fba 2019-07-11 stsp goto done;
7260 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7262 69844fba 2019-07-11 stsp goto done;
7263 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7265 69844fba 2019-07-11 stsp goto done;
7267 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7269 69844fba 2019-07-11 stsp goto done;
7270 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7272 69844fba 2019-07-11 stsp goto done;
7274 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7276 69844fba 2019-07-11 stsp goto done;
7277 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7279 69844fba 2019-07-11 stsp goto done;
7282 69844fba 2019-07-11 stsp free(tmp_branch_name);
7283 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7284 69844fba 2019-07-11 stsp free(branch_ref_name);
7285 69844fba 2019-07-11 stsp free(commit_ref_name);
7286 e600f124 2021-03-21 stsp return err;
7289 ef20f542 2022-06-26 thomas static const struct got_error *
7290 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7291 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7293 e600f124 2021-03-21 stsp const struct got_error *err;
7294 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7295 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7296 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7297 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7298 e600f124 2021-03-21 stsp char *refname = NULL;
7300 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7301 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7302 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7303 e600f124 2021-03-21 stsp branch_name += 11;
7305 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7307 e600f124 2021-03-21 stsp return err;
7309 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7310 e600f124 2021-03-21 stsp new_id_str) == -1) {
7311 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7312 e600f124 2021-03-21 stsp goto done;
7315 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7317 e600f124 2021-03-21 stsp goto done;
7319 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7321 e600f124 2021-03-21 stsp goto done;
7323 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7325 e600f124 2021-03-21 stsp free(new_id_str);
7326 e600f124 2021-03-21 stsp free(refname);
7327 e600f124 2021-03-21 stsp free(old_commit_id);
7329 e600f124 2021-03-21 stsp got_ref_close(ref);
7330 69844fba 2019-07-11 stsp return err;
7333 818c7501 2019-07-11 stsp const struct got_error *
7334 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7335 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7336 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7337 f08f28be 2023-02-03 thomas int create_backup)
7339 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7340 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7341 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7343 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7345 818c7501 2019-07-11 stsp return err;
7347 e600f124 2021-03-21 stsp if (create_backup) {
7348 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7349 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7351 e600f124 2021-03-21 stsp goto done;
7354 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7356 818c7501 2019-07-11 stsp goto done;
7358 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7360 818c7501 2019-07-11 stsp goto done;
7362 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7364 818c7501 2019-07-11 stsp goto done;
7366 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7368 a615e0e7 2020-12-16 stsp goto done;
7370 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7372 a615e0e7 2020-12-16 stsp goto done;
7373 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7374 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7375 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7376 a615e0e7 2020-12-16 stsp err = sync_err;
7378 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7379 a615e0e7 2020-12-16 stsp free(fileindex_path);
7380 818c7501 2019-07-11 stsp free(new_head_commit_id);
7381 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7382 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7383 818c7501 2019-07-11 stsp err = unlockerr;
7384 818c7501 2019-07-11 stsp return err;
7387 8641a332 2023-04-14 thomas static const struct got_error *
7388 8641a332 2023-04-14 thomas get_paths_changed_between_commits(struct got_pathlist_head *paths,
7389 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7390 8641a332 2023-04-14 thomas struct got_repository *repo)
7392 8641a332 2023-04-14 thomas const struct got_error *err;
7393 8641a332 2023-04-14 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7394 8641a332 2023-04-14 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7396 8641a332 2023-04-14 thomas if (id1) {
7397 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit1, repo, id1);
7398 8641a332 2023-04-14 thomas if (err)
7399 8641a332 2023-04-14 thomas goto done;
7401 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree1, repo,
7402 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit1));
7403 8641a332 2023-04-14 thomas if (err)
7404 8641a332 2023-04-14 thomas goto done;
7407 8641a332 2023-04-14 thomas if (id2) {
7408 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit2, repo, id2);
7409 8641a332 2023-04-14 thomas if (err)
7410 8641a332 2023-04-14 thomas goto done;
7412 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree2, repo,
7413 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit2));
7414 8641a332 2023-04-14 thomas if (err)
7415 8641a332 2023-04-14 thomas goto done;
7418 8641a332 2023-04-14 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7419 8641a332 2023-04-14 thomas got_diff_tree_collect_changed_paths, paths, 0);
7420 8641a332 2023-04-14 thomas if (err)
7421 8641a332 2023-04-14 thomas goto done;
7423 8641a332 2023-04-14 thomas if (commit1)
7424 8641a332 2023-04-14 thomas got_object_commit_close(commit1);
7425 8641a332 2023-04-14 thomas if (commit2)
7426 8641a332 2023-04-14 thomas got_object_commit_close(commit2);
7427 8641a332 2023-04-14 thomas if (tree1)
7428 8641a332 2023-04-14 thomas got_object_tree_close(tree1);
7429 8641a332 2023-04-14 thomas if (tree2)
7430 8641a332 2023-04-14 thomas got_object_tree_close(tree2);
7431 8641a332 2023-04-14 thomas return err;
7434 8641a332 2023-04-14 thomas static const struct got_error *
7435 8641a332 2023-04-14 thomas get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7436 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7437 8641a332 2023-04-14 thomas const char *path_prefix, struct got_repository *repo)
7439 8641a332 2023-04-14 thomas const struct got_error *err;
7440 8641a332 2023-04-14 thomas struct got_pathlist_head merged_paths;
7441 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
7442 8641a332 2023-04-14 thomas char *abspath = NULL, *wt_path = NULL;
7444 8641a332 2023-04-14 thomas TAILQ_INIT(&merged_paths);
7446 8641a332 2023-04-14 thomas err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7447 8641a332 2023-04-14 thomas if (err)
7448 8641a332 2023-04-14 thomas goto done;
7450 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, &merged_paths, entry) {
7451 8641a332 2023-04-14 thomas struct got_diff_changed_path *change = pe->data;
7453 8641a332 2023-04-14 thomas if (change->status != GOT_STATUS_ADD)
7454 8641a332 2023-04-14 thomas continue;
7456 8641a332 2023-04-14 thomas if (got_path_is_root_dir(path_prefix)) {
7457 8641a332 2023-04-14 thomas wt_path = strdup(pe->path);
7458 8641a332 2023-04-14 thomas if (wt_path == NULL) {
7459 8641a332 2023-04-14 thomas err = got_error_from_errno("strdup");
7460 8641a332 2023-04-14 thomas goto done;
7462 8641a332 2023-04-14 thomas } else {
7463 8641a332 2023-04-14 thomas if (asprintf(&abspath, "/%s", pe->path) == -1) {
7464 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
7465 8641a332 2023-04-14 thomas goto done;
7468 8641a332 2023-04-14 thomas err = got_path_skip_common_ancestor(&wt_path,
7469 8641a332 2023-04-14 thomas path_prefix, abspath);
7470 8641a332 2023-04-14 thomas if (err)
7471 8641a332 2023-04-14 thomas goto done;
7472 8641a332 2023-04-14 thomas free(abspath);
7473 8641a332 2023-04-14 thomas abspath = NULL;
7476 8641a332 2023-04-14 thomas err = got_pathlist_append(added_paths, wt_path, NULL);
7477 8641a332 2023-04-14 thomas if (err)
7478 8641a332 2023-04-14 thomas goto done;
7479 8641a332 2023-04-14 thomas wt_path = NULL;
7483 8641a332 2023-04-14 thomas got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7484 8641a332 2023-04-14 thomas free(abspath);
7485 8641a332 2023-04-14 thomas free(wt_path);
7486 8641a332 2023-04-14 thomas return err;
7489 8641a332 2023-04-14 thomas static const struct got_error *
7490 8641a332 2023-04-14 thomas get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7491 8641a332 2023-04-14 thomas struct got_object_id *id, const char *path_prefix,
7492 8641a332 2023-04-14 thomas struct got_repository *repo)
7494 8641a332 2023-04-14 thomas const struct got_error *err;
7495 8641a332 2023-04-14 thomas struct got_commit_object *commit = NULL;
7496 8641a332 2023-04-14 thomas struct got_object_qid *pid;
7498 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo, id);
7499 8641a332 2023-04-14 thomas if (err)
7500 8641a332 2023-04-14 thomas goto done;
7502 8641a332 2023-04-14 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7504 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(added_paths,
7505 8641a332 2023-04-14 thomas pid ? &pid->id : NULL, id, path_prefix, repo);
7506 8641a332 2023-04-14 thomas if (err)
7507 8641a332 2023-04-14 thomas goto done;
7509 8641a332 2023-04-14 thomas if (commit)
7510 8641a332 2023-04-14 thomas got_object_commit_close(commit);
7511 8641a332 2023-04-14 thomas return err;
7514 818c7501 2019-07-11 stsp const struct got_error *
7515 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7516 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7517 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7518 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7520 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7521 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7522 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7523 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7524 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7525 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7526 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7527 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7528 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7529 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7530 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7532 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7534 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7536 818c7501 2019-07-11 stsp return err;
7538 8641a332 2023-04-14 thomas err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7539 8641a332 2023-04-14 thomas if (err)
7540 8641a332 2023-04-14 thomas goto done;
7542 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7543 8641a332 2023-04-14 thomas if (err)
7544 8641a332 2023-04-14 thomas goto done;
7546 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7547 8641a332 2023-04-14 thomas if (err)
7548 8641a332 2023-04-14 thomas goto done;
7551 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7552 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7553 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
7554 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
7556 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7557 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7558 8641a332 2023-04-14 thomas if (err)
7559 8641a332 2023-04-14 thomas goto done;
7561 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7562 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7564 818c7501 2019-07-11 stsp goto done;
7566 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7568 818c7501 2019-07-11 stsp goto done;
7571 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7572 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7573 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7575 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7577 818c7501 2019-07-11 stsp goto done;
7579 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7580 25a102ed 2023-04-14 thomas if (err)
7581 25a102ed 2023-04-14 thomas goto done;
7583 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7584 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7586 818c7501 2019-07-11 stsp goto done;
7588 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7589 945f9229 2022-04-16 thomas worktree->path_prefix);
7591 ca355955 2019-07-12 stsp goto done;
7593 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7595 ca355955 2019-07-12 stsp goto done;
7597 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7599 818c7501 2019-07-11 stsp goto done;
7601 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7602 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7603 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7604 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7605 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7606 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7607 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7608 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
7609 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
7610 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7611 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7613 1f1abb7e 2019-08-08 stsp goto sync;
7615 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7616 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7618 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7619 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7620 ca355955 2019-07-12 stsp err = sync_err;
7622 8641a332 2023-04-14 thomas got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7623 818c7501 2019-07-11 stsp got_ref_close(resolved);
7624 a3a2faf2 2019-07-12 stsp free(tree_id);
7625 818c7501 2019-07-11 stsp free(commit_id);
7626 8641a332 2023-04-14 thomas free(merged_commit_id);
7627 945f9229 2022-04-16 thomas if (commit)
7628 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7629 0ebf8283 2019-07-24 stsp if (fileindex)
7630 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7631 0ebf8283 2019-07-24 stsp free(fileindex_path);
7632 8641a332 2023-04-14 thomas free(commit_ref_name);
7633 8641a332 2023-04-14 thomas if (commit_ref)
7634 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
7636 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7637 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7638 0ebf8283 2019-07-24 stsp err = unlockerr;
7639 0ebf8283 2019-07-24 stsp return err;
7642 0ebf8283 2019-07-24 stsp const struct got_error *
7643 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7644 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7645 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7646 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7648 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7649 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7650 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7651 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7652 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7653 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7654 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7655 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7657 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7658 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7659 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7660 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7662 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7664 0ebf8283 2019-07-24 stsp return err;
7666 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7668 0ebf8283 2019-07-24 stsp goto done;
7670 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7671 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7672 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7675 0ebf8283 2019-07-24 stsp goto done;
7677 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7679 0ebf8283 2019-07-24 stsp goto done;
7681 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7683 0ebf8283 2019-07-24 stsp goto done;
7685 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7686 0ebf8283 2019-07-24 stsp worktree);
7688 0ebf8283 2019-07-24 stsp goto done;
7690 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7693 0ebf8283 2019-07-24 stsp goto done;
7695 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7697 0ebf8283 2019-07-24 stsp goto done;
7699 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7701 0ebf8283 2019-07-24 stsp goto done;
7703 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_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(base_commit_ref, repo);
7709 0ebf8283 2019-07-24 stsp goto done;
7710 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7711 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7712 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7713 0ebf8283 2019-07-24 stsp goto done;
7716 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7717 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7719 0ebf8283 2019-07-24 stsp goto done;
7720 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7722 0ebf8283 2019-07-24 stsp goto done;
7724 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7726 0ebf8283 2019-07-24 stsp goto done;
7728 0ebf8283 2019-07-24 stsp free(fileindex_path);
7729 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7730 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7731 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7732 0ebf8283 2019-07-24 stsp if (wt_branch)
7733 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7734 0ebf8283 2019-07-24 stsp if (err) {
7735 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7736 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7737 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7739 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7740 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7741 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7743 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7744 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7745 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7746 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7748 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7750 0ebf8283 2019-07-24 stsp return err;
7753 0ebf8283 2019-07-24 stsp const struct got_error *
7754 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7755 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7757 3e3a69f1 2019-07-25 stsp if (fileindex)
7758 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7759 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7762 0ebf8283 2019-07-24 stsp const struct got_error *
7763 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7764 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7766 0ebf8283 2019-07-24 stsp const struct got_error *err;
7767 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7769 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7771 0ebf8283 2019-07-24 stsp return err;
7773 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7774 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7775 0ebf8283 2019-07-24 stsp return NULL;
7778 0ebf8283 2019-07-24 stsp const struct got_error *
7779 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7780 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7781 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7782 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7784 0ebf8283 2019-07-24 stsp const struct got_error *err;
7785 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7786 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7787 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7788 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7789 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7790 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7792 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7793 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7794 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7795 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7797 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7799 3e3a69f1 2019-07-25 stsp return err;
7801 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7803 f032f1f7 2019-08-04 stsp goto done;
7805 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7806 f032f1f7 2019-08-04 stsp &have_staged_files);
7807 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7808 f032f1f7 2019-08-04 stsp goto done;
7809 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7810 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7811 3e3a69f1 2019-07-25 stsp goto done;
7814 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7816 f032f1f7 2019-08-04 stsp goto done;
7818 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7820 0ebf8283 2019-07-24 stsp goto done;
7822 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7824 0ebf8283 2019-07-24 stsp goto done;
7826 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7827 0ebf8283 2019-07-24 stsp worktree);
7829 0ebf8283 2019-07-24 stsp goto done;
7831 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7833 0ebf8283 2019-07-24 stsp goto done;
7835 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7837 0ebf8283 2019-07-24 stsp goto done;
7838 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7840 0ebf8283 2019-07-24 stsp goto done;
7842 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7844 0ebf8283 2019-07-24 stsp goto done;
7845 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7847 0ebf8283 2019-07-24 stsp goto done;
7849 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7851 0ebf8283 2019-07-24 stsp goto done;
7853 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7854 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7855 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7856 0ebf8283 2019-07-24 stsp if (commit_ref)
7857 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7858 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7859 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7860 0ebf8283 2019-07-24 stsp if (err) {
7861 0ebf8283 2019-07-24 stsp free(*commit_id);
7862 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7863 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7864 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7865 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7866 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7867 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7869 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7870 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7871 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7873 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7875 0ebf8283 2019-07-24 stsp return err;
7878 0ebf8283 2019-07-24 stsp static const struct got_error *
7879 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7881 0ebf8283 2019-07-24 stsp const struct got_error *err;
7882 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7883 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7885 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7887 0ebf8283 2019-07-24 stsp goto done;
7888 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7890 0ebf8283 2019-07-24 stsp goto done;
7892 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7893 0ebf8283 2019-07-24 stsp worktree);
7895 0ebf8283 2019-07-24 stsp goto done;
7896 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7898 0ebf8283 2019-07-24 stsp goto done;
7900 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7902 0ebf8283 2019-07-24 stsp goto done;
7903 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7905 0ebf8283 2019-07-24 stsp goto done;
7907 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7909 0ebf8283 2019-07-24 stsp goto done;
7910 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7912 0ebf8283 2019-07-24 stsp goto done;
7914 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7915 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7916 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7917 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7918 0ebf8283 2019-07-24 stsp return err;
7921 0ebf8283 2019-07-24 stsp const struct got_error *
7922 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7923 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7924 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7925 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7927 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7928 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7929 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7930 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7931 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7932 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7933 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7934 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7935 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7936 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7938 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7940 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7942 0ebf8283 2019-07-24 stsp return err;
7944 8641a332 2023-04-14 thomas err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7945 8641a332 2023-04-14 thomas if (err)
7946 8641a332 2023-04-14 thomas goto done;
7948 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7949 8641a332 2023-04-14 thomas if (err) {
7950 8641a332 2023-04-14 thomas if (err->code != GOT_ERR_NOT_REF)
7951 8641a332 2023-04-14 thomas goto done;
7952 8641a332 2023-04-14 thomas /* Can happen on early abort due to invalid histedit script. */
7953 8641a332 2023-04-14 thomas commit_ref = NULL;
7956 8641a332 2023-04-14 thomas if (commit_ref) {
7957 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7958 8641a332 2023-04-14 thomas if (err)
7959 8641a332 2023-04-14 thomas goto done;
7962 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7963 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7964 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files added by the
7965 8641a332 2023-04-14 thomas * user during conflict resolution or during histedit -e.
7967 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7968 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7969 8641a332 2023-04-14 thomas if (err)
7970 8641a332 2023-04-14 thomas goto done;
7973 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7974 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7976 0ebf8283 2019-07-24 stsp goto done;
7978 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7980 0ebf8283 2019-07-24 stsp goto done;
7982 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7984 0ebf8283 2019-07-24 stsp goto done;
7986 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7987 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7988 25a102ed 2023-04-14 thomas if (err)
7989 25a102ed 2023-04-14 thomas goto done;
7991 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7992 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7994 0ebf8283 2019-07-24 stsp goto done;
7996 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7998 0ebf8283 2019-07-24 stsp goto done;
8000 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
8002 0ebf8283 2019-07-24 stsp goto done;
8004 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
8005 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
8006 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
8007 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
8008 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
8009 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
8010 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
8011 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
8012 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8013 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8014 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8016 1f1abb7e 2019-08-08 stsp goto sync;
8018 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8019 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8021 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8022 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
8023 0ebf8283 2019-07-24 stsp err = sync_err;
8025 8641a332 2023-04-14 thomas if (resolved)
8026 8641a332 2023-04-14 thomas got_ref_close(resolved);
8027 8641a332 2023-04-14 thomas if (commit_ref)
8028 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8029 8641a332 2023-04-14 thomas free(merged_commit_id);
8030 0ebf8283 2019-07-24 stsp free(tree_id);
8031 818c7501 2019-07-11 stsp free(fileindex_path);
8032 8641a332 2023-04-14 thomas free(commit_ref_name);
8034 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8035 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
8036 818c7501 2019-07-11 stsp err = unlockerr;
8037 818c7501 2019-07-11 stsp return err;
8040 0ebf8283 2019-07-24 stsp const struct got_error *
8041 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8042 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8043 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8045 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8046 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8047 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8048 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8050 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8052 0ebf8283 2019-07-24 stsp return err;
8054 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8055 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8057 e600f124 2021-03-21 stsp goto done;
8059 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8060 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8062 0ebf8283 2019-07-24 stsp goto done;
8064 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8066 0ebf8283 2019-07-24 stsp goto done;
8068 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
8070 0ebf8283 2019-07-24 stsp goto done;
8072 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8074 0ebf8283 2019-07-24 stsp goto done;
8076 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8078 a615e0e7 2020-12-16 stsp goto done;
8080 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8082 a615e0e7 2020-12-16 stsp goto done;
8083 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8084 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8085 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8086 a615e0e7 2020-12-16 stsp err = sync_err;
8088 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8089 a615e0e7 2020-12-16 stsp free(fileindex_path);
8090 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8091 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8092 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8093 0ebf8283 2019-07-24 stsp err = unlockerr;
8094 0ebf8283 2019-07-24 stsp return err;
8097 0ebf8283 2019-07-24 stsp const struct got_error *
8098 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8099 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8101 0ebf8283 2019-07-24 stsp const struct got_error *err;
8102 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8104 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8106 0ebf8283 2019-07-24 stsp return err;
8108 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8110 0ebf8283 2019-07-24 stsp goto done;
8112 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8114 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8115 2822a352 2019-10-15 stsp return err;
8118 2822a352 2019-10-15 stsp const struct got_error *
8119 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8120 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8121 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8122 2822a352 2019-10-15 stsp struct got_repository *repo)
8124 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8125 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8126 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8128 2822a352 2019-10-15 stsp *fileindex = NULL;
8129 2822a352 2019-10-15 stsp *branch_ref = NULL;
8130 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8132 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8134 2822a352 2019-10-15 stsp return err;
8136 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8137 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8138 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8139 2822a352 2019-10-15 stsp "update -b or different branch name required");
8140 2822a352 2019-10-15 stsp goto done;
8143 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8145 2822a352 2019-10-15 stsp goto done;
8147 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8148 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8149 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8150 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8153 2822a352 2019-10-15 stsp goto done;
8155 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8157 2822a352 2019-10-15 stsp goto done;
8159 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8160 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8162 2822a352 2019-10-15 stsp if (err) {
8163 2822a352 2019-10-15 stsp if (*branch_ref) {
8164 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8165 2822a352 2019-10-15 stsp *branch_ref = NULL;
8167 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8168 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8169 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8171 2822a352 2019-10-15 stsp if (*fileindex) {
8172 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8173 2822a352 2019-10-15 stsp *fileindex = NULL;
8175 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8177 0cb83759 2019-08-03 stsp return err;
8180 2822a352 2019-10-15 stsp const struct got_error *
8181 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8182 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8183 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8184 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8185 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8187 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8188 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8189 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8190 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8192 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8194 2822a352 2019-10-15 stsp goto done;
8196 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8198 2822a352 2019-10-15 stsp goto done;
8200 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
8201 945f9229 2022-04-16 thomas if (err)
8202 945f9229 2022-04-16 thomas goto done;
8204 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8205 2822a352 2019-10-15 stsp worktree->path_prefix);
8207 2822a352 2019-10-15 stsp goto done;
8209 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8211 2822a352 2019-10-15 stsp goto done;
8213 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8214 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8216 2822a352 2019-10-15 stsp goto sync;
8218 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8220 2822a352 2019-10-15 stsp goto sync;
8222 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8224 6e210706 2021-01-22 stsp goto sync;
8226 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8228 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8229 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8230 2822a352 2019-10-15 stsp err = sync_err;
8233 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8234 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8235 2822a352 2019-10-15 stsp err = unlockerr;
8236 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8238 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8239 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8240 2822a352 2019-10-15 stsp err = unlockerr;
8241 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8243 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8244 2822a352 2019-10-15 stsp free(fileindex_path);
8245 2822a352 2019-10-15 stsp free(tree_id);
8246 945f9229 2022-04-16 thomas if (commit)
8247 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8249 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8250 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8251 2822a352 2019-10-15 stsp err = unlockerr;
8252 2822a352 2019-10-15 stsp return err;
8255 2822a352 2019-10-15 stsp const struct got_error *
8256 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8257 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8258 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8260 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8262 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8264 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8266 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8267 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8268 8b692cd0 2019-10-21 stsp err = unlockerr;
8269 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8271 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8272 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8273 8b692cd0 2019-10-21 stsp err = unlockerr;
8274 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8276 10604dce 2021-09-24 thomas return err;
8279 10604dce 2021-09-24 thomas const struct got_error *
8280 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
8281 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
8283 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
8284 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8286 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8287 10604dce 2021-09-24 thomas if (err)
8288 10604dce 2021-09-24 thomas goto done;
8290 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8292 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
8293 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8294 10604dce 2021-09-24 thomas err = sync_err;
8296 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8297 10604dce 2021-09-24 thomas free(fileindex_path);
8298 8b692cd0 2019-10-21 stsp return err;
8301 10604dce 2021-09-24 thomas static const struct got_error *
8302 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8304 10604dce 2021-09-24 thomas const struct got_error *err;
8305 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8307 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8308 10604dce 2021-09-24 thomas if (err)
8309 10604dce 2021-09-24 thomas goto done;
8310 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
8311 10604dce 2021-09-24 thomas if (err)
8312 10604dce 2021-09-24 thomas goto done;
8314 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8315 10604dce 2021-09-24 thomas if (err)
8316 10604dce 2021-09-24 thomas goto done;
8317 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
8318 10604dce 2021-09-24 thomas if (err)
8319 10604dce 2021-09-24 thomas goto done;
8322 10604dce 2021-09-24 thomas free(branch_refname);
8323 10604dce 2021-09-24 thomas free(commit_refname);
8324 10604dce 2021-09-24 thomas return err;
8327 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
8328 10604dce 2021-09-24 thomas struct got_worktree *worktree;
8329 10604dce 2021-09-24 thomas const char *branch_name;
8332 10604dce 2021-09-24 thomas static const struct got_error *
8333 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8334 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
8336 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
8338 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8339 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
8340 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
8342 10604dce 2021-09-24 thomas return NULL;
8346 10604dce 2021-09-24 thomas const struct got_error *
8347 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
8348 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
8349 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
8350 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
8351 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8352 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8354 10604dce 2021-09-24 thomas const struct got_error *err;
8355 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8357 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8358 10604dce 2021-09-24 thomas if (err)
8359 10604dce 2021-09-24 thomas goto done;
8361 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8362 10604dce 2021-09-24 thomas worktree);
8363 10604dce 2021-09-24 thomas if (err)
8364 10604dce 2021-09-24 thomas goto done;
8366 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8367 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
8368 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
8370 10604dce 2021-09-24 thomas free(fileindex_path);
8371 10604dce 2021-09-24 thomas return err;
8374 10604dce 2021-09-24 thomas const struct got_error *
8375 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
8376 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
8377 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
8378 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
8379 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
8380 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
8383 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
8384 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
8385 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
8386 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
8387 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
8388 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
8389 10604dce 2021-09-24 thomas int have_staged_files = 0;
8390 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
8391 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8393 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
8394 10604dce 2021-09-24 thomas *new_commit_id = NULL;
8396 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
8398 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8399 10604dce 2021-09-24 thomas if (err)
8400 10604dce 2021-09-24 thomas goto done;
8402 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8403 10604dce 2021-09-24 thomas if (err)
8404 10604dce 2021-09-24 thomas goto done;
8406 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
8407 10604dce 2021-09-24 thomas if (err)
8408 10604dce 2021-09-24 thomas goto done;
8410 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8411 10604dce 2021-09-24 thomas &have_staged_files);
8412 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8413 10604dce 2021-09-24 thomas goto done;
8414 10604dce 2021-09-24 thomas if (have_staged_files) {
8415 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8416 10604dce 2021-09-24 thomas goto done;
8419 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
8420 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
8421 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
8422 10604dce 2021-09-24 thomas cc_arg.repo = repo;
8423 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
8424 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8425 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
8426 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8427 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8428 10604dce 2021-09-24 thomas if (err)
8429 10604dce 2021-09-24 thomas goto done;
8431 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
8432 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
8433 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
8434 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
8435 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8436 10604dce 2021-09-24 thomas if (err)
8437 10604dce 2021-09-24 thomas goto done;
8439 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
8440 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
8441 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8442 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8443 10604dce 2021-09-24 thomas err = sync_err;
8445 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8446 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8448 10604dce 2021-09-24 thomas free_commitable(ct);
8450 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8451 10604dce 2021-09-24 thomas free(fileindex_path);
8452 10604dce 2021-09-24 thomas return err;
8455 10604dce 2021-09-24 thomas const struct got_error *
8456 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8457 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8459 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8460 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8462 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8463 10604dce 2021-09-24 thomas if (err)
8464 10604dce 2021-09-24 thomas goto done;
8466 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8467 10604dce 2021-09-24 thomas if (err)
8468 10604dce 2021-09-24 thomas goto done;
8469 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8470 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8471 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8472 10604dce 2021-09-24 thomas err = sync_err;
8474 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8475 10604dce 2021-09-24 thomas free(fileindex_path);
8476 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8477 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8478 10604dce 2021-09-24 thomas err = unlockerr;
8479 10604dce 2021-09-24 thomas return err;
8482 10604dce 2021-09-24 thomas const struct got_error *
8483 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8484 10604dce 2021-09-24 thomas struct got_repository *repo)
8486 10604dce 2021-09-24 thomas const struct got_error *err;
8487 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8488 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8490 10604dce 2021-09-24 thomas *in_progress = 0;
8492 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8493 10604dce 2021-09-24 thomas if (err)
8494 10604dce 2021-09-24 thomas return err;
8495 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8496 dfe86d1f 2021-09-24 thomas free(branch_refname);
8497 10604dce 2021-09-24 thomas if (err) {
8498 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8499 10604dce 2021-09-24 thomas return err;
8501 10604dce 2021-09-24 thomas *in_progress = 1;
8503 10604dce 2021-09-24 thomas return NULL;
8506 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8507 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8508 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8510 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8511 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8512 2b72f32d 2023-06-22 thomas struct got_reference *wt_branch = NULL;
8513 2b72f32d 2023-06-22 thomas struct got_object_id *wt_branch_tip = NULL;
8514 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8516 10604dce 2021-09-24 thomas *fileindex = NULL;
8518 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8519 10604dce 2021-09-24 thomas if (err)
8520 10604dce 2021-09-24 thomas return err;
8522 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8523 10604dce 2021-09-24 thomas if (err)
8524 10604dce 2021-09-24 thomas goto done;
8526 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8527 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8528 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8529 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8530 10604dce 2021-09-24 thomas &ok_arg);
8531 10604dce 2021-09-24 thomas if (err)
8532 10604dce 2021-09-24 thomas goto done;
8534 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8536 10604dce 2021-09-24 thomas if (err)
8537 10604dce 2021-09-24 thomas goto done;
8539 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8540 10604dce 2021-09-24 thomas if (err)
8541 10604dce 2021-09-24 thomas goto done;
8543 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8544 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8545 10604dce 2021-09-24 thomas goto done;
8549 2b72f32d 2023-06-22 thomas free(fileindex_path);
8550 2b72f32d 2023-06-22 thomas if (wt_branch)
8551 2b72f32d 2023-06-22 thomas got_ref_close(wt_branch);
8552 2b72f32d 2023-06-22 thomas free(wt_branch_tip);
8553 2b72f32d 2023-06-22 thomas if (err) {
8554 2b72f32d 2023-06-22 thomas if (*fileindex) {
8555 2b72f32d 2023-06-22 thomas got_fileindex_free(*fileindex);
8556 2b72f32d 2023-06-22 thomas *fileindex = NULL;
8558 2b72f32d 2023-06-22 thomas lock_worktree(worktree, LOCK_SH);
8560 2b72f32d 2023-06-22 thomas return err;
8563 2b72f32d 2023-06-22 thomas const struct got_error *got_worktree_merge_write_refs(
8564 2b72f32d 2023-06-22 thomas struct got_worktree *worktree, struct got_reference *branch,
8565 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8567 2b72f32d 2023-06-22 thomas const struct got_error *err = NULL;
8568 2b72f32d 2023-06-22 thomas char *branch_refname = NULL, *commit_refname = NULL;
8569 2b72f32d 2023-06-22 thomas struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8570 2b72f32d 2023-06-22 thomas struct got_object_id *branch_tip = NULL;
8572 2b72f32d 2023-06-22 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8573 2b72f32d 2023-06-22 thomas if (err)
8574 2b72f32d 2023-06-22 thomas return err;
8576 2b72f32d 2023-06-22 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8577 2b72f32d 2023-06-22 thomas if (err)
8578 2b72f32d 2023-06-22 thomas return err;
8580 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8581 10604dce 2021-09-24 thomas if (err)
8582 10604dce 2021-09-24 thomas goto done;
8584 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8585 10604dce 2021-09-24 thomas if (err)
8586 10604dce 2021-09-24 thomas goto done;
8587 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8588 10604dce 2021-09-24 thomas if (err)
8589 10604dce 2021-09-24 thomas goto done;
8591 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8592 10604dce 2021-09-24 thomas if (err)
8593 10604dce 2021-09-24 thomas goto done;
8594 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8595 10604dce 2021-09-24 thomas if (err)
8596 10604dce 2021-09-24 thomas goto done;
8599 10604dce 2021-09-24 thomas free(branch_refname);
8600 10604dce 2021-09-24 thomas free(commit_refname);
8601 10604dce 2021-09-24 thomas if (branch_ref)
8602 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8603 10604dce 2021-09-24 thomas if (commit_ref)
8604 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8605 2b72f32d 2023-06-22 thomas free(branch_tip);
8606 10604dce 2021-09-24 thomas return err;
8609 10604dce 2021-09-24 thomas const struct got_error *
8610 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8611 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8612 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8614 10604dce 2021-09-24 thomas const struct got_error *err;
8615 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8616 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8617 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8618 10604dce 2021-09-24 thomas int have_staged_files = 0;
8620 10604dce 2021-09-24 thomas *branch_name = NULL;
8621 10604dce 2021-09-24 thomas *branch_tip = NULL;
8622 10604dce 2021-09-24 thomas *fileindex = NULL;
8624 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8625 10604dce 2021-09-24 thomas if (err)
8626 10604dce 2021-09-24 thomas return err;
8628 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8629 10604dce 2021-09-24 thomas if (err)
8630 10604dce 2021-09-24 thomas goto done;
8632 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8633 10604dce 2021-09-24 thomas &have_staged_files);
8634 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8635 10604dce 2021-09-24 thomas goto done;
8636 10604dce 2021-09-24 thomas if (have_staged_files) {
8637 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8638 10604dce 2021-09-24 thomas goto done;
8641 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8642 10604dce 2021-09-24 thomas if (err)
8643 10604dce 2021-09-24 thomas goto done;
8645 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8646 10604dce 2021-09-24 thomas if (err)
8647 10604dce 2021-09-24 thomas goto done;
8649 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8650 10604dce 2021-09-24 thomas if (err)
8651 10604dce 2021-09-24 thomas goto done;
8653 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8654 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8655 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8656 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8657 10604dce 2021-09-24 thomas goto done;
8659 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8660 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8661 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8662 10604dce 2021-09-24 thomas goto done;
8665 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8666 10604dce 2021-09-24 thomas if (err)
8667 10604dce 2021-09-24 thomas goto done;
8669 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8670 10604dce 2021-09-24 thomas if (err)
8671 10604dce 2021-09-24 thomas goto done;
8673 10604dce 2021-09-24 thomas free(commit_refname);
8674 10604dce 2021-09-24 thomas free(branch_refname);
8675 10604dce 2021-09-24 thomas free(fileindex_path);
8676 10604dce 2021-09-24 thomas if (commit_ref)
8677 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8678 10604dce 2021-09-24 thomas if (branch_ref)
8679 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8680 10604dce 2021-09-24 thomas if (err) {
8681 10604dce 2021-09-24 thomas if (*branch_name) {
8682 10604dce 2021-09-24 thomas free(*branch_name);
8683 10604dce 2021-09-24 thomas *branch_name = NULL;
8685 10604dce 2021-09-24 thomas free(*branch_tip);
8686 10604dce 2021-09-24 thomas *branch_tip = NULL;
8687 10604dce 2021-09-24 thomas if (*fileindex) {
8688 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8689 10604dce 2021-09-24 thomas *fileindex = NULL;
8691 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8693 10604dce 2021-09-24 thomas return err;
8696 10604dce 2021-09-24 thomas const struct got_error *
8697 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8698 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8699 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8701 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8702 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8703 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8704 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8705 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
8706 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
8707 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
8708 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8709 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
8711 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
8713 8641a332 2023-04-14 thomas err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8714 8641a332 2023-04-14 thomas if (err)
8715 8641a332 2023-04-14 thomas goto done;
8717 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8718 8641a332 2023-04-14 thomas if (err)
8719 8641a332 2023-04-14 thomas goto done;
8721 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8722 8641a332 2023-04-14 thomas if (err)
8723 8641a332 2023-04-14 thomas goto done;
8726 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
8727 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
8728 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
8729 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
8731 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(&added_paths,
8732 8641a332 2023-04-14 thomas got_worktree_get_base_commit_id(worktree), merged_commit_id,
8733 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
8734 8641a332 2023-04-14 thomas if (err)
8735 8641a332 2023-04-14 thomas goto done;
8738 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8739 945f9229 2022-04-16 thomas worktree->base_commit_id);
8740 945f9229 2022-04-16 thomas if (err)
8741 945f9229 2022-04-16 thomas goto done;
8743 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8744 945f9229 2022-04-16 thomas worktree->path_prefix);
8745 10604dce 2021-09-24 thomas if (err)
8746 10604dce 2021-09-24 thomas goto done;
8748 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8749 10604dce 2021-09-24 thomas if (err)
8750 10604dce 2021-09-24 thomas goto done;
8752 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8753 10604dce 2021-09-24 thomas if (err)
8754 10604dce 2021-09-24 thomas goto done;
8756 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8757 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8758 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8759 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8760 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8761 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8762 10604dce 2021-09-24 thomas rfa.repo = repo;
8763 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8764 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8765 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8766 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8767 10604dce 2021-09-24 thomas if (err)
8768 10604dce 2021-09-24 thomas goto sync;
8770 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8771 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8773 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8774 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8775 10604dce 2021-09-24 thomas err = sync_err;
8777 10604dce 2021-09-24 thomas free(tree_id);
8778 8641a332 2023-04-14 thomas free(merged_commit_id);
8779 945f9229 2022-04-16 thomas if (commit)
8780 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8781 10604dce 2021-09-24 thomas if (fileindex)
8782 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8783 10604dce 2021-09-24 thomas free(fileindex_path);
8784 8641a332 2023-04-14 thomas if (commit_ref)
8785 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8786 8641a332 2023-04-14 thomas free(commit_ref_name);
8788 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8789 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8790 10604dce 2021-09-24 thomas err = unlockerr;
8791 10604dce 2021-09-24 thomas return err;
8794 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8795 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8796 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8797 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8798 2db2652d 2019-08-07 stsp struct got_repository *repo;
8799 2db2652d 2019-08-07 stsp int have_changes;
8802 ef20f542 2022-06-26 thomas static const struct got_error *
8803 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8804 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8805 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8806 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8808 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8809 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8810 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8811 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8812 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8813 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8815 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8816 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8817 8b13ce36 2019-08-08 stsp return NULL;
8818 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8819 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8821 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8822 735ef5ac 2019-08-03 stsp if (ie == NULL)
8823 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8825 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8826 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8827 735ef5ac 2019-08-03 stsp relpath) == -1)
8828 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8830 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8831 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
8832 43010591 2023-02-17 thomas &base_commit_id, ie);
8835 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8836 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8837 3aa5969e 2019-08-06 stsp goto done;
8838 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8839 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8840 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8841 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8842 735ef5ac 2019-08-03 stsp goto done;
8845 2db2652d 2019-08-07 stsp a->have_changes = 1;
8847 735ef5ac 2019-08-03 stsp p = in_repo_path;
8848 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8850 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8851 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8852 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8854 735ef5ac 2019-08-03 stsp free(in_repo_path);
8855 dc424a06 2019-08-07 stsp return err;
8858 2db2652d 2019-08-07 stsp struct stage_path_arg {
8859 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8860 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8861 2db2652d 2019-08-07 stsp struct got_repository *repo;
8862 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8863 2db2652d 2019-08-07 stsp void *status_arg;
8864 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8865 2db2652d 2019-08-07 stsp void *patch_arg;
8866 7b5dc508 2019-10-28 stsp int staged_something;
8867 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8870 2db2652d 2019-08-07 stsp static const struct got_error *
8871 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8872 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8873 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8874 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8876 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8877 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8878 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8879 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8880 0cb83759 2019-08-03 stsp uint32_t stage;
8881 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8882 0aeb8099 2020-07-23 stsp struct stat sb;
8884 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8885 8b13ce36 2019-08-08 stsp return NULL;
8887 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8888 d3e7c587 2019-08-03 stsp if (ie == NULL)
8889 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8891 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8892 2db2652d 2019-08-07 stsp relpath)== -1)
8893 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8895 0cb83759 2019-08-03 stsp switch (status) {
8896 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8897 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8898 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8899 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8900 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8903 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8904 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8905 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8906 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8907 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8910 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8913 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8914 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8915 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8916 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8917 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8921 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8922 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8925 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8926 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8927 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8928 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8930 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8931 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8932 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8933 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8934 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8935 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8936 35213c7c 2020-07-23 stsp ssize_t target_len;
8937 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8938 35213c7c 2020-07-23 stsp sizeof(target_path));
8939 35213c7c 2020-07-23 stsp if (target_len == -1) {
8940 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8941 35213c7c 2020-07-23 stsp ondisk_path);
8944 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8945 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8946 35213c7c 2020-07-23 stsp a->worktree->root_path);
8949 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8950 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8951 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8955 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8956 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8957 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8959 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8960 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8962 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8963 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8965 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8966 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8968 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8969 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8970 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8971 f289c82c 2022-06-13 thomas if (err)
8974 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8975 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8977 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8978 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8979 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8980 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8983 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8984 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8986 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8987 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8988 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8989 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8992 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8994 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8995 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8999 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
9000 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9001 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9002 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9004 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9005 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9008 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
9010 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
9011 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9013 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
9014 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
9017 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9021 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
9022 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
9023 dc424a06 2019-08-07 stsp free(path_content);
9024 2db2652d 2019-08-07 stsp free(ondisk_path);
9025 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
9026 0ebf8283 2019-07-24 stsp return err;
9029 0cb83759 2019-08-03 stsp const struct got_error *
9030 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
9031 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
9032 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
9033 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9034 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
9036 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9037 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9038 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9039 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9040 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9041 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9042 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9043 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9045 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9047 0cb83759 2019-08-03 stsp return err;
9049 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9050 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9052 735ef5ac 2019-08-03 stsp goto done;
9053 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9055 735ef5ac 2019-08-03 stsp goto done;
9056 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9058 0cb83759 2019-08-03 stsp goto done;
9060 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9061 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9062 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9063 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9064 2db2652d 2019-08-07 stsp oka.repo = repo;
9065 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9066 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9067 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9068 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
9070 735ef5ac 2019-08-03 stsp goto done;
9072 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9073 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9074 2db2652d 2019-08-07 stsp goto done;
9077 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9078 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9079 2db2652d 2019-08-07 stsp spa.repo = repo;
9080 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9081 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9082 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9083 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9084 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9085 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9086 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9087 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9088 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
9090 2db2652d 2019-08-07 stsp goto done;
9092 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9093 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9094 7b5dc508 2019-10-28 stsp goto done;
9097 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9098 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9099 0cb83759 2019-08-03 stsp err = sync_err;
9101 735ef5ac 2019-08-03 stsp if (head_ref)
9102 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9103 735ef5ac 2019-08-03 stsp free(head_commit_id);
9104 0cb83759 2019-08-03 stsp free(fileindex_path);
9105 0cb83759 2019-08-03 stsp if (fileindex)
9106 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9107 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9108 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9109 0cb83759 2019-08-03 stsp err = unlockerr;
9110 0cb83759 2019-08-03 stsp return err;
9113 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9114 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9115 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9116 ad493afc 2019-08-03 stsp struct got_repository *repo;
9117 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9118 ad493afc 2019-08-03 stsp void *progress_arg;
9119 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9120 2e1f37b0 2019-08-08 stsp void *patch_arg;
9123 2e1f37b0 2019-08-08 stsp static const struct got_error *
9124 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9125 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9126 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9127 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9128 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9130 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9131 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9132 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9133 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9134 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9135 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9136 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9137 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9139 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9140 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9142 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9144 2e1f37b0 2019-08-08 stsp return err;
9146 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9147 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9148 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9149 f4ae6ddb 2022-07-01 thomas goto done;
9151 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9152 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9153 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9154 f4ae6ddb 2022-07-01 thomas goto done;
9157 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9159 2e1f37b0 2019-08-08 stsp goto done;
9161 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9163 2e1f37b0 2019-08-08 stsp goto done;
9165 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9167 2e1f37b0 2019-08-08 stsp goto done;
9169 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9172 2e1f37b0 2019-08-08 stsp goto done;
9174 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9176 2e1f37b0 2019-08-08 stsp goto done;
9178 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9180 2e1f37b0 2019-08-08 stsp goto done;
9182 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9183 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9185 2e1f37b0 2019-08-08 stsp goto done;
9187 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9188 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
9190 2e1f37b0 2019-08-08 stsp goto done;
9191 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9192 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
9194 2e1f37b0 2019-08-08 stsp goto done;
9196 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9197 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9198 2e1f37b0 2019-08-08 stsp goto done;
9200 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9201 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9202 2e1f37b0 2019-08-08 stsp goto done;
9204 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9205 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9206 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
9207 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9208 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9209 fe621944 2020-11-10 stsp nchanges++;
9211 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9212 2e1f37b0 2019-08-08 stsp int choice;
9213 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9214 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9215 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9216 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9218 2e1f37b0 2019-08-08 stsp goto done;
9219 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9220 2e1f37b0 2019-08-08 stsp have_content = 1;
9222 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9223 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9226 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9227 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9228 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9230 2e1f37b0 2019-08-08 stsp free(label1);
9231 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9232 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9234 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9235 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9236 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9237 2e1f37b0 2019-08-08 stsp if (staged_blob)
9238 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9239 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9240 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9241 fe621944 2020-11-10 stsp err = free_err;
9242 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9243 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9244 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9245 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9246 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9247 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9248 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9249 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9250 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9251 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9252 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9253 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9254 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9255 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9256 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9257 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9258 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9259 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9260 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9262 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9263 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9264 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9265 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9266 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9267 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9268 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9270 2e1f37b0 2019-08-08 stsp free(path1);
9271 2e1f37b0 2019-08-08 stsp free(path2);
9272 fda8017d 2020-07-23 stsp return err;
9275 fda8017d 2020-07-23 stsp static const struct got_error *
9276 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9277 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9278 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9279 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9280 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9281 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9282 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9284 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9285 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9286 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9287 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9288 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9289 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9290 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9291 fda8017d 2020-07-23 stsp struct stat sb;
9293 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9294 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9295 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9297 fda8017d 2020-07-23 stsp return err;
9299 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9300 fda8017d 2020-07-23 stsp return NULL;
9302 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9303 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9304 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9306 fda8017d 2020-07-23 stsp goto done;
9309 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
9310 fda8017d 2020-07-23 stsp if (f == NULL) {
9311 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9312 fda8017d 2020-07-23 stsp path_unstaged_content);
9313 fda8017d 2020-07-23 stsp goto done;
9315 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9316 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9317 fda8017d 2020-07-23 stsp goto done;
9319 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9320 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9321 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9323 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9324 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9325 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9326 fda8017d 2020-07-23 stsp goto done;
9328 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9329 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9330 fda8017d 2020-07-23 stsp goto done;
9332 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9333 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9334 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9335 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9336 fda8017d 2020-07-23 stsp progress_arg);
9338 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9340 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9342 67a66647 2021-05-31 stsp return err;
9344 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9345 67a66647 2021-05-31 stsp parent) == -1) {
9346 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9347 67a66647 2021-05-31 stsp base_path = NULL;
9348 67a66647 2021-05-31 stsp goto done;
9351 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
9352 fc2a50f2 2022-11-01 thomas base_path, "");
9354 67a66647 2021-05-31 stsp goto done;
9355 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9356 67a66647 2021-05-31 stsp blob_base);
9358 67a66647 2021-05-31 stsp goto done;
9361 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9362 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9364 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9365 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9366 eec2f5a9 2021-06-03 stsp ondisk_path);
9368 eec2f5a9 2021-06-03 stsp goto done;
9371 06340621 2021-12-31 thomas fd = open(ondisk_path,
9372 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9373 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9374 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9375 eec2f5a9 2021-06-03 stsp goto done;
9377 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9378 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9379 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9380 eec2f5a9 2021-06-03 stsp close(fd);
9381 eec2f5a9 2021-06-03 stsp goto done;
9385 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9386 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9387 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9388 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9389 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9392 fda8017d 2020-07-23 stsp goto done;
9394 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9395 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9396 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9398 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9399 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9402 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9403 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9404 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9405 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9406 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9407 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9408 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9409 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9410 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9411 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9412 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9413 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9414 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9415 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9416 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9417 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9418 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9419 67a66647 2021-05-31 stsp free(blob_base_path);
9420 67a66647 2021-05-31 stsp free(parent);
9421 67a66647 2021-05-31 stsp free(base_path);
9422 2e1f37b0 2019-08-08 stsp return err;
9425 ad493afc 2019-08-03 stsp static const struct got_error *
9426 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9427 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9428 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9429 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9431 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9432 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9433 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9434 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9435 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9436 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9437 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9438 9bc94a15 2019-08-03 stsp struct stat sb;
9439 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9441 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9442 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9443 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9444 2e1f37b0 2019-08-08 stsp return NULL;
9446 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9447 ad493afc 2019-08-03 stsp if (ie == NULL)
9448 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9450 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9452 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9454 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9455 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9457 f69721c3 2019-10-21 stsp goto done;
9458 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9459 f69721c3 2019-10-21 stsp id_str) == -1) {
9460 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9461 f4ae6ddb 2022-07-01 thomas goto done;
9464 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9465 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9466 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9467 f4ae6ddb 2022-07-01 thomas goto done;
9469 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9470 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9471 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9472 f69721c3 2019-10-21 stsp goto done;
9475 ad493afc 2019-08-03 stsp switch (staged_status) {
9476 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9477 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9478 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
9481 ad493afc 2019-08-03 stsp /* fall through */
9482 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9483 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9484 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9485 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9486 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9487 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9490 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9493 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9494 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9495 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9496 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9497 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9498 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9501 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9502 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9505 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9506 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9507 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9508 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9509 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9510 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9511 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9512 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9513 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9515 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9516 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9517 36bf999c 2020-07-23 stsp char *staged_target;
9518 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9519 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9521 36bf999c 2020-07-23 stsp goto done;
9522 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9523 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9524 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9525 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9526 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9527 36bf999c 2020-07-23 stsp free(staged_target);
9529 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9530 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9531 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9532 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9533 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9534 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9535 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9539 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9542 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9543 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9544 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9545 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9548 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9549 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9550 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9551 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9552 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9555 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9557 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9558 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9562 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9563 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9564 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9565 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9568 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9572 ad493afc 2019-08-03 stsp free(ondisk_path);
9573 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9574 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9575 ad493afc 2019-08-03 stsp if (blob_base)
9576 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9577 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9578 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9579 ad493afc 2019-08-03 stsp if (blob_staged)
9580 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9581 f69721c3 2019-10-21 stsp free(id_str);
9582 f69721c3 2019-10-21 stsp free(label_orig);
9583 ad493afc 2019-08-03 stsp return err;
9586 ad493afc 2019-08-03 stsp const struct got_error *
9587 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9588 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9589 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9590 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9591 ad493afc 2019-08-03 stsp struct got_repository *repo)
9593 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9594 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9595 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9596 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9597 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9599 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9601 ad493afc 2019-08-03 stsp return err;
9603 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9605 ad493afc 2019-08-03 stsp goto done;
9607 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9608 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9609 ad493afc 2019-08-03 stsp upa.repo = repo;
9610 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9611 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9612 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9613 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9614 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9615 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9616 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9618 ad493afc 2019-08-03 stsp goto done;
9621 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9622 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9623 ad493afc 2019-08-03 stsp err = sync_err;
9625 ad493afc 2019-08-03 stsp free(fileindex_path);
9626 ad493afc 2019-08-03 stsp if (fileindex)
9627 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9628 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9629 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9630 ad493afc 2019-08-03 stsp err = unlockerr;
9631 ad493afc 2019-08-03 stsp return err;
9634 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9635 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9636 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9637 b2118c49 2020-07-28 stsp void *info_arg;
9638 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9639 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9640 b2118c49 2020-07-28 stsp void *cancel_arg;
9643 b2118c49 2020-07-28 stsp static const struct got_error *
9644 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9646 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9647 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9648 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9649 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9650 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9651 b2118c49 2020-07-28 stsp int stage;
9653 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9654 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9656 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9657 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9658 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9661 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9662 b2118c49 2020-07-28 stsp return NULL;
9664 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9665 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9666 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9667 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9668 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9669 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9670 43010591 2023-02-17 thomas &staged_blob_id, ie);
9673 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9674 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9676 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9677 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9680 b2118c49 2020-07-28 stsp const struct got_error *
9681 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9682 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9683 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9684 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9687 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9688 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9689 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9690 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9692 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9694 b2118c49 2020-07-28 stsp return err;
9696 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9698 b2118c49 2020-07-28 stsp goto done;
9700 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9701 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9702 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9703 b2118c49 2020-07-28 stsp arg.paths = paths;
9704 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9705 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9706 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9709 b2118c49 2020-07-28 stsp free(fileindex_path);
9710 b2118c49 2020-07-28 stsp if (fileindex)
9711 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9712 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9713 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9714 b2118c49 2020-07-28 stsp err = unlockerr;
9715 b2118c49 2020-07-28 stsp return err;
9718 814624e7 2022-03-22 thomas static const struct got_error *
9719 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9720 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9721 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9723 814624e7 2022-03-22 thomas const struct got_error *err;
9724 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9725 814624e7 2022-03-22 thomas struct stat sb;
9726 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9728 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9729 814624e7 2022-03-22 thomas if (err)
9730 814624e7 2022-03-22 thomas return err;
9732 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9733 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9734 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9736 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9737 814624e7 2022-03-22 thomas if (ie) {
9738 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9739 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9741 814624e7 2022-03-22 thomas if (err)
9742 814624e7 2022-03-22 thomas goto done;
9743 814624e7 2022-03-22 thomas } else {
9744 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9745 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9746 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9747 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9748 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9749 814624e7 2022-03-22 thomas ondisk_path);
9750 814624e7 2022-03-22 thomas goto done;
9752 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9757 814624e7 2022-03-22 thomas free(ondisk_path);
9758 814624e7 2022-03-22 thomas return err;
9761 814624e7 2022-03-22 thomas static const struct got_error *
9762 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
9763 814624e7 2022-03-22 thomas unsigned char staged_status)
9765 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9766 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9767 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9768 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9769 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9770 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9771 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9772 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9773 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9774 814624e7 2022-03-22 thomas return NULL;
9777 814624e7 2022-03-22 thomas static const struct got_error *
9778 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9780 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9781 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9782 814624e7 2022-03-22 thomas return NULL;
9785 814624e7 2022-03-22 thomas static const struct got_error *
9786 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9787 814624e7 2022-03-22 thomas unsigned char staged_status)
9789 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9790 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9791 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9792 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9793 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9794 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9795 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9796 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9797 814624e7 2022-03-22 thomas return NULL;
9800 814624e7 2022-03-22 thomas const struct got_error *
9801 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9802 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9804 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9807 814624e7 2022-03-22 thomas const struct got_error *
9808 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9809 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9810 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9812 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9813 814624e7 2022-03-22 thomas int file_renamed = 0;
9814 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9815 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9817 814624e7 2022-03-22 thomas *oldpath = NULL;
9818 814624e7 2022-03-22 thomas *newpath = NULL;
9820 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9821 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9822 814624e7 2022-03-22 thomas if (err)
9823 814624e7 2022-03-22 thomas goto done;
9825 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9826 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9827 814624e7 2022-03-22 thomas if (err)
9828 814624e7 2022-03-22 thomas goto done;
9830 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9831 814624e7 2022-03-22 thomas file_renamed = 1;
9833 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9834 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9835 814624e7 2022-03-22 thomas else if (file_renamed) {
9836 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9837 814624e7 2022-03-22 thomas if (err == NULL)
9838 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9839 814624e7 2022-03-22 thomas } else if (old == NULL)
9840 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9842 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9845 814624e7 2022-03-22 thomas if (err) {
9846 814624e7 2022-03-22 thomas free(*oldpath);
9847 814624e7 2022-03-22 thomas *oldpath = NULL;
9848 814624e7 2022-03-22 thomas free(*newpath);
9849 814624e7 2022-03-22 thomas *newpath = NULL;
9851 814624e7 2022-03-22 thomas return err;
9854 814624e7 2022-03-22 thomas const struct got_error *
9855 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9856 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9857 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9859 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9861 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9862 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9863 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9864 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9865 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9866 5e22b737 2022-05-31 thomas saa.repo = repo;
9868 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9869 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9872 5e22b737 2022-05-31 thomas const struct got_error *
9873 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9874 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9875 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9877 8ec7352a 2023-06-01 thomas const struct got_error *err;
9878 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9879 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
9881 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9882 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9883 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9884 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9885 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9886 5e22b737 2022-05-31 thomas sda.repo = repo;
9887 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9888 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9889 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9890 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9891 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s/%s",
9892 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree), path) == -1)
9893 8ec7352a 2023-06-01 thomas return got_error_from_errno("asprintf");
9894 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
9895 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
9897 8ec7352a 2023-06-01 thomas err = worktree_status(worktree, path, fileindex, repo,
9898 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9899 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
9900 8ec7352a 2023-06-01 thomas return err;
9903 5e22b737 2022-05-31 thomas const struct got_error *
9904 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9905 36832a8e 2022-05-31 thomas const char *fileindex_path)
9907 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9909 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9910 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9912 5e22b737 2022-05-31 thomas return err;