2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
20 d1f6d47b 2019-02-04 stsp #include <dirent.h>
21 12ce7a6c 2019-08-12 stsp #include <limits.h>
22 f5c49f82 2019-01-06 stsp #include <stddef.h>
23 86c3caaf 2018-03-09 stsp #include <string.h>
24 86c3caaf 2018-03-09 stsp #include <stdio.h>
25 86c3caaf 2018-03-09 stsp #include <stdlib.h>
26 303e14b5 2019-09-23 stsp #include <time.h>
27 86c3caaf 2018-03-09 stsp #include <fcntl.h>
28 86c3caaf 2018-03-09 stsp #include <errno.h>
29 86c3caaf 2018-03-09 stsp #include <unistd.h>
30 9d31a1d8 2018-03-11 stsp #include <zlib.h>
31 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
32 512f0d0e 2019-01-02 stsp #include <libgen.h>
34 dd038bc6 2021-09-21 thomas.ad #include "got_compat.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 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.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 99724ed4 2018-03-10 stsp static const struct got_error *
66 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
68 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
71 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
72 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
74 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
79 507dc3bb 2018-12-29 stsp static const struct got_error *
80 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
82 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
83 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
84 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
85 507dc3bb 2018-12-29 stsp char *path = NULL;
87 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
88 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
89 507dc3bb 2018-12-29 stsp path = NULL;
93 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
97 507dc3bb 2018-12-29 stsp if (content) {
98 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
99 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
100 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
105 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
106 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
107 2a57020b 2019-02-20 stsp unlink(tmppath);
112 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
113 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
114 230a42bd 2019-05-11 jcs free(tmppath);
115 09fe317a 2018-03-11 stsp return err;
118 024e9686 2019-05-14 stsp static const struct got_error *
119 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
121 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
122 024e9686 2019-05-14 stsp char *refstr = NULL;
124 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
125 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
126 024e9686 2019-05-14 stsp if (refstr == NULL)
127 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
129 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
130 024e9686 2019-05-14 stsp if (refstr == NULL)
131 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
133 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
134 024e9686 2019-05-14 stsp free(refstr);
135 024e9686 2019-05-14 stsp return err;
138 86c3caaf 2018-03-09 stsp const struct got_error *
139 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
140 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
142 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
143 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
144 ec22038e 2019-03-10 stsp uuid_t uuid;
145 ec22038e 2019-03-10 stsp uint32_t uuid_status;
146 65596e15 2018-12-24 stsp int obj_type;
147 7ac97322 2018-03-11 stsp char *path_got = NULL;
148 1451e70d 2018-03-10 stsp char *formatstr = NULL;
149 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
150 65596e15 2018-12-24 stsp char *basestr = NULL;
151 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
153 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
154 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
158 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
160 65596e15 2018-12-24 stsp return err;
161 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
163 65596e15 2018-12-24 stsp return err;
164 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
165 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
167 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
168 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
169 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
172 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
173 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
174 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
178 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
179 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
180 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
183 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
184 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
188 056e7441 2018-03-11 stsp /* Create an empty lock file. */
189 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
193 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
194 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
198 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
199 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
203 65596e15 2018-12-24 stsp /* Record our base commit. */
204 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
207 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
211 1451e70d 2018-03-10 stsp /* Store path to repository. */
212 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
213 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
217 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
218 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
219 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
223 ec22038e 2019-03-10 stsp /* Generate UUID. */
224 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
225 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
226 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
229 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
230 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
231 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
234 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
238 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
239 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
240 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
243 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
248 65596e15 2018-12-24 stsp free(commit_id);
249 7ac97322 2018-03-11 stsp free(path_got);
250 1451e70d 2018-03-10 stsp free(formatstr);
251 0bb8a95e 2018-03-12 stsp free(absprefix);
252 65596e15 2018-12-24 stsp free(basestr);
253 ec22038e 2019-03-10 stsp free(uuidstr);
254 86c3caaf 2018-03-09 stsp return err;
257 247140b2 2019-02-05 stsp const struct got_error *
258 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
259 e5dc7198 2018-12-29 stsp const char *path_prefix)
261 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
263 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
264 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
265 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
267 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
268 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
269 e5dc7198 2018-12-29 stsp free(absprefix);
270 e5dc7198 2018-12-29 stsp return NULL;
273 bc70eb79 2019-05-09 stsp const char *
274 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
276 36a38700 2019-05-10 stsp return worktree->head_ref_name;
279 024e9686 2019-05-14 stsp const struct got_error *
280 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
281 024e9686 2019-05-14 stsp struct got_reference *head_ref)
283 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
284 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
286 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
287 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
288 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
289 024e9686 2019-05-14 stsp path_got = NULL;
293 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
294 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
295 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
299 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
303 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
304 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
306 024e9686 2019-05-14 stsp free(path_got);
308 024e9686 2019-05-14 stsp free(head_ref_name);
309 024e9686 2019-05-14 stsp return err;
312 b72f483a 2019-02-05 stsp struct got_object_id *
313 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
315 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
318 507dc3bb 2018-12-29 stsp const struct got_error *
319 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
320 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
322 507dc3bb 2018-12-29 stsp const struct got_error *err;
323 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
324 507dc3bb 2018-12-29 stsp char *id_str = NULL;
325 507dc3bb 2018-12-29 stsp char *path_got = NULL;
327 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
328 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
329 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
330 507dc3bb 2018-12-29 stsp path_got = NULL;
334 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
336 507dc3bb 2018-12-29 stsp return err;
338 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
339 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
343 507dc3bb 2018-12-29 stsp /* Record our base commit. */
344 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
347 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
351 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
352 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
353 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
354 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
359 507dc3bb 2018-12-29 stsp got_object_close(obj);
360 507dc3bb 2018-12-29 stsp free(id_str);
361 507dc3bb 2018-12-29 stsp free(path_got);
362 507dc3bb 2018-12-29 stsp return err;
365 50b0790e 2020-09-11 stsp const struct got_gotconfig *
366 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
368 50b0790e 2020-09-11 stsp return worktree->gotconfig;
371 9d31a1d8 2018-03-11 stsp static const struct got_error *
372 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
374 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
375 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
376 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
377 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
378 86c3caaf 2018-03-09 stsp return NULL;
381 21908da4 2019-01-13 stsp static const struct got_error *
382 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
384 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
385 4a1ddfc2 2019-01-12 stsp char *abspath;
387 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
388 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
390 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
391 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
392 ddcd8544 2019-03-11 stsp struct stat sb;
393 ddcd8544 2019-03-11 stsp err = NULL;
394 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
395 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
396 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
397 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
398 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
401 4a1ddfc2 2019-01-12 stsp free(abspath);
402 68c76935 2019-02-19 stsp return err;
405 68c76935 2019-02-19 stsp static const struct got_error *
406 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
408 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
409 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
410 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
411 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
416 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
417 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
418 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
421 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
422 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
423 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
426 68c76935 2019-02-19 stsp if (flen1 == 0) {
427 68c76935 2019-02-19 stsp if (flen2 != 0)
430 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
431 68c76935 2019-02-19 stsp if (flen1 != 0)
434 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
435 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
445 68c76935 2019-02-19 stsp return err;
448 68c76935 2019-02-19 stsp static const struct got_error *
449 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
451 68c76935 2019-02-19 stsp struct stat sb;
452 68c76935 2019-02-19 stsp size_t size1, size2;
456 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
457 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
458 68c76935 2019-02-19 stsp size1 = sb.st_size;
460 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
461 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
462 68c76935 2019-02-19 stsp size2 = sb.st_size;
464 68c76935 2019-02-19 stsp if (size1 != size2) {
466 68c76935 2019-02-19 stsp return NULL;
469 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
470 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
471 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
472 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
474 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
477 24f136e0 2021-11-20 thomas static const struct got_error *
478 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
480 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
481 24f136e0 2021-11-20 thomas size_t flen;
482 24f136e0 2021-11-20 thomas ssize_t outlen;
484 24f136e0 2021-11-20 thomas *outsize = 0;
486 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
487 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
489 24f136e0 2021-11-20 thomas for (;;) {
490 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
491 24f136e0 2021-11-20 thomas if (flen == 0) {
492 24f136e0 2021-11-20 thomas if (ferror(f))
493 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
494 24f136e0 2021-11-20 thomas if (feof(f))
497 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
498 24f136e0 2021-11-20 thomas if (outlen == -1)
499 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
500 24f136e0 2021-11-20 thomas if (outlen != flen)
501 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
502 24f136e0 2021-11-20 thomas *outsize += outlen;
505 24f136e0 2021-11-20 thomas return NULL;
508 24f136e0 2021-11-20 thomas static const struct got_error *
509 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
510 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
511 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
512 24f136e0 2021-11-20 thomas const char *ondisk_path)
514 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
515 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
516 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
517 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
518 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
519 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
520 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
522 24f136e0 2021-11-20 thomas *overlapcnt = 0;
524 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
526 24f136e0 2021-11-20 thomas return err;
528 24f136e0 2021-11-20 thomas if (same_content)
529 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
531 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
533 24f136e0 2021-11-20 thomas return err;
534 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
535 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
537 24f136e0 2021-11-20 thomas return err;
538 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
540 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
541 24f136e0 2021-11-20 thomas *overlapcnt = 1;
542 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
543 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
544 24f136e0 2021-11-20 thomas goto done;
546 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
547 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
548 24f136e0 2021-11-20 thomas goto done;
550 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
551 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
552 24f136e0 2021-11-20 thomas goto done;
554 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
555 24f136e0 2021-11-20 thomas base_path_orig);
557 24f136e0 2021-11-20 thomas goto done;
558 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
559 24f136e0 2021-11-20 thomas base_path_deriv);
561 24f136e0 2021-11-20 thomas goto done;
562 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
563 24f136e0 2021-11-20 thomas base_path_deriv2);
565 24f136e0 2021-11-20 thomas goto done;
566 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
568 24f136e0 2021-11-20 thomas goto done;
569 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
571 24f136e0 2021-11-20 thomas goto done;
572 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
574 24f136e0 2021-11-20 thomas goto done;
575 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
576 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
577 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
578 24f136e0 2021-11-20 thomas goto done;
580 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
581 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
582 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
583 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
584 24f136e0 2021-11-20 thomas path_deriv) < 0) {
585 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
586 24f136e0 2021-11-20 thomas goto done;
588 24f136e0 2021-11-20 thomas if (size_orig > 0) {
589 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
590 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
591 24f136e0 2021-11-20 thomas label_orig ? " " : "",
592 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
593 24f136e0 2021-11-20 thomas path_orig) < 0) {
594 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
595 24f136e0 2021-11-20 thomas goto done;
598 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
599 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
600 24f136e0 2021-11-20 thomas path_deriv2,
601 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
602 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
603 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
604 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
605 24f136e0 2021-11-20 thomas goto done;
607 24f136e0 2021-11-20 thomas } else if (changed_deriv)
608 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
609 24f136e0 2021-11-20 thomas else if (changed_deriv2)
610 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
612 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
613 24f136e0 2021-11-20 thomas err == NULL)
614 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
615 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
616 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
617 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
618 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
619 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
620 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
621 24f136e0 2021-11-20 thomas free(path_orig);
622 24f136e0 2021-11-20 thomas free(path_deriv);
623 24f136e0 2021-11-20 thomas free(path_deriv2);
624 24f136e0 2021-11-20 thomas free(base_path_orig);
625 24f136e0 2021-11-20 thomas free(base_path_deriv);
626 24f136e0 2021-11-20 thomas free(base_path_deriv2);
627 24f136e0 2021-11-20 thomas return err;
631 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
632 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
633 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
634 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
635 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
637 6353ad76 2019-02-08 stsp static const struct got_error *
638 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
639 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
640 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
641 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
642 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
643 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
645 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
646 6353ad76 2019-02-08 stsp int merged_fd = -1;
647 db590691 2021-06-02 stsp FILE *f_merged = NULL;
648 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
649 234035bc 2019-06-01 stsp int overlapcnt = 0;
650 3524bbf9 2020-10-20 stsp char *parent = NULL;
652 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
654 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
656 3524bbf9 2020-10-20 stsp return err;
658 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
659 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
663 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
667 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
668 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
669 24f136e0 2021-11-20 thomas if (err) {
670 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
671 24f136e0 2021-11-20 thomas goto done;
672 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
673 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
674 24f136e0 2021-11-20 thomas ondisk_path);
676 24f136e0 2021-11-20 thomas goto done;
679 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
680 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
684 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
685 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
689 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
690 db590691 2021-06-02 stsp if (f_merged == NULL) {
691 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
694 db590691 2021-06-02 stsp merged_fd = -1;
696 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
697 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
698 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
704 db590691 2021-06-02 stsp if (fchmod(fileno(f_merged), st_mode) != 0) {
705 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
709 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
710 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
711 230a42bd 2019-05-11 jcs ondisk_path);
716 fdcb7daf 2019-12-15 stsp if (merged_path)
717 fdcb7daf 2019-12-15 stsp unlink(merged_path);
719 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
720 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
721 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
722 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
723 6353ad76 2019-02-08 stsp free(merged_path);
724 af54ae4a 2019-02-19 stsp free(base_path);
725 3524bbf9 2020-10-20 stsp free(parent);
726 af57b12a 2020-07-23 stsp return err;
729 af57b12a 2020-07-23 stsp static const struct got_error *
730 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
731 af57b12a 2020-07-23 stsp size_t target_len)
733 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
734 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
735 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
736 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
737 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
738 af57b12a 2020-07-23 stsp ondisk_path);
739 af57b12a 2020-07-23 stsp return NULL;
743 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
744 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
745 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
746 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
748 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
749 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
751 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
752 993e2a1b 2020-07-23 stsp * has deleted this symlink.
754 11cc08c1 2020-07-23 stsp static const struct got_error *
755 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
756 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
757 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
759 11cc08c1 2020-07-23 stsp const struct got_error *err;
760 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
761 11cc08c1 2020-07-23 stsp FILE *f = NULL;
763 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
765 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
767 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
768 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
769 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
773 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
775 3818e3c4 2020-11-01 naddy goto done;
777 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
778 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
782 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
783 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
784 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
785 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
786 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
787 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
788 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
789 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
790 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
791 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
795 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
796 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
799 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
800 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
804 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
805 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
806 11cc08c1 2020-07-23 stsp free(path);
807 11cc08c1 2020-07-23 stsp free(id_str);
808 11cc08c1 2020-07-23 stsp free(label_deriv);
809 11cc08c1 2020-07-23 stsp return err;
812 d219f183 2020-07-23 stsp /* forward declaration */
813 d219f183 2020-07-23 stsp static const struct got_error *
814 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
815 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
816 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
817 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
820 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
821 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
822 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
823 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
825 af57b12a 2020-07-23 stsp static const struct got_error *
826 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
827 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
828 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
829 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
830 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
832 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
833 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
834 af57b12a 2020-07-23 stsp struct stat sb;
835 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
836 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
837 11cc08c1 2020-07-23 stsp int have_local_change = 0;
838 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
840 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
841 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
843 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
844 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
845 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
846 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
847 af57b12a 2020-07-23 stsp ondisk_path);
850 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
852 526a746f 2020-07-23 stsp if (blob_orig) {
853 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
858 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
859 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
860 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
861 11cc08c1 2020-07-23 stsp have_local_change = 1;
863 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
864 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
865 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
866 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
867 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
869 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
870 11cc08c1 2020-07-23 stsp if (ancestor_target) {
871 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
872 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
874 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
875 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
876 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
877 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
881 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
882 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
883 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
886 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
890 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
891 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
892 af57b12a 2020-07-23 stsp strlen(deriv_target));
895 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
896 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
897 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
898 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
899 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
900 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
904 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
905 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
908 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
914 af57b12a 2020-07-23 stsp free(ancestor_target);
915 eec2f5a9 2021-06-03 stsp return err;
918 eec2f5a9 2021-06-03 stsp static const struct got_error *
919 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
921 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
922 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
923 eec2f5a9 2021-06-03 stsp ssize_t target_len;
927 eec2f5a9 2021-06-03 stsp *outfile = NULL;
929 eec2f5a9 2021-06-03 stsp f = got_opentemp();
930 eec2f5a9 2021-06-03 stsp if (f == NULL)
931 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
932 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
933 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
934 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
937 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
938 eec2f5a9 2021-06-03 stsp if (n != target_len) {
939 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
942 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
943 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
946 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
947 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
954 eec2f5a9 2021-06-03 stsp *outfile = f;
955 14c901f1 2019-08-08 stsp return err;
959 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
960 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
961 14c901f1 2019-08-08 stsp * acts as the second derived version.
963 14c901f1 2019-08-08 stsp static const struct got_error *
964 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
965 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
966 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
967 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
968 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
969 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
971 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
972 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
973 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
974 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
975 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
977 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
979 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
981 ed6b5030 2020-10-20 stsp return err;
983 67a66647 2021-05-31 stsp if (blob_orig) {
984 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
985 67a66647 2021-05-31 stsp parent) == -1) {
986 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
987 67a66647 2021-05-31 stsp base_path = NULL;
991 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
994 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
995 67a66647 2021-05-31 stsp blob_orig);
998 67a66647 2021-05-31 stsp free(base_path);
1001 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1002 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1003 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1005 db590691 2021-06-02 stsp f_orig = got_opentemp();
1006 db590691 2021-06-02 stsp if (f_orig == NULL) {
1007 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1008 db590691 2021-06-02 stsp goto done;
1012 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1013 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1014 14c901f1 2019-08-08 stsp base_path = NULL;
1015 14c901f1 2019-08-08 stsp goto done;
1018 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1020 14c901f1 2019-08-08 stsp goto done;
1021 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1022 14c901f1 2019-08-08 stsp blob_deriv);
1024 14c901f1 2019-08-08 stsp goto done;
1026 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1028 14c901f1 2019-08-08 stsp goto done;
1029 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1030 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1031 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1032 14c901f1 2019-08-08 stsp goto done;
1036 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1037 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1039 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1040 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1042 eec2f5a9 2021-06-03 stsp goto done;
1045 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1046 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1047 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1048 eec2f5a9 2021-06-03 stsp goto done;
1050 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1051 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1052 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1053 eec2f5a9 2021-06-03 stsp close(fd);
1054 eec2f5a9 2021-06-03 stsp goto done;
1058 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1059 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1060 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1062 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1063 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1064 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1065 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1066 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1067 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1068 14c901f1 2019-08-08 stsp free(base_path);
1069 67a66647 2021-05-31 stsp if (blob_orig_path) {
1070 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1071 67a66647 2021-05-31 stsp free(blob_orig_path);
1073 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1074 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1075 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1077 6353ad76 2019-02-08 stsp free(id_str);
1078 818c7501 2019-07-11 stsp free(label_deriv);
1079 ed6b5030 2020-10-20 stsp free(parent);
1080 4a1ddfc2 2019-01-12 stsp return err;
1083 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1084 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1085 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1086 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1088 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1089 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1091 65b05cec 2020-07-23 stsp *new_iep = NULL;
1093 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1095 054041d0 2020-06-24 stsp return err;
1097 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1098 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1100 054041d0 2020-06-24 stsp goto done;
1102 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1105 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1107 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1108 13d9040b 2019-03-27 stsp return err;
1111 1ebedb77 2019-10-19 stsp static mode_t
1112 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1114 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1116 1ebedb77 2019-10-19 stsp if (executable) {
1117 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1118 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1119 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1120 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1121 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1122 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1125 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1128 8ba819a3 2020-07-23 stsp /* forward declaration */
1129 13d9040b 2019-03-27 stsp static const struct got_error *
1130 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1131 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1132 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1133 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1134 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1135 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1138 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1139 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1141 8ba819a3 2020-07-23 stsp static const struct got_error *
1142 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1143 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1145 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1146 5a1fbc73 2020-07-23 stsp ssize_t elen;
1147 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1150 c6e8a826 2021-04-05 stsp *did_something = 0;
1153 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1154 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1155 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1156 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1157 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1158 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1160 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1161 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1162 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1163 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1165 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1166 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1167 5a1fbc73 2020-07-23 stsp if (elen == -1)
1168 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1170 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1171 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1172 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1175 c6e8a826 2021-04-05 stsp *did_something = 1;
1176 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1177 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1178 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1179 5a1fbc73 2020-07-23 stsp return err;
1182 3c1ec782 2020-07-23 stsp static const struct got_error *
1183 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1184 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1186 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1187 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1188 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1190 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1192 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1193 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1194 3c1ec782 2020-07-23 stsp return NULL;
1198 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1199 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1200 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1201 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1202 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1204 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1205 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1206 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1208 ce031e9e 2020-10-20 stsp return err;
1209 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1210 ce031e9e 2020-10-20 stsp free(parent);
1211 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1213 ce031e9e 2020-10-20 stsp free(parent);
1214 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1215 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1216 3c1ec782 2020-07-23 stsp free(abspath);
1217 3c1ec782 2020-07-23 stsp return err;
1219 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1220 3c1ec782 2020-07-23 stsp free(abspath);
1222 3c1ec782 2020-07-23 stsp return err;
1224 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1226 3c1ec782 2020-07-23 stsp return err;
1229 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1230 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1231 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1232 3c1ec782 2020-07-23 stsp return NULL;
1235 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1236 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1237 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1238 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1239 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1240 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1242 3c1ec782 2020-07-23 stsp free(path_got);
1243 3c1ec782 2020-07-23 stsp return NULL;
1246 5a1fbc73 2020-07-23 stsp static const struct got_error *
1247 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1248 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1249 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1250 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1251 ace90326 2021-09-27 thomas struct got_repository *repo,
1252 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1254 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1255 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1256 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1257 906c123b 2020-07-23 stsp char *path_got = NULL;
1258 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1259 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1261 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1264 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1265 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1266 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1267 8ba819a3 2020-07-23 stsp * in the blob object.
1270 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1271 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1272 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1273 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1274 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1275 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1276 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1277 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1278 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1279 3b9f0f87 2020-07-23 stsp progress_arg);
1281 8ba819a3 2020-07-23 stsp if (len > 0) {
1282 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1283 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1284 8ba819a3 2020-07-23 stsp len - hdrlen);
1285 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1286 8ba819a3 2020-07-23 stsp hdrlen = 0;
1288 8ba819a3 2020-07-23 stsp } while (len != 0);
1289 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1291 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1292 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1294 3c1ec782 2020-07-23 stsp return err;
1296 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1297 906c123b 2020-07-23 stsp /* install as a regular file */
1298 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1299 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1300 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1301 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1302 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1303 906c123b 2020-07-23 stsp goto done;
1306 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1307 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1308 c6e8a826 2021-04-05 stsp int symlink_replaced;
1309 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1310 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1311 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1312 c90c8ce3 2020-07-23 stsp goto done;
1314 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1315 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1317 f35fa46a 2020-07-23 stsp goto done;
1318 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1319 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1320 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1321 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1322 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1323 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1325 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1326 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1329 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1332 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1333 f4994adc 2020-10-20 stsp char *parent;
1334 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1336 f4994adc 2020-10-20 stsp goto done;
1337 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1338 f4994adc 2020-10-20 stsp free(parent);
1340 f35fa46a 2020-07-23 stsp goto done;
1342 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1343 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1345 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1346 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1347 f35fa46a 2020-07-23 stsp goto done;
1351 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1352 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1353 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1354 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1355 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1356 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1357 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1358 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1359 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1360 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1361 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1362 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1363 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1365 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1366 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1368 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1369 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1370 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1372 906c123b 2020-07-23 stsp free(path_got);
1373 8ba819a3 2020-07-23 stsp return err;
1376 8ba819a3 2020-07-23 stsp static const struct got_error *
1377 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1378 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1379 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1380 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1381 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1382 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1384 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1385 507dc3bb 2018-12-29 stsp int fd = -1;
1386 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1387 507dc3bb 2018-12-29 stsp int update = 0;
1388 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1390 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1391 06340621 2021-12-31 thomas O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1392 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1393 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1394 f5375317 2020-10-20 stsp char *parent;
1395 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1397 f5375317 2020-10-20 stsp return err;
1398 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1399 f5375317 2020-10-20 stsp free(parent);
1401 21908da4 2019-01-13 stsp return err;
1402 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1403 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1404 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1405 21908da4 2019-01-13 stsp if (fd == -1)
1406 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1407 230a42bd 2019-05-11 jcs ondisk_path);
1408 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1409 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1410 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1411 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1412 3b9f0f87 2020-07-23 stsp goto done;
1414 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1415 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1416 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1417 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1418 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1419 507dc3bb 2018-12-29 stsp goto done;
1421 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1422 507dc3bb 2018-12-29 stsp ondisk_path);
1424 507dc3bb 2018-12-29 stsp goto done;
1425 507dc3bb 2018-12-29 stsp update = 1;
1428 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1431 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1432 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1433 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1434 3818e3c4 2020-11-01 naddy goto done;
1437 bd6aa359 2020-07-23 stsp if (progress_cb) {
1438 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1439 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1441 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1442 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1445 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1446 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1448 bd6aa359 2020-07-23 stsp goto done;
1451 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1453 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1454 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1457 9d31a1d8 2018-03-11 stsp if (len > 0) {
1458 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1459 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1460 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1461 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1462 b87c6f83 2018-12-24 stsp goto done;
1463 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1464 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1465 b87c6f83 2018-12-24 stsp goto done;
1467 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1469 9d31a1d8 2018-03-11 stsp } while (len != 0);
1471 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1472 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1473 816dc654 2019-02-16 stsp goto done;
1476 507dc3bb 2018-12-29 stsp if (update) {
1477 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1478 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1479 f6d8c0ac 2020-11-09 stsp goto done;
1481 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1482 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1483 230a42bd 2019-05-11 jcs ondisk_path);
1484 68ed9ba5 2019-02-10 stsp goto done;
1486 63df146d 2020-11-09 stsp free(tmppath);
1487 63df146d 2020-11-09 stsp tmppath = NULL;
1491 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1492 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1493 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1494 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1495 507dc3bb 2018-12-29 stsp free(tmppath);
1496 6353ad76 2019-02-08 stsp return err;
1499 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1500 6353ad76 2019-02-08 stsp static const struct got_error *
1501 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1503 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1504 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1505 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1506 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1507 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1509 7154f6ce 2019-03-27 stsp int i = 0;
1510 9bdd68dd 2020-01-02 naddy char *line = NULL;
1511 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1512 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1514 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1515 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1516 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1517 7154f6ce 2019-03-27 stsp if (feof(f))
1519 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1523 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1524 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1526 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1531 9bdd68dd 2020-01-02 naddy free(line);
1533 7154f6ce 2019-03-27 stsp return err;
1536 e2b1e152 2019-07-13 stsp static int
1537 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1539 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1540 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1543 1ebedb77 2019-10-19 stsp static int
1544 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1546 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1547 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1548 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1549 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1550 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1551 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1554 c363b2c1 2019-08-03 stsp static unsigned char
1555 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1557 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1558 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1559 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1560 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1561 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1562 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1563 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1565 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1569 a919d5c4 2020-07-23 stsp static const struct got_error *
1570 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1571 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1572 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1574 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1575 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1576 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1577 a919d5c4 2020-07-23 stsp ssize_t elen;
1578 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1579 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1580 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1582 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1584 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1586 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1588 a919d5c4 2020-07-23 stsp return err;
1589 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1591 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1592 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1594 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1596 a919d5c4 2020-07-23 stsp if (len > 0) {
1597 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1598 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1599 a919d5c4 2020-07-23 stsp len - hdrlen);
1600 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1601 a919d5c4 2020-07-23 stsp hdrlen = 0;
1603 a919d5c4 2020-07-23 stsp } while (len != 0);
1604 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1606 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1607 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1608 a919d5c4 2020-07-23 stsp if (elen == -1)
1609 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1611 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1612 a919d5c4 2020-07-23 stsp if (elen == -1)
1613 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1616 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1617 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1619 a919d5c4 2020-07-23 stsp return NULL;
1622 7154f6ce 2019-03-27 stsp static const struct got_error *
1623 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1624 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1625 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1627 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1628 6353ad76 2019-02-08 stsp struct got_object_id id;
1629 6353ad76 2019-02-08 stsp size_t hdrlen;
1630 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1631 6353ad76 2019-02-08 stsp FILE *f = NULL;
1632 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1633 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1634 6353ad76 2019-02-08 stsp size_t flen, blen;
1635 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1637 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1638 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1641 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1642 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1643 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1645 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1646 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1647 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1648 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1649 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1651 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1652 882ef1b9 2019-12-13 stsp goto done;
1654 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1655 3d35a492 2019-12-13 stsp goto done;
1658 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1659 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1660 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1661 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1662 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1663 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1664 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1665 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1666 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1667 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1668 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1670 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1671 3d35a492 2019-12-13 stsp goto done;
1673 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1674 1338848f 2019-12-13 stsp goto done;
1678 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1679 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1680 1338848f 2019-12-13 stsp goto done;
1683 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1684 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1685 1338848f 2019-12-13 stsp goto done;
1686 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1687 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1688 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1689 1338848f 2019-12-13 stsp goto done;
1692 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1693 f179e66d 2020-07-23 stsp goto done;
1695 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1696 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1697 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1698 1338848f 2019-12-13 stsp goto done;
1701 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1702 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1703 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1705 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1707 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1708 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1709 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1710 f4ae6ddb 2022-07-01 thomas goto done;
1712 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1714 1338848f 2019-12-13 stsp goto done;
1716 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1717 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1718 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1719 a919d5c4 2020-07-23 stsp goto done;
1722 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1723 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1724 ab0d4361 2019-12-13 stsp if (fd == -1) {
1725 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1726 ab0d4361 2019-12-13 stsp goto done;
1730 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1731 6353ad76 2019-02-08 stsp if (f == NULL) {
1732 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1733 6353ad76 2019-02-08 stsp goto done;
1736 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1738 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1739 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1741 7154f6ce 2019-03-27 stsp goto done;
1742 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1743 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1744 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1745 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1746 7154f6ce 2019-03-27 stsp goto done;
1748 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1749 6353ad76 2019-02-08 stsp if (flen != 0)
1750 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1752 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1753 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1754 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1756 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1757 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1758 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1759 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1763 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1766 6353ad76 2019-02-08 stsp hdrlen = 0;
1769 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1770 7154f6ce 2019-03-27 stsp rewind(f);
1771 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1772 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1773 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1775 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1776 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1778 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1779 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1780 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1781 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1782 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1783 9d31a1d8 2018-03-11 stsp return err;
1787 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1788 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1790 e2b1e152 2019-07-13 stsp static const struct got_error *
1791 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1792 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1794 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1795 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1796 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1798 e2b1e152 2019-07-13 stsp return NULL;
1801 9d31a1d8 2018-03-11 stsp static const struct got_error *
1802 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1803 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1804 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1805 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1806 0584f854 2019-04-06 stsp void *progress_arg)
1808 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1809 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1810 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1811 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1812 b8f41171 2019-02-10 stsp struct stat sb;
1813 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1815 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1816 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1819 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1820 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1821 a76c42e6 2019-08-03 stsp goto done;
1823 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1826 abb4604f 2019-07-27 stsp goto done;
1827 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1828 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1830 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1831 e6f4ba31 2021-09-24 thomas if (errno != ENOENT) {
1832 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1833 e6f4ba31 2021-09-24 thomas ondisk_path);
1834 e6f4ba31 2021-09-24 thomas goto done;
1836 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1837 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1838 e6f4ba31 2021-09-24 thomas } else {
1839 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1840 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1842 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1846 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1848 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1849 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1850 b8f41171 2019-02-10 stsp goto done;
1852 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1854 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1855 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1857 5036ab18 2020-04-18 stsp goto done;
1860 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1861 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1862 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1864 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1865 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1866 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1867 c6e8a826 2021-04-05 stsp * updating contents of this file.
1869 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1870 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1871 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1872 c6e8a826 2021-04-05 stsp /* Same commit. */
1873 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1874 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1876 e2b1e152 2019-07-13 stsp goto done;
1877 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1879 6353ad76 2019-02-08 stsp goto done;
1881 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1882 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1883 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1884 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1885 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1886 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1888 0f58026f 2021-04-05 stsp goto done;
1889 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1891 b8f41171 2019-02-10 stsp goto done;
1895 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1896 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1897 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1898 f4ae6ddb 2022-07-01 thomas goto done;
1900 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1902 6353ad76 2019-02-08 stsp goto done;
1904 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1905 234035bc 2019-06-01 stsp int update_timestamps;
1906 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1907 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1908 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1909 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
1910 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
1911 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1912 f4ae6ddb 2022-07-01 thomas goto done;
1914 234035bc 2019-06-01 stsp struct got_object_id id2;
1915 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1916 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1919 f69721c3 2019-10-21 stsp goto done;
1921 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1922 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1923 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1924 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1925 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1926 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1927 f69721c3 2019-10-21 stsp goto done;
1929 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1930 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1931 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1932 234035bc 2019-06-01 stsp goto done;
1935 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1936 36bf999c 2020-07-23 stsp char *link_target;
1937 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1939 36bf999c 2020-07-23 stsp goto done;
1940 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1941 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1942 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1943 36bf999c 2020-07-23 stsp free(link_target);
1945 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1946 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1947 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1948 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1950 f69721c3 2019-10-21 stsp free(label_orig);
1951 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1952 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1953 f4ae6ddb 2022-07-01 thomas goto done;
1955 234035bc 2019-06-01 stsp if (blob2)
1956 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1958 909d120e 2019-09-22 stsp goto done;
1960 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1961 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1962 234035bc 2019-06-01 stsp * unmodified files again.
1964 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1965 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1966 234035bc 2019-06-01 stsp update_timestamps);
1967 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1968 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1969 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1970 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1971 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1973 1ee397ad 2019-07-12 stsp goto done;
1974 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1975 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1977 13d9040b 2019-03-27 stsp goto done;
1979 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1980 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1981 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1982 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1983 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1984 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
1985 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
1987 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1988 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
1989 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
1990 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1991 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1994 13d9040b 2019-03-27 stsp goto done;
1997 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
1998 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
1999 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2001 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2002 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2003 054041d0 2020-06-24 stsp &blob->id);
2006 13d9040b 2019-03-27 stsp goto done;
2008 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2009 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2010 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2014 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2015 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2016 f4ae6ddb 2022-07-01 thomas goto done;
2018 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2020 6353ad76 2019-02-08 stsp free(ondisk_path);
2021 8da9e5f4 2019-01-12 stsp return err;
2024 90285c3b 2019-01-08 stsp static const struct got_error *
2025 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2027 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2028 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2030 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2031 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2033 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2034 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2035 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2037 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2038 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2040 1c4cdd89 2021-06-20 stsp goto done;
2041 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2042 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2043 fddefe3b 2020-10-20 stsp free(ondisk_path);
2044 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2045 1c4cdd89 2021-06-20 stsp parent = NULL;
2046 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2047 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2048 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2049 fddefe3b 2020-10-20 stsp ondisk_path);
2052 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2058 90285c3b 2019-01-08 stsp free(ondisk_path);
2059 1c4cdd89 2021-06-20 stsp free(parent);
2060 90285c3b 2019-01-08 stsp return err;
2063 708d8e67 2019-03-27 stsp static const struct got_error *
2064 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2065 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2066 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2068 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2069 708d8e67 2019-03-27 stsp unsigned char status;
2070 708d8e67 2019-03-27 stsp struct stat sb;
2071 708d8e67 2019-03-27 stsp char *ondisk_path;
2073 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2074 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2076 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2078 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2080 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2082 5a58a424 2020-06-23 stsp goto done;
2084 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2085 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2086 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2087 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2088 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2089 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2090 993e2a1b 2020-07-23 stsp goto done;
2092 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2093 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2094 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2095 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2097 993e2a1b 2020-07-23 stsp goto done;
2098 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2099 993e2a1b 2020-07-23 stsp ie->path);
2100 993e2a1b 2020-07-23 stsp goto done;
2103 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2104 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2105 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2107 5a58a424 2020-06-23 stsp goto done;
2109 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2110 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2112 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2113 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2115 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2117 5a58a424 2020-06-23 stsp goto done;
2118 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2119 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2121 5a58a424 2020-06-23 stsp goto done;
2123 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2126 5a58a424 2020-06-23 stsp free(ondisk_path);
2127 fc6346c4 2019-03-27 stsp return err;
2130 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2131 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2132 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2133 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2134 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2135 8da9e5f4 2019-01-12 stsp void *progress_arg;
2136 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2137 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2140 512f0d0e 2019-01-02 stsp static const struct got_error *
2141 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2142 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2144 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2146 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2147 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2149 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2150 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2153 8da9e5f4 2019-01-12 stsp static const struct got_error *
2154 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2156 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2158 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2159 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2161 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2162 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2165 9d31a1d8 2018-03-11 stsp static const struct got_error *
2166 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2168 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2169 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2170 8da9e5f4 2019-01-12 stsp char *path;
2172 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2173 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2175 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2176 63c5ca5d 2019-08-24 stsp return NULL;
2178 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2179 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2181 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2183 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2184 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2186 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2187 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2189 8da9e5f4 2019-01-12 stsp free(path);
2190 0cd1c46a 2019-03-11 stsp return err;
2193 b2118c49 2020-07-28 stsp const struct got_error *
2194 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2196 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2198 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2199 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2200 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2201 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2204 b2118c49 2020-07-28 stsp return NULL;
2207 818c7501 2019-07-11 stsp static const struct got_error *
2208 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2210 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2211 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2213 517bab73 2019-03-11 stsp *refname = NULL;
2215 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2217 b2118c49 2020-07-28 stsp return err;
2219 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2220 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2221 0647c563 2019-03-11 stsp *refname = NULL;
2223 517bab73 2019-03-11 stsp free(uuidstr);
2224 517bab73 2019-03-11 stsp return err;
2227 818c7501 2019-07-11 stsp const struct got_error *
2228 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2230 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2233 818c7501 2019-07-11 stsp static const struct got_error *
2234 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2236 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2237 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2240 818c7501 2019-07-11 stsp static const struct got_error *
2241 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2243 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2246 818c7501 2019-07-11 stsp static const struct got_error *
2247 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2249 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2250 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2253 818c7501 2019-07-11 stsp static const struct got_error *
2254 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2256 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2257 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2260 0ebf8283 2019-07-24 stsp static const struct got_error *
2261 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2263 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2264 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2267 0ebf8283 2019-07-24 stsp static const struct got_error *
2268 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2270 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2271 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2274 0ebf8283 2019-07-24 stsp static const struct got_error *
2275 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2277 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2278 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2281 0ebf8283 2019-07-24 stsp static const struct got_error *
2282 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2284 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2285 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2288 0ebf8283 2019-07-24 stsp const struct got_error *
2289 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2290 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2292 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2293 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2294 0ebf8283 2019-07-24 stsp *path = NULL;
2295 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2297 0ebf8283 2019-07-24 stsp return NULL;
2300 10604dce 2021-09-24 thomas static const struct got_error *
2301 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2303 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2304 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2307 10604dce 2021-09-24 thomas static const struct got_error *
2308 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2310 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2311 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2315 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2316 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2318 517bab73 2019-03-11 stsp static const struct got_error *
2319 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2321 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2322 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2323 517bab73 2019-03-11 stsp char *refname;
2325 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2327 517bab73 2019-03-11 stsp return err;
2329 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2331 0cd1c46a 2019-03-11 stsp goto done;
2333 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2335 0cd1c46a 2019-03-11 stsp free(refname);
2337 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2338 3e3a69f1 2019-07-25 stsp return err;
2341 3e3a69f1 2019-07-25 stsp static const struct got_error *
2342 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2344 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2346 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2347 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2348 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2349 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2351 9d31a1d8 2018-03-11 stsp return err;
2355 ebf99748 2019-05-09 stsp static const struct got_error *
2356 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2357 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2359 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2360 ebf99748 2019-05-09 stsp FILE *index = NULL;
2362 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2363 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2364 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2365 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2367 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2369 ebf99748 2019-05-09 stsp goto done;
2371 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2372 ebf99748 2019-05-09 stsp if (index == NULL) {
2373 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2374 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2376 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2377 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2378 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2381 ebf99748 2019-05-09 stsp if (err) {
2382 ebf99748 2019-05-09 stsp free(*fileindex_path);
2383 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2384 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2385 ebf99748 2019-05-09 stsp *fileindex = NULL;
2387 ebf99748 2019-05-09 stsp return err;
2390 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2391 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2392 c932eeeb 2019-05-22 stsp const char *path;
2393 c932eeeb 2019-05-22 stsp size_t path_len;
2394 c932eeeb 2019-05-22 stsp const char *entry_name;
2395 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2396 a484d721 2019-06-10 stsp void *progress_arg;
2399 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2400 c932eeeb 2019-05-22 stsp static const struct got_error *
2401 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2403 1ee397ad 2019-07-12 stsp const struct got_error *err;
2404 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2406 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2407 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2408 c932eeeb 2019-05-22 stsp return NULL;
2409 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2410 102ff934 2019-06-11 stsp return NULL;
2412 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2413 a769b60b 2021-06-27 stsp return NULL;
2415 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2416 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2417 c932eeeb 2019-05-22 stsp return NULL;
2419 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2420 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2421 edd02c5e 2019-07-11 stsp ie->path);
2423 1ee397ad 2019-07-12 stsp return err;
2425 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2426 c932eeeb 2019-05-22 stsp return NULL;
2429 a615e0e7 2020-12-16 stsp static const struct got_error *
2430 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2431 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2432 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2434 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2436 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2437 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2438 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2439 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2440 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2441 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2443 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2444 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2447 9c6338c4 2019-06-02 stsp static const struct got_error *
2448 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2450 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2451 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2452 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2453 867630bb 2020-01-17 stsp struct timespec timeout;
2455 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2456 9c6338c4 2019-06-02 stsp fileindex_path);
2458 9c6338c4 2019-06-02 stsp goto done;
2460 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2462 9c6338c4 2019-06-02 stsp goto done;
2464 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2465 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2466 9c6338c4 2019-06-02 stsp fileindex_path);
2467 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2471 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2472 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2473 867630bb 2020-01-17 stsp * was recorded in the file index.
2475 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2476 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2477 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2479 9c6338c4 2019-06-02 stsp if (new_index)
2480 9c6338c4 2019-06-02 stsp fclose(new_index);
2481 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2482 9c6338c4 2019-06-02 stsp return err;
2485 6ced4176 2019-07-12 stsp static const struct got_error *
2486 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2487 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2488 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2489 945f9229 2022-04-16 thomas struct got_repository *repo)
2491 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2492 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2493 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2494 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2496 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2497 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2498 6ced4176 2019-07-12 stsp *tree_id = NULL;
2500 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2501 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2502 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2503 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2504 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2505 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2506 6ced4176 2019-07-12 stsp goto done;
2508 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2509 945f9229 2022-04-16 thomas worktree->path_prefix);
2511 6ced4176 2019-07-12 stsp goto done;
2512 6ced4176 2019-07-12 stsp return NULL;
2515 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2517 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2518 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2519 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2520 6ced4176 2019-07-12 stsp goto done;
2523 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2525 6ced4176 2019-07-12 stsp goto done;
2527 6ced4176 2019-07-12 stsp free(in_repo_path);
2528 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2530 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2532 6ced4176 2019-07-12 stsp goto done;
2534 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2535 6ced4176 2019-07-12 stsp /* Check out a single file. */
2536 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2537 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2538 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2539 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2540 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2541 6ced4176 2019-07-12 stsp goto done;
2543 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2544 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2545 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2546 6ced4176 2019-07-12 stsp goto done;
2549 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2550 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2552 6ced4176 2019-07-12 stsp return err;
2553 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2554 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2555 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2556 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2557 6ced4176 2019-07-12 stsp goto done;
2560 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2561 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2563 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2564 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2565 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2566 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2567 6ced4176 2019-07-12 stsp goto done;
2569 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2570 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2571 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2572 6ced4176 2019-07-12 stsp goto done;
2577 6ced4176 2019-07-12 stsp free(in_repo_path);
2578 6ced4176 2019-07-12 stsp if (err) {
2579 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2580 6ced4176 2019-07-12 stsp free(*tree_relpath);
2581 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2582 6ced4176 2019-07-12 stsp free(*tree_id);
2583 6ced4176 2019-07-12 stsp *tree_id = NULL;
2585 07ed472d 2019-07-12 stsp return err;
2588 07ed472d 2019-07-12 stsp static const struct got_error *
2589 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2590 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2591 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2592 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2594 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2595 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2596 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2597 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2598 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2600 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2601 7f47418f 2019-12-20 stsp if (err) {
2602 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2603 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2604 7f47418f 2019-12-20 stsp goto done;
2605 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2606 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2608 7f47418f 2019-12-20 stsp return err;
2611 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2612 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2614 07ed472d 2019-07-12 stsp goto done;
2616 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2618 07ed472d 2019-07-12 stsp goto done;
2620 07ed472d 2019-07-12 stsp if (entry_name &&
2621 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2622 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2623 07ed472d 2019-07-12 stsp goto done;
2626 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2627 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2628 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2629 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2630 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2631 07ed472d 2019-07-12 stsp arg.repo = repo;
2632 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2633 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2634 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2635 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2636 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2637 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2640 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2641 07ed472d 2019-07-12 stsp if (commit)
2642 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2643 6ced4176 2019-07-12 stsp return err;
2646 9d31a1d8 2018-03-11 stsp const struct got_error *
2647 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2648 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2649 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2650 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2652 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2653 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2654 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2655 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2656 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2657 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2658 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2659 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2660 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2661 f2ea84fa 2019-07-27 stsp int entry_type;
2662 f2ea84fa 2019-07-27 stsp char *relpath;
2663 f2ea84fa 2019-07-27 stsp char *entry_name;
2664 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2665 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2667 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2669 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2671 9d31a1d8 2018-03-11 stsp return err;
2673 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2674 945f9229 2022-04-16 thomas worktree->base_commit_id);
2675 945f9229 2022-04-16 thomas if (err)
2676 945f9229 2022-04-16 thomas goto done;
2678 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2679 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2680 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2681 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2682 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2683 f2ea84fa 2019-07-27 stsp goto done;
2686 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2687 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2688 945f9229 2022-04-16 thomas worktree, repo);
2689 f2ea84fa 2019-07-27 stsp if (err) {
2690 f2ea84fa 2019-07-27 stsp free(tpd);
2691 6ced4176 2019-07-12 stsp goto done;
2694 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2695 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2696 f2ea84fa 2019-07-27 stsp if (err) {
2697 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2698 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2699 f2ea84fa 2019-07-27 stsp free(tpd);
2700 f2ea84fa 2019-07-27 stsp goto done;
2703 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2705 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2709 51514078 2018-12-25 stsp * Read the file index.
2710 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2711 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2713 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2715 c4cdcb68 2019-04-03 stsp goto done;
2717 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2718 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2719 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2721 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2722 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2723 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2727 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2728 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2729 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2730 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2731 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2732 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2733 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2734 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2738 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2740 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2741 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2742 af12c6b9 2019-06-04 stsp err = sync_err;
2744 9c6338c4 2019-06-02 stsp free(fileindex_path);
2746 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2747 9d31a1d8 2018-03-11 stsp if (commit)
2748 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2749 5ade8233 2019-07-12 stsp if (fileindex)
2750 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2751 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2752 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2753 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2754 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2755 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2756 f2ea84fa 2019-07-27 stsp free(tpd);
2758 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2759 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2760 9d31a1d8 2018-03-11 stsp err = unlockerr;
2761 f8d1f275 2019-02-04 stsp return err;
2764 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2765 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2766 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2767 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2768 234035bc 2019-06-01 stsp void *progress_arg;
2769 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2770 234035bc 2019-06-01 stsp void *cancel_arg;
2771 f69721c3 2019-10-21 stsp const char *label_orig;
2772 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2773 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2776 234035bc 2019-06-01 stsp static const struct got_error *
2777 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2778 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2779 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2780 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2781 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2783 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2784 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2785 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2786 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2787 234035bc 2019-06-01 stsp struct stat sb;
2788 234035bc 2019-06-01 stsp unsigned char status;
2789 234035bc 2019-06-01 stsp int local_changes_subsumed;
2790 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2791 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2793 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2794 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2795 d6c87207 2019-08-02 stsp strlen(path2));
2796 1ee397ad 2019-07-12 stsp if (ie == NULL)
2797 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2798 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2800 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2801 234035bc 2019-06-01 stsp path2) == -1)
2802 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2804 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2807 234035bc 2019-06-01 stsp goto done;
2809 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2810 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2811 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2812 234035bc 2019-06-01 stsp goto done;
2814 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2815 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2816 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2817 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2818 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2819 234035bc 2019-06-01 stsp goto done;
2822 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2823 36bf999c 2020-07-23 stsp char *link_target2;
2824 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2826 36bf999c 2020-07-23 stsp goto done;
2827 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2828 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2829 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2830 36bf999c 2020-07-23 stsp free(link_target2);
2834 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2835 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2836 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2837 1af628f4 2021-06-03 stsp goto done;
2839 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2840 1af628f4 2021-06-03 stsp f_orig, blob1);
2842 1af628f4 2021-06-03 stsp goto done;
2844 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2845 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2846 1af628f4 2021-06-03 stsp goto done;
2847 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2848 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2850 1af628f4 2021-06-03 stsp goto done;
2852 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
2853 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2854 1af628f4 2021-06-03 stsp if (fd == -1) {
2855 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2856 1af628f4 2021-06-03 stsp ondisk_path);
2857 1af628f4 2021-06-03 stsp goto done;
2859 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2860 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2861 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2862 1af628f4 2021-06-03 stsp ondisk_path);
2863 1af628f4 2021-06-03 stsp close(fd);
2864 1af628f4 2021-06-03 stsp goto done;
2866 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2868 1af628f4 2021-06-03 stsp goto done;
2869 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2870 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2871 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2872 1af628f4 2021-06-03 stsp goto done;
2874 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2875 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2876 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2877 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2878 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2880 234035bc 2019-06-01 stsp } else if (blob1) {
2881 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2882 d6c87207 2019-08-02 stsp strlen(path1));
2883 1ee397ad 2019-07-12 stsp if (ie == NULL)
2884 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2885 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2887 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2888 2b92fad7 2019-06-02 stsp path1) == -1)
2889 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2891 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2894 2b92fad7 2019-06-02 stsp goto done;
2896 2b92fad7 2019-06-02 stsp switch (status) {
2897 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2898 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2899 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2901 1ee397ad 2019-07-12 stsp goto done;
2902 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2904 2b92fad7 2019-06-02 stsp goto done;
2906 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2908 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2909 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2910 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2911 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2913 1ee397ad 2019-07-12 stsp goto done;
2915 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2917 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2918 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2919 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2920 e8f02263 2022-01-23 thomas off_t blob1_size;
2922 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2923 0a22ca1a 2020-09-23 stsp * exists in the repository.
2925 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
2926 e8f02263 2022-01-23 thomas &blob1_size, path1);
2928 0a22ca1a 2020-09-23 stsp goto done;
2929 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2930 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2931 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2933 0a22ca1a 2020-09-23 stsp goto done;
2934 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2937 0a22ca1a 2020-09-23 stsp goto done;
2939 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2942 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2943 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2945 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2946 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2949 0a22ca1a 2020-09-23 stsp goto done;
2952 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2953 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2954 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2955 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2957 1ee397ad 2019-07-12 stsp goto done;
2959 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2960 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2962 1ee397ad 2019-07-12 stsp goto done;
2967 234035bc 2019-06-01 stsp } else if (blob2) {
2968 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2969 234035bc 2019-06-01 stsp path2) == -1)
2970 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2971 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2972 d6c87207 2019-08-02 stsp strlen(path2));
2974 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2975 7f91a133 2019-12-13 stsp -1, NULL, repo);
2977 234035bc 2019-06-01 stsp goto done;
2978 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2979 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2980 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2981 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2982 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2983 1ee397ad 2019-07-12 stsp status, path2);
2984 234035bc 2019-06-01 stsp goto done;
2986 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2987 36bf999c 2020-07-23 stsp char *link_target2;
2988 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2991 36bf999c 2020-07-23 stsp goto done;
2992 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2993 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2994 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2995 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2996 36bf999c 2020-07-23 stsp free(link_target2);
2997 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2998 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2999 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3000 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3001 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3002 526a746f 2020-07-23 stsp a->progress_arg);
3004 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3005 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3008 dfe9fba0 2020-07-23 stsp goto done;
3009 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3010 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3011 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3012 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3014 234035bc 2019-06-01 stsp goto done;
3017 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3018 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3019 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3020 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3021 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3022 ace90326 2021-09-27 thomas 0, 1, a->allow_bad_symlinks, repo,
3023 ace90326 2021-09-27 thomas a->progress_cb, a->progress_arg);
3025 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3026 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3027 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3030 234035bc 2019-06-01 stsp goto done;
3031 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3033 234035bc 2019-06-01 stsp goto done;
3034 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3035 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3036 3969253a 2020-03-07 stsp if (err) {
3037 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3038 3969253a 2020-03-07 stsp goto done;
3040 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3041 2b92fad7 2019-06-02 stsp if (err) {
3042 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3043 2b92fad7 2019-06-02 stsp goto done;
3045 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3046 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3047 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3052 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3053 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3054 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3055 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3056 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3057 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3058 1af628f4 2021-06-03 stsp free(id_str);
3059 54d5be07 2021-06-03 stsp free(label_deriv2);
3060 234035bc 2019-06-01 stsp free(ondisk_path);
3061 234035bc 2019-06-01 stsp return err;
3064 69de9dd4 2021-09-03 stsp static const struct got_error *
3065 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3067 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3069 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3070 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3071 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3072 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3073 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3075 69de9dd4 2021-09-03 stsp return NULL;
3078 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3079 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3080 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3081 234035bc 2019-06-01 stsp struct got_repository *repo;
3084 234035bc 2019-06-01 stsp static const struct got_error *
3085 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3086 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3087 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3088 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3089 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3091 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3092 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3093 234035bc 2019-06-01 stsp unsigned char status;
3094 234035bc 2019-06-01 stsp struct stat sb;
3095 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3096 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3097 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3098 234035bc 2019-06-01 stsp char *ondisk_path;
3100 69de9dd4 2021-09-03 stsp if (id == NULL)
3101 69de9dd4 2021-09-03 stsp return NULL;
3103 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3104 69de9dd4 2021-09-03 stsp if (ie == NULL)
3105 69de9dd4 2021-09-03 stsp return NULL;
3107 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3109 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3111 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3112 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3113 5546d466 2021-09-02 stsp free(ondisk_path);
3115 234035bc 2019-06-01 stsp return err;
3116 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3117 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3119 234035bc 2019-06-01 stsp return NULL;
3122 818c7501 2019-07-11 stsp static const struct got_error *
3123 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3124 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3125 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3126 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3127 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3129 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3130 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3131 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3132 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3133 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3134 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3135 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3136 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3137 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3139 03415a1a 2019-06-02 stsp if (commit_id1) {
3140 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3141 945f9229 2022-04-16 thomas if (err)
3142 945f9229 2022-04-16 thomas goto done;
3143 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3144 03415a1a 2019-06-02 stsp worktree->path_prefix);
3145 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3146 03415a1a 2019-06-02 stsp goto done;
3148 69d57f3d 2020-07-31 stsp if (tree_id1) {
3149 69d57f3d 2020-07-31 stsp char *id_str;
3151 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3153 03415a1a 2019-06-02 stsp goto done;
3155 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3157 f69721c3 2019-10-21 stsp goto done;
3159 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3160 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3161 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3162 f69721c3 2019-10-21 stsp free(id_str);
3163 f69721c3 2019-10-21 stsp goto done;
3165 f69721c3 2019-10-21 stsp free(id_str);
3167 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3168 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3169 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3170 a0f32f33 2022-06-13 thomas goto done;
3174 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3175 945f9229 2022-04-16 thomas if (err)
3176 945f9229 2022-04-16 thomas goto done;
3178 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3179 234035bc 2019-06-01 stsp worktree->path_prefix);
3181 234035bc 2019-06-01 stsp goto done;
3183 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3185 234035bc 2019-06-01 stsp goto done;
3187 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3188 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3189 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3190 19a6a6b5 2022-07-01 thomas goto done;
3193 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3194 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3195 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3196 a0f32f33 2022-06-13 thomas goto done;
3199 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3200 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3201 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3202 19a6a6b5 2022-07-01 thomas goto done;
3205 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3206 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3207 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3208 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3209 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3211 69de9dd4 2021-09-03 stsp goto done;
3213 234035bc 2019-06-01 stsp arg.worktree = worktree;
3214 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3215 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3216 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3217 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3218 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3219 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3220 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3221 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3222 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3223 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3224 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3225 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3226 af12c6b9 2019-06-04 stsp err = sync_err;
3228 945f9229 2022-04-16 thomas if (commit1)
3229 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3230 945f9229 2022-04-16 thomas if (commit2)
3231 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3232 234035bc 2019-06-01 stsp if (tree1)
3233 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3234 234035bc 2019-06-01 stsp if (tree2)
3235 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3236 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3237 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3238 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3239 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3240 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3241 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3242 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3243 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3244 f69721c3 2019-10-21 stsp free(label_orig);
3245 818c7501 2019-07-11 stsp return err;
3248 818c7501 2019-07-11 stsp const struct got_error *
3249 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3250 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3251 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3252 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3254 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3255 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3256 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3258 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3260 818c7501 2019-07-11 stsp return err;
3262 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3264 818c7501 2019-07-11 stsp goto done;
3266 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3267 69de9dd4 2021-09-03 stsp worktree);
3269 818c7501 2019-07-11 stsp goto done;
3271 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3272 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3273 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3275 818c7501 2019-07-11 stsp if (fileindex)
3276 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3277 818c7501 2019-07-11 stsp free(fileindex_path);
3278 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3279 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3280 234035bc 2019-06-01 stsp err = unlockerr;
3281 234035bc 2019-06-01 stsp return err;
3284 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3285 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3286 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3287 927df6b7 2019-02-10 stsp const char *status_path;
3288 927df6b7 2019-02-10 stsp size_t status_path_len;
3289 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3290 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3291 f8d1f275 2019-02-04 stsp void *status_arg;
3292 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3293 f8d1f275 2019-02-04 stsp void *cancel_arg;
3294 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3295 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3296 f2a9dc41 2019-12-13 tracey int report_unchanged;
3297 3143d852 2020-06-25 stsp int no_ignores;
3300 f8d1f275 2019-02-04 stsp static const struct got_error *
3301 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3302 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3303 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3304 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3306 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3307 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3308 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3309 927df6b7 2019-02-10 stsp struct stat sb;
3310 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3311 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3312 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3314 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3316 98eaaa12 2019-08-03 stsp return err;
3318 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3319 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3320 98eaaa12 2019-08-03 stsp return NULL;
3322 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3323 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3324 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3326 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3327 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3328 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3330 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3331 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3332 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3333 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3334 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3337 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3338 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3341 927df6b7 2019-02-10 stsp static const struct got_error *
3342 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3343 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3345 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3346 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3347 f8d1f275 2019-02-04 stsp char *abspath;
3349 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3350 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3352 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3353 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3354 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3355 927df6b7 2019-02-10 stsp return NULL;
3357 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3358 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3359 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3360 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3362 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3363 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3364 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3367 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3368 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3369 f8d1f275 2019-02-04 stsp free(abspath);
3370 9d31a1d8 2018-03-11 stsp return err;
3373 f8d1f275 2019-02-04 stsp static const struct got_error *
3374 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3376 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3377 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3378 2ec1f75b 2019-03-26 stsp unsigned char status;
3380 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3381 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3383 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3384 927df6b7 2019-02-10 stsp return NULL;
3386 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3387 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3388 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3389 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3391 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3392 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3393 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3396 ef20f542 2022-06-26 thomas static void
3397 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3399 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3401 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3402 6841da00 2019-08-08 stsp free((char *)pe->path);
3403 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3406 ef20f542 2022-06-26 thomas static void
3407 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3409 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3411 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3412 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3413 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3414 6841da00 2019-08-08 stsp free((char *)pe->path);
3416 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3419 6841da00 2019-08-08 stsp static const struct got_error *
3420 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3422 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3423 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3424 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3425 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3426 6841da00 2019-08-08 stsp size_t linesize = 0;
3427 6841da00 2019-08-08 stsp ssize_t linelen;
3429 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3430 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3431 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3432 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3434 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3435 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3436 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3438 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3439 bd8de430 2019-10-04 stsp if (line[0] == '#')
3442 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3443 bd8de430 2019-10-04 stsp if (line[0] == '!')
3446 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3447 6841da00 2019-08-08 stsp line) == -1) {
3448 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3449 6841da00 2019-08-08 stsp goto done;
3451 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3453 6841da00 2019-08-08 stsp goto done;
3455 6841da00 2019-08-08 stsp if (ferror(f)) {
3456 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3457 6841da00 2019-08-08 stsp goto done;
3460 6841da00 2019-08-08 stsp dirpath = strdup(path);
3461 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3462 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3463 6841da00 2019-08-08 stsp goto done;
3465 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3467 6841da00 2019-08-08 stsp free(line);
3468 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3469 6841da00 2019-08-08 stsp free(dirpath);
3470 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3472 6841da00 2019-08-08 stsp return err;
3475 ef20f542 2022-06-26 thomas static int
3476 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3478 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3480 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3481 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3482 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3483 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3485 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3486 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3488 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3490 bd8de430 2019-10-04 stsp pattern += 3;
3492 bd8de430 2019-10-04 stsp while (*p) {
3493 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3494 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3495 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3496 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3498 bd8de430 2019-10-04 stsp while (*p == '/')
3508 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3509 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3510 6841da00 2019-08-08 stsp * ignores backwards.
3512 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3513 6841da00 2019-08-08 stsp while (pe) {
3514 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3515 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3516 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3517 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3518 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3519 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3520 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3521 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3522 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3527 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3533 6841da00 2019-08-08 stsp static const struct got_error *
3534 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3535 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3537 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3538 6841da00 2019-08-08 stsp char *ignorespath;
3539 886cec17 2019-12-15 stsp int fd = -1;
3540 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3542 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3543 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3544 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3546 886cec17 2019-12-15 stsp if (dirfd != -1) {
3547 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3548 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3549 886cec17 2019-12-15 stsp if (fd == -1) {
3550 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3551 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3552 886cec17 2019-12-15 stsp ignorespath);
3554 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3555 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3556 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3557 886cec17 2019-12-15 stsp ignorespath);
3560 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3564 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3565 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3566 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3567 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3568 886cec17 2019-12-15 stsp ignorespath);
3570 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3573 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3574 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3575 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3576 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3577 6841da00 2019-08-08 stsp free(ignorespath);
3578 6841da00 2019-08-08 stsp return err;
3581 f8d1f275 2019-02-04 stsp static const struct got_error *
3582 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3583 6092c299 2021-10-04 thomas int dirfd)
3585 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3586 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3587 f8d1f275 2019-02-04 stsp char *path = NULL;
3589 6092c299 2021-10-04 thomas if (ignore != NULL)
3590 6092c299 2021-10-04 thomas *ignore = 0;
3592 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3593 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3595 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3596 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3597 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3599 f8d1f275 2019-02-04 stsp path = de->d_name;
3602 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3603 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3604 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3605 6092c299 2021-10-04 thomas *ignore = 1;
3606 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3607 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3608 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3609 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3610 f8d1f275 2019-02-04 stsp if (parent_path[0])
3611 f8d1f275 2019-02-04 stsp free(path);
3612 b72f483a 2019-02-05 stsp return err;
3615 347d1d3e 2019-07-12 stsp static const struct got_error *
3616 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3618 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3619 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3621 3143d852 2020-06-25 stsp if (a->no_ignores)
3622 3143d852 2020-06-25 stsp return NULL;
3624 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3625 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3627 3143d852 2020-06-25 stsp return err;
3629 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3630 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3632 3143d852 2020-06-25 stsp return err;
3635 3143d852 2020-06-25 stsp static const struct got_error *
3636 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3637 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3638 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3639 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3641 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3642 abb4604f 2019-07-27 stsp struct stat sb;
3644 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3646 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3647 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3649 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3650 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3651 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3652 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3653 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3656 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3657 fe1d8685 2022-02-12 thomas return NULL;
3659 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3660 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3661 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3663 abb4604f 2019-07-27 stsp return NULL;
3666 3143d852 2020-06-25 stsp static const struct got_error *
3667 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3668 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3670 3143d852 2020-06-25 stsp const struct got_error *err;
3671 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3673 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3674 3143d852 2020-06-25 stsp ".cvsignore");
3676 3143d852 2020-06-25 stsp return err;
3678 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3679 3143d852 2020-06-25 stsp ".gitignore");
3681 3143d852 2020-06-25 stsp return err;
3683 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3684 3143d852 2020-06-25 stsp if (err) {
3685 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3686 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3687 3143d852 2020-06-25 stsp return err;
3689 3143d852 2020-06-25 stsp for (;;) {
3690 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3691 3143d852 2020-06-25 stsp ".cvsignore");
3694 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3695 3143d852 2020-06-25 stsp ".gitignore");
3698 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3699 3143d852 2020-06-25 stsp if (err) {
3700 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3701 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3704 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3706 b737c85a 2020-06-26 stsp free(parent_path);
3707 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3708 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3711 b737c85a 2020-06-26 stsp free(parent_path);
3712 b737c85a 2020-06-26 stsp free(next_parent_path);
3713 3143d852 2020-06-25 stsp return err;
3716 abb4604f 2019-07-27 stsp static const struct got_error *
3717 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3718 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3719 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3720 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3721 f2a9dc41 2019-12-13 tracey int report_unchanged)
3723 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3724 6fc93f37 2019-12-13 stsp int fd = -1;
3725 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3726 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3727 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3728 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3729 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3731 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3733 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3734 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3735 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3737 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3738 a78810f8 2022-03-13 thomas if (ie) {
3739 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3740 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3741 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3742 a78810f8 2022-03-13 thomas goto done;
3745 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3746 6fc93f37 2019-12-13 stsp if (fd == -1) {
3747 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3748 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3749 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3751 ff56836b 2021-07-08 stsp if (!no_ignores) {
3752 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3753 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3755 ff56836b 2021-07-08 stsp goto done;
3757 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3758 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3759 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3762 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3763 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3764 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3765 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3766 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3767 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3768 abb4604f 2019-07-27 stsp arg.status_path = path;
3769 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3770 abb4604f 2019-07-27 stsp arg.repo = repo;
3771 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3772 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3773 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3774 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3775 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3776 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3777 022fae89 2019-12-06 tracey if (!no_ignores) {
3778 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3779 3143d852 2020-06-25 stsp worktree->root_path, path);
3781 3143d852 2020-06-25 stsp goto done;
3783 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3784 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3785 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3788 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3789 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3790 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3791 927df6b7 2019-02-10 stsp free(ondisk_path);
3792 347d1d3e 2019-07-12 stsp return err;
3795 347d1d3e 2019-07-12 stsp const struct got_error *
3796 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3797 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3798 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3799 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3801 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3802 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3803 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3804 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3806 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3808 347d1d3e 2019-07-12 stsp return err;
3810 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3811 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3812 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3813 f6343036 2021-06-22 stsp no_ignores, 0);
3817 f8d1f275 2019-02-04 stsp free(fileindex_path);
3818 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3819 f8d1f275 2019-02-04 stsp return err;
3822 6c7ab921 2019-03-18 stsp const struct got_error *
3823 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3824 6c7ab921 2019-03-18 stsp const char *arg)
3826 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3827 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3828 6c7ab921 2019-03-18 stsp size_t len;
3829 00bb5ea0 2020-07-23 stsp struct stat sb;
3830 01740607 2020-11-04 stsp char *abspath = NULL;
3831 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3833 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3835 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3836 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3837 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3839 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3840 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3841 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3842 00bb5ea0 2020-07-23 stsp goto done;
3844 727173c3 2020-11-06 stsp sb.st_mode = 0;
3846 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3848 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3849 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3850 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3851 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3853 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3854 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3855 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3856 00bb5ea0 2020-07-23 stsp goto done;
3860 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3861 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3863 d0710d08 2019-07-22 stsp goto done;
3864 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3865 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3866 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3867 00bb5ea0 2020-07-23 stsp goto done;
3870 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3871 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3872 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3873 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3874 00bb5ea0 2020-07-23 stsp goto done;
3876 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3877 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3878 01740607 2020-11-04 stsp goto done;
3880 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3881 01740607 2020-11-04 stsp sizeof(canonpath));
3883 00bb5ea0 2020-07-23 stsp goto done;
3884 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3885 01740607 2020-11-04 stsp if (resolved == NULL) {
3886 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3887 01740607 2020-11-04 stsp goto done;
3892 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3893 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3894 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3895 6c7ab921 2019-03-18 stsp goto done;
3898 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3899 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3900 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3902 f6d88e1a 2019-05-29 stsp goto done;
3904 f6d88e1a 2019-05-29 stsp path = strdup("");
3905 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3906 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3907 f6d88e1a 2019-05-29 stsp goto done;
3911 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3912 6c7ab921 2019-03-18 stsp len = strlen(path);
3913 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3914 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3918 01740607 2020-11-04 stsp free(abspath);
3919 6c7ab921 2019-03-18 stsp free(resolved);
3920 d0710d08 2019-07-22 stsp free(cwd);
3921 6c7ab921 2019-03-18 stsp if (err == NULL)
3922 6c7ab921 2019-03-18 stsp *wt_path = path;
3924 6c7ab921 2019-03-18 stsp free(path);
3925 d00136be 2019-03-26 stsp return err;
3928 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3929 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3930 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3931 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3932 4e68cba3 2019-11-23 stsp void *progress_arg;
3933 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3936 4e68cba3 2019-11-23 stsp static const struct got_error *
3937 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3938 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3939 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3940 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3942 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3943 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3944 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3945 6d022e97 2019-08-04 stsp struct stat sb;
3946 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3948 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3949 dbb83fbd 2019-12-12 stsp relpath) == -1)
3950 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3952 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3954 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3955 12463d8b 2019-12-13 stsp de_name, a->repo);
3957 dbb83fbd 2019-12-12 stsp goto done;
3958 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3959 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3960 dbb83fbd 2019-12-12 stsp goto done;
3961 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3963 dbb83fbd 2019-12-12 stsp goto done;
3966 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3967 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
3968 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
3970 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3971 dbb83fbd 2019-12-12 stsp goto done;
3974 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3976 4e68cba3 2019-11-23 stsp goto done;
3977 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3978 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3979 3969253a 2020-03-07 stsp if (err) {
3980 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3981 3969253a 2020-03-07 stsp goto done;
3983 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3984 07f5b47a 2019-06-02 stsp if (err) {
3985 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3986 4e68cba3 2019-11-23 stsp goto done;
3989 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3991 dbb83fbd 2019-12-12 stsp return err;
3992 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3993 dbb83fbd 2019-12-12 stsp return NULL;
3994 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3997 d00136be 2019-03-26 stsp const struct got_error *
3998 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3999 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4000 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4001 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4003 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4004 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4005 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4006 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4007 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4009 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4011 d00136be 2019-03-26 stsp return err;
4013 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4015 d00136be 2019-03-26 stsp goto done;
4017 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4018 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4019 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4020 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4021 4e68cba3 2019-11-23 stsp saa.repo = repo;
4023 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4024 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4025 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4029 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4030 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4031 af12c6b9 2019-06-04 stsp err = sync_err;
4033 fb399478 2019-07-12 stsp free(fileindex_path);
4034 d00136be 2019-03-26 stsp if (fileindex)
4035 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4036 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4037 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4038 d00136be 2019-03-26 stsp err = unlockerr;
4039 6c7ab921 2019-03-18 stsp return err;
4042 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4043 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4044 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4045 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4046 f2a9dc41 2019-12-13 tracey void *progress_arg;
4047 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4048 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4049 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4050 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4051 766841c2 2020-08-13 stsp const char *status_codes;
4054 f2a9dc41 2019-12-13 tracey static const struct got_error *
4055 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4056 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4057 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4058 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4060 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4061 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4062 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4063 17ed4618 2019-06-02 stsp struct stat sb;
4064 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4066 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4067 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4068 d64cc78a 2022-01-25 thomas return NULL;
4069 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4072 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4073 17ed4618 2019-06-02 stsp if (ie == NULL)
4074 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4076 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4077 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4078 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4079 9acbc4fa 2019-08-03 stsp return NULL;
4080 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4083 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4084 f2a9dc41 2019-12-13 tracey relpath) == -1)
4085 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4087 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4090 f2a9dc41 2019-12-13 tracey goto done;
4092 766841c2 2020-08-13 stsp if (a->status_codes) {
4093 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4095 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4096 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4099 766841c2 2020-08-13 stsp if (i == ncodes) {
4100 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4101 766841c2 2020-08-13 stsp free(ondisk_path);
4102 766841c2 2020-08-13 stsp return NULL;
4104 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4105 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4106 766841c2 2020-08-13 stsp static char msg[64];
4107 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4108 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4109 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4110 766841c2 2020-08-13 stsp goto done;
4114 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4115 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4116 f2a9dc41 2019-12-13 tracey goto done;
4117 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4118 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4119 f2a9dc41 2019-12-13 tracey goto done;
4121 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4122 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4123 d64cc78a 2022-01-25 thomas goto done;
4125 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4126 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4127 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4128 f2a9dc41 2019-12-13 tracey goto done;
4132 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4133 20a2ad1c 2020-10-20 stsp size_t root_len;
4135 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4136 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
4137 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4138 12463d8b 2019-12-13 stsp ondisk_path);
4139 12463d8b 2019-12-13 stsp goto done;
4141 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
4142 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4143 12463d8b 2019-12-13 stsp goto done;
4146 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4148 20a2ad1c 2020-10-20 stsp char *parent;
4149 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4151 2513f20a 2020-10-20 stsp goto done;
4152 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4153 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4154 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4155 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4156 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4157 20a2ad1c 2020-10-20 stsp ondisk_path);
4160 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4161 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4164 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4166 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4167 f2a9dc41 2019-12-13 tracey if (err)
4168 f2a9dc41 2019-12-13 tracey return err;
4169 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4170 f2a9dc41 2019-12-13 tracey return NULL;
4171 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4172 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4175 2ec1f75b 2019-03-26 stsp const struct got_error *
4176 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4177 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4178 766841c2 2020-08-13 stsp const char *status_codes,
4179 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4180 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4182 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4183 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4184 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4185 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4186 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4188 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4190 2ec1f75b 2019-03-26 stsp return err;
4192 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4194 2ec1f75b 2019-03-26 stsp goto done;
4196 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4197 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4198 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4199 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4200 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4201 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4202 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4203 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4204 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4206 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4207 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4208 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4212 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4213 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4214 af12c6b9 2019-06-04 stsp err = sync_err;
4216 fb399478 2019-07-12 stsp free(fileindex_path);
4217 2ec1f75b 2019-03-26 stsp if (fileindex)
4218 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4219 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4220 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4221 2ec1f75b 2019-03-26 stsp err = unlockerr;
4222 33aa809d 2019-08-08 stsp return err;
4225 33aa809d 2019-08-08 stsp static const struct got_error *
4226 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4228 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4229 33aa809d 2019-08-08 stsp char *line = NULL;
4230 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4231 33aa809d 2019-08-08 stsp ssize_t linelen;
4233 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4234 33aa809d 2019-08-08 stsp if (linelen == -1) {
4235 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4236 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4237 33aa809d 2019-08-08 stsp goto done;
4239 33aa809d 2019-08-08 stsp return NULL;
4241 33aa809d 2019-08-08 stsp if (outfile) {
4242 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4243 33aa809d 2019-08-08 stsp if (n != linelen) {
4244 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4245 33aa809d 2019-08-08 stsp goto done;
4248 33aa809d 2019-08-08 stsp if (rejectfile) {
4249 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4250 33aa809d 2019-08-08 stsp if (n != linelen)
4251 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4254 33aa809d 2019-08-08 stsp free(line);
4255 2ec1f75b 2019-03-26 stsp return err;
4258 33aa809d 2019-08-08 stsp static const struct got_error *
4259 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4261 33aa809d 2019-08-08 stsp char *line = NULL;
4262 33aa809d 2019-08-08 stsp size_t linesize = 0;
4263 33aa809d 2019-08-08 stsp ssize_t linelen;
4265 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4266 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4267 500467ff 2019-09-25 hiltjo if (ferror(f))
4268 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4269 500467ff 2019-09-25 hiltjo return NULL;
4271 33aa809d 2019-08-08 stsp free(line);
4272 33aa809d 2019-08-08 stsp return NULL;
4275 33aa809d 2019-08-08 stsp static const struct got_error *
4276 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4277 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4278 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4280 33aa809d 2019-08-08 stsp const struct got_error *err;
4282 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4283 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4284 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4286 33aa809d 2019-08-08 stsp return err;
4287 33aa809d 2019-08-08 stsp (*line_cur1)++;
4289 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4290 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4291 33aa809d 2019-08-08 stsp if (rejectfile)
4292 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4294 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4296 33aa809d 2019-08-08 stsp return err;
4297 33aa809d 2019-08-08 stsp (*line_cur2)++;
4299 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4300 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4301 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4303 33aa809d 2019-08-08 stsp return err;
4304 33aa809d 2019-08-08 stsp (*line_cur2)++;
4306 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4307 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4308 33aa809d 2019-08-08 stsp if (rejectfile)
4309 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4311 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4313 33aa809d 2019-08-08 stsp return err;
4314 33aa809d 2019-08-08 stsp (*line_cur1)++;
4317 f1e81a05 2019-08-10 stsp return NULL;
4320 f1e81a05 2019-08-10 stsp static const struct got_error *
4321 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4322 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4324 f1e81a05 2019-08-10 stsp const struct got_error *err;
4326 f1e81a05 2019-08-10 stsp if (outfile) {
4327 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4328 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4329 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4331 f1e81a05 2019-08-10 stsp return err;
4332 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4335 f1e81a05 2019-08-10 stsp if (rejectfile) {
4336 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4337 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4338 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4340 f1e81a05 2019-08-10 stsp return err;
4341 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4345 33aa809d 2019-08-08 stsp return NULL;
4348 33aa809d 2019-08-08 stsp static const struct got_error *
4349 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4350 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4351 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4352 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4353 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4355 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4356 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4357 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4358 33aa809d 2019-08-08 stsp FILE *hunkfile;
4359 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4360 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4363 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4365 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4366 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4367 fe621944 2020-11-10 stsp start_old = cc.left.start;
4368 fe621944 2020-11-10 stsp end_old = cc.left.end;
4369 fe621944 2020-11-10 stsp start_new = cc.right.start;
4370 fe621944 2020-11-10 stsp end_new = cc.right.end;
4372 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4373 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4374 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4376 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4377 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4378 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4380 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4381 fe621944 2020-11-10 stsp if (diff_state == NULL)
4382 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4383 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4385 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4386 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4387 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4388 33aa809d 2019-08-08 stsp goto done;
4391 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4392 fe621944 2020-11-10 stsp diff_result, &cc);
4393 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4394 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4395 33aa809d 2019-08-08 stsp goto done;
4398 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4399 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4400 33aa809d 2019-08-08 stsp goto done;
4403 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4404 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4406 33aa809d 2019-08-08 stsp goto done;
4408 33aa809d 2019-08-08 stsp switch (*choice) {
4409 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4410 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4411 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4413 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4414 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4415 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4417 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4420 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4424 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4425 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4426 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4427 33aa809d 2019-08-08 stsp return err;
4430 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4431 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4432 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4433 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4434 1f1abb7e 2019-08-08 stsp void *progress_arg;
4435 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4436 33aa809d 2019-08-08 stsp void *patch_arg;
4437 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4438 10604dce 2021-09-24 thomas int unlink_added_files;
4441 e20a8b6f 2019-06-04 stsp static const struct got_error *
4442 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4443 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4444 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4445 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4446 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4448 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4449 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4450 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4451 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4452 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4453 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4454 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4455 fe621944 2020-11-10 stsp struct stat sb2;
4456 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4457 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4458 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4460 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4462 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4464 33aa809d 2019-08-08 stsp return err;
4466 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4467 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4468 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4469 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4470 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4471 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4472 fa3cef63 2020-07-23 stsp goto done;
4474 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4475 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4476 48b4f239 2021-12-31 thomas if (link_len == -1) {
4477 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4480 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4481 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4484 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4485 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4486 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4487 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4488 fa3cef63 2020-07-23 stsp goto done;
4490 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4491 fa3cef63 2020-07-23 stsp sizeof(link_target));
4492 fa3cef63 2020-07-23 stsp if (link_len == -1)
4493 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4494 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4495 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4498 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4499 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4500 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4501 fa3cef63 2020-07-23 stsp goto done;
4504 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4505 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4506 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4507 fa3cef63 2020-07-23 stsp goto done;
4512 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4513 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4514 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4515 fa3cef63 2020-07-23 stsp goto done;
4517 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4518 fa3cef63 2020-07-23 stsp if (n != link_len) {
4519 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4520 fa3cef63 2020-07-23 stsp goto done;
4522 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4523 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4524 fa3cef63 2020-07-23 stsp goto done;
4526 fa3cef63 2020-07-23 stsp rewind(f2);
4529 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4530 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4531 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4532 f4ae6ddb 2022-07-01 thomas goto done;
4535 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4537 33aa809d 2019-08-08 stsp goto done;
4539 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4541 33aa809d 2019-08-08 stsp goto done;
4543 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4545 33aa809d 2019-08-08 stsp goto done;
4547 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4548 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4550 33aa809d 2019-08-08 stsp goto done;
4552 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4554 33aa809d 2019-08-08 stsp goto done;
4556 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4557 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4558 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4559 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4561 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4562 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4563 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4564 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4565 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4566 fe621944 2020-11-10 stsp nchanges++;
4568 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4569 33aa809d 2019-08-08 stsp int choice;
4570 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4571 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4572 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4573 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4574 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4575 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4577 33aa809d 2019-08-08 stsp goto done;
4578 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4579 33aa809d 2019-08-08 stsp have_content = 1;
4580 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4583 1ebedb77 2019-10-19 stsp if (have_content) {
4584 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4585 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4586 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4588 1ebedb77 2019-10-19 stsp goto done;
4590 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4591 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4592 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4593 fa3cef63 2020-07-23 stsp goto done;
4598 33aa809d 2019-08-08 stsp free(id_str);
4599 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4600 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4602 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4603 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4604 fe621944 2020-11-10 stsp if (err == NULL)
4605 fe621944 2020-11-10 stsp err = free_err;
4606 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4607 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4608 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4609 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4610 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4611 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4612 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4613 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4614 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4615 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4616 33aa809d 2019-08-08 stsp if (err || !have_content) {
4617 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4618 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4619 33aa809d 2019-08-08 stsp free(*path_outfile);
4620 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4622 33aa809d 2019-08-08 stsp free(path1);
4623 33aa809d 2019-08-08 stsp return err;
4626 33aa809d 2019-08-08 stsp static const struct got_error *
4627 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4628 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4629 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4630 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4632 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4633 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4634 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4635 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4636 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4637 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4638 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4639 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4640 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4641 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4642 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4643 f4ae6ddb 2022-07-01 thomas int fd = -1;
4645 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4646 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4647 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4648 d3bcc3d1 2019-08-08 stsp return NULL;
4650 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4651 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4652 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4654 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4655 65084dad 2019-08-08 stsp if (ie == NULL)
4656 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4658 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4659 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4660 a129376b 2019-03-28 stsp if (err) {
4661 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4662 a129376b 2019-03-28 stsp goto done;
4663 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4664 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4665 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4666 e20a8b6f 2019-06-04 stsp goto done;
4669 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4670 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4671 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4672 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4673 a129376b 2019-03-28 stsp goto done;
4676 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4677 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4678 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4679 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4680 a129376b 2019-03-28 stsp goto done;
4683 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4684 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4685 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4686 a129376b 2019-03-28 stsp goto done;
4691 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4692 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
4693 945f9229 2022-04-16 thomas if (err)
4694 945f9229 2022-04-16 thomas goto done;
4696 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4697 a9fa2909 2019-07-27 stsp if (err) {
4698 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4699 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4700 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4701 a9fa2909 2019-07-27 stsp goto done;
4703 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4705 a9fa2909 2019-07-27 stsp goto done;
4707 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4709 a9fa2909 2019-07-27 stsp goto done;
4711 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4712 1233e6b6 2020-10-19 stsp free(te_name);
4713 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4714 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4715 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4716 a9fa2909 2019-07-27 stsp goto done;
4720 a129376b 2019-03-28 stsp switch (status) {
4721 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4722 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4723 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4724 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4725 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4727 33aa809d 2019-08-08 stsp goto done;
4728 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4731 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4732 1f1abb7e 2019-08-08 stsp ie->path);
4734 1ee397ad 2019-07-12 stsp goto done;
4735 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4736 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
4737 10604dce 2021-09-24 thomas if (asprintf(&ondisk_path, "%s/%s",
4738 10604dce 2021-09-24 thomas got_worktree_get_root_path(a->worktree),
4739 10604dce 2021-09-24 thomas relpath) == -1) {
4740 10604dce 2021-09-24 thomas err = got_error_from_errno("asprintf");
4741 10604dce 2021-09-24 thomas goto done;
4743 10604dce 2021-09-24 thomas if (unlink(ondisk_path) == -1) {
4744 10604dce 2021-09-24 thomas err = got_error_from_errno2("unlink",
4745 10604dce 2021-09-24 thomas ondisk_path);
4750 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4751 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4752 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4753 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4754 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4756 33aa809d 2019-08-08 stsp goto done;
4757 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4760 33aa809d 2019-08-08 stsp /* fall through */
4761 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4762 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4763 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4764 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4765 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4766 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4767 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4768 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4769 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4771 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4772 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4773 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4774 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4775 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4776 f4ae6ddb 2022-07-01 thomas goto done;
4779 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4781 65084dad 2019-08-08 stsp goto done;
4783 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4784 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4785 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4786 a129376b 2019-03-28 stsp goto done;
4789 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4790 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4791 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4792 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4793 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4794 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4795 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4797 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4798 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4799 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4800 369fd7e5 2020-07-23 stsp path_content);
4803 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4804 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4805 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4806 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4808 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4809 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4810 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4811 369fd7e5 2020-07-23 stsp goto done;
4815 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4816 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4817 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4818 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4819 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4820 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4822 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4824 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4825 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4826 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4827 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4830 a129376b 2019-03-28 stsp goto done;
4831 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4832 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4833 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4834 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4835 437adc9d 2020-12-10 yzhong blob->id.sha1,
4836 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4838 2e1fa222 2020-07-23 stsp goto done;
4840 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4841 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4842 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4851 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4852 33aa809d 2019-08-08 stsp free(path_content);
4853 e20a8b6f 2019-06-04 stsp free(parent_path);
4854 a129376b 2019-03-28 stsp free(tree_path);
4855 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4856 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4858 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4860 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4861 a129376b 2019-03-28 stsp free(tree_id);
4862 945f9229 2022-04-16 thomas if (base_commit)
4863 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
4864 e20a8b6f 2019-06-04 stsp return err;
4867 e20a8b6f 2019-06-04 stsp const struct got_error *
4868 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4869 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4870 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4871 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4872 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4874 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4875 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4876 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4877 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4878 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4879 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4881 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4883 e20a8b6f 2019-06-04 stsp return err;
4885 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4887 e20a8b6f 2019-06-04 stsp goto done;
4889 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4890 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4891 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4892 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4893 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4894 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4895 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4896 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
4897 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4898 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4899 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
4903 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4904 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4905 e20a8b6f 2019-06-04 stsp err = sync_err;
4907 fb399478 2019-07-12 stsp free(fileindex_path);
4908 a129376b 2019-03-28 stsp if (fileindex)
4909 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4910 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4911 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4912 a129376b 2019-03-28 stsp err = unlockerr;
4913 c4296144 2019-05-09 stsp return err;
4916 cf066bf8 2019-05-09 stsp static void
4917 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4919 24519714 2019-05-09 stsp free(ct->path);
4920 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4921 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4922 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4923 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4924 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4925 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4929 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4930 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4931 24519714 2019-05-09 stsp struct got_repository *repo;
4932 24519714 2019-05-09 stsp struct got_worktree *worktree;
4933 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4934 5f8a88c6 2019-08-03 stsp int have_staged_files;
4935 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4938 c4296144 2019-05-09 stsp static const struct got_error *
4939 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
4940 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
4941 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4942 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4944 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
4945 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4946 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4947 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4948 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4949 768aea60 2019-05-09 stsp struct stat sb;
4951 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
4952 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4953 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4954 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4955 5f8a88c6 2019-08-03 stsp return NULL;
4957 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4958 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4960 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4961 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4962 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4963 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4964 5f8a88c6 2019-08-03 stsp return NULL;
4967 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4968 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4969 036813ee 2019-05-09 stsp goto done;
4971 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4972 036813ee 2019-05-09 stsp parent_path = strdup("");
4973 69960a46 2019-05-09 stsp if (parent_path == NULL)
4974 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4976 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4978 69960a46 2019-05-09 stsp return err;
4981 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4982 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4983 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4984 c4296144 2019-05-09 stsp goto done;
4987 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4988 768aea60 2019-05-09 stsp relpath) == -1) {
4989 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4990 768aea60 2019-05-09 stsp goto done;
4993 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4994 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4995 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4996 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4997 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4998 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4999 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5000 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5002 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5003 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5006 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5007 0aeb8099 2020-07-23 stsp goto done;
5009 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5010 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5011 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5012 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5013 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5014 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5015 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5016 12463d8b 2019-12-13 stsp ct->ondisk_path);
5017 12463d8b 2019-12-13 stsp goto done;
5019 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5020 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5021 768aea60 2019-05-09 stsp goto done;
5023 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5026 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5027 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5028 44d03001 2019-05-09 stsp relpath) == -1) {
5029 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5030 44d03001 2019-05-09 stsp goto done;
5033 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5034 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5035 35213c7c 2020-07-23 stsp int is_bad_symlink;
5036 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5037 35213c7c 2020-07-23 stsp ssize_t target_len;
5038 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5039 35213c7c 2020-07-23 stsp sizeof(target_path));
5040 35213c7c 2020-07-23 stsp if (target_len == -1) {
5041 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5042 35213c7c 2020-07-23 stsp ct->ondisk_path);
5043 35213c7c 2020-07-23 stsp goto done;
5045 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5046 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5048 35213c7c 2020-07-23 stsp goto done;
5049 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5050 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5051 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5052 35213c7c 2020-07-23 stsp goto done;
5057 cf066bf8 2019-05-09 stsp ct->status = status;
5058 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5059 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5060 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5061 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5062 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5063 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5064 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5065 b416585c 2019-05-13 stsp goto done;
5067 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5068 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5069 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5070 f0b75401 2019-08-03 stsp goto done;
5073 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5074 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5075 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5076 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5077 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5078 036813ee 2019-05-09 stsp goto done;
5081 24519714 2019-05-09 stsp ct->path = strdup(path);
5082 24519714 2019-05-09 stsp if (ct->path == NULL) {
5083 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5084 24519714 2019-05-09 stsp goto done;
5086 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5088 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5089 ed175427 2019-05-09 stsp free_commitable(ct);
5090 24519714 2019-05-09 stsp free(parent_path);
5091 036813ee 2019-05-09 stsp free(path);
5092 a129376b 2019-03-28 stsp return err;
5095 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5096 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5097 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5098 036813ee 2019-05-09 stsp struct got_repository *);
5100 ed175427 2019-05-09 stsp static const struct got_error *
5101 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5102 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5103 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5104 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5105 afa376bf 2019-05-09 stsp struct got_repository *repo)
5107 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5108 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5109 036813ee 2019-05-09 stsp char *subpath;
5111 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5112 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5113 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5115 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5117 ed175427 2019-05-09 stsp return err;
5119 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5120 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5121 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5122 036813ee 2019-05-09 stsp free(subpath);
5123 036813ee 2019-05-09 stsp return err;
5126 036813ee 2019-05-09 stsp static const struct got_error *
5127 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5129 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5130 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5132 036813ee 2019-05-09 stsp *match = 0;
5134 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5135 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5136 0f63689d 2019-05-10 stsp return NULL;
5139 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5141 0f63689d 2019-05-10 stsp return err;
5142 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5143 036813ee 2019-05-09 stsp free(ct_parent_path);
5144 036813ee 2019-05-09 stsp return err;
5147 768aea60 2019-05-09 stsp static mode_t
5148 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5150 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5151 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5153 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5156 036813ee 2019-05-09 stsp static const struct got_error *
5157 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5158 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5160 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5162 036813ee 2019-05-09 stsp *new_te = NULL;
5164 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5166 036813ee 2019-05-09 stsp goto done;
5168 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5170 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5171 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5172 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5174 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5176 036813ee 2019-05-09 stsp if (err && *new_te) {
5177 56e0773d 2019-11-28 stsp free(*new_te);
5178 036813ee 2019-05-09 stsp *new_te = NULL;
5180 036813ee 2019-05-09 stsp return err;
5183 036813ee 2019-05-09 stsp static const struct got_error *
5184 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5185 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5187 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5188 102b254e 2020-10-19 stsp char *ct_name = NULL;
5190 036813ee 2019-05-09 stsp *new_te = NULL;
5192 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5193 036813ee 2019-05-09 stsp if (*new_te == NULL)
5194 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5196 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5198 036813ee 2019-05-09 stsp goto done;
5199 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5200 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5201 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5202 036813ee 2019-05-09 stsp goto done;
5205 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5207 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5208 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5209 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5211 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5213 102b254e 2020-10-19 stsp free(ct_name);
5214 036813ee 2019-05-09 stsp if (err && *new_te) {
5215 56e0773d 2019-11-28 stsp free(*new_te);
5216 036813ee 2019-05-09 stsp *new_te = NULL;
5218 036813ee 2019-05-09 stsp return err;
5221 036813ee 2019-05-09 stsp static const struct got_error *
5222 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5223 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5225 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5226 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5228 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5230 036813ee 2019-05-09 stsp return err;
5231 036813ee 2019-05-09 stsp if (new_pe == NULL)
5232 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5233 036813ee 2019-05-09 stsp return NULL;
5236 afa376bf 2019-05-09 stsp static const struct got_error *
5237 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5238 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5240 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5241 5f8a88c6 2019-08-03 stsp unsigned char status;
5243 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5244 ae1e948a 2021-09-28 thomas return NULL;
5246 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5247 afa376bf 2019-05-09 stsp ct_path++;
5249 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5250 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5252 5f8a88c6 2019-08-03 stsp status = ct->status;
5254 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5255 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5258 036813ee 2019-05-09 stsp static const struct got_error *
5259 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5260 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5262 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5263 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5264 44d03001 2019-05-09 stsp char *te_path;
5266 44d03001 2019-05-09 stsp *modified = 0;
5268 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5269 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5270 44d03001 2019-05-09 stsp te->name) == -1)
5271 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5273 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5274 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5275 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5276 44d03001 2019-05-09 stsp strlen(te_path));
5277 62d463ca 2020-10-20 naddy if (*modified)
5281 44d03001 2019-05-09 stsp free(te_path);
5282 44d03001 2019-05-09 stsp return err;
5285 44d03001 2019-05-09 stsp static const struct got_error *
5286 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5287 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5288 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5290 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5291 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5293 036813ee 2019-05-09 stsp *ctp = NULL;
5295 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5296 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5297 036813ee 2019-05-09 stsp char *ct_name = NULL;
5298 036813ee 2019-05-09 stsp int path_matches;
5300 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5301 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5302 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5303 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5306 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5307 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5311 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5314 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5316 036813ee 2019-05-09 stsp return err;
5317 036813ee 2019-05-09 stsp if (!path_matches)
5320 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5322 d34b633e 2020-10-19 stsp return err;
5324 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5325 d34b633e 2020-10-19 stsp free(ct_name);
5328 d34b633e 2020-10-19 stsp free(ct_name);
5330 036813ee 2019-05-09 stsp *ctp = ct;
5334 2b496619 2019-07-10 stsp return err;
5337 2b496619 2019-07-10 stsp static const struct got_error *
5338 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5339 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5340 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5341 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5342 2b496619 2019-07-10 stsp struct got_repository *repo)
5344 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5345 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5346 2b496619 2019-07-10 stsp char *subtree_path;
5347 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5348 ba580f68 2020-03-22 stsp int nentries;
5350 2b496619 2019-07-10 stsp *new_tep = NULL;
5352 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5353 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5354 2b496619 2019-07-10 stsp child_path) == -1)
5355 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5357 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5358 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5359 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5360 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5362 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5363 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5364 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5365 2b496619 2019-07-10 stsp goto done;
5367 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5368 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5369 2b496619 2019-07-10 stsp if (err) {
5370 56e0773d 2019-11-28 stsp free(new_te);
5371 2b496619 2019-07-10 stsp goto done;
5373 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5376 2b496619 2019-07-10 stsp free(subtree_path);
5377 2b496619 2019-07-10 stsp if (err == NULL)
5378 2b496619 2019-07-10 stsp *new_tep = new_te;
5379 036813ee 2019-05-09 stsp return err;
5382 036813ee 2019-05-09 stsp static const struct got_error *
5383 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5384 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5385 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5386 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5387 036813ee 2019-05-09 stsp struct got_repository *repo)
5389 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5390 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5391 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5392 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5394 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5395 ba580f68 2020-03-22 stsp *nentries = 0;
5397 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5398 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5399 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5400 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5402 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5403 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5404 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5407 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5408 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5411 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5412 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5414 036813ee 2019-05-09 stsp goto done;
5416 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5417 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5418 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5420 036813ee 2019-05-09 stsp goto done;
5421 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5423 afa376bf 2019-05-09 stsp goto done;
5424 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5425 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5427 2f51b5b3 2019-05-09 stsp goto done;
5428 ba580f68 2020-03-22 stsp (*nentries)++;
5430 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5431 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5432 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5433 2b496619 2019-07-10 stsp == NULL) {
5434 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5435 2b496619 2019-07-10 stsp child_path, path_base_tree,
5436 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5439 2b496619 2019-07-10 stsp goto done;
5440 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5442 2b496619 2019-07-10 stsp goto done;
5443 ba580f68 2020-03-22 stsp (*nentries)++;
5448 2f51b5b3 2019-05-09 stsp if (base_tree) {
5449 56e0773d 2019-11-28 stsp int i, nbase_entries;
5450 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5451 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5452 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5453 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5455 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5456 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5457 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5458 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5460 63c5ca5d 2019-08-24 stsp goto done;
5461 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5463 63c5ca5d 2019-08-24 stsp goto done;
5464 ba580f68 2020-03-22 stsp (*nentries)++;
5468 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5469 44d03001 2019-05-09 stsp int modified;
5470 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5472 036813ee 2019-05-09 stsp goto done;
5473 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5474 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5476 2f51b5b3 2019-05-09 stsp goto done;
5477 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5478 44d03001 2019-05-09 stsp if (modified) {
5479 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5480 ba580f68 2020-03-22 stsp int nsubentries;
5481 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5482 ba580f68 2020-03-22 stsp &nsubentries, te,
5483 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5484 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5486 44d03001 2019-05-09 stsp goto done;
5487 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5488 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5489 ba580f68 2020-03-22 stsp free(new_id);
5492 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5493 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5494 56e0773d 2019-11-28 stsp free(new_id);
5496 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5498 036813ee 2019-05-09 stsp goto done;
5499 ba580f68 2020-03-22 stsp (*nentries)++;
5503 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5504 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5506 f66c734c 2019-09-22 stsp goto done;
5508 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5509 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5510 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5511 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5512 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5513 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5515 2f51b5b3 2019-05-09 stsp goto done;
5516 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5518 2f51b5b3 2019-05-09 stsp goto done;
5519 ba580f68 2020-03-22 stsp (*nentries)++;
5521 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5522 b416585c 2019-05-13 stsp status_arg);
5524 afa376bf 2019-05-09 stsp goto done;
5526 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5527 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5529 2f51b5b3 2019-05-09 stsp goto done;
5530 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5532 2f51b5b3 2019-05-09 stsp goto done;
5533 ba580f68 2020-03-22 stsp (*nentries)++;
5538 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5539 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5541 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5542 2e1fa222 2020-07-23 stsp return err;
5545 2e1fa222 2020-07-23 stsp static const struct got_error *
5546 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5547 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5548 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5549 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5551 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5552 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5553 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5555 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5556 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5557 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5559 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5561 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5562 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5563 437adc9d 2020-12-10 yzhong if (err)
5564 437adc9d 2020-12-10 yzhong goto done;
5567 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5568 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5569 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5570 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5571 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5572 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5573 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5574 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5576 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5577 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5578 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5579 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5580 72fd46fa 2019-09-06 stsp !have_staged_files);
5582 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5583 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5584 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5585 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5586 72fd46fa 2019-09-06 stsp !have_staged_files);
5588 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5590 437adc9d 2020-12-10 yzhong goto done;
5591 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5592 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5593 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5594 3969253a 2020-03-07 stsp if (err) {
5595 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5596 437adc9d 2020-12-10 yzhong goto done;
5598 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5599 3969253a 2020-03-07 stsp if (err) {
5600 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5601 437adc9d 2020-12-10 yzhong goto done;
5604 437adc9d 2020-12-10 yzhong free(relpath);
5605 437adc9d 2020-12-10 yzhong relpath = NULL;
5608 437adc9d 2020-12-10 yzhong free(relpath);
5609 d56d26ce 2019-05-10 stsp return err;
5613 d56d26ce 2019-05-10 stsp static const struct got_error *
5614 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5615 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5616 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5617 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5618 735ef5ac 2019-08-03 stsp int ood_errcode)
5620 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5621 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5622 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5624 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5625 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5626 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5627 1a36436d 2019-06-10 stsp return NULL;
5629 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5630 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5632 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5633 945f9229 2022-04-16 thomas if (err)
5634 945f9229 2022-04-16 thomas goto done;
5635 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5636 9bead371 2019-07-28 stsp if (err) {
5637 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5638 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5639 1a36436d 2019-06-10 stsp goto done;
5640 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5641 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5643 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5644 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5645 945f9229 2022-04-16 thomas if (err)
5646 945f9229 2022-04-16 thomas goto done;
5647 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5648 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5649 1a36436d 2019-06-10 stsp goto done;
5650 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5654 945f9229 2022-04-16 thomas if (commit)
5655 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5656 1a36436d 2019-06-10 stsp return err;
5659 ef20f542 2022-06-26 thomas static const struct got_error *
5660 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5661 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5662 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
5663 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
5664 10604dce 2021-09-24 thomas struct got_worktree *worktree,
5665 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5666 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5667 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5668 afa376bf 2019-05-09 stsp struct got_repository *repo)
5670 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5671 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5672 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5673 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5674 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5675 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5676 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5677 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5678 10604dce 2021-09-24 thomas int nentries, nparents = 0;
5679 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5680 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5681 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5683 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5685 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5687 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5689 588edf97 2019-05-10 stsp goto done;
5691 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5693 588edf97 2019-05-10 stsp goto done;
5695 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5696 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5701 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5702 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5706 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5707 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5708 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5709 cf066bf8 2019-05-09 stsp char *ondisk_path;
5711 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5712 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5713 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5716 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5717 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5718 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5721 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5722 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5723 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5724 cf066bf8 2019-05-09 stsp goto done;
5726 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5727 2e1fa222 2020-07-23 stsp free(ondisk_path);
5729 cf066bf8 2019-05-09 stsp goto done;
5732 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5733 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5734 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5736 036813ee 2019-05-09 stsp goto done;
5738 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5740 de18fc63 2019-05-09 stsp goto done;
5741 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5742 10604dce 2021-09-24 thomas nparents++;
5743 10604dce 2021-09-24 thomas if (parent_id2) {
5744 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
5745 10604dce 2021-09-24 thomas if (err)
5746 10604dce 2021-09-24 thomas goto done;
5747 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5748 10604dce 2021-09-24 thomas nparents++;
5750 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5751 10604dce 2021-09-24 thomas nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5752 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5753 33ad4cbe 2019-05-12 jcs free(logmsg);
5755 9d40349a 2019-05-09 stsp goto done;
5757 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5758 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5759 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5760 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5761 09f5bd90 2019-05-09 stsp goto done;
5763 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5764 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5766 09f5bd90 2019-05-09 stsp goto done;
5767 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5769 ebf99748 2019-05-09 stsp goto done;
5770 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5771 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5772 09f5bd90 2019-05-09 stsp goto done;
5774 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5775 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5777 f2c16586 2019-05-09 stsp goto done;
5778 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5780 f2c16586 2019-05-09 stsp goto done;
5782 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5784 09f5bd90 2019-05-09 stsp goto done;
5786 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5788 ebf99748 2019-05-09 stsp goto done;
5790 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
5791 588edf97 2019-05-10 stsp if (head_tree)
5792 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5793 588edf97 2019-05-10 stsp if (head_commit)
5794 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5795 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5796 2f17228e 2019-05-12 stsp if (head_ref2) {
5797 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5798 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5799 2f17228e 2019-05-12 stsp err = unlockerr;
5800 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5802 39cd0ff6 2019-07-12 stsp return err;
5805 5c1e53bc 2019-07-28 stsp static const struct got_error *
5806 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5807 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5809 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5810 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5812 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5813 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5814 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5816 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5817 5c1e53bc 2019-07-28 stsp ct_path++;
5819 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5820 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5824 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5825 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5827 5c1e53bc 2019-07-28 stsp return NULL;
5830 f0b75401 2019-08-03 stsp static const struct got_error *
5831 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5833 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5835 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5836 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5837 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5840 f0b75401 2019-08-03 stsp return NULL;
5843 5f8a88c6 2019-08-03 stsp static const struct got_error *
5844 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5845 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5847 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5848 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5850 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5851 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5853 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5854 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5855 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5856 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5857 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5858 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5861 5f8a88c6 2019-08-03 stsp return NULL;
5864 39cd0ff6 2019-07-12 stsp const struct got_error *
5865 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5866 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5867 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5868 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5869 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5870 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5872 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5873 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5874 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5875 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5876 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5877 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5878 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5879 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5880 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5882 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5884 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5886 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5888 39cd0ff6 2019-07-12 stsp goto done;
5890 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5892 39cd0ff6 2019-07-12 stsp goto done;
5894 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5896 39cd0ff6 2019-07-12 stsp goto done;
5898 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5900 39cd0ff6 2019-07-12 stsp goto done;
5902 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5903 5f8a88c6 2019-08-03 stsp &have_staged_files);
5904 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5905 5f8a88c6 2019-08-03 stsp goto done;
5906 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5907 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5909 5f8a88c6 2019-08-03 stsp goto done;
5912 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5913 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5914 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5915 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5916 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5917 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5918 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5919 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5920 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5922 5c1e53bc 2019-07-28 stsp goto done;
5925 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5926 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5927 39cd0ff6 2019-07-12 stsp goto done;
5930 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5931 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5933 5c1e53bc 2019-07-28 stsp goto done;
5936 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5937 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5938 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5940 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5941 f0b75401 2019-08-03 stsp ct_path++;
5942 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5943 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5944 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5946 b50cabdf 2019-07-12 stsp goto done;
5950 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5951 10604dce 2021-09-24 thomas head_commit_id, NULL, worktree, author, committer,
5952 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5954 39cd0ff6 2019-07-12 stsp goto done;
5956 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5957 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5958 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5959 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5960 39cd0ff6 2019-07-12 stsp err = sync_err;
5962 39cd0ff6 2019-07-12 stsp if (fileindex)
5963 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5964 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5965 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5966 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5967 39cd0ff6 2019-07-12 stsp err = unlockerr;
5968 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5969 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5970 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5972 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5973 c4296144 2019-05-09 stsp return err;
5976 8656d6c4 2019-05-20 stsp const char *
5977 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5979 8656d6c4 2019-05-20 stsp return ct->path;
5982 8656d6c4 2019-05-20 stsp unsigned int
5983 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5985 8656d6c4 2019-05-20 stsp return ct->status;
5988 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5989 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5990 818c7501 2019-07-11 stsp struct got_repository *repo;
5993 818c7501 2019-07-11 stsp static const struct got_error *
5994 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5996 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5997 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5998 818c7501 2019-07-11 stsp unsigned char status;
5999 818c7501 2019-07-11 stsp struct stat sb;
6000 818c7501 2019-07-11 stsp char *ondisk_path;
6002 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6003 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6004 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6005 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6007 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6009 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6011 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6012 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6013 818c7501 2019-07-11 stsp free(ondisk_path);
6015 818c7501 2019-07-11 stsp return err;
6017 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6018 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6019 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6020 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6022 818c7501 2019-07-11 stsp return NULL;
6025 818c7501 2019-07-11 stsp const struct got_error *
6026 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6027 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6028 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6029 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6031 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6032 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6033 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6034 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6035 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6036 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6037 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6039 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6040 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6041 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6043 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6045 818c7501 2019-07-11 stsp return err;
6047 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6049 818c7501 2019-07-11 stsp goto done;
6051 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6052 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6053 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6056 818c7501 2019-07-11 stsp goto done;
6058 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6060 818c7501 2019-07-11 stsp goto done;
6062 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6064 818c7501 2019-07-11 stsp goto done;
6066 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6068 818c7501 2019-07-11 stsp goto done;
6070 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6073 e51d7b55 2020-01-04 stsp goto done;
6075 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6077 818c7501 2019-07-11 stsp goto done;
6078 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6079 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6080 e51d7b55 2020-01-04 stsp goto done;
6083 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6084 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6086 818c7501 2019-07-11 stsp goto done;
6087 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6089 818c7501 2019-07-11 stsp goto done;
6091 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6093 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6095 818c7501 2019-07-11 stsp goto done;
6097 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6099 818c7501 2019-07-11 stsp goto done;
6101 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6102 818c7501 2019-07-11 stsp worktree->base_commit_id);
6104 818c7501 2019-07-11 stsp goto done;
6105 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6107 818c7501 2019-07-11 stsp goto done;
6109 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6111 818c7501 2019-07-11 stsp goto done;
6113 818c7501 2019-07-11 stsp free(fileindex_path);
6114 818c7501 2019-07-11 stsp free(tmp_branch_name);
6115 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6116 818c7501 2019-07-11 stsp free(branch_ref_name);
6117 818c7501 2019-07-11 stsp if (branch_ref)
6118 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6119 818c7501 2019-07-11 stsp if (wt_branch)
6120 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6121 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6122 818c7501 2019-07-11 stsp if (err) {
6123 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6124 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6125 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6127 818c7501 2019-07-11 stsp if (*tmp_branch) {
6128 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6129 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6131 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6132 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6133 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6135 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6137 818c7501 2019-07-11 stsp return err;
6140 818c7501 2019-07-11 stsp const struct got_error *
6141 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6142 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6143 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6144 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6146 818c7501 2019-07-11 stsp const struct got_error *err;
6147 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6148 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6149 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6150 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6151 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6153 818c7501 2019-07-11 stsp *commit_id = NULL;
6154 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6155 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6156 3e3a69f1 2019-07-25 stsp *branch = NULL;
6157 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6159 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6161 3e3a69f1 2019-07-25 stsp return err;
6163 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6165 f032f1f7 2019-08-04 stsp goto done;
6167 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6168 f032f1f7 2019-08-04 stsp &have_staged_files);
6169 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6170 f032f1f7 2019-08-04 stsp goto done;
6171 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6172 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6173 3e3a69f1 2019-07-25 stsp goto done;
6176 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6178 f032f1f7 2019-08-04 stsp goto done;
6180 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6182 818c7501 2019-07-11 stsp goto done;
6184 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6186 818c7501 2019-07-11 stsp goto done;
6188 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6190 818c7501 2019-07-11 stsp goto done;
6192 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6194 818c7501 2019-07-11 stsp goto done;
6196 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6197 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6199 818c7501 2019-07-11 stsp goto done;
6201 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6203 818c7501 2019-07-11 stsp goto done;
6205 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6207 818c7501 2019-07-11 stsp goto done;
6209 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6210 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6212 818c7501 2019-07-11 stsp goto done;
6214 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6216 818c7501 2019-07-11 stsp goto done;
6218 818c7501 2019-07-11 stsp free(commit_ref_name);
6219 818c7501 2019-07-11 stsp free(branch_ref_name);
6220 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6221 818c7501 2019-07-11 stsp if (commit_ref)
6222 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6223 818c7501 2019-07-11 stsp if (branch_ref)
6224 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6225 818c7501 2019-07-11 stsp if (err) {
6226 818c7501 2019-07-11 stsp free(*commit_id);
6227 818c7501 2019-07-11 stsp *commit_id = NULL;
6228 818c7501 2019-07-11 stsp if (*tmp_branch) {
6229 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6230 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6232 818c7501 2019-07-11 stsp if (*new_base_branch) {
6233 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6234 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6236 818c7501 2019-07-11 stsp if (*branch) {
6237 818c7501 2019-07-11 stsp got_ref_close(*branch);
6238 818c7501 2019-07-11 stsp *branch = NULL;
6240 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6241 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6242 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6244 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6246 818c7501 2019-07-11 stsp return err;
6249 818c7501 2019-07-11 stsp const struct got_error *
6250 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6252 818c7501 2019-07-11 stsp const struct got_error *err;
6253 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6255 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6257 818c7501 2019-07-11 stsp return err;
6259 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6260 818c7501 2019-07-11 stsp free(tmp_branch_name);
6261 818c7501 2019-07-11 stsp return NULL;
6264 818c7501 2019-07-11 stsp static const struct got_error *
6265 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6266 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6268 0ebf8283 2019-07-24 stsp *logmsg = arg;
6269 818c7501 2019-07-11 stsp return NULL;
6272 818c7501 2019-07-11 stsp static const struct got_error *
6273 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6274 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6275 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6276 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6278 818c7501 2019-07-11 stsp return NULL;
6281 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6282 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6283 01757395 2019-07-12 stsp void *progress_arg;
6284 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6287 01757395 2019-07-12 stsp static const struct got_error *
6288 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6290 01757395 2019-07-12 stsp const struct got_error *err;
6291 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6293 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6295 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6297 01757395 2019-07-12 stsp return err;
6299 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6300 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6301 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6302 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6303 01757395 2019-07-12 stsp return NULL;
6305 01757395 2019-07-12 stsp p = strdup(path);
6306 01757395 2019-07-12 stsp if (p == NULL)
6307 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6309 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6310 01757395 2019-07-12 stsp if (err || new == NULL)
6312 01757395 2019-07-12 stsp return err;
6316 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6318 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6320 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6321 01757395 2019-07-12 stsp free((char *)pe->path);
6323 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6326 0ebf8283 2019-07-24 stsp static const struct got_error *
6327 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6328 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6330 818c7501 2019-07-11 stsp const struct got_error *err;
6331 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6333 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6334 818c7501 2019-07-11 stsp if (err) {
6335 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6336 818c7501 2019-07-11 stsp goto done;
6337 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6339 818c7501 2019-07-11 stsp goto done;
6340 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6342 818c7501 2019-07-11 stsp goto done;
6343 de05890f 2020-03-05 stsp } else if (is_rebase) {
6344 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6347 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6349 818c7501 2019-07-11 stsp goto done;
6350 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6351 818c7501 2019-07-11 stsp free(stored_id);
6352 818c7501 2019-07-11 stsp if (cmp != 0) {
6353 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6354 818c7501 2019-07-11 stsp goto done;
6358 0ebf8283 2019-07-24 stsp if (commit_ref)
6359 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6360 0ebf8283 2019-07-24 stsp return err;
6363 0ebf8283 2019-07-24 stsp static const struct got_error *
6364 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6365 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6366 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6367 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6368 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6369 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6371 0ebf8283 2019-07-24 stsp const struct got_error *err;
6372 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6373 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6374 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6376 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6378 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6380 0ebf8283 2019-07-24 stsp return err;
6382 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6383 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6384 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6385 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6386 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6387 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6388 818c7501 2019-07-11 stsp if (commit_ref)
6389 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6390 818c7501 2019-07-11 stsp return err;
6393 818c7501 2019-07-11 stsp const struct got_error *
6394 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6395 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6396 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6397 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6398 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6399 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6401 0ebf8283 2019-07-24 stsp const struct got_error *err;
6402 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6404 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6406 0ebf8283 2019-07-24 stsp return err;
6408 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6410 0ebf8283 2019-07-24 stsp goto done;
6412 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6413 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6414 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6416 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6417 0ebf8283 2019-07-24 stsp return err;
6420 0ebf8283 2019-07-24 stsp const struct got_error *
6421 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6422 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6423 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6424 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6425 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6426 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6428 0ebf8283 2019-07-24 stsp const struct got_error *err;
6429 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6431 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6433 0ebf8283 2019-07-24 stsp return err;
6435 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6437 0ebf8283 2019-07-24 stsp goto done;
6439 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6440 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6441 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6443 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6444 0ebf8283 2019-07-24 stsp return err;
6447 0ebf8283 2019-07-24 stsp static const struct got_error *
6448 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6449 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6450 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6451 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6452 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6454 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6455 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6456 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6457 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6458 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6459 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6460 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6462 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6463 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6465 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6467 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6469 3e3a69f1 2019-07-25 stsp return err;
6471 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6472 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6473 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6474 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6476 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6477 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6479 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6480 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6481 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6482 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6483 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6484 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6486 01757395 2019-07-12 stsp if (merged_paths) {
6487 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6488 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6489 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6490 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6493 01757395 2019-07-12 stsp goto done;
6496 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6497 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6499 01757395 2019-07-12 stsp goto done;
6502 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6503 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6504 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6506 a0e95631 2019-07-12 stsp goto done;
6507 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6508 a0e95631 2019-07-12 stsp goto done;
6511 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6513 edd02c5e 2019-07-11 stsp goto done;
6515 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6517 818c7501 2019-07-11 stsp goto done;
6519 5943eee2 2019-08-13 stsp if (new_logmsg) {
6520 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6521 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6522 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6523 5943eee2 2019-08-13 stsp goto done;
6526 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6528 5943eee2 2019-08-13 stsp goto done;
6531 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6532 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6533 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
6534 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6535 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6537 a0e95631 2019-07-12 stsp goto done;
6539 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6541 a0e95631 2019-07-12 stsp goto done;
6543 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6545 a0e95631 2019-07-12 stsp goto done;
6547 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6548 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6549 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6550 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6551 a0e95631 2019-07-12 stsp err = sync_err;
6553 a0e95631 2019-07-12 stsp free(fileindex_path);
6554 a0e95631 2019-07-12 stsp free(head_commit_id);
6555 a0e95631 2019-07-12 stsp if (head_ref)
6556 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6557 818c7501 2019-07-11 stsp if (err) {
6558 818c7501 2019-07-11 stsp free(*new_commit_id);
6559 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6561 818c7501 2019-07-11 stsp return err;
6564 818c7501 2019-07-11 stsp const struct got_error *
6565 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6566 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6567 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6568 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6569 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6571 0ebf8283 2019-07-24 stsp const struct got_error *err;
6572 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6573 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6574 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6576 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6578 0ebf8283 2019-07-24 stsp return err;
6580 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6582 0ebf8283 2019-07-24 stsp goto done;
6583 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6585 0ebf8283 2019-07-24 stsp goto done;
6586 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6587 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6588 0ebf8283 2019-07-24 stsp goto done;
6591 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6592 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6594 0ebf8283 2019-07-24 stsp if (commit_ref)
6595 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6596 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6597 0ebf8283 2019-07-24 stsp free(commit_id);
6598 0ebf8283 2019-07-24 stsp return err;
6601 0ebf8283 2019-07-24 stsp const struct got_error *
6602 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6603 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6604 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6605 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6606 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6607 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6609 0ebf8283 2019-07-24 stsp const struct got_error *err;
6610 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6611 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6613 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6615 0ebf8283 2019-07-24 stsp return err;
6617 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6619 0ebf8283 2019-07-24 stsp goto done;
6621 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6622 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6624 0ebf8283 2019-07-24 stsp if (commit_ref)
6625 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6626 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6627 0ebf8283 2019-07-24 stsp return err;
6630 0ebf8283 2019-07-24 stsp const struct got_error *
6631 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6632 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6634 3e3a69f1 2019-07-25 stsp if (fileindex)
6635 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6636 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6639 69844fba 2019-07-11 stsp static const struct got_error *
6640 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6642 69844fba 2019-07-11 stsp const struct got_error *err;
6643 69844fba 2019-07-11 stsp struct got_reference *ref;
6645 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6646 69844fba 2019-07-11 stsp if (err) {
6647 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6648 69844fba 2019-07-11 stsp return NULL;
6649 69844fba 2019-07-11 stsp return err;
6652 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6653 69844fba 2019-07-11 stsp got_ref_close(ref);
6654 69844fba 2019-07-11 stsp return err;
6657 69844fba 2019-07-11 stsp static const struct got_error *
6658 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6660 69844fba 2019-07-11 stsp const struct got_error *err;
6661 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6662 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6664 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6666 69844fba 2019-07-11 stsp goto done;
6667 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6669 69844fba 2019-07-11 stsp goto done;
6671 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6673 69844fba 2019-07-11 stsp goto done;
6674 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6676 69844fba 2019-07-11 stsp goto done;
6678 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6680 69844fba 2019-07-11 stsp goto done;
6681 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6683 69844fba 2019-07-11 stsp goto done;
6685 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6687 69844fba 2019-07-11 stsp goto done;
6688 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6690 69844fba 2019-07-11 stsp goto done;
6693 69844fba 2019-07-11 stsp free(tmp_branch_name);
6694 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6695 69844fba 2019-07-11 stsp free(branch_ref_name);
6696 69844fba 2019-07-11 stsp free(commit_ref_name);
6697 e600f124 2021-03-21 stsp return err;
6700 ef20f542 2022-06-26 thomas static const struct got_error *
6701 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6702 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6704 e600f124 2021-03-21 stsp const struct got_error *err;
6705 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6706 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6707 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6708 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6709 e600f124 2021-03-21 stsp char *refname = NULL;
6711 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6712 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6713 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6714 e600f124 2021-03-21 stsp branch_name += 11;
6716 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6718 e600f124 2021-03-21 stsp return err;
6720 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6721 e600f124 2021-03-21 stsp new_id_str) == -1) {
6722 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6723 e600f124 2021-03-21 stsp goto done;
6726 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6728 e600f124 2021-03-21 stsp goto done;
6730 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6732 e600f124 2021-03-21 stsp goto done;
6734 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6736 e600f124 2021-03-21 stsp free(new_id_str);
6737 e600f124 2021-03-21 stsp free(refname);
6738 e600f124 2021-03-21 stsp free(old_commit_id);
6740 e600f124 2021-03-21 stsp got_ref_close(ref);
6741 69844fba 2019-07-11 stsp return err;
6744 818c7501 2019-07-11 stsp const struct got_error *
6745 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6746 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6747 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6748 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6750 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6751 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6752 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6754 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6756 818c7501 2019-07-11 stsp return err;
6758 e600f124 2021-03-21 stsp if (create_backup) {
6759 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6760 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6762 e600f124 2021-03-21 stsp goto done;
6765 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6767 818c7501 2019-07-11 stsp goto done;
6769 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6771 818c7501 2019-07-11 stsp goto done;
6773 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6775 818c7501 2019-07-11 stsp goto done;
6777 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6779 a615e0e7 2020-12-16 stsp goto done;
6781 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6783 a615e0e7 2020-12-16 stsp goto done;
6784 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6785 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6786 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6787 a615e0e7 2020-12-16 stsp err = sync_err;
6789 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6790 a615e0e7 2020-12-16 stsp free(fileindex_path);
6791 818c7501 2019-07-11 stsp free(new_head_commit_id);
6792 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6793 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6794 818c7501 2019-07-11 stsp err = unlockerr;
6795 818c7501 2019-07-11 stsp return err;
6798 818c7501 2019-07-11 stsp const struct got_error *
6799 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6800 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6801 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6802 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
6804 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6805 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6806 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6807 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6808 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6809 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6810 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6812 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6814 818c7501 2019-07-11 stsp return err;
6816 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
6817 945f9229 2022-04-16 thomas worktree->base_commit_id);
6818 945f9229 2022-04-16 thomas if (err)
6819 945f9229 2022-04-16 thomas goto done;
6821 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6822 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6824 818c7501 2019-07-11 stsp goto done;
6826 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6828 818c7501 2019-07-11 stsp goto done;
6831 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6832 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6833 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6835 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6837 818c7501 2019-07-11 stsp goto done;
6839 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6841 818c7501 2019-07-11 stsp goto done;
6843 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
6844 945f9229 2022-04-16 thomas worktree->path_prefix);
6846 ca355955 2019-07-12 stsp goto done;
6848 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6850 ca355955 2019-07-12 stsp goto done;
6852 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6854 818c7501 2019-07-11 stsp goto done;
6856 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6857 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6858 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6859 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6860 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6861 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6862 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6863 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
6864 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6865 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
6867 1f1abb7e 2019-08-08 stsp goto sync;
6869 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6870 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6872 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6873 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6874 ca355955 2019-07-12 stsp err = sync_err;
6876 818c7501 2019-07-11 stsp got_ref_close(resolved);
6877 a3a2faf2 2019-07-12 stsp free(tree_id);
6878 818c7501 2019-07-11 stsp free(commit_id);
6879 945f9229 2022-04-16 thomas if (commit)
6880 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6881 0ebf8283 2019-07-24 stsp if (fileindex)
6882 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6883 0ebf8283 2019-07-24 stsp free(fileindex_path);
6885 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6886 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6887 0ebf8283 2019-07-24 stsp err = unlockerr;
6888 0ebf8283 2019-07-24 stsp return err;
6891 0ebf8283 2019-07-24 stsp const struct got_error *
6892 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6893 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6894 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6895 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6897 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6898 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6899 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6900 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6901 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6902 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6903 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6904 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6906 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6907 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6908 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6909 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6911 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6913 0ebf8283 2019-07-24 stsp return err;
6915 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6917 0ebf8283 2019-07-24 stsp goto done;
6919 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6920 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6921 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6924 0ebf8283 2019-07-24 stsp goto done;
6926 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6928 0ebf8283 2019-07-24 stsp goto done;
6930 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6932 0ebf8283 2019-07-24 stsp goto done;
6934 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6935 0ebf8283 2019-07-24 stsp worktree);
6937 0ebf8283 2019-07-24 stsp goto done;
6939 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6942 0ebf8283 2019-07-24 stsp goto done;
6944 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6946 0ebf8283 2019-07-24 stsp goto done;
6948 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6950 0ebf8283 2019-07-24 stsp goto done;
6952 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6953 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6955 0ebf8283 2019-07-24 stsp goto done;
6956 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6958 0ebf8283 2019-07-24 stsp goto done;
6959 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6960 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6961 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6962 0ebf8283 2019-07-24 stsp goto done;
6965 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6966 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6968 0ebf8283 2019-07-24 stsp goto done;
6969 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6971 0ebf8283 2019-07-24 stsp goto done;
6973 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6975 0ebf8283 2019-07-24 stsp goto done;
6977 0ebf8283 2019-07-24 stsp free(fileindex_path);
6978 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6979 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6980 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6981 0ebf8283 2019-07-24 stsp if (wt_branch)
6982 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6983 0ebf8283 2019-07-24 stsp if (err) {
6984 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6985 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6986 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6988 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6989 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6990 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6992 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6993 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6994 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6995 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6997 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6999 0ebf8283 2019-07-24 stsp return err;
7002 0ebf8283 2019-07-24 stsp const struct got_error *
7003 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7004 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7006 3e3a69f1 2019-07-25 stsp if (fileindex)
7007 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7008 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7011 0ebf8283 2019-07-24 stsp const struct got_error *
7012 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7013 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7015 0ebf8283 2019-07-24 stsp const struct got_error *err;
7016 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7018 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7020 0ebf8283 2019-07-24 stsp return err;
7022 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7023 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7024 0ebf8283 2019-07-24 stsp return NULL;
7027 0ebf8283 2019-07-24 stsp const struct got_error *
7028 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7029 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7030 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7031 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7033 0ebf8283 2019-07-24 stsp const struct got_error *err;
7034 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7035 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7036 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7037 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7038 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7039 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7041 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7042 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7043 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7044 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7046 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7048 3e3a69f1 2019-07-25 stsp return err;
7050 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7052 f032f1f7 2019-08-04 stsp goto done;
7054 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7055 f032f1f7 2019-08-04 stsp &have_staged_files);
7056 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7057 f032f1f7 2019-08-04 stsp goto done;
7058 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7059 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7060 3e3a69f1 2019-07-25 stsp goto done;
7063 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7065 f032f1f7 2019-08-04 stsp goto done;
7067 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7069 0ebf8283 2019-07-24 stsp goto done;
7071 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7073 0ebf8283 2019-07-24 stsp goto done;
7075 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7076 0ebf8283 2019-07-24 stsp worktree);
7078 0ebf8283 2019-07-24 stsp goto done;
7080 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7082 0ebf8283 2019-07-24 stsp goto done;
7084 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7086 0ebf8283 2019-07-24 stsp goto done;
7087 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7089 0ebf8283 2019-07-24 stsp goto done;
7091 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7093 0ebf8283 2019-07-24 stsp goto done;
7094 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7096 0ebf8283 2019-07-24 stsp goto done;
7098 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7100 0ebf8283 2019-07-24 stsp goto done;
7102 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7103 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7104 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7105 0ebf8283 2019-07-24 stsp if (commit_ref)
7106 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7107 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7108 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7109 0ebf8283 2019-07-24 stsp if (err) {
7110 0ebf8283 2019-07-24 stsp free(*commit_id);
7111 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7112 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7113 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7114 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7115 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7116 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7118 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7119 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7120 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7122 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7124 0ebf8283 2019-07-24 stsp return err;
7127 0ebf8283 2019-07-24 stsp static const struct got_error *
7128 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7130 0ebf8283 2019-07-24 stsp const struct got_error *err;
7131 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7132 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7134 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7136 0ebf8283 2019-07-24 stsp goto done;
7137 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7139 0ebf8283 2019-07-24 stsp goto done;
7141 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7142 0ebf8283 2019-07-24 stsp worktree);
7144 0ebf8283 2019-07-24 stsp goto done;
7145 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7147 0ebf8283 2019-07-24 stsp goto done;
7149 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7151 0ebf8283 2019-07-24 stsp goto done;
7152 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7154 0ebf8283 2019-07-24 stsp goto done;
7156 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7158 0ebf8283 2019-07-24 stsp goto done;
7159 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7161 0ebf8283 2019-07-24 stsp goto done;
7163 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7164 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7165 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7166 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7167 0ebf8283 2019-07-24 stsp return err;
7170 0ebf8283 2019-07-24 stsp const struct got_error *
7171 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7172 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7173 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7174 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7176 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7177 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7178 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7179 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7180 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7181 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7183 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7185 0ebf8283 2019-07-24 stsp return err;
7187 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7188 945f9229 2022-04-16 thomas worktree->base_commit_id);
7189 945f9229 2022-04-16 thomas if (err)
7190 945f9229 2022-04-16 thomas goto done;
7192 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7193 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7195 0ebf8283 2019-07-24 stsp goto done;
7197 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7199 0ebf8283 2019-07-24 stsp goto done;
7201 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7203 0ebf8283 2019-07-24 stsp goto done;
7205 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7206 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7208 0ebf8283 2019-07-24 stsp goto done;
7210 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7212 0ebf8283 2019-07-24 stsp goto done;
7214 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7216 0ebf8283 2019-07-24 stsp goto done;
7218 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7219 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7220 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7221 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7222 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7223 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7224 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7225 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7226 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7227 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7229 1f1abb7e 2019-08-08 stsp goto sync;
7231 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7232 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7234 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7235 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7236 0ebf8283 2019-07-24 stsp err = sync_err;
7238 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7239 0ebf8283 2019-07-24 stsp free(tree_id);
7240 818c7501 2019-07-11 stsp free(fileindex_path);
7242 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7243 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7244 818c7501 2019-07-11 stsp err = unlockerr;
7245 818c7501 2019-07-11 stsp return err;
7248 0ebf8283 2019-07-24 stsp const struct got_error *
7249 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7250 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7251 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7253 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7254 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7255 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7256 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7258 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7260 0ebf8283 2019-07-24 stsp return err;
7262 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7263 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7265 e600f124 2021-03-21 stsp goto done;
7267 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7268 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7270 0ebf8283 2019-07-24 stsp goto done;
7272 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7274 0ebf8283 2019-07-24 stsp goto done;
7276 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7278 0ebf8283 2019-07-24 stsp goto done;
7280 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7282 0ebf8283 2019-07-24 stsp goto done;
7284 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7286 a615e0e7 2020-12-16 stsp goto done;
7288 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7290 a615e0e7 2020-12-16 stsp goto done;
7291 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7292 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7293 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7294 a615e0e7 2020-12-16 stsp err = sync_err;
7296 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7297 a615e0e7 2020-12-16 stsp free(fileindex_path);
7298 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7299 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7300 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7301 0ebf8283 2019-07-24 stsp err = unlockerr;
7302 0ebf8283 2019-07-24 stsp return err;
7305 0ebf8283 2019-07-24 stsp const struct got_error *
7306 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7307 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7309 0ebf8283 2019-07-24 stsp const struct got_error *err;
7310 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7312 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7314 0ebf8283 2019-07-24 stsp return err;
7316 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7318 0ebf8283 2019-07-24 stsp goto done;
7320 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7322 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7323 2822a352 2019-10-15 stsp return err;
7326 2822a352 2019-10-15 stsp const struct got_error *
7327 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7328 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7329 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7330 2822a352 2019-10-15 stsp struct got_repository *repo)
7332 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7333 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7334 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7336 2822a352 2019-10-15 stsp *fileindex = NULL;
7337 2822a352 2019-10-15 stsp *branch_ref = NULL;
7338 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7340 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7342 2822a352 2019-10-15 stsp return err;
7344 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7345 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7346 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7347 2822a352 2019-10-15 stsp "update -b or different branch name required");
7348 2822a352 2019-10-15 stsp goto done;
7351 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7353 2822a352 2019-10-15 stsp goto done;
7355 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7356 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7357 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7358 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7361 2822a352 2019-10-15 stsp goto done;
7363 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7365 2822a352 2019-10-15 stsp goto done;
7367 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7368 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7370 2822a352 2019-10-15 stsp if (err) {
7371 2822a352 2019-10-15 stsp if (*branch_ref) {
7372 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7373 2822a352 2019-10-15 stsp *branch_ref = NULL;
7375 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7376 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7377 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7379 2822a352 2019-10-15 stsp if (*fileindex) {
7380 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7381 2822a352 2019-10-15 stsp *fileindex = NULL;
7383 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7385 0cb83759 2019-08-03 stsp return err;
7388 2822a352 2019-10-15 stsp const struct got_error *
7389 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7390 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7391 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7392 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7393 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7395 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7396 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7397 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7398 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7400 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7402 2822a352 2019-10-15 stsp goto done;
7404 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7406 2822a352 2019-10-15 stsp goto done;
7408 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
7409 945f9229 2022-04-16 thomas if (err)
7410 945f9229 2022-04-16 thomas goto done;
7412 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7413 2822a352 2019-10-15 stsp worktree->path_prefix);
7415 2822a352 2019-10-15 stsp goto done;
7417 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7419 2822a352 2019-10-15 stsp goto done;
7421 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7422 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7424 2822a352 2019-10-15 stsp goto sync;
7426 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7428 2822a352 2019-10-15 stsp goto sync;
7430 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7432 6e210706 2021-01-22 stsp goto sync;
7434 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7436 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7437 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7438 2822a352 2019-10-15 stsp err = sync_err;
7441 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7442 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7443 2822a352 2019-10-15 stsp err = unlockerr;
7444 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7446 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7447 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7448 2822a352 2019-10-15 stsp err = unlockerr;
7449 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7451 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7452 2822a352 2019-10-15 stsp free(fileindex_path);
7453 2822a352 2019-10-15 stsp free(tree_id);
7454 945f9229 2022-04-16 thomas if (commit)
7455 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7457 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7458 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7459 2822a352 2019-10-15 stsp err = unlockerr;
7460 2822a352 2019-10-15 stsp return err;
7463 2822a352 2019-10-15 stsp const struct got_error *
7464 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7465 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7466 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7468 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7470 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7472 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7474 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7475 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7476 8b692cd0 2019-10-21 stsp err = unlockerr;
7477 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7479 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7480 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7481 8b692cd0 2019-10-21 stsp err = unlockerr;
7482 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7484 10604dce 2021-09-24 thomas return err;
7487 10604dce 2021-09-24 thomas const struct got_error *
7488 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
7489 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
7491 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
7492 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7494 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7495 10604dce 2021-09-24 thomas if (err)
7496 10604dce 2021-09-24 thomas goto done;
7498 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7500 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
7501 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7502 10604dce 2021-09-24 thomas err = sync_err;
7504 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7505 10604dce 2021-09-24 thomas free(fileindex_path);
7506 8b692cd0 2019-10-21 stsp return err;
7509 10604dce 2021-09-24 thomas static const struct got_error *
7510 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7512 10604dce 2021-09-24 thomas const struct got_error *err;
7513 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7515 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7516 10604dce 2021-09-24 thomas if (err)
7517 10604dce 2021-09-24 thomas goto done;
7518 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
7519 10604dce 2021-09-24 thomas if (err)
7520 10604dce 2021-09-24 thomas goto done;
7522 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7523 10604dce 2021-09-24 thomas if (err)
7524 10604dce 2021-09-24 thomas goto done;
7525 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
7526 10604dce 2021-09-24 thomas if (err)
7527 10604dce 2021-09-24 thomas goto done;
7530 10604dce 2021-09-24 thomas free(branch_refname);
7531 10604dce 2021-09-24 thomas free(commit_refname);
7532 10604dce 2021-09-24 thomas return err;
7535 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
7536 10604dce 2021-09-24 thomas struct got_worktree *worktree;
7537 10604dce 2021-09-24 thomas const char *branch_name;
7540 10604dce 2021-09-24 thomas static const struct got_error *
7541 10604dce 2021-09-24 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7542 10604dce 2021-09-24 thomas void *arg)
7544 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
7546 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7547 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
7548 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
7550 10604dce 2021-09-24 thomas return NULL;
7554 10604dce 2021-09-24 thomas const struct got_error *
7555 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
7556 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
7557 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
7558 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
7559 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7560 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7562 10604dce 2021-09-24 thomas const struct got_error *err;
7563 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7565 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7566 10604dce 2021-09-24 thomas if (err)
7567 10604dce 2021-09-24 thomas goto done;
7569 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7570 10604dce 2021-09-24 thomas worktree);
7571 10604dce 2021-09-24 thomas if (err)
7572 10604dce 2021-09-24 thomas goto done;
7574 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7575 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
7576 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
7578 10604dce 2021-09-24 thomas free(fileindex_path);
7579 10604dce 2021-09-24 thomas return err;
7582 10604dce 2021-09-24 thomas const struct got_error *
7583 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
7584 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
7585 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
7586 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
7587 ae1e948a 2021-09-28 thomas struct got_repository *repo,
7588 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
7591 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
7592 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
7593 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
7594 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
7595 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
7596 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
7597 10604dce 2021-09-24 thomas int have_staged_files = 0;
7598 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
7599 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7601 10604dce 2021-09-24 thomas *new_commit_id = NULL;
7603 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
7605 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7606 10604dce 2021-09-24 thomas if (err)
7607 10604dce 2021-09-24 thomas goto done;
7609 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7610 10604dce 2021-09-24 thomas if (err)
7611 10604dce 2021-09-24 thomas goto done;
7613 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
7614 10604dce 2021-09-24 thomas if (err)
7615 10604dce 2021-09-24 thomas goto done;
7617 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7618 10604dce 2021-09-24 thomas &have_staged_files);
7619 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7620 10604dce 2021-09-24 thomas goto done;
7621 10604dce 2021-09-24 thomas if (have_staged_files) {
7622 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7623 10604dce 2021-09-24 thomas goto done;
7626 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
7627 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
7628 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
7629 10604dce 2021-09-24 thomas cc_arg.repo = repo;
7630 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
7631 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7632 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7633 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7634 10604dce 2021-09-24 thomas if (err)
7635 10604dce 2021-09-24 thomas goto done;
7637 10604dce 2021-09-24 thomas if (TAILQ_EMPTY(&commitable_paths)) {
7638 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7639 10604dce 2021-09-24 thomas "merge of %s cannot proceed", branch_name);
7640 10604dce 2021-09-24 thomas goto done;
7643 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7644 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7645 10604dce 2021-09-24 thomas const char *ct_path = ct->in_repo_path;
7647 10604dce 2021-09-24 thomas while (ct_path[0] == '/')
7648 10604dce 2021-09-24 thomas ct_path++;
7649 10604dce 2021-09-24 thomas err = check_out_of_date(ct_path, ct->status,
7650 10604dce 2021-09-24 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7651 10604dce 2021-09-24 thomas head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7652 10604dce 2021-09-24 thomas if (err)
7653 10604dce 2021-09-24 thomas goto done;
7657 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
7658 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
7659 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
7660 10604dce 2021-09-24 thomas head_commit_id, branch_tip, worktree, author, committer,
7661 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7662 10604dce 2021-09-24 thomas if (err)
7663 10604dce 2021-09-24 thomas goto done;
7665 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
7666 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
7667 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7668 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7669 10604dce 2021-09-24 thomas err = sync_err;
7671 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7672 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7673 10604dce 2021-09-24 thomas free_commitable(ct);
7675 10604dce 2021-09-24 thomas got_pathlist_free(&commitable_paths);
7676 10604dce 2021-09-24 thomas free(fileindex_path);
7677 10604dce 2021-09-24 thomas return err;
7680 10604dce 2021-09-24 thomas const struct got_error *
7681 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
7682 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
7684 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
7685 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7687 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
7688 10604dce 2021-09-24 thomas if (err)
7689 10604dce 2021-09-24 thomas goto done;
7691 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7692 10604dce 2021-09-24 thomas if (err)
7693 10604dce 2021-09-24 thomas goto done;
7694 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7695 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7696 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7697 10604dce 2021-09-24 thomas err = sync_err;
7699 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7700 10604dce 2021-09-24 thomas free(fileindex_path);
7701 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
7702 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
7703 10604dce 2021-09-24 thomas err = unlockerr;
7704 10604dce 2021-09-24 thomas return err;
7707 10604dce 2021-09-24 thomas const struct got_error *
7708 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7709 10604dce 2021-09-24 thomas struct got_repository *repo)
7711 10604dce 2021-09-24 thomas const struct got_error *err;
7712 10604dce 2021-09-24 thomas char *branch_refname = NULL;
7713 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
7715 10604dce 2021-09-24 thomas *in_progress = 0;
7717 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7718 10604dce 2021-09-24 thomas if (err)
7719 10604dce 2021-09-24 thomas return err;
7720 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7721 dfe86d1f 2021-09-24 thomas free(branch_refname);
7722 10604dce 2021-09-24 thomas if (err) {
7723 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
7724 10604dce 2021-09-24 thomas return err;
7726 10604dce 2021-09-24 thomas *in_progress = 1;
7728 10604dce 2021-09-24 thomas return NULL;
7731 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
7732 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
7733 10604dce 2021-09-24 thomas struct got_reference *branch, struct got_repository *repo)
7735 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
7736 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7737 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7738 10604dce 2021-09-24 thomas struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7739 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL;
7740 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7741 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
7743 10604dce 2021-09-24 thomas *fileindex = NULL;
7745 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
7746 10604dce 2021-09-24 thomas if (err)
7747 10604dce 2021-09-24 thomas return err;
7749 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
7750 10604dce 2021-09-24 thomas if (err)
7751 10604dce 2021-09-24 thomas goto done;
7753 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
7754 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
7755 10604dce 2021-09-24 thomas ok_arg.repo = repo;
7756 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7757 10604dce 2021-09-24 thomas &ok_arg);
7758 10604dce 2021-09-24 thomas if (err)
7759 10604dce 2021-09-24 thomas goto done;
7761 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7762 10604dce 2021-09-24 thomas if (err)
7763 10604dce 2021-09-24 thomas return err;
7765 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7766 10604dce 2021-09-24 thomas if (err)
7767 10604dce 2021-09-24 thomas return err;
7769 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7771 10604dce 2021-09-24 thomas if (err)
7772 10604dce 2021-09-24 thomas goto done;
7774 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7775 10604dce 2021-09-24 thomas if (err)
7776 10604dce 2021-09-24 thomas goto done;
7778 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7779 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7780 10604dce 2021-09-24 thomas goto done;
7783 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
7784 10604dce 2021-09-24 thomas if (err)
7785 10604dce 2021-09-24 thomas goto done;
7787 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7788 10604dce 2021-09-24 thomas if (err)
7789 10604dce 2021-09-24 thomas goto done;
7790 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
7791 10604dce 2021-09-24 thomas if (err)
7792 10604dce 2021-09-24 thomas goto done;
7794 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7795 10604dce 2021-09-24 thomas if (err)
7796 10604dce 2021-09-24 thomas goto done;
7797 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
7798 10604dce 2021-09-24 thomas if (err)
7799 10604dce 2021-09-24 thomas goto done;
7802 10604dce 2021-09-24 thomas free(branch_refname);
7803 10604dce 2021-09-24 thomas free(commit_refname);
7804 10604dce 2021-09-24 thomas free(fileindex_path);
7805 10604dce 2021-09-24 thomas if (branch_ref)
7806 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
7807 10604dce 2021-09-24 thomas if (commit_ref)
7808 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
7809 10604dce 2021-09-24 thomas if (wt_branch)
7810 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
7811 10604dce 2021-09-24 thomas free(wt_branch_tip);
7812 10604dce 2021-09-24 thomas if (err) {
7813 10604dce 2021-09-24 thomas if (*fileindex) {
7814 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
7815 10604dce 2021-09-24 thomas *fileindex = NULL;
7817 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
7819 10604dce 2021-09-24 thomas return err;
7822 10604dce 2021-09-24 thomas const struct got_error *
7823 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
7824 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7825 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
7827 10604dce 2021-09-24 thomas const struct got_error *err;
7828 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
7829 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7830 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7831 10604dce 2021-09-24 thomas int have_staged_files = 0;
7833 10604dce 2021-09-24 thomas *branch_name = NULL;
7834 10604dce 2021-09-24 thomas *branch_tip = NULL;
7835 10604dce 2021-09-24 thomas *fileindex = NULL;
7837 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
7838 10604dce 2021-09-24 thomas if (err)
7839 10604dce 2021-09-24 thomas return err;
7841 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
7842 10604dce 2021-09-24 thomas if (err)
7843 10604dce 2021-09-24 thomas goto done;
7845 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7846 10604dce 2021-09-24 thomas &have_staged_files);
7847 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7848 10604dce 2021-09-24 thomas goto done;
7849 10604dce 2021-09-24 thomas if (have_staged_files) {
7850 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
7851 10604dce 2021-09-24 thomas goto done;
7854 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7855 10604dce 2021-09-24 thomas if (err)
7856 10604dce 2021-09-24 thomas goto done;
7858 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7859 10604dce 2021-09-24 thomas if (err)
7860 10604dce 2021-09-24 thomas goto done;
7862 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7863 10604dce 2021-09-24 thomas if (err)
7864 10604dce 2021-09-24 thomas goto done;
7866 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
7867 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7868 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
7869 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
7870 10604dce 2021-09-24 thomas goto done;
7872 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7873 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
7874 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
7875 10604dce 2021-09-24 thomas goto done;
7878 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7879 10604dce 2021-09-24 thomas if (err)
7880 10604dce 2021-09-24 thomas goto done;
7882 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
7883 10604dce 2021-09-24 thomas if (err)
7884 10604dce 2021-09-24 thomas goto done;
7886 10604dce 2021-09-24 thomas free(commit_refname);
7887 10604dce 2021-09-24 thomas free(branch_refname);
7888 10604dce 2021-09-24 thomas free(fileindex_path);
7889 10604dce 2021-09-24 thomas if (commit_ref)
7890 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
7891 10604dce 2021-09-24 thomas if (branch_ref)
7892 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
7893 10604dce 2021-09-24 thomas if (err) {
7894 10604dce 2021-09-24 thomas if (*branch_name) {
7895 10604dce 2021-09-24 thomas free(*branch_name);
7896 10604dce 2021-09-24 thomas *branch_name = NULL;
7898 10604dce 2021-09-24 thomas free(*branch_tip);
7899 10604dce 2021-09-24 thomas *branch_tip = NULL;
7900 10604dce 2021-09-24 thomas if (*fileindex) {
7901 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
7902 10604dce 2021-09-24 thomas *fileindex = NULL;
7904 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
7906 10604dce 2021-09-24 thomas return err;
7909 10604dce 2021-09-24 thomas const struct got_error *
7910 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
7911 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
7912 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
7914 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
7915 10604dce 2021-09-24 thomas struct got_object_id *commit_id = NULL;
7916 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7917 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7918 10604dce 2021-09-24 thomas struct revert_file_args rfa;
7919 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
7921 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7922 945f9229 2022-04-16 thomas worktree->base_commit_id);
7923 945f9229 2022-04-16 thomas if (err)
7924 945f9229 2022-04-16 thomas goto done;
7926 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7927 945f9229 2022-04-16 thomas worktree->path_prefix);
7928 10604dce 2021-09-24 thomas if (err)
7929 10604dce 2021-09-24 thomas goto done;
7931 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
7932 10604dce 2021-09-24 thomas if (err)
7933 10604dce 2021-09-24 thomas goto done;
7935 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7936 10604dce 2021-09-24 thomas if (err)
7937 10604dce 2021-09-24 thomas goto done;
7939 10604dce 2021-09-24 thomas rfa.worktree = worktree;
7940 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
7941 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
7942 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
7943 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
7944 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
7945 10604dce 2021-09-24 thomas rfa.repo = repo;
7946 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
7947 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7948 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7949 10604dce 2021-09-24 thomas if (err)
7950 10604dce 2021-09-24 thomas goto sync;
7952 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7953 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
7955 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7956 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7957 10604dce 2021-09-24 thomas err = sync_err;
7959 10604dce 2021-09-24 thomas free(tree_id);
7960 10604dce 2021-09-24 thomas free(commit_id);
7961 945f9229 2022-04-16 thomas if (commit)
7962 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7963 10604dce 2021-09-24 thomas if (fileindex)
7964 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7965 10604dce 2021-09-24 thomas free(fileindex_path);
7967 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
7968 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
7969 10604dce 2021-09-24 thomas err = unlockerr;
7970 10604dce 2021-09-24 thomas return err;
7973 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7974 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7975 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7976 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7977 2db2652d 2019-08-07 stsp struct got_repository *repo;
7978 2db2652d 2019-08-07 stsp int have_changes;
7981 ef20f542 2022-06-26 thomas static const struct got_error *
7982 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7983 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7984 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7985 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7987 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7988 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7989 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7990 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7991 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7992 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7994 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7995 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7996 8b13ce36 2019-08-08 stsp return NULL;
7997 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7998 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8000 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8001 735ef5ac 2019-08-03 stsp if (ie == NULL)
8002 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8004 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8005 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8006 735ef5ac 2019-08-03 stsp relpath) == -1)
8007 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8009 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8010 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
8011 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8012 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
8015 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8016 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8017 3aa5969e 2019-08-06 stsp goto done;
8018 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8019 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8020 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8021 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8022 735ef5ac 2019-08-03 stsp goto done;
8025 2db2652d 2019-08-07 stsp a->have_changes = 1;
8027 735ef5ac 2019-08-03 stsp p = in_repo_path;
8028 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8030 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8031 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8032 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8034 735ef5ac 2019-08-03 stsp free(in_repo_path);
8035 dc424a06 2019-08-07 stsp return err;
8038 2db2652d 2019-08-07 stsp struct stage_path_arg {
8039 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8040 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8041 2db2652d 2019-08-07 stsp struct got_repository *repo;
8042 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8043 2db2652d 2019-08-07 stsp void *status_arg;
8044 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8045 2db2652d 2019-08-07 stsp void *patch_arg;
8046 7b5dc508 2019-10-28 stsp int staged_something;
8047 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8050 2db2652d 2019-08-07 stsp static const struct got_error *
8051 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8052 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8053 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8054 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8056 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8057 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8058 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8059 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8060 0cb83759 2019-08-03 stsp uint32_t stage;
8061 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8062 0aeb8099 2020-07-23 stsp struct stat sb;
8064 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8065 8b13ce36 2019-08-08 stsp return NULL;
8067 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8068 d3e7c587 2019-08-03 stsp if (ie == NULL)
8069 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8071 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8072 2db2652d 2019-08-07 stsp relpath)== -1)
8073 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8075 0cb83759 2019-08-03 stsp switch (status) {
8076 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8077 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8078 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8079 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8080 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8083 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8084 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8085 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8086 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8087 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8090 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8093 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8094 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8095 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8096 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8097 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8101 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8102 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8105 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8106 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8107 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8108 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8110 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8111 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8112 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8113 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8114 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8115 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8116 35213c7c 2020-07-23 stsp ssize_t target_len;
8117 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8118 35213c7c 2020-07-23 stsp sizeof(target_path));
8119 35213c7c 2020-07-23 stsp if (target_len == -1) {
8120 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8121 35213c7c 2020-07-23 stsp ondisk_path);
8124 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8125 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8126 35213c7c 2020-07-23 stsp a->worktree->root_path);
8129 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8130 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8131 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8135 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8136 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8137 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8139 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8140 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8142 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8143 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8145 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8146 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8148 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8149 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8150 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8151 f289c82c 2022-06-13 thomas if (err)
8154 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8155 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8157 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8158 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8159 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8160 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8163 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8164 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8166 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8167 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8168 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8169 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8172 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8174 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8175 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8179 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8180 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8181 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8182 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8184 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8185 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8188 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8190 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8191 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8193 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8194 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8197 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8201 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8202 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8203 dc424a06 2019-08-07 stsp free(path_content);
8204 2db2652d 2019-08-07 stsp free(ondisk_path);
8205 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8206 0ebf8283 2019-07-24 stsp return err;
8209 0cb83759 2019-08-03 stsp const struct got_error *
8210 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8211 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8212 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8213 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8214 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8216 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8217 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8218 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8219 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8220 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8221 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8222 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8223 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8225 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8227 0cb83759 2019-08-03 stsp return err;
8229 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8230 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8232 735ef5ac 2019-08-03 stsp goto done;
8233 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8235 735ef5ac 2019-08-03 stsp goto done;
8236 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8238 0cb83759 2019-08-03 stsp goto done;
8240 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8241 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8242 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8243 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8244 2db2652d 2019-08-07 stsp oka.repo = repo;
8245 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8246 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8247 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8248 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
8250 735ef5ac 2019-08-03 stsp goto done;
8252 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8253 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8254 2db2652d 2019-08-07 stsp goto done;
8257 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8258 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8259 2db2652d 2019-08-07 stsp spa.repo = repo;
8260 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8261 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8262 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8263 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8264 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8265 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8266 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8267 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8268 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
8270 2db2652d 2019-08-07 stsp goto done;
8272 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8273 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8274 7b5dc508 2019-10-28 stsp goto done;
8277 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8278 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8279 0cb83759 2019-08-03 stsp err = sync_err;
8281 735ef5ac 2019-08-03 stsp if (head_ref)
8282 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8283 735ef5ac 2019-08-03 stsp free(head_commit_id);
8284 0cb83759 2019-08-03 stsp free(fileindex_path);
8285 0cb83759 2019-08-03 stsp if (fileindex)
8286 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8287 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8288 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8289 0cb83759 2019-08-03 stsp err = unlockerr;
8290 0cb83759 2019-08-03 stsp return err;
8293 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8294 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8295 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8296 ad493afc 2019-08-03 stsp struct got_repository *repo;
8297 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8298 ad493afc 2019-08-03 stsp void *progress_arg;
8299 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8300 2e1f37b0 2019-08-08 stsp void *patch_arg;
8303 2e1f37b0 2019-08-08 stsp static const struct got_error *
8304 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8305 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8306 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8307 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8308 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8310 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8311 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8312 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8313 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8314 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8315 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8316 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8317 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8319 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8320 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8322 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8324 2e1f37b0 2019-08-08 stsp return err;
8326 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8327 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8328 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8329 f4ae6ddb 2022-07-01 thomas goto done;
8331 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8332 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8333 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8334 f4ae6ddb 2022-07-01 thomas goto done;
8337 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8339 2e1f37b0 2019-08-08 stsp goto done;
8341 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8343 2e1f37b0 2019-08-08 stsp goto done;
8345 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8347 2e1f37b0 2019-08-08 stsp goto done;
8349 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8352 2e1f37b0 2019-08-08 stsp goto done;
8354 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8356 2e1f37b0 2019-08-08 stsp goto done;
8358 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8360 2e1f37b0 2019-08-08 stsp goto done;
8362 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8363 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8365 2e1f37b0 2019-08-08 stsp goto done;
8367 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8368 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
8370 2e1f37b0 2019-08-08 stsp goto done;
8371 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8372 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
8374 2e1f37b0 2019-08-08 stsp goto done;
8376 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8377 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8378 2e1f37b0 2019-08-08 stsp goto done;
8380 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8381 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8382 2e1f37b0 2019-08-08 stsp goto done;
8384 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8385 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8386 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
8387 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8388 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8389 fe621944 2020-11-10 stsp nchanges++;
8391 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8392 2e1f37b0 2019-08-08 stsp int choice;
8393 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8394 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8395 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8396 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8398 2e1f37b0 2019-08-08 stsp goto done;
8399 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8400 2e1f37b0 2019-08-08 stsp have_content = 1;
8402 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8403 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8406 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8407 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8408 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8410 2e1f37b0 2019-08-08 stsp free(label1);
8411 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8412 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8414 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8415 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8416 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8417 2e1f37b0 2019-08-08 stsp if (staged_blob)
8418 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8419 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8420 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8421 fe621944 2020-11-10 stsp err = free_err;
8422 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8423 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8424 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8425 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8426 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8427 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8428 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8429 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8430 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8431 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8432 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8433 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8434 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8435 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8436 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8437 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8438 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8439 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8440 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8442 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8443 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8444 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8445 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8446 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8447 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8448 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8450 2e1f37b0 2019-08-08 stsp free(path1);
8451 2e1f37b0 2019-08-08 stsp free(path2);
8452 fda8017d 2020-07-23 stsp return err;
8455 fda8017d 2020-07-23 stsp static const struct got_error *
8456 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8457 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8458 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8459 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8460 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8461 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8462 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8464 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8465 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8466 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8467 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8468 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8469 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8470 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8471 fda8017d 2020-07-23 stsp struct stat sb;
8473 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8474 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8475 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8477 fda8017d 2020-07-23 stsp return err;
8479 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8480 fda8017d 2020-07-23 stsp return NULL;
8482 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8483 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8484 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8486 fda8017d 2020-07-23 stsp goto done;
8489 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
8490 fda8017d 2020-07-23 stsp if (f == NULL) {
8491 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8492 fda8017d 2020-07-23 stsp path_unstaged_content);
8493 fda8017d 2020-07-23 stsp goto done;
8495 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8496 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8497 fda8017d 2020-07-23 stsp goto done;
8499 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8500 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8501 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8503 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8504 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8505 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8506 fda8017d 2020-07-23 stsp goto done;
8508 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8509 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8510 fda8017d 2020-07-23 stsp goto done;
8512 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8513 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8514 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8515 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8516 fda8017d 2020-07-23 stsp progress_arg);
8518 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8520 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8522 67a66647 2021-05-31 stsp return err;
8524 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8525 67a66647 2021-05-31 stsp parent) == -1) {
8526 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8527 67a66647 2021-05-31 stsp base_path = NULL;
8528 67a66647 2021-05-31 stsp goto done;
8531 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8533 67a66647 2021-05-31 stsp goto done;
8534 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8535 67a66647 2021-05-31 stsp blob_base);
8537 67a66647 2021-05-31 stsp goto done;
8540 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8541 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8543 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8544 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8545 eec2f5a9 2021-06-03 stsp ondisk_path);
8547 eec2f5a9 2021-06-03 stsp goto done;
8550 06340621 2021-12-31 thomas fd = open(ondisk_path,
8551 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8552 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8553 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8554 eec2f5a9 2021-06-03 stsp goto done;
8556 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8557 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8558 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8559 eec2f5a9 2021-06-03 stsp close(fd);
8560 eec2f5a9 2021-06-03 stsp goto done;
8564 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8565 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8566 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8567 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8568 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8571 fda8017d 2020-07-23 stsp goto done;
8573 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8574 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8575 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8577 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8578 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8581 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8582 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8583 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8584 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8585 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8586 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8587 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8588 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8589 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8590 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8591 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8592 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8593 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8594 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8595 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8596 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8597 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8598 67a66647 2021-05-31 stsp free(blob_base_path);
8599 67a66647 2021-05-31 stsp free(parent);
8600 67a66647 2021-05-31 stsp free(base_path);
8601 2e1f37b0 2019-08-08 stsp return err;
8604 ad493afc 2019-08-03 stsp static const struct got_error *
8605 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8606 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8607 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8608 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8610 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8611 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8612 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8613 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8614 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8615 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8616 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8617 9bc94a15 2019-08-03 stsp struct stat sb;
8618 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8620 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8621 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8622 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8623 2e1f37b0 2019-08-08 stsp return NULL;
8625 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8626 ad493afc 2019-08-03 stsp if (ie == NULL)
8627 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8629 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8631 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8633 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8634 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8636 f69721c3 2019-10-21 stsp goto done;
8637 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8638 f69721c3 2019-10-21 stsp id_str) == -1) {
8639 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8640 f4ae6ddb 2022-07-01 thomas goto done;
8643 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8644 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8645 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8646 f4ae6ddb 2022-07-01 thomas goto done;
8648 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8649 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8650 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8651 f69721c3 2019-10-21 stsp goto done;
8654 ad493afc 2019-08-03 stsp switch (staged_status) {
8655 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8656 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8657 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
8660 ad493afc 2019-08-03 stsp /* fall through */
8661 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8662 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8663 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8664 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8665 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8666 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8669 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8672 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8673 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8674 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8675 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8676 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8677 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8680 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8681 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
8684 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8685 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8686 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8687 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8688 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8689 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8690 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8691 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8692 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8694 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8695 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8696 36bf999c 2020-07-23 stsp char *staged_target;
8697 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8698 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8700 36bf999c 2020-07-23 stsp goto done;
8701 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8702 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8703 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8704 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8705 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8706 36bf999c 2020-07-23 stsp free(staged_target);
8708 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8709 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8710 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8711 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8712 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8713 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8714 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8718 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8721 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8722 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8723 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8724 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8727 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8728 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8729 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8730 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8731 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8734 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8736 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8737 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8741 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8742 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8743 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8744 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8747 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8751 ad493afc 2019-08-03 stsp free(ondisk_path);
8752 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8753 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8754 ad493afc 2019-08-03 stsp if (blob_base)
8755 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8756 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8757 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8758 ad493afc 2019-08-03 stsp if (blob_staged)
8759 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8760 f69721c3 2019-10-21 stsp free(id_str);
8761 f69721c3 2019-10-21 stsp free(label_orig);
8762 ad493afc 2019-08-03 stsp return err;
8765 ad493afc 2019-08-03 stsp const struct got_error *
8766 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8767 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8768 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8769 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8770 ad493afc 2019-08-03 stsp struct got_repository *repo)
8772 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8773 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8774 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8775 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8776 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8778 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8780 ad493afc 2019-08-03 stsp return err;
8782 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8784 ad493afc 2019-08-03 stsp goto done;
8786 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8787 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8788 ad493afc 2019-08-03 stsp upa.repo = repo;
8789 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8790 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8791 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8792 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8793 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8794 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8795 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
8797 ad493afc 2019-08-03 stsp goto done;
8800 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8801 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8802 ad493afc 2019-08-03 stsp err = sync_err;
8804 ad493afc 2019-08-03 stsp free(fileindex_path);
8805 ad493afc 2019-08-03 stsp if (fileindex)
8806 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8807 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8808 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8809 ad493afc 2019-08-03 stsp err = unlockerr;
8810 ad493afc 2019-08-03 stsp return err;
8813 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8814 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8815 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8816 b2118c49 2020-07-28 stsp void *info_arg;
8817 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8818 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8819 b2118c49 2020-07-28 stsp void *cancel_arg;
8822 b2118c49 2020-07-28 stsp static const struct got_error *
8823 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8825 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8826 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8827 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8828 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8829 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8830 b2118c49 2020-07-28 stsp int stage;
8832 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8833 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8835 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8836 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8837 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8840 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8841 b2118c49 2020-07-28 stsp return NULL;
8843 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8844 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8845 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8847 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8848 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8849 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8850 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8851 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8852 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8855 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8856 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8857 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8860 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8861 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8864 b2118c49 2020-07-28 stsp const struct got_error *
8865 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8866 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8867 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8868 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8871 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8872 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8873 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8874 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8876 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8878 b2118c49 2020-07-28 stsp return err;
8880 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8882 b2118c49 2020-07-28 stsp goto done;
8884 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8885 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8886 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8887 b2118c49 2020-07-28 stsp arg.paths = paths;
8888 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8889 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8890 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8893 b2118c49 2020-07-28 stsp free(fileindex_path);
8894 b2118c49 2020-07-28 stsp if (fileindex)
8895 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8896 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8897 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8898 b2118c49 2020-07-28 stsp err = unlockerr;
8899 b2118c49 2020-07-28 stsp return err;
8902 814624e7 2022-03-22 thomas static const struct got_error *
8903 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
8904 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
8905 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
8907 814624e7 2022-03-22 thomas const struct got_error *err;
8908 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
8909 814624e7 2022-03-22 thomas struct stat sb;
8910 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
8912 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
8913 814624e7 2022-03-22 thomas if (err)
8914 814624e7 2022-03-22 thomas return err;
8916 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8917 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
8918 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
8920 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8921 814624e7 2022-03-22 thomas if (ie) {
8922 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
8923 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8925 814624e7 2022-03-22 thomas if (err)
8926 814624e7 2022-03-22 thomas goto done;
8927 814624e7 2022-03-22 thomas } else {
8928 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
8929 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
8930 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
8931 814624e7 2022-03-22 thomas if (errno != ENOENT) {
8932 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
8933 814624e7 2022-03-22 thomas ondisk_path);
8934 814624e7 2022-03-22 thomas goto done;
8936 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
8941 814624e7 2022-03-22 thomas free(ondisk_path);
8942 814624e7 2022-03-22 thomas return err;
8945 814624e7 2022-03-22 thomas static const struct got_error *
8946 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
8947 814624e7 2022-03-22 thomas unsigned char staged_status)
8949 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
8950 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
8951 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
8952 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
8953 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
8954 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
8955 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8956 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
8957 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8958 814624e7 2022-03-22 thomas return NULL;
8961 814624e7 2022-03-22 thomas static const struct got_error *
8962 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
8964 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
8965 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8966 814624e7 2022-03-22 thomas return NULL;
8969 814624e7 2022-03-22 thomas static const struct got_error *
8970 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
8971 814624e7 2022-03-22 thomas unsigned char staged_status)
8973 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
8974 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
8975 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
8976 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
8977 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
8978 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8979 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
8980 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8981 814624e7 2022-03-22 thomas return NULL;
8984 814624e7 2022-03-22 thomas const struct got_error *
8985 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
8986 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
8988 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
8991 814624e7 2022-03-22 thomas const struct got_error *
8992 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
8993 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
8994 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
8996 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
8997 814624e7 2022-03-22 thomas int file_renamed = 0;
8998 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
8999 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9001 814624e7 2022-03-22 thomas *oldpath = NULL;
9002 814624e7 2022-03-22 thomas *newpath = NULL;
9004 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9005 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9006 814624e7 2022-03-22 thomas if (err)
9007 814624e7 2022-03-22 thomas goto done;
9009 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9010 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9011 814624e7 2022-03-22 thomas if (err)
9012 814624e7 2022-03-22 thomas goto done;
9014 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9015 814624e7 2022-03-22 thomas file_renamed = 1;
9017 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9018 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9019 814624e7 2022-03-22 thomas else if (file_renamed) {
9020 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9021 814624e7 2022-03-22 thomas if (err == NULL)
9022 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9023 814624e7 2022-03-22 thomas } else if (old == NULL)
9024 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9026 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9029 814624e7 2022-03-22 thomas if (err) {
9030 814624e7 2022-03-22 thomas free(*oldpath);
9031 814624e7 2022-03-22 thomas *oldpath = NULL;
9032 814624e7 2022-03-22 thomas free(*newpath);
9033 814624e7 2022-03-22 thomas *newpath = NULL;
9035 814624e7 2022-03-22 thomas return err;
9038 814624e7 2022-03-22 thomas const struct got_error *
9039 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9040 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9041 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9043 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9045 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9046 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9047 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9048 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9049 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9050 5e22b737 2022-05-31 thomas saa.repo = repo;
9052 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9053 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9056 5e22b737 2022-05-31 thomas const struct got_error *
9057 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9058 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9059 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9061 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9063 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9064 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9065 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9066 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9067 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9068 5e22b737 2022-05-31 thomas sda.repo = repo;
9069 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9070 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9071 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9072 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9074 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9075 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9078 5e22b737 2022-05-31 thomas const struct got_error *
9079 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9080 36832a8e 2022-05-31 thomas const char *fileindex_path)
9082 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9084 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9085 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9087 5e22b737 2022-05-31 thomas return err;