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 588a8092 2023-02-23 thomas #include <sha2.h>
31 9d31a1d8 2018-03-11 stsp #include <zlib.h>
32 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
33 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
37 86c3caaf 2018-03-09 stsp #include "got_error.h"
38 86c3caaf 2018-03-09 stsp #include "got_repository.h"
39 5261c201 2018-04-01 stsp #include "got_reference.h"
40 9d31a1d8 2018-03-11 stsp #include "got_object.h"
41 1dd54920 2019-05-11 stsp #include "got_path.h"
42 e6209546 2019-08-22 stsp #include "got_cancel.h"
43 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
44 511a516b 2018-05-19 stsp #include "got_opentemp.h"
45 234035bc 2019-06-01 stsp #include "got_diff.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
48 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
50 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
53 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
54 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
55 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
56 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
57 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
60 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 a2c162eb 2022-10-30 thomas static mode_t apply_umask(mode_t);
68 99724ed4 2018-03-10 stsp static const struct got_error *
69 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
71 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
74 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
75 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
77 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
82 507dc3bb 2018-12-29 stsp static const struct got_error *
83 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
85 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
86 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
87 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
88 507dc3bb 2018-12-29 stsp char *path = NULL;
90 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
91 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
92 507dc3bb 2018-12-29 stsp path = NULL;
96 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &tmpfile, path, "");
100 507dc3bb 2018-12-29 stsp if (content) {
101 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
102 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
103 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
108 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
109 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
110 2a57020b 2019-02-20 stsp unlink(tmppath);
115 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
116 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
117 230a42bd 2019-05-11 jcs free(tmppath);
118 09fe317a 2018-03-11 stsp return err;
121 024e9686 2019-05-14 stsp static const struct got_error *
122 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
124 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
125 024e9686 2019-05-14 stsp char *refstr = NULL;
127 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
128 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
129 024e9686 2019-05-14 stsp if (refstr == NULL)
130 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
132 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
133 024e9686 2019-05-14 stsp if (refstr == NULL)
134 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
136 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
137 024e9686 2019-05-14 stsp free(refstr);
138 024e9686 2019-05-14 stsp return err;
141 86c3caaf 2018-03-09 stsp const struct got_error *
142 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
143 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
145 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
146 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
147 ec22038e 2019-03-10 stsp uuid_t uuid;
148 ec22038e 2019-03-10 stsp uint32_t uuid_status;
149 65596e15 2018-12-24 stsp int obj_type;
150 7ac97322 2018-03-11 stsp char *path_got = NULL;
151 1451e70d 2018-03-10 stsp char *formatstr = NULL;
152 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
153 65596e15 2018-12-24 stsp char *basestr = NULL;
154 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
156 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
157 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
161 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
163 65596e15 2018-12-24 stsp return err;
164 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
166 65596e15 2018-12-24 stsp return err;
167 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
168 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
170 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
171 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
172 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
175 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
176 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
177 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
181 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
182 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
183 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
186 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
187 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
191 056e7441 2018-03-11 stsp /* Create an empty lock file. */
192 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
196 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
197 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
201 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
202 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
206 65596e15 2018-12-24 stsp /* Record our base commit. */
207 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
210 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
214 1451e70d 2018-03-10 stsp /* Store path to repository. */
215 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
216 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
220 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
221 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
222 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
226 ec22038e 2019-03-10 stsp /* Generate UUID. */
227 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
228 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
229 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
232 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
233 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
234 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
237 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
241 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
242 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
243 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
246 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
251 65596e15 2018-12-24 stsp free(commit_id);
252 7ac97322 2018-03-11 stsp free(path_got);
253 1451e70d 2018-03-10 stsp free(formatstr);
254 0bb8a95e 2018-03-12 stsp free(absprefix);
255 65596e15 2018-12-24 stsp free(basestr);
256 ec22038e 2019-03-10 stsp free(uuidstr);
257 86c3caaf 2018-03-09 stsp return err;
260 247140b2 2019-02-05 stsp const struct got_error *
261 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
262 e5dc7198 2018-12-29 stsp const char *path_prefix)
264 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
266 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
267 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
268 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
270 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
271 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
272 e5dc7198 2018-12-29 stsp free(absprefix);
273 e5dc7198 2018-12-29 stsp return NULL;
276 bc70eb79 2019-05-09 stsp const char *
277 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
279 36a38700 2019-05-10 stsp return worktree->head_ref_name;
282 024e9686 2019-05-14 stsp const struct got_error *
283 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
284 024e9686 2019-05-14 stsp struct got_reference *head_ref)
286 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
287 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
289 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
290 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
291 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
292 024e9686 2019-05-14 stsp path_got = NULL;
296 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
297 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
298 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
302 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
306 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
307 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
309 024e9686 2019-05-14 stsp free(path_got);
311 024e9686 2019-05-14 stsp free(head_ref_name);
312 024e9686 2019-05-14 stsp return err;
315 b72f483a 2019-02-05 stsp struct got_object_id *
316 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
318 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
321 507dc3bb 2018-12-29 stsp const struct got_error *
322 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
323 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
325 507dc3bb 2018-12-29 stsp const struct got_error *err;
326 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
327 507dc3bb 2018-12-29 stsp char *id_str = NULL;
328 507dc3bb 2018-12-29 stsp char *path_got = NULL;
330 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
331 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
332 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
333 507dc3bb 2018-12-29 stsp path_got = NULL;
337 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
339 507dc3bb 2018-12-29 stsp return err;
341 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
342 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
346 507dc3bb 2018-12-29 stsp /* Record our base commit. */
347 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
350 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
354 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
355 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
356 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
357 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
362 507dc3bb 2018-12-29 stsp got_object_close(obj);
363 507dc3bb 2018-12-29 stsp free(id_str);
364 507dc3bb 2018-12-29 stsp free(path_got);
365 507dc3bb 2018-12-29 stsp return err;
368 50b0790e 2020-09-11 stsp const struct got_gotconfig *
369 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
371 50b0790e 2020-09-11 stsp return worktree->gotconfig;
374 9d31a1d8 2018-03-11 stsp static const struct got_error *
375 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
377 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
378 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
379 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
380 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
381 86c3caaf 2018-03-09 stsp return NULL;
384 21908da4 2019-01-13 stsp static const struct got_error *
385 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
387 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
388 4a1ddfc2 2019-01-12 stsp char *abspath;
390 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
391 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
393 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
394 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
395 ddcd8544 2019-03-11 stsp struct stat sb;
396 ddcd8544 2019-03-11 stsp err = NULL;
397 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
398 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
399 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
400 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
401 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
404 4a1ddfc2 2019-01-12 stsp free(abspath);
405 68c76935 2019-02-19 stsp return err;
408 68c76935 2019-02-19 stsp static const struct got_error *
409 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
411 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
412 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
413 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
414 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
419 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
420 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
421 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
424 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
425 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
426 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
429 68c76935 2019-02-19 stsp if (flen1 == 0) {
430 68c76935 2019-02-19 stsp if (flen2 != 0)
433 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
434 68c76935 2019-02-19 stsp if (flen1 != 0)
437 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
438 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
448 68c76935 2019-02-19 stsp return err;
451 68c76935 2019-02-19 stsp static const struct got_error *
452 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
454 68c76935 2019-02-19 stsp struct stat sb;
455 68c76935 2019-02-19 stsp size_t size1, size2;
459 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
460 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
461 68c76935 2019-02-19 stsp size1 = sb.st_size;
463 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
464 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
465 68c76935 2019-02-19 stsp size2 = sb.st_size;
467 68c76935 2019-02-19 stsp if (size1 != size2) {
469 68c76935 2019-02-19 stsp return NULL;
472 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
473 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
474 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
475 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
477 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
480 24f136e0 2021-11-20 thomas static const struct got_error *
481 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
483 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
484 24f136e0 2021-11-20 thomas size_t flen;
485 24f136e0 2021-11-20 thomas ssize_t outlen;
487 24f136e0 2021-11-20 thomas *outsize = 0;
489 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
490 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
492 24f136e0 2021-11-20 thomas for (;;) {
493 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
494 24f136e0 2021-11-20 thomas if (flen == 0) {
495 24f136e0 2021-11-20 thomas if (ferror(f))
496 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
497 24f136e0 2021-11-20 thomas if (feof(f))
500 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
501 24f136e0 2021-11-20 thomas if (outlen == -1)
502 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
503 24f136e0 2021-11-20 thomas if (outlen != flen)
504 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
505 24f136e0 2021-11-20 thomas *outsize += outlen;
508 24f136e0 2021-11-20 thomas return NULL;
511 24f136e0 2021-11-20 thomas static const struct got_error *
512 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
513 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
514 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
515 24f136e0 2021-11-20 thomas const char *ondisk_path)
517 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
518 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
519 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
520 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
521 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
522 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
523 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
525 24f136e0 2021-11-20 thomas *overlapcnt = 0;
527 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
529 24f136e0 2021-11-20 thomas return err;
531 24f136e0 2021-11-20 thomas if (same_content)
532 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
534 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
536 24f136e0 2021-11-20 thomas return err;
537 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
538 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
540 24f136e0 2021-11-20 thomas return err;
541 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
543 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
544 24f136e0 2021-11-20 thomas *overlapcnt = 1;
545 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
546 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
547 24f136e0 2021-11-20 thomas goto done;
549 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
550 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
551 24f136e0 2021-11-20 thomas goto done;
553 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
554 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
555 24f136e0 2021-11-20 thomas goto done;
557 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
558 fc2a50f2 2022-11-01 thomas base_path_orig, "");
560 24f136e0 2021-11-20 thomas goto done;
561 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
562 fc2a50f2 2022-11-01 thomas base_path_deriv, "");
564 24f136e0 2021-11-20 thomas goto done;
565 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
566 fc2a50f2 2022-11-01 thomas base_path_deriv2, "");
568 24f136e0 2021-11-20 thomas goto done;
569 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
571 24f136e0 2021-11-20 thomas goto done;
572 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
574 24f136e0 2021-11-20 thomas goto done;
575 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
577 24f136e0 2021-11-20 thomas goto done;
578 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
579 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
580 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
581 24f136e0 2021-11-20 thomas goto done;
583 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
584 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
585 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
586 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
587 24f136e0 2021-11-20 thomas path_deriv) < 0) {
588 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
589 24f136e0 2021-11-20 thomas goto done;
591 24f136e0 2021-11-20 thomas if (size_orig > 0) {
592 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
593 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
594 24f136e0 2021-11-20 thomas label_orig ? " " : "",
595 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
596 24f136e0 2021-11-20 thomas path_orig) < 0) {
597 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
598 24f136e0 2021-11-20 thomas goto done;
601 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
602 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
603 24f136e0 2021-11-20 thomas path_deriv2,
604 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
605 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
606 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
607 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
608 24f136e0 2021-11-20 thomas goto done;
610 24f136e0 2021-11-20 thomas } else if (changed_deriv)
611 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
612 24f136e0 2021-11-20 thomas else if (changed_deriv2)
613 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
615 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
616 24f136e0 2021-11-20 thomas err == NULL)
617 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
618 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
619 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
620 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
621 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
622 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
623 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
624 24f136e0 2021-11-20 thomas free(path_orig);
625 24f136e0 2021-11-20 thomas free(path_deriv);
626 24f136e0 2021-11-20 thomas free(path_deriv2);
627 24f136e0 2021-11-20 thomas free(base_path_orig);
628 24f136e0 2021-11-20 thomas free(base_path_deriv);
629 24f136e0 2021-11-20 thomas free(base_path_deriv2);
630 24f136e0 2021-11-20 thomas return err;
634 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
635 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
636 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
637 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
638 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
640 6353ad76 2019-02-08 stsp static const struct got_error *
641 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
642 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
643 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
644 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
645 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
646 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
648 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
649 6353ad76 2019-02-08 stsp int merged_fd = -1;
650 db590691 2021-06-02 stsp FILE *f_merged = NULL;
651 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
652 234035bc 2019-06-01 stsp int overlapcnt = 0;
653 3524bbf9 2020-10-20 stsp char *parent = NULL;
655 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
657 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
659 3524bbf9 2020-10-20 stsp return err;
661 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
662 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
666 fc2a50f2 2022-11-01 thomas err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
670 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
671 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
672 24f136e0 2021-11-20 thomas if (err) {
673 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
674 24f136e0 2021-11-20 thomas goto done;
675 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
676 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
677 24f136e0 2021-11-20 thomas ondisk_path);
679 24f136e0 2021-11-20 thomas goto done;
682 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
683 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
687 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
688 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
692 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
693 db590691 2021-06-02 stsp if (f_merged == NULL) {
694 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
697 db590691 2021-06-02 stsp merged_fd = -1;
699 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
700 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
701 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
707 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
708 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
712 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
713 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
714 230a42bd 2019-05-11 jcs ondisk_path);
719 fdcb7daf 2019-12-15 stsp if (merged_path)
720 fdcb7daf 2019-12-15 stsp unlink(merged_path);
722 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
723 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
724 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
725 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
726 6353ad76 2019-02-08 stsp free(merged_path);
727 af54ae4a 2019-02-19 stsp free(base_path);
728 3524bbf9 2020-10-20 stsp free(parent);
729 af57b12a 2020-07-23 stsp return err;
732 af57b12a 2020-07-23 stsp static const struct got_error *
733 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
734 af57b12a 2020-07-23 stsp size_t target_len)
736 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
737 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
738 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
739 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
740 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
741 af57b12a 2020-07-23 stsp ondisk_path);
742 af57b12a 2020-07-23 stsp return NULL;
746 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
747 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
748 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
749 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
751 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
752 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
754 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
755 993e2a1b 2020-07-23 stsp * has deleted this symlink.
757 11cc08c1 2020-07-23 stsp static const struct got_error *
758 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
759 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
760 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
762 11cc08c1 2020-07-23 stsp const struct got_error *err;
763 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
764 11cc08c1 2020-07-23 stsp FILE *f = NULL;
766 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
768 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
770 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
771 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
772 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
776 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
778 3818e3c4 2020-11-01 naddy goto done;
780 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
781 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
785 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
786 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
787 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
788 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
789 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
790 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
791 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
792 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
793 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
794 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
798 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
799 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
802 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
803 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
807 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
808 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
809 11cc08c1 2020-07-23 stsp free(path);
810 11cc08c1 2020-07-23 stsp free(id_str);
811 11cc08c1 2020-07-23 stsp free(label_deriv);
812 11cc08c1 2020-07-23 stsp return err;
815 d219f183 2020-07-23 stsp /* forward declaration */
816 d219f183 2020-07-23 stsp static const struct got_error *
817 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
818 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
819 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
820 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
823 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
824 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
825 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
826 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
828 af57b12a 2020-07-23 stsp static const struct got_error *
829 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
830 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
831 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
832 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
833 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
835 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
836 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
837 af57b12a 2020-07-23 stsp struct stat sb;
838 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
839 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
840 11cc08c1 2020-07-23 stsp int have_local_change = 0;
841 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
843 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
844 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
846 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
847 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
848 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
849 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
850 af57b12a 2020-07-23 stsp ondisk_path);
853 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
855 526a746f 2020-07-23 stsp if (blob_orig) {
856 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
861 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
862 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
863 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
864 11cc08c1 2020-07-23 stsp have_local_change = 1;
866 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
867 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
868 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
869 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
870 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
872 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
873 11cc08c1 2020-07-23 stsp if (ancestor_target) {
874 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
875 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
877 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
878 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
879 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
880 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
883 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
884 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
885 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
886 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
889 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
892 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
893 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
894 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
895 af57b12a 2020-07-23 stsp strlen(deriv_target));
898 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
899 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
900 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
901 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
902 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
903 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
906 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
907 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
908 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
911 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
917 af57b12a 2020-07-23 stsp free(ancestor_target);
918 eec2f5a9 2021-06-03 stsp return err;
921 eec2f5a9 2021-06-03 stsp static const struct got_error *
922 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
924 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
925 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
926 eec2f5a9 2021-06-03 stsp ssize_t target_len;
930 eec2f5a9 2021-06-03 stsp *outfile = NULL;
932 eec2f5a9 2021-06-03 stsp f = got_opentemp();
933 eec2f5a9 2021-06-03 stsp if (f == NULL)
934 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
935 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
936 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
937 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
940 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
941 eec2f5a9 2021-06-03 stsp if (n != target_len) {
942 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
945 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
946 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
949 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
950 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
957 eec2f5a9 2021-06-03 stsp *outfile = f;
958 14c901f1 2019-08-08 stsp return err;
962 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
963 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
964 14c901f1 2019-08-08 stsp * acts as the second derived version.
966 14c901f1 2019-08-08 stsp static const struct got_error *
967 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
968 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
969 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
970 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
971 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
972 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
974 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
975 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
976 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
977 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
978 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
980 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
982 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
984 ed6b5030 2020-10-20 stsp return err;
986 67a66647 2021-05-31 stsp if (blob_orig) {
987 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
988 67a66647 2021-05-31 stsp parent) == -1) {
989 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
990 67a66647 2021-05-31 stsp base_path = NULL;
994 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_orig_path, &f_orig,
995 fc2a50f2 2022-11-01 thomas base_path, "");
998 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
999 67a66647 2021-05-31 stsp blob_orig);
1001 67a66647 2021-05-31 stsp goto done;
1002 67a66647 2021-05-31 stsp free(base_path);
1005 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1006 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1007 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1009 db590691 2021-06-02 stsp f_orig = got_opentemp();
1010 db590691 2021-06-02 stsp if (f_orig == NULL) {
1011 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1012 db590691 2021-06-02 stsp goto done;
1016 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1017 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1018 14c901f1 2019-08-08 stsp base_path = NULL;
1019 14c901f1 2019-08-08 stsp goto done;
1022 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1024 14c901f1 2019-08-08 stsp goto done;
1025 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1026 14c901f1 2019-08-08 stsp blob_deriv);
1028 14c901f1 2019-08-08 stsp goto done;
1030 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1032 14c901f1 2019-08-08 stsp goto done;
1033 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1034 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1035 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1036 14c901f1 2019-08-08 stsp goto done;
1040 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1041 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1043 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1044 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1046 eec2f5a9 2021-06-03 stsp goto done;
1049 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1050 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1051 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1052 eec2f5a9 2021-06-03 stsp goto done;
1054 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1055 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1056 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1057 eec2f5a9 2021-06-03 stsp close(fd);
1058 eec2f5a9 2021-06-03 stsp goto done;
1062 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1063 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1064 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1066 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1067 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1068 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1069 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1070 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1071 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1072 14c901f1 2019-08-08 stsp free(base_path);
1073 67a66647 2021-05-31 stsp if (blob_orig_path) {
1074 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1075 67a66647 2021-05-31 stsp free(blob_orig_path);
1077 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1078 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1079 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1081 6353ad76 2019-02-08 stsp free(id_str);
1082 818c7501 2019-07-11 stsp free(label_deriv);
1083 ed6b5030 2020-10-20 stsp free(parent);
1084 4a1ddfc2 2019-01-12 stsp return err;
1087 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1088 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1089 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1090 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1092 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1093 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1095 65b05cec 2020-07-23 stsp *new_iep = NULL;
1097 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1099 054041d0 2020-06-24 stsp return err;
1101 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1102 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1104 054041d0 2020-06-24 stsp goto done;
1106 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1109 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1111 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1112 13d9040b 2019-03-27 stsp return err;
1115 1ebedb77 2019-10-19 stsp static mode_t
1116 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1118 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1120 1ebedb77 2019-10-19 stsp if (executable) {
1121 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1122 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1123 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1124 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1125 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1126 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1129 a2c162eb 2022-10-30 thomas return st_mode;
1132 a2c162eb 2022-10-30 thomas static mode_t
1133 a2c162eb 2022-10-30 thomas apply_umask(mode_t mode)
1135 a2c162eb 2022-10-30 thomas mode_t um;
1137 a2c162eb 2022-10-30 thomas um = umask(000);
1138 a2c162eb 2022-10-30 thomas umask(um);
1139 a2c162eb 2022-10-30 thomas return mode & ~um;
1142 8ba819a3 2020-07-23 stsp /* forward declaration */
1143 13d9040b 2019-03-27 stsp static const struct got_error *
1144 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1145 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1146 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1147 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1148 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1149 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1152 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1153 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1155 8ba819a3 2020-07-23 stsp static const struct got_error *
1156 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1157 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1159 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1160 5a1fbc73 2020-07-23 stsp ssize_t elen;
1161 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1164 c6e8a826 2021-04-05 stsp *did_something = 0;
1167 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1168 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1169 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1170 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1171 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1172 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1174 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1175 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1176 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1177 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1179 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1180 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1181 5a1fbc73 2020-07-23 stsp if (elen == -1)
1182 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1184 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1185 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1186 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1189 c6e8a826 2021-04-05 stsp *did_something = 1;
1190 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1191 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1192 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1193 5a1fbc73 2020-07-23 stsp return err;
1196 3c1ec782 2020-07-23 stsp static const struct got_error *
1197 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1198 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1200 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1201 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1202 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1204 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1206 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1207 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1208 3c1ec782 2020-07-23 stsp return NULL;
1212 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1213 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1214 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1215 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1216 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1218 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1219 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1220 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1222 ce031e9e 2020-10-20 stsp return err;
1223 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1224 ce031e9e 2020-10-20 stsp free(parent);
1225 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1227 ce031e9e 2020-10-20 stsp free(parent);
1228 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1229 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1230 3c1ec782 2020-07-23 stsp free(abspath);
1231 3c1ec782 2020-07-23 stsp return err;
1233 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1234 3c1ec782 2020-07-23 stsp free(abspath);
1236 3c1ec782 2020-07-23 stsp return err;
1238 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1240 3c1ec782 2020-07-23 stsp return err;
1243 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1244 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1245 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1246 3c1ec782 2020-07-23 stsp return NULL;
1249 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1250 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1251 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1252 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1253 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1254 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1256 3c1ec782 2020-07-23 stsp free(path_got);
1257 3c1ec782 2020-07-23 stsp return NULL;
1260 5a1fbc73 2020-07-23 stsp static const struct got_error *
1261 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1262 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1263 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1264 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1265 ace90326 2021-09-27 thomas struct got_repository *repo,
1266 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1268 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1269 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1270 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1271 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1272 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1274 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1277 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1278 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1279 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1280 8ba819a3 2020-07-23 stsp * in the blob object.
1283 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1284 3521b466 2022-07-21 thomas if (err)
1285 3521b466 2022-07-21 thomas return err;
1287 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1288 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1289 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1290 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1291 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1292 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1293 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1294 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1295 3b9f0f87 2020-07-23 stsp progress_arg);
1297 8ba819a3 2020-07-23 stsp if (len > 0) {
1298 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1299 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1300 8ba819a3 2020-07-23 stsp len - hdrlen);
1301 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1302 8ba819a3 2020-07-23 stsp hdrlen = 0;
1304 8ba819a3 2020-07-23 stsp } while (len != 0);
1305 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1307 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1308 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1310 3c1ec782 2020-07-23 stsp return err;
1312 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1313 906c123b 2020-07-23 stsp /* install as a regular file */
1314 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1315 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1316 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1317 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1318 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1319 9e39ca77 2022-07-21 thomas return err;
1322 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1323 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1324 c6e8a826 2021-04-05 stsp int symlink_replaced;
1325 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1326 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1327 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1328 9e39ca77 2022-07-21 thomas return err;
1330 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1331 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1333 9e39ca77 2022-07-21 thomas return err;
1334 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1335 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1336 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1337 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1338 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1339 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1341 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1342 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1345 9e39ca77 2022-07-21 thomas return err; /* Nothing else to do. */
1348 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1349 f4994adc 2020-10-20 stsp char *parent;
1350 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1352 9e39ca77 2022-07-21 thomas return err;
1353 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1354 f4994adc 2020-10-20 stsp free(parent);
1356 9e39ca77 2022-07-21 thomas return err;
1358 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1359 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1361 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1362 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1363 9e39ca77 2022-07-21 thomas return err;
1367 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1368 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1369 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1370 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1371 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1372 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1373 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1374 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1375 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1376 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1377 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1378 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1379 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1381 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1382 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1384 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1385 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1386 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1387 8ba819a3 2020-07-23 stsp return err;
1390 8ba819a3 2020-07-23 stsp static const struct got_error *
1391 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1392 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1393 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1394 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1395 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1396 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1398 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1399 507dc3bb 2018-12-29 stsp int fd = -1;
1400 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1401 507dc3bb 2018-12-29 stsp int update = 0;
1402 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1403 a2c162eb 2022-10-30 thomas mode_t mode;
1405 a2c162eb 2022-10-30 thomas mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1406 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1407 a2c162eb 2022-10-30 thomas O_CLOEXEC, mode);
1408 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1409 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1410 f5375317 2020-10-20 stsp char *parent;
1411 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1413 f5375317 2020-10-20 stsp return err;
1414 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1415 f5375317 2020-10-20 stsp free(parent);
1417 21908da4 2019-01-13 stsp return err;
1418 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1419 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1421 21908da4 2019-01-13 stsp if (fd == -1)
1422 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1423 230a42bd 2019-05-11 jcs ondisk_path);
1424 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1425 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1426 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1427 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1428 3b9f0f87 2020-07-23 stsp goto done;
1430 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1431 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1432 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1433 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1434 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1435 507dc3bb 2018-12-29 stsp goto done;
1437 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1438 fc2a50f2 2022-11-01 thomas ondisk_path, "");
1440 507dc3bb 2018-12-29 stsp goto done;
1441 507dc3bb 2018-12-29 stsp update = 1;
1443 a2c162eb 2022-10-30 thomas if (fchmod(fd, apply_umask(mode)) == -1) {
1444 a2c162eb 2022-10-30 thomas err = got_error_from_errno2("fchmod",
1445 a2c162eb 2022-10-30 thomas tmppath);
1446 a2c162eb 2022-10-30 thomas goto done;
1450 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1453 bd6aa359 2020-07-23 stsp if (progress_cb) {
1454 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1455 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1457 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1458 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1461 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1462 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1464 bd6aa359 2020-07-23 stsp goto done;
1467 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1469 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1470 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1473 9d31a1d8 2018-03-11 stsp if (len > 0) {
1474 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1475 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1476 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1477 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1478 b87c6f83 2018-12-24 stsp goto done;
1479 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1480 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1481 b87c6f83 2018-12-24 stsp goto done;
1483 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1485 9d31a1d8 2018-03-11 stsp } while (len != 0);
1487 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1488 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1489 816dc654 2019-02-16 stsp goto done;
1492 507dc3bb 2018-12-29 stsp if (update) {
1493 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1494 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1495 f6d8c0ac 2020-11-09 stsp goto done;
1497 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1498 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1499 230a42bd 2019-05-11 jcs ondisk_path);
1500 68ed9ba5 2019-02-10 stsp goto done;
1502 63df146d 2020-11-09 stsp free(tmppath);
1503 63df146d 2020-11-09 stsp tmppath = NULL;
1507 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1508 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1509 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1510 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1511 507dc3bb 2018-12-29 stsp free(tmppath);
1512 6353ad76 2019-02-08 stsp return err;
1516 be94c032 2023-02-20 thomas * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1517 be94c032 2023-02-20 thomas * conflict marker is found in newly added lines only.
1519 6353ad76 2019-02-08 stsp static const struct got_error *
1520 be94c032 2023-02-20 thomas get_modified_file_content_status(unsigned char *status,
1521 be94c032 2023-02-20 thomas struct got_blob_object *blob, const char *path, struct stat *sb,
1522 be94c032 2023-02-20 thomas FILE *ondisk_file)
1524 8de9d8ad 2023-02-20 thomas const struct got_error *err, *free_err;
1525 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1526 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1527 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1528 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1530 8de9d8ad 2023-02-20 thomas FILE *f1 = NULL;
1531 8de9d8ad 2023-02-20 thomas struct got_diffreg_result *diffreg_result = NULL;
1532 8de9d8ad 2023-02-20 thomas struct diff_result *r;
1533 8de9d8ad 2023-02-20 thomas int nchunks_parsed, n, i = 0, ln = 0;
1534 9bdd68dd 2020-01-02 naddy char *line = NULL;
1535 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1536 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1538 8de9d8ad 2023-02-20 thomas if (*status != GOT_STATUS_MODIFY)
1539 8de9d8ad 2023-02-20 thomas return NULL;
1541 8de9d8ad 2023-02-20 thomas f1 = got_opentemp();
1542 8de9d8ad 2023-02-20 thomas if (f1 == NULL)
1543 8de9d8ad 2023-02-20 thomas return got_error_from_errno("got_opentemp");
1545 8de9d8ad 2023-02-20 thomas if (blob) {
1546 8de9d8ad 2023-02-20 thomas got_object_blob_rewind(blob);
1547 8de9d8ad 2023-02-20 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1548 be94c032 2023-02-20 thomas if (err)
1549 be94c032 2023-02-20 thomas goto done;
1552 8de9d8ad 2023-02-20 thomas err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1553 8de9d8ad 2023-02-20 thomas 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1554 8de9d8ad 2023-02-20 thomas if (err)
1555 8de9d8ad 2023-02-20 thomas goto done;
1557 8de9d8ad 2023-02-20 thomas r = diffreg_result->result;
1559 8de9d8ad 2023-02-20 thomas for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1560 8de9d8ad 2023-02-20 thomas struct diff_chunk *c;
1561 8de9d8ad 2023-02-20 thomas struct diff_chunk_context cc = {};
1562 450d9f6b 2023-02-20 thomas off_t pos;
1565 8de9d8ad 2023-02-20 thomas * We can optimise a little by advancing straight
1566 8de9d8ad 2023-02-20 thomas * to the next chunk if this one has no added lines.
1568 8de9d8ad 2023-02-20 thomas c = diff_chunk_get(r, n);
1570 47c7ee21 2023-02-20 thomas if (diff_chunk_type(c) != CHUNK_PLUS) {
1571 8de9d8ad 2023-02-20 thomas nchunks_parsed = 1;
1572 450d9f6b 2023-02-20 thomas continue; /* removed or unchanged lines */
1575 450d9f6b 2023-02-20 thomas pos = diff_chunk_get_right_start_pos(c);
1576 450d9f6b 2023-02-20 thomas if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1577 450d9f6b 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1578 450d9f6b 2023-02-20 thomas goto done;
1581 450d9f6b 2023-02-20 thomas diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1582 450d9f6b 2023-02-20 thomas ln = cc.right.start;
1584 8de9d8ad 2023-02-20 thomas while (ln < cc.right.end) {
1585 8de9d8ad 2023-02-20 thomas linelen = getline(&line, &linesize, ondisk_file);
1586 8de9d8ad 2023-02-20 thomas if (linelen == -1) {
1587 8de9d8ad 2023-02-20 thomas if (feof(ondisk_file))
1589 8de9d8ad 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1593 8de9d8ad 2023-02-20 thomas if (line && strncmp(line, markers[i],
1594 8de9d8ad 2023-02-20 thomas strlen(markers[i])) == 0) {
1595 8de9d8ad 2023-02-20 thomas if (strcmp(markers[i],
1596 8de9d8ad 2023-02-20 thomas GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1597 8de9d8ad 2023-02-20 thomas *status = GOT_STATUS_CONFLICT;
1598 8de9d8ad 2023-02-20 thomas goto done;
1607 9bdd68dd 2020-01-02 naddy free(line);
1608 be94c032 2023-02-20 thomas if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1609 be94c032 2023-02-20 thomas err = got_error_from_errno("fclose");
1610 8de9d8ad 2023-02-20 thomas free_err = got_diffreg_result_free(diffreg_result);
1611 8de9d8ad 2023-02-20 thomas if (err == NULL)
1612 8de9d8ad 2023-02-20 thomas err = free_err;
1614 7154f6ce 2019-03-27 stsp return err;
1617 e2b1e152 2019-07-13 stsp static int
1618 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1620 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1621 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1624 1ebedb77 2019-10-19 stsp static int
1625 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1627 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1628 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1629 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1630 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1631 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1632 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1635 c363b2c1 2019-08-03 stsp static unsigned char
1636 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1638 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1639 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1640 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1641 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1642 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1643 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1644 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1646 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1650 a919d5c4 2020-07-23 stsp static const struct got_error *
1651 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1652 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1653 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1655 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1656 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1657 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1658 a919d5c4 2020-07-23 stsp ssize_t elen;
1659 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1660 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1661 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1663 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1665 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1667 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1669 a919d5c4 2020-07-23 stsp return err;
1670 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1672 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1673 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1675 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1677 a919d5c4 2020-07-23 stsp if (len > 0) {
1678 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1679 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1680 a919d5c4 2020-07-23 stsp len - hdrlen);
1681 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1682 a919d5c4 2020-07-23 stsp hdrlen = 0;
1684 a919d5c4 2020-07-23 stsp } while (len != 0);
1685 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1687 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1688 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1689 a919d5c4 2020-07-23 stsp if (elen == -1)
1690 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1692 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1693 a919d5c4 2020-07-23 stsp if (elen == -1)
1694 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1697 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1698 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1700 a919d5c4 2020-07-23 stsp return NULL;
1703 7154f6ce 2019-03-27 stsp static const struct got_error *
1704 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1705 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1706 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1708 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1709 6353ad76 2019-02-08 stsp struct got_object_id id;
1710 6353ad76 2019-02-08 stsp size_t hdrlen;
1711 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1712 6353ad76 2019-02-08 stsp FILE *f = NULL;
1713 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1714 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1715 6353ad76 2019-02-08 stsp size_t flen, blen;
1716 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
1718 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
1719 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1720 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1723 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1724 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1725 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1727 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1728 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1729 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1730 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1731 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1733 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1734 882ef1b9 2019-12-13 stsp goto done;
1736 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1737 3d35a492 2019-12-13 stsp goto done;
1740 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1741 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1742 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1743 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1744 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1745 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1746 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1747 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1748 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1749 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1750 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1752 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1753 3d35a492 2019-12-13 stsp goto done;
1755 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1756 1338848f 2019-12-13 stsp goto done;
1760 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1761 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1762 1338848f 2019-12-13 stsp goto done;
1765 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1766 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1767 1338848f 2019-12-13 stsp goto done;
1768 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1769 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1770 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1771 1338848f 2019-12-13 stsp goto done;
1774 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1775 f179e66d 2020-07-23 stsp goto done;
1777 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1778 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1779 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1780 1338848f 2019-12-13 stsp goto done;
1783 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1784 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1785 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
1787 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
1789 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1790 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1791 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1792 f4ae6ddb 2022-07-01 thomas goto done;
1794 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1796 1338848f 2019-12-13 stsp goto done;
1798 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1799 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1800 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1801 a919d5c4 2020-07-23 stsp goto done;
1804 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1805 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1806 ab0d4361 2019-12-13 stsp if (fd == -1) {
1807 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1808 ab0d4361 2019-12-13 stsp goto done;
1812 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1813 6353ad76 2019-02-08 stsp if (f == NULL) {
1814 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1815 6353ad76 2019-02-08 stsp goto done;
1818 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1820 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1821 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1823 7154f6ce 2019-03-27 stsp goto done;
1824 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1825 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1826 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1827 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1828 7154f6ce 2019-03-27 stsp goto done;
1830 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1831 6353ad76 2019-02-08 stsp if (flen != 0)
1832 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1834 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1835 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1836 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1838 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1839 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1840 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1841 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1845 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1848 6353ad76 2019-02-08 stsp hdrlen = 0;
1851 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1852 7154f6ce 2019-03-27 stsp rewind(f);
1853 be94c032 2023-02-20 thomas err = get_modified_file_content_status(status, blob, ie->path,
1855 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1856 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1858 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1859 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1861 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1862 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1863 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1864 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1865 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1866 9d31a1d8 2018-03-11 stsp return err;
1870 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1871 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1873 e2b1e152 2019-07-13 stsp static const struct got_error *
1874 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1875 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1877 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1878 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1879 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1881 e2b1e152 2019-07-13 stsp return NULL;
1884 9d31a1d8 2018-03-11 stsp static const struct got_error *
1885 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1886 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1887 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1888 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1889 0584f854 2019-04-06 stsp void *progress_arg)
1891 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1892 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1893 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1894 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1895 b8f41171 2019-02-10 stsp struct stat sb;
1896 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1898 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1899 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1902 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1903 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1904 a76c42e6 2019-08-03 stsp goto done;
1906 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1909 abb4604f 2019-07-27 stsp goto done;
1910 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1911 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1913 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1914 e6f4ba31 2021-09-24 thomas if (errno != ENOENT) {
1915 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1916 e6f4ba31 2021-09-24 thomas ondisk_path);
1917 e6f4ba31 2021-09-24 thomas goto done;
1919 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1920 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1921 e6f4ba31 2021-09-24 thomas } else {
1922 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1923 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1925 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1929 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1931 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1932 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1933 b8f41171 2019-02-10 stsp goto done;
1935 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1937 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1938 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1940 5036ab18 2020-04-18 stsp goto done;
1943 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1944 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1945 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1947 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1948 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1949 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1950 c6e8a826 2021-04-05 stsp * updating contents of this file.
1952 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1953 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1954 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1955 c6e8a826 2021-04-05 stsp /* Same commit. */
1956 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1957 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1959 e2b1e152 2019-07-13 stsp goto done;
1960 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1962 6353ad76 2019-02-08 stsp goto done;
1964 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1965 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1966 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1967 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1968 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1969 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1971 0f58026f 2021-04-05 stsp goto done;
1972 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1974 b8f41171 2019-02-10 stsp goto done;
1978 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1979 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1980 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1981 f4ae6ddb 2022-07-01 thomas goto done;
1983 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1985 6353ad76 2019-02-08 stsp goto done;
1987 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1988 234035bc 2019-06-01 stsp int update_timestamps;
1989 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1990 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1991 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1992 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
1993 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
1994 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1995 f4ae6ddb 2022-07-01 thomas goto done;
1997 234035bc 2019-06-01 stsp struct got_object_id id2;
1998 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id2, ie);
1999 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2002 f69721c3 2019-10-21 stsp goto done;
2004 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2005 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2006 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2007 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2008 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2009 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2010 f69721c3 2019-10-21 stsp goto done;
2012 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2013 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2014 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2015 234035bc 2019-06-01 stsp goto done;
2018 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2019 36bf999c 2020-07-23 stsp char *link_target;
2020 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2022 36bf999c 2020-07-23 stsp goto done;
2023 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2024 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2025 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2026 36bf999c 2020-07-23 stsp free(link_target);
2028 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2029 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2030 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2031 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2033 f69721c3 2019-10-21 stsp free(label_orig);
2034 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2035 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2036 f4ae6ddb 2022-07-01 thomas goto done;
2038 234035bc 2019-06-01 stsp if (blob2)
2039 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2041 909d120e 2019-09-22 stsp goto done;
2043 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2044 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2045 234035bc 2019-06-01 stsp * unmodified files again.
2047 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2048 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2049 234035bc 2019-06-01 stsp update_timestamps);
2050 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2051 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2052 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2053 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2054 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2056 1ee397ad 2019-07-12 stsp goto done;
2057 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2058 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2060 13d9040b 2019-03-27 stsp goto done;
2062 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2063 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2064 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2065 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2066 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2067 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2068 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2070 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2071 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2072 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2073 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2074 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2077 13d9040b 2019-03-27 stsp goto done;
2080 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2081 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2082 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2084 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2085 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2086 054041d0 2020-06-24 stsp &blob->id);
2089 13d9040b 2019-03-27 stsp goto done;
2091 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2092 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2093 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2097 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2098 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2099 f4ae6ddb 2022-07-01 thomas goto done;
2101 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2103 6353ad76 2019-02-08 stsp free(ondisk_path);
2104 8da9e5f4 2019-01-12 stsp return err;
2107 90285c3b 2019-01-08 stsp static const struct got_error *
2108 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2110 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2111 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2113 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2114 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2116 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2117 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2118 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2120 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2121 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2123 1c4cdd89 2021-06-20 stsp goto done;
2124 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2125 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2126 fddefe3b 2020-10-20 stsp free(ondisk_path);
2127 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2128 1c4cdd89 2021-06-20 stsp parent = NULL;
2129 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2130 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2131 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2132 fddefe3b 2020-10-20 stsp ondisk_path);
2135 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2141 90285c3b 2019-01-08 stsp free(ondisk_path);
2142 1c4cdd89 2021-06-20 stsp free(parent);
2143 90285c3b 2019-01-08 stsp return err;
2146 708d8e67 2019-03-27 stsp static const struct got_error *
2147 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2148 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2149 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2151 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2152 708d8e67 2019-03-27 stsp unsigned char status;
2153 708d8e67 2019-03-27 stsp struct stat sb;
2154 708d8e67 2019-03-27 stsp char *ondisk_path;
2156 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2157 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2159 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2161 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2163 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2165 5a58a424 2020-06-23 stsp goto done;
2167 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2168 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2169 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2170 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2171 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2172 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2173 993e2a1b 2020-07-23 stsp goto done;
2175 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2176 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2177 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2178 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2180 993e2a1b 2020-07-23 stsp goto done;
2181 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2182 993e2a1b 2020-07-23 stsp ie->path);
2183 993e2a1b 2020-07-23 stsp goto done;
2186 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2187 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2188 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2190 5a58a424 2020-06-23 stsp goto done;
2192 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2193 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2195 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2196 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2198 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2200 5a58a424 2020-06-23 stsp goto done;
2201 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2202 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2204 5a58a424 2020-06-23 stsp goto done;
2206 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2209 5a58a424 2020-06-23 stsp free(ondisk_path);
2210 fc6346c4 2019-03-27 stsp return err;
2213 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2214 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2215 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2216 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2217 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2218 8da9e5f4 2019-01-12 stsp void *progress_arg;
2219 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2220 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2223 512f0d0e 2019-01-02 stsp static const struct got_error *
2224 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2225 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2227 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2229 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2230 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2232 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2233 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2236 8da9e5f4 2019-01-12 stsp static const struct got_error *
2237 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2239 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2241 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2242 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2244 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2245 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2248 9d31a1d8 2018-03-11 stsp static const struct got_error *
2249 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2251 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2252 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2253 8da9e5f4 2019-01-12 stsp char *path;
2255 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2256 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2258 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2259 63c5ca5d 2019-08-24 stsp return NULL;
2261 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2262 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2264 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2266 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2267 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2269 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2270 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2272 8da9e5f4 2019-01-12 stsp free(path);
2273 0cd1c46a 2019-03-11 stsp return err;
2276 b2118c49 2020-07-28 stsp const struct got_error *
2277 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2279 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2281 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2282 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2283 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2284 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2287 b2118c49 2020-07-28 stsp return NULL;
2290 818c7501 2019-07-11 stsp static const struct got_error *
2291 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2293 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2294 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2296 517bab73 2019-03-11 stsp *refname = NULL;
2298 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2300 b2118c49 2020-07-28 stsp return err;
2302 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2303 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2304 0647c563 2019-03-11 stsp *refname = NULL;
2306 517bab73 2019-03-11 stsp free(uuidstr);
2307 517bab73 2019-03-11 stsp return err;
2310 818c7501 2019-07-11 stsp const struct got_error *
2311 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2312 f6cd0243 2023-01-28 thomas const char *prefix)
2314 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2317 f6cd0243 2023-01-28 thomas const struct got_error *
2318 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2320 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2323 818c7501 2019-07-11 stsp static const struct got_error *
2324 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2326 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2327 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2330 818c7501 2019-07-11 stsp static const struct got_error *
2331 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2333 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2336 818c7501 2019-07-11 stsp static const struct got_error *
2337 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2339 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2340 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2343 818c7501 2019-07-11 stsp static const struct got_error *
2344 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2346 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2347 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2350 0ebf8283 2019-07-24 stsp static const struct got_error *
2351 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2353 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2354 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2357 0ebf8283 2019-07-24 stsp static const struct got_error *
2358 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2360 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2361 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2364 0ebf8283 2019-07-24 stsp static const struct got_error *
2365 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2367 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2368 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2371 0ebf8283 2019-07-24 stsp static const struct got_error *
2372 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2374 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2375 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2378 0ebf8283 2019-07-24 stsp const struct got_error *
2379 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2380 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2382 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2383 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2384 0ebf8283 2019-07-24 stsp *path = NULL;
2385 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2387 0ebf8283 2019-07-24 stsp return NULL;
2390 10604dce 2021-09-24 thomas static const struct got_error *
2391 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2393 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2394 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2397 10604dce 2021-09-24 thomas static const struct got_error *
2398 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2400 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2401 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2405 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2406 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2408 517bab73 2019-03-11 stsp static const struct got_error *
2409 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2411 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2412 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2413 517bab73 2019-03-11 stsp char *refname;
2415 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2417 517bab73 2019-03-11 stsp return err;
2419 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2421 0cd1c46a 2019-03-11 stsp goto done;
2423 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2425 0cd1c46a 2019-03-11 stsp free(refname);
2427 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2428 3e3a69f1 2019-07-25 stsp return err;
2431 3e3a69f1 2019-07-25 stsp static const struct got_error *
2432 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2434 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2436 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2437 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2438 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2439 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2441 9d31a1d8 2018-03-11 stsp return err;
2445 ebf99748 2019-05-09 stsp static const struct got_error *
2446 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2447 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2449 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2450 ebf99748 2019-05-09 stsp FILE *index = NULL;
2452 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2453 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2454 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2455 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2457 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2459 ebf99748 2019-05-09 stsp goto done;
2461 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2462 ebf99748 2019-05-09 stsp if (index == NULL) {
2463 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2464 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2466 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2467 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2468 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2471 ebf99748 2019-05-09 stsp if (err) {
2472 ebf99748 2019-05-09 stsp free(*fileindex_path);
2473 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2474 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2475 ebf99748 2019-05-09 stsp *fileindex = NULL;
2477 ebf99748 2019-05-09 stsp return err;
2480 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2481 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2482 c932eeeb 2019-05-22 stsp const char *path;
2483 c932eeeb 2019-05-22 stsp size_t path_len;
2484 c932eeeb 2019-05-22 stsp const char *entry_name;
2485 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2486 a484d721 2019-06-10 stsp void *progress_arg;
2489 c932eeeb 2019-05-22 stsp static const struct got_error *
2490 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2492 1ee397ad 2019-07-12 stsp const struct got_error *err;
2493 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2495 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2496 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2497 c932eeeb 2019-05-22 stsp return NULL;
2498 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2499 102ff934 2019-06-11 stsp return NULL;
2501 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2502 a769b60b 2021-06-27 stsp return NULL;
2504 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2505 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2506 c932eeeb 2019-05-22 stsp return NULL;
2508 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2509 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2510 edd02c5e 2019-07-11 stsp ie->path);
2512 1ee397ad 2019-07-12 stsp return err;
2514 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2515 c932eeeb 2019-05-22 stsp return NULL;
2518 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2519 a615e0e7 2020-12-16 stsp static const struct got_error *
2520 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2521 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2522 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2524 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2526 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2527 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2528 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2529 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2530 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2531 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2533 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2534 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2537 9c6338c4 2019-06-02 stsp static const struct got_error *
2538 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2540 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2541 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2542 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2543 867630bb 2020-01-17 stsp struct timespec timeout;
2545 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2546 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2548 9c6338c4 2019-06-02 stsp goto done;
2550 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2552 9c6338c4 2019-06-02 stsp goto done;
2554 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2555 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2556 9c6338c4 2019-06-02 stsp fileindex_path);
2557 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2561 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2562 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2563 867630bb 2020-01-17 stsp * was recorded in the file index.
2565 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2566 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2567 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2569 9c6338c4 2019-06-02 stsp if (new_index)
2570 9c6338c4 2019-06-02 stsp fclose(new_index);
2571 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2572 9c6338c4 2019-06-02 stsp return err;
2575 6ced4176 2019-07-12 stsp static const struct got_error *
2576 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2577 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2578 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2579 945f9229 2022-04-16 thomas struct got_repository *repo)
2581 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2582 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2583 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2584 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2586 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2587 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2588 6ced4176 2019-07-12 stsp *tree_id = NULL;
2590 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2591 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2592 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2593 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2594 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2595 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2596 6ced4176 2019-07-12 stsp goto done;
2598 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2599 945f9229 2022-04-16 thomas worktree->path_prefix);
2601 6ced4176 2019-07-12 stsp goto done;
2602 6ced4176 2019-07-12 stsp return NULL;
2605 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2607 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2608 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2609 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2610 6ced4176 2019-07-12 stsp goto done;
2613 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2615 6ced4176 2019-07-12 stsp goto done;
2617 6ced4176 2019-07-12 stsp free(in_repo_path);
2618 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2620 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2622 6ced4176 2019-07-12 stsp goto done;
2624 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2625 6ced4176 2019-07-12 stsp /* Check out a single file. */
2626 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2627 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2628 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2629 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2630 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2631 6ced4176 2019-07-12 stsp goto done;
2633 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2634 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2635 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2636 6ced4176 2019-07-12 stsp goto done;
2639 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2640 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2642 6ced4176 2019-07-12 stsp return err;
2643 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2644 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2645 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2646 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2647 6ced4176 2019-07-12 stsp goto done;
2650 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2651 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2653 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2654 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2655 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2656 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2657 6ced4176 2019-07-12 stsp goto done;
2659 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2660 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2661 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2662 6ced4176 2019-07-12 stsp goto done;
2667 6ced4176 2019-07-12 stsp free(in_repo_path);
2668 6ced4176 2019-07-12 stsp if (err) {
2669 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2670 6ced4176 2019-07-12 stsp free(*tree_relpath);
2671 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2672 6ced4176 2019-07-12 stsp free(*tree_id);
2673 6ced4176 2019-07-12 stsp *tree_id = NULL;
2675 07ed472d 2019-07-12 stsp return err;
2678 07ed472d 2019-07-12 stsp static const struct got_error *
2679 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2680 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2681 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2682 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2684 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2685 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2686 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2687 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2688 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2690 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2691 7f47418f 2019-12-20 stsp if (err) {
2692 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2693 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2694 7f47418f 2019-12-20 stsp goto done;
2695 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2696 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2698 7f47418f 2019-12-20 stsp return err;
2701 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2702 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2704 07ed472d 2019-07-12 stsp goto done;
2706 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2708 07ed472d 2019-07-12 stsp goto done;
2710 07ed472d 2019-07-12 stsp if (entry_name &&
2711 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2712 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2713 07ed472d 2019-07-12 stsp goto done;
2716 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2717 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2718 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2719 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2720 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2721 07ed472d 2019-07-12 stsp arg.repo = repo;
2722 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2723 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2724 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2725 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2726 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2727 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2730 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2731 07ed472d 2019-07-12 stsp if (commit)
2732 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2733 6ced4176 2019-07-12 stsp return err;
2736 9d31a1d8 2018-03-11 stsp const struct got_error *
2737 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2738 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2739 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2740 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2742 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2743 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2744 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2745 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2746 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2747 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2748 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2749 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2750 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2751 f2ea84fa 2019-07-27 stsp int entry_type;
2752 f2ea84fa 2019-07-27 stsp char *relpath;
2753 f2ea84fa 2019-07-27 stsp char *entry_name;
2754 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2755 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2757 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2759 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2761 9d31a1d8 2018-03-11 stsp return err;
2763 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2764 945f9229 2022-04-16 thomas worktree->base_commit_id);
2765 945f9229 2022-04-16 thomas if (err)
2766 945f9229 2022-04-16 thomas goto done;
2768 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2769 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2770 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2771 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2772 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2773 f2ea84fa 2019-07-27 stsp goto done;
2776 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2777 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2778 945f9229 2022-04-16 thomas worktree, repo);
2779 f2ea84fa 2019-07-27 stsp if (err) {
2780 f2ea84fa 2019-07-27 stsp free(tpd);
2781 6ced4176 2019-07-12 stsp goto done;
2784 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2785 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2786 f2ea84fa 2019-07-27 stsp if (err) {
2787 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2788 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2789 f2ea84fa 2019-07-27 stsp free(tpd);
2790 f2ea84fa 2019-07-27 stsp goto done;
2793 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2795 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2799 51514078 2018-12-25 stsp * Read the file index.
2800 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2801 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2803 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2805 c4cdcb68 2019-04-03 stsp goto done;
2807 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2808 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2809 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2811 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2812 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2813 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2817 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2818 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2819 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2820 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2821 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2822 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2823 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2824 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2828 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2830 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2831 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2832 af12c6b9 2019-06-04 stsp err = sync_err;
2834 9c6338c4 2019-06-02 stsp free(fileindex_path);
2836 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2837 9d31a1d8 2018-03-11 stsp if (commit)
2838 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2839 5ade8233 2019-07-12 stsp if (fileindex)
2840 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2841 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2842 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2843 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2844 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2845 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2846 f2ea84fa 2019-07-27 stsp free(tpd);
2848 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2849 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2850 9d31a1d8 2018-03-11 stsp err = unlockerr;
2851 f8d1f275 2019-02-04 stsp return err;
2854 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2855 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2856 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2857 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2858 234035bc 2019-06-01 stsp void *progress_arg;
2859 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2860 234035bc 2019-06-01 stsp void *cancel_arg;
2861 f69721c3 2019-10-21 stsp const char *label_orig;
2862 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2863 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2866 234035bc 2019-06-01 stsp static const struct got_error *
2867 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2868 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2869 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2870 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2871 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2873 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2874 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2875 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2876 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2877 234035bc 2019-06-01 stsp struct stat sb;
2878 234035bc 2019-06-01 stsp unsigned char status;
2879 234035bc 2019-06-01 stsp int local_changes_subsumed;
2880 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2881 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2883 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2884 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2885 d6c87207 2019-08-02 stsp strlen(path2));
2886 1ee397ad 2019-07-12 stsp if (ie == NULL)
2887 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2888 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2890 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2891 234035bc 2019-06-01 stsp path2) == -1)
2892 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2894 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2897 234035bc 2019-06-01 stsp goto done;
2899 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2900 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2901 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2902 234035bc 2019-06-01 stsp goto done;
2904 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2905 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2906 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2907 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2908 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2909 234035bc 2019-06-01 stsp goto done;
2912 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2913 36bf999c 2020-07-23 stsp char *link_target2;
2914 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2916 36bf999c 2020-07-23 stsp goto done;
2917 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2918 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2919 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2920 36bf999c 2020-07-23 stsp free(link_target2);
2924 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2925 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2926 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2927 1af628f4 2021-06-03 stsp goto done;
2929 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2930 1af628f4 2021-06-03 stsp f_orig, blob1);
2932 1af628f4 2021-06-03 stsp goto done;
2934 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2935 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2936 1af628f4 2021-06-03 stsp goto done;
2937 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2938 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2940 1af628f4 2021-06-03 stsp goto done;
2942 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
2943 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2944 1af628f4 2021-06-03 stsp if (fd == -1) {
2945 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2946 1af628f4 2021-06-03 stsp ondisk_path);
2947 1af628f4 2021-06-03 stsp goto done;
2949 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2950 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2951 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2952 1af628f4 2021-06-03 stsp ondisk_path);
2953 1af628f4 2021-06-03 stsp close(fd);
2954 1af628f4 2021-06-03 stsp goto done;
2956 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2958 1af628f4 2021-06-03 stsp goto done;
2959 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2960 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2961 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2962 1af628f4 2021-06-03 stsp goto done;
2964 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2965 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2966 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
2967 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2968 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2970 234035bc 2019-06-01 stsp } else if (blob1) {
2971 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2972 d6c87207 2019-08-02 stsp strlen(path1));
2973 1ee397ad 2019-07-12 stsp if (ie == NULL)
2974 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2975 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2977 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2978 2b92fad7 2019-06-02 stsp path1) == -1)
2979 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2981 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2984 2b92fad7 2019-06-02 stsp goto done;
2986 2b92fad7 2019-06-02 stsp switch (status) {
2987 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2988 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2989 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2991 1ee397ad 2019-07-12 stsp goto done;
2992 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2994 2b92fad7 2019-06-02 stsp goto done;
2996 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2998 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2999 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3000 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3001 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3003 1ee397ad 2019-07-12 stsp goto done;
3005 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3007 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3008 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3009 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3010 e8f02263 2022-01-23 thomas off_t blob1_size;
3012 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3013 0a22ca1a 2020-09-23 stsp * exists in the repository.
3015 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
3016 e8f02263 2022-01-23 thomas &blob1_size, path1);
3018 0a22ca1a 2020-09-23 stsp goto done;
3019 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3020 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3021 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3023 0a22ca1a 2020-09-23 stsp goto done;
3024 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3027 0a22ca1a 2020-09-23 stsp goto done;
3029 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3032 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3033 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3035 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3036 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3039 0a22ca1a 2020-09-23 stsp goto done;
3042 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3043 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3044 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3045 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3047 1ee397ad 2019-07-12 stsp goto done;
3049 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3050 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3052 1ee397ad 2019-07-12 stsp goto done;
3057 234035bc 2019-06-01 stsp } else if (blob2) {
3058 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3059 234035bc 2019-06-01 stsp path2) == -1)
3060 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3061 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3062 d6c87207 2019-08-02 stsp strlen(path2));
3064 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3065 7f91a133 2019-12-13 stsp -1, NULL, repo);
3067 234035bc 2019-06-01 stsp goto done;
3068 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3069 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3070 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3071 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3072 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3073 1ee397ad 2019-07-12 stsp status, path2);
3074 234035bc 2019-06-01 stsp goto done;
3076 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3077 36bf999c 2020-07-23 stsp char *link_target2;
3078 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3081 36bf999c 2020-07-23 stsp goto done;
3082 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3083 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3084 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3085 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3086 36bf999c 2020-07-23 stsp free(link_target2);
3087 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3088 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3089 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3090 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3091 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3092 526a746f 2020-07-23 stsp a->progress_arg);
3094 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3095 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3098 dfe9fba0 2020-07-23 stsp goto done;
3099 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3100 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3101 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3102 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3104 234035bc 2019-06-01 stsp goto done;
3107 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3108 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3109 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3110 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3111 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3112 ace90326 2021-09-27 thomas 0, 1, a->allow_bad_symlinks, repo,
3113 ace90326 2021-09-27 thomas a->progress_cb, a->progress_arg);
3115 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3116 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3117 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3120 234035bc 2019-06-01 stsp goto done;
3121 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3123 234035bc 2019-06-01 stsp goto done;
3124 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3125 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3126 3969253a 2020-03-07 stsp if (err) {
3127 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3128 3969253a 2020-03-07 stsp goto done;
3130 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3131 2b92fad7 2019-06-02 stsp if (err) {
3132 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3133 2b92fad7 2019-06-02 stsp goto done;
3135 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3136 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3137 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3142 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3143 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3144 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3145 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3146 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3147 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3148 1af628f4 2021-06-03 stsp free(id_str);
3149 54d5be07 2021-06-03 stsp free(label_deriv2);
3150 234035bc 2019-06-01 stsp free(ondisk_path);
3151 234035bc 2019-06-01 stsp return err;
3154 69de9dd4 2021-09-03 stsp static const struct got_error *
3155 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3157 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3159 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3160 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3161 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3162 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3163 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3165 69de9dd4 2021-09-03 stsp return NULL;
3168 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3169 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3170 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3171 234035bc 2019-06-01 stsp struct got_repository *repo;
3174 234035bc 2019-06-01 stsp static const struct got_error *
3175 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3176 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3177 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3178 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3179 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3181 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3182 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3183 234035bc 2019-06-01 stsp unsigned char status;
3184 234035bc 2019-06-01 stsp struct stat sb;
3185 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3186 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3187 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3188 234035bc 2019-06-01 stsp char *ondisk_path;
3190 69de9dd4 2021-09-03 stsp if (id == NULL)
3191 69de9dd4 2021-09-03 stsp return NULL;
3193 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3194 69de9dd4 2021-09-03 stsp if (ie == NULL)
3195 69de9dd4 2021-09-03 stsp return NULL;
3197 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3199 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3201 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3202 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3203 5546d466 2021-09-02 stsp free(ondisk_path);
3205 234035bc 2019-06-01 stsp return err;
3206 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3207 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3209 234035bc 2019-06-01 stsp return NULL;
3212 818c7501 2019-07-11 stsp static const struct got_error *
3213 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3214 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3215 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3216 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3217 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3219 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3220 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3221 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3222 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3223 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3224 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3225 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3226 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3227 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3229 03415a1a 2019-06-02 stsp if (commit_id1) {
3230 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3231 945f9229 2022-04-16 thomas if (err)
3232 945f9229 2022-04-16 thomas goto done;
3233 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3234 03415a1a 2019-06-02 stsp worktree->path_prefix);
3235 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3236 03415a1a 2019-06-02 stsp goto done;
3238 69d57f3d 2020-07-31 stsp if (tree_id1) {
3239 69d57f3d 2020-07-31 stsp char *id_str;
3241 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3243 03415a1a 2019-06-02 stsp goto done;
3245 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3247 f69721c3 2019-10-21 stsp goto done;
3249 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3250 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3251 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3252 f69721c3 2019-10-21 stsp free(id_str);
3253 f69721c3 2019-10-21 stsp goto done;
3255 f69721c3 2019-10-21 stsp free(id_str);
3257 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3258 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3259 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3260 a0f32f33 2022-06-13 thomas goto done;
3264 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3265 945f9229 2022-04-16 thomas if (err)
3266 945f9229 2022-04-16 thomas goto done;
3268 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3269 234035bc 2019-06-01 stsp worktree->path_prefix);
3271 234035bc 2019-06-01 stsp goto done;
3273 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3275 234035bc 2019-06-01 stsp goto done;
3277 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3278 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3279 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3280 19a6a6b5 2022-07-01 thomas goto done;
3283 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3284 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3285 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3286 a0f32f33 2022-06-13 thomas goto done;
3289 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3290 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3291 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3292 19a6a6b5 2022-07-01 thomas goto done;
3295 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3296 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3297 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3298 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3299 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3301 69de9dd4 2021-09-03 stsp goto done;
3303 234035bc 2019-06-01 stsp arg.worktree = worktree;
3304 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3305 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3306 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3307 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3308 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3309 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3310 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3311 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3312 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3313 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3314 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3315 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3316 af12c6b9 2019-06-04 stsp err = sync_err;
3318 945f9229 2022-04-16 thomas if (commit1)
3319 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3320 945f9229 2022-04-16 thomas if (commit2)
3321 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3322 234035bc 2019-06-01 stsp if (tree1)
3323 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3324 234035bc 2019-06-01 stsp if (tree2)
3325 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3326 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3327 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3328 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3329 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3330 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3331 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3332 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3333 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3334 f69721c3 2019-10-21 stsp free(label_orig);
3335 818c7501 2019-07-11 stsp return err;
3338 818c7501 2019-07-11 stsp const struct got_error *
3339 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3340 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3341 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3342 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3344 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3345 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3346 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3348 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3350 818c7501 2019-07-11 stsp return err;
3352 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3354 818c7501 2019-07-11 stsp goto done;
3356 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3357 69de9dd4 2021-09-03 stsp worktree);
3359 818c7501 2019-07-11 stsp goto done;
3361 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3362 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3363 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3365 818c7501 2019-07-11 stsp if (fileindex)
3366 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3367 818c7501 2019-07-11 stsp free(fileindex_path);
3368 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3369 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3370 234035bc 2019-06-01 stsp err = unlockerr;
3371 234035bc 2019-06-01 stsp return err;
3374 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3375 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3376 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3377 927df6b7 2019-02-10 stsp const char *status_path;
3378 927df6b7 2019-02-10 stsp size_t status_path_len;
3379 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3380 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3381 f8d1f275 2019-02-04 stsp void *status_arg;
3382 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3383 f8d1f275 2019-02-04 stsp void *cancel_arg;
3384 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3385 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3386 f2a9dc41 2019-12-13 tracey int report_unchanged;
3387 3143d852 2020-06-25 stsp int no_ignores;
3390 f8d1f275 2019-02-04 stsp static const struct got_error *
3391 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3392 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3393 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3394 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3396 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3397 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3398 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3399 927df6b7 2019-02-10 stsp struct stat sb;
3400 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3401 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3402 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3404 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3405 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3407 98eaaa12 2019-08-03 stsp return err;
3409 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3410 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3411 98eaaa12 2019-08-03 stsp return NULL;
3413 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3414 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3415 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3416 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3417 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3418 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3419 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3420 43010591 2023-02-17 thomas &staged_blob_id, ie);
3423 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3424 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3427 927df6b7 2019-02-10 stsp static const struct got_error *
3428 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3429 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3431 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3432 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3433 f8d1f275 2019-02-04 stsp char *abspath;
3435 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3436 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3438 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3439 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3440 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3441 927df6b7 2019-02-10 stsp return NULL;
3443 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3444 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3445 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3446 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3448 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3449 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3450 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3453 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3454 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3455 f8d1f275 2019-02-04 stsp free(abspath);
3456 9d31a1d8 2018-03-11 stsp return err;
3459 f8d1f275 2019-02-04 stsp static const struct got_error *
3460 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3462 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3463 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3464 2ec1f75b 2019-03-26 stsp unsigned char status;
3466 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3467 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3469 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3470 927df6b7 2019-02-10 stsp return NULL;
3472 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3473 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3474 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3475 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3477 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3478 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3479 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3482 ef20f542 2022-06-26 thomas static void
3483 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3485 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3487 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3488 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3490 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3492 21c2d8be 2023-01-10 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3495 6841da00 2019-08-08 stsp static const struct got_error *
3496 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3498 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3499 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3500 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3501 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3502 6841da00 2019-08-08 stsp size_t linesize = 0;
3503 6841da00 2019-08-08 stsp ssize_t linelen;
3505 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3506 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3507 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3508 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3510 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3511 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3512 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3514 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3515 bd8de430 2019-10-04 stsp if (line[0] == '#')
3518 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3519 bd8de430 2019-10-04 stsp if (line[0] == '!')
3522 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3523 6841da00 2019-08-08 stsp line) == -1) {
3524 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3525 6841da00 2019-08-08 stsp goto done;
3527 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3529 6841da00 2019-08-08 stsp goto done;
3531 6841da00 2019-08-08 stsp if (ferror(f)) {
3532 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3533 6841da00 2019-08-08 stsp goto done;
3536 6841da00 2019-08-08 stsp dirpath = strdup(path);
3537 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3538 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3539 6841da00 2019-08-08 stsp goto done;
3541 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3543 6841da00 2019-08-08 stsp free(line);
3544 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3545 6841da00 2019-08-08 stsp free(dirpath);
3546 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3548 6841da00 2019-08-08 stsp return err;
3551 1b5d300f 2023-02-20 thomas static int
3552 1b5d300f 2023-02-20 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
3553 1b5d300f 2023-02-20 thomas int flags)
3555 1b5d300f 2023-02-20 thomas char buf[PATH_MAX];
3558 1b5d300f 2023-02-20 thomas * Trailing slashes signify directories.
3559 1b5d300f 2023-02-20 thomas * Append a * to make such patterns conform to fnmatch rules.
3561 1b5d300f 2023-02-20 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3562 1b5d300f 2023-02-20 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3563 1b5d300f 2023-02-20 thomas return FNM_NOMATCH; /* XXX */
3565 1b5d300f 2023-02-20 thomas return fnmatch(buf, path, flags);
3568 1b5d300f 2023-02-20 thomas return fnmatch(pattern, path, flags);
3571 ef20f542 2022-06-26 thomas static int
3572 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3574 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3576 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3577 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3578 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3579 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3581 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3582 1b5d300f 2023-02-20 thomas const char *p;
3584 1b5d300f 2023-02-20 thomas if (pi->path_len < 3 ||
3585 1b5d300f 2023-02-20 thomas strncmp(pi->path, "**/", 3) != 0)
3588 bd8de430 2019-10-04 stsp while (*p) {
3589 1b5d300f 2023-02-20 thomas if (match_path(pi->path + 3,
3590 1b5d300f 2023-02-20 thomas pi->path_len - 3, p,
3591 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3592 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3593 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3595 bd8de430 2019-10-04 stsp while (*p == '/')
3605 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3606 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3607 6841da00 2019-08-08 stsp * ignores backwards.
3609 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3610 6841da00 2019-08-08 stsp while (pe) {
3611 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3612 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3613 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3614 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3615 1b5d300f 2023-02-20 thomas int flags = FNM_LEADING_DIR;
3616 1b5d300f 2023-02-20 thomas if (strstr(pi->path, "/**/") == NULL)
3617 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3618 1b5d300f 2023-02-20 thomas if (match_path(pi->path, pi->path_len,
3619 1b5d300f 2023-02-20 thomas path, flags))
3624 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3630 6841da00 2019-08-08 stsp static const struct got_error *
3631 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3632 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3634 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3635 6841da00 2019-08-08 stsp char *ignorespath;
3636 886cec17 2019-12-15 stsp int fd = -1;
3637 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3639 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3640 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3641 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3643 886cec17 2019-12-15 stsp if (dirfd != -1) {
3644 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3645 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3646 886cec17 2019-12-15 stsp if (fd == -1) {
3647 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3648 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3649 886cec17 2019-12-15 stsp ignorespath);
3651 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3652 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3653 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3654 886cec17 2019-12-15 stsp ignorespath);
3657 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3661 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3662 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3663 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3664 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3665 886cec17 2019-12-15 stsp ignorespath);
3667 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3670 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3671 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3672 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3673 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3674 6841da00 2019-08-08 stsp free(ignorespath);
3675 6841da00 2019-08-08 stsp return err;
3678 f8d1f275 2019-02-04 stsp static const struct got_error *
3679 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3680 6092c299 2021-10-04 thomas int dirfd)
3682 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3683 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3684 f8d1f275 2019-02-04 stsp char *path = NULL;
3686 6092c299 2021-10-04 thomas if (ignore != NULL)
3687 6092c299 2021-10-04 thomas *ignore = 0;
3689 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3690 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3692 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3693 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3694 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3696 f8d1f275 2019-02-04 stsp path = de->d_name;
3699 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3700 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3701 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3702 6092c299 2021-10-04 thomas *ignore = 1;
3703 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3704 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3705 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3706 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3707 f8d1f275 2019-02-04 stsp if (parent_path[0])
3708 f8d1f275 2019-02-04 stsp free(path);
3709 b72f483a 2019-02-05 stsp return err;
3712 347d1d3e 2019-07-12 stsp static const struct got_error *
3713 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3715 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3716 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3718 3143d852 2020-06-25 stsp if (a->no_ignores)
3719 3143d852 2020-06-25 stsp return NULL;
3721 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3722 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3724 3143d852 2020-06-25 stsp return err;
3726 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3727 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3729 3143d852 2020-06-25 stsp return err;
3732 3143d852 2020-06-25 stsp static const struct got_error *
3733 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3734 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3735 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3736 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3738 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3739 abb4604f 2019-07-27 stsp struct stat sb;
3741 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3743 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3744 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3746 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3747 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3748 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3749 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3750 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3753 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3754 fe1d8685 2022-02-12 thomas return NULL;
3756 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3757 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3758 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3760 abb4604f 2019-07-27 stsp return NULL;
3763 3143d852 2020-06-25 stsp static const struct got_error *
3764 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3765 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3767 3143d852 2020-06-25 stsp const struct got_error *err;
3768 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3770 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3771 3143d852 2020-06-25 stsp ".cvsignore");
3773 3143d852 2020-06-25 stsp return err;
3775 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3776 3143d852 2020-06-25 stsp ".gitignore");
3778 3143d852 2020-06-25 stsp return err;
3780 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3781 3143d852 2020-06-25 stsp if (err) {
3782 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3783 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3784 3143d852 2020-06-25 stsp return err;
3786 3143d852 2020-06-25 stsp for (;;) {
3787 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3788 3143d852 2020-06-25 stsp ".cvsignore");
3791 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3792 3143d852 2020-06-25 stsp ".gitignore");
3795 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3796 3143d852 2020-06-25 stsp if (err) {
3797 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3798 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3801 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3803 b737c85a 2020-06-26 stsp free(parent_path);
3804 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3805 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3808 b737c85a 2020-06-26 stsp free(parent_path);
3809 b737c85a 2020-06-26 stsp free(next_parent_path);
3810 3143d852 2020-06-25 stsp return err;
3813 abb4604f 2019-07-27 stsp static const struct got_error *
3814 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3815 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3816 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3817 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3818 f2a9dc41 2019-12-13 tracey int report_unchanged)
3820 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3821 6fc93f37 2019-12-13 stsp int fd = -1;
3822 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3823 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3824 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3825 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3826 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3828 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3830 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3831 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3832 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3834 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3835 a78810f8 2022-03-13 thomas if (ie) {
3836 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3837 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3838 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3839 a78810f8 2022-03-13 thomas goto done;
3842 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3843 6fc93f37 2019-12-13 stsp if (fd == -1) {
3844 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3845 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3846 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3848 ff56836b 2021-07-08 stsp if (!no_ignores) {
3849 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3850 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3852 ff56836b 2021-07-08 stsp goto done;
3854 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3855 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3856 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3859 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3860 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3861 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3862 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3863 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3864 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3865 abb4604f 2019-07-27 stsp arg.status_path = path;
3866 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3867 abb4604f 2019-07-27 stsp arg.repo = repo;
3868 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3869 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3870 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3871 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3872 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3873 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3874 022fae89 2019-12-06 tracey if (!no_ignores) {
3875 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3876 3143d852 2020-06-25 stsp worktree->root_path, path);
3878 3143d852 2020-06-25 stsp goto done;
3880 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3881 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3882 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3885 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3886 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3887 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3888 927df6b7 2019-02-10 stsp free(ondisk_path);
3889 347d1d3e 2019-07-12 stsp return err;
3892 347d1d3e 2019-07-12 stsp const struct got_error *
3893 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3894 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3895 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3896 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3898 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3899 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3900 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3901 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3903 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3905 347d1d3e 2019-07-12 stsp return err;
3907 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3908 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3909 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3910 f6343036 2021-06-22 stsp no_ignores, 0);
3914 f8d1f275 2019-02-04 stsp free(fileindex_path);
3915 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3916 f8d1f275 2019-02-04 stsp return err;
3919 6c7ab921 2019-03-18 stsp const struct got_error *
3920 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3921 6c7ab921 2019-03-18 stsp const char *arg)
3923 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3924 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3925 6c7ab921 2019-03-18 stsp size_t len;
3926 00bb5ea0 2020-07-23 stsp struct stat sb;
3927 01740607 2020-11-04 stsp char *abspath = NULL;
3928 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3930 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3932 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3933 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3934 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3936 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3937 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3938 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3939 00bb5ea0 2020-07-23 stsp goto done;
3941 727173c3 2020-11-06 stsp sb.st_mode = 0;
3943 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3945 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3946 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3947 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3948 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3950 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3951 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3952 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3953 00bb5ea0 2020-07-23 stsp goto done;
3957 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3958 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3960 d0710d08 2019-07-22 stsp goto done;
3961 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3962 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3963 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3964 00bb5ea0 2020-07-23 stsp goto done;
3967 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3968 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3969 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3970 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3971 00bb5ea0 2020-07-23 stsp goto done;
3973 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3974 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3975 01740607 2020-11-04 stsp goto done;
3977 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3978 01740607 2020-11-04 stsp sizeof(canonpath));
3980 00bb5ea0 2020-07-23 stsp goto done;
3981 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3982 01740607 2020-11-04 stsp if (resolved == NULL) {
3983 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3984 01740607 2020-11-04 stsp goto done;
3989 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3990 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3991 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3992 6c7ab921 2019-03-18 stsp goto done;
3995 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3996 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3997 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3999 f6d88e1a 2019-05-29 stsp goto done;
4001 f6d88e1a 2019-05-29 stsp path = strdup("");
4002 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4003 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4004 f6d88e1a 2019-05-29 stsp goto done;
4008 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4009 6c7ab921 2019-03-18 stsp len = strlen(path);
4010 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4011 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4015 01740607 2020-11-04 stsp free(abspath);
4016 6c7ab921 2019-03-18 stsp free(resolved);
4017 d0710d08 2019-07-22 stsp free(cwd);
4018 6c7ab921 2019-03-18 stsp if (err == NULL)
4019 6c7ab921 2019-03-18 stsp *wt_path = path;
4021 6c7ab921 2019-03-18 stsp free(path);
4022 d00136be 2019-03-26 stsp return err;
4025 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4026 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4027 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4028 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4029 4e68cba3 2019-11-23 stsp void *progress_arg;
4030 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4033 4e68cba3 2019-11-23 stsp static const struct got_error *
4034 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4035 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4036 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4037 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4039 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4040 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4041 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4042 6d022e97 2019-08-04 stsp struct stat sb;
4043 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4045 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4046 dbb83fbd 2019-12-12 stsp relpath) == -1)
4047 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4049 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4051 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4052 12463d8b 2019-12-13 stsp de_name, a->repo);
4054 dbb83fbd 2019-12-12 stsp goto done;
4055 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4056 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
4057 dbb83fbd 2019-12-12 stsp goto done;
4058 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4060 dbb83fbd 2019-12-12 stsp goto done;
4063 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4064 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4065 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4067 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4068 dbb83fbd 2019-12-12 stsp goto done;
4071 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4073 4e68cba3 2019-11-23 stsp goto done;
4074 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4075 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4076 3969253a 2020-03-07 stsp if (err) {
4077 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4078 3969253a 2020-03-07 stsp goto done;
4080 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4081 07f5b47a 2019-06-02 stsp if (err) {
4082 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4083 4e68cba3 2019-11-23 stsp goto done;
4086 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4088 dbb83fbd 2019-12-12 stsp return err;
4089 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4090 dbb83fbd 2019-12-12 stsp return NULL;
4091 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4094 d00136be 2019-03-26 stsp const struct got_error *
4095 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4096 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4097 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4098 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4100 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4101 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4102 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4103 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4104 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4106 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4108 d00136be 2019-03-26 stsp return err;
4110 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4112 d00136be 2019-03-26 stsp goto done;
4114 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4115 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4116 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4117 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4118 4e68cba3 2019-11-23 stsp saa.repo = repo;
4120 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4121 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4122 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4126 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4127 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4128 af12c6b9 2019-06-04 stsp err = sync_err;
4130 fb399478 2019-07-12 stsp free(fileindex_path);
4131 d00136be 2019-03-26 stsp if (fileindex)
4132 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4133 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4134 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4135 d00136be 2019-03-26 stsp err = unlockerr;
4136 6c7ab921 2019-03-18 stsp return err;
4139 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4140 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4141 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4142 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4143 f2a9dc41 2019-12-13 tracey void *progress_arg;
4144 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4145 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4146 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4147 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4148 766841c2 2020-08-13 stsp const char *status_codes;
4151 f2a9dc41 2019-12-13 tracey static const struct got_error *
4152 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4153 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4154 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4155 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4157 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4158 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4159 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4160 17ed4618 2019-06-02 stsp struct stat sb;
4161 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4163 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4164 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4165 d64cc78a 2022-01-25 thomas return NULL;
4166 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4169 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4170 17ed4618 2019-06-02 stsp if (ie == NULL)
4171 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4173 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4174 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4175 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4176 9acbc4fa 2019-08-03 stsp return NULL;
4177 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4180 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4181 f2a9dc41 2019-12-13 tracey relpath) == -1)
4182 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4184 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4187 f2a9dc41 2019-12-13 tracey goto done;
4189 766841c2 2020-08-13 stsp if (a->status_codes) {
4190 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4192 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4193 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4196 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4197 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4198 766841c2 2020-08-13 stsp free(ondisk_path);
4199 766841c2 2020-08-13 stsp return NULL;
4201 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4202 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4203 766841c2 2020-08-13 stsp static char msg[64];
4204 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4205 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4206 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4207 766841c2 2020-08-13 stsp goto done;
4211 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4212 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4213 f2a9dc41 2019-12-13 tracey goto done;
4214 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4215 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4216 f2a9dc41 2019-12-13 tracey goto done;
4218 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4219 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4220 d64cc78a 2022-01-25 thomas goto done;
4222 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4223 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4224 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4225 f2a9dc41 2019-12-13 tracey goto done;
4229 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4230 20a2ad1c 2020-10-20 stsp size_t root_len;
4232 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4233 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4234 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4235 12463d8b 2019-12-13 stsp ondisk_path);
4236 12463d8b 2019-12-13 stsp goto done;
4238 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4239 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4240 12463d8b 2019-12-13 stsp goto done;
4243 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4245 20a2ad1c 2020-10-20 stsp char *parent;
4246 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4248 2513f20a 2020-10-20 stsp goto done;
4249 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4250 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4251 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4252 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4253 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4254 20a2ad1c 2020-10-20 stsp ondisk_path);
4257 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4258 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4261 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4263 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4264 f2a9dc41 2019-12-13 tracey if (err)
4265 f2a9dc41 2019-12-13 tracey return err;
4266 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4267 f2a9dc41 2019-12-13 tracey return NULL;
4268 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4269 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4272 2ec1f75b 2019-03-26 stsp const struct got_error *
4273 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4274 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4275 766841c2 2020-08-13 stsp const char *status_codes,
4276 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4277 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4279 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4280 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4281 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4282 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4283 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4285 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4287 2ec1f75b 2019-03-26 stsp return err;
4289 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4291 2ec1f75b 2019-03-26 stsp goto done;
4293 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4294 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4295 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4296 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4297 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4298 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4299 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4300 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4301 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4303 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4304 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4305 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4309 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4310 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4311 af12c6b9 2019-06-04 stsp err = sync_err;
4313 fb399478 2019-07-12 stsp free(fileindex_path);
4314 2ec1f75b 2019-03-26 stsp if (fileindex)
4315 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4316 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4317 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4318 2ec1f75b 2019-03-26 stsp err = unlockerr;
4319 33aa809d 2019-08-08 stsp return err;
4322 33aa809d 2019-08-08 stsp static const struct got_error *
4323 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4325 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4326 33aa809d 2019-08-08 stsp char *line = NULL;
4327 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4328 33aa809d 2019-08-08 stsp ssize_t linelen;
4330 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4331 33aa809d 2019-08-08 stsp if (linelen == -1) {
4332 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4333 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4334 33aa809d 2019-08-08 stsp goto done;
4336 33aa809d 2019-08-08 stsp return NULL;
4338 33aa809d 2019-08-08 stsp if (outfile) {
4339 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4340 33aa809d 2019-08-08 stsp if (n != linelen) {
4341 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4342 33aa809d 2019-08-08 stsp goto done;
4345 33aa809d 2019-08-08 stsp if (rejectfile) {
4346 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4347 33aa809d 2019-08-08 stsp if (n != linelen)
4348 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4351 33aa809d 2019-08-08 stsp free(line);
4352 2ec1f75b 2019-03-26 stsp return err;
4355 33aa809d 2019-08-08 stsp static const struct got_error *
4356 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4358 33aa809d 2019-08-08 stsp char *line = NULL;
4359 33aa809d 2019-08-08 stsp size_t linesize = 0;
4360 33aa809d 2019-08-08 stsp ssize_t linelen;
4362 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4363 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4364 500467ff 2019-09-25 hiltjo if (ferror(f))
4365 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4366 500467ff 2019-09-25 hiltjo return NULL;
4368 33aa809d 2019-08-08 stsp free(line);
4369 33aa809d 2019-08-08 stsp return NULL;
4372 33aa809d 2019-08-08 stsp static const struct got_error *
4373 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4374 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4375 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4377 33aa809d 2019-08-08 stsp const struct got_error *err;
4379 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4380 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4381 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4383 33aa809d 2019-08-08 stsp return err;
4384 33aa809d 2019-08-08 stsp (*line_cur1)++;
4386 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4387 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4388 33aa809d 2019-08-08 stsp if (rejectfile)
4389 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4391 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4393 33aa809d 2019-08-08 stsp return err;
4394 33aa809d 2019-08-08 stsp (*line_cur2)++;
4396 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4397 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4398 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4400 33aa809d 2019-08-08 stsp return err;
4401 33aa809d 2019-08-08 stsp (*line_cur2)++;
4403 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4404 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4405 33aa809d 2019-08-08 stsp if (rejectfile)
4406 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4408 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4410 33aa809d 2019-08-08 stsp return err;
4411 33aa809d 2019-08-08 stsp (*line_cur1)++;
4414 f1e81a05 2019-08-10 stsp return NULL;
4417 f1e81a05 2019-08-10 stsp static const struct got_error *
4418 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4419 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4421 f1e81a05 2019-08-10 stsp const struct got_error *err;
4423 f1e81a05 2019-08-10 stsp if (outfile) {
4424 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4425 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4426 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4428 f1e81a05 2019-08-10 stsp return err;
4429 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4432 f1e81a05 2019-08-10 stsp if (rejectfile) {
4433 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4434 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4435 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4437 f1e81a05 2019-08-10 stsp return err;
4438 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4442 33aa809d 2019-08-08 stsp return NULL;
4445 33aa809d 2019-08-08 stsp static const struct got_error *
4446 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4447 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4448 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4449 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4450 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4452 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4453 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4454 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4455 33aa809d 2019-08-08 stsp FILE *hunkfile;
4456 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4457 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4460 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4462 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4463 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4464 fe621944 2020-11-10 stsp start_old = cc.left.start;
4465 fe621944 2020-11-10 stsp end_old = cc.left.end;
4466 fe621944 2020-11-10 stsp start_new = cc.right.start;
4467 fe621944 2020-11-10 stsp end_new = cc.right.end;
4469 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4470 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4471 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4473 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4474 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4475 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4477 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4478 fe621944 2020-11-10 stsp if (diff_state == NULL)
4479 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4480 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4482 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4483 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4484 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4485 33aa809d 2019-08-08 stsp goto done;
4488 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4489 fe621944 2020-11-10 stsp diff_result, &cc);
4490 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4491 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4492 33aa809d 2019-08-08 stsp goto done;
4495 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4496 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4497 33aa809d 2019-08-08 stsp goto done;
4500 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4501 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4503 33aa809d 2019-08-08 stsp goto done;
4505 33aa809d 2019-08-08 stsp switch (*choice) {
4506 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4507 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4508 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4510 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4511 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4512 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4514 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4517 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4521 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4522 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4523 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4524 33aa809d 2019-08-08 stsp return err;
4527 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4528 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4529 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4530 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4531 1f1abb7e 2019-08-08 stsp void *progress_arg;
4532 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4533 33aa809d 2019-08-08 stsp void *patch_arg;
4534 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4535 10604dce 2021-09-24 thomas int unlink_added_files;
4538 e20a8b6f 2019-06-04 stsp static const struct got_error *
4539 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4540 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4541 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4542 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4543 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4545 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4546 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4547 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4548 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4549 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4550 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4551 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4552 fe621944 2020-11-10 stsp struct stat sb2;
4553 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4554 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4555 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4557 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4559 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4561 33aa809d 2019-08-08 stsp return err;
4563 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4564 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4565 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4566 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4567 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4568 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4569 fa3cef63 2020-07-23 stsp goto done;
4571 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4572 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4573 48b4f239 2021-12-31 thomas if (link_len == -1) {
4574 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4577 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4578 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4581 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4582 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4583 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4584 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4585 fa3cef63 2020-07-23 stsp goto done;
4587 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4588 fa3cef63 2020-07-23 stsp sizeof(link_target));
4589 fa3cef63 2020-07-23 stsp if (link_len == -1)
4590 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4591 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4592 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4595 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4596 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4597 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4598 fa3cef63 2020-07-23 stsp goto done;
4601 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4602 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4603 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4604 fa3cef63 2020-07-23 stsp goto done;
4609 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4610 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4611 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4612 fa3cef63 2020-07-23 stsp goto done;
4614 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4615 fa3cef63 2020-07-23 stsp if (n != link_len) {
4616 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4617 fa3cef63 2020-07-23 stsp goto done;
4619 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4620 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4621 fa3cef63 2020-07-23 stsp goto done;
4623 fa3cef63 2020-07-23 stsp rewind(f2);
4626 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4627 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4628 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4629 f4ae6ddb 2022-07-01 thomas goto done;
4632 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4634 33aa809d 2019-08-08 stsp goto done;
4636 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4638 33aa809d 2019-08-08 stsp goto done;
4640 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4642 33aa809d 2019-08-08 stsp goto done;
4644 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4645 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4647 33aa809d 2019-08-08 stsp goto done;
4649 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4652 33aa809d 2019-08-08 stsp goto done;
4654 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4655 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4656 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4657 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4659 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4660 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4661 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4662 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4663 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4664 fe621944 2020-11-10 stsp nchanges++;
4666 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4667 33aa809d 2019-08-08 stsp int choice;
4668 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4669 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4670 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4671 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4672 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4673 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4675 33aa809d 2019-08-08 stsp goto done;
4676 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4677 33aa809d 2019-08-08 stsp have_content = 1;
4678 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4681 1ebedb77 2019-10-19 stsp if (have_content) {
4682 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4683 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4684 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4686 1ebedb77 2019-10-19 stsp goto done;
4688 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4689 a2c162eb 2022-10-30 thomas mode_t mode;
4691 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
4692 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
4693 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4694 fa3cef63 2020-07-23 stsp goto done;
4699 33aa809d 2019-08-08 stsp free(id_str);
4700 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4701 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4703 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4704 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4705 fe621944 2020-11-10 stsp if (err == NULL)
4706 fe621944 2020-11-10 stsp err = free_err;
4707 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4708 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4709 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4710 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4711 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4712 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4713 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4714 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4715 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4716 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4717 33aa809d 2019-08-08 stsp if (err || !have_content) {
4718 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4719 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4720 33aa809d 2019-08-08 stsp free(*path_outfile);
4721 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4723 33aa809d 2019-08-08 stsp free(path1);
4724 33aa809d 2019-08-08 stsp return err;
4727 33aa809d 2019-08-08 stsp static const struct got_error *
4728 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4729 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4730 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4731 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4733 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4734 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4735 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4736 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4737 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4738 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4739 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4740 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4741 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4742 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4743 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4744 f4ae6ddb 2022-07-01 thomas int fd = -1;
4746 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4747 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4748 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4749 d3bcc3d1 2019-08-08 stsp return NULL;
4751 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4752 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4753 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4755 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4756 65084dad 2019-08-08 stsp if (ie == NULL)
4757 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4759 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4760 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4761 a129376b 2019-03-28 stsp if (err) {
4762 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4763 a129376b 2019-03-28 stsp goto done;
4764 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4765 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4766 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4767 e20a8b6f 2019-06-04 stsp goto done;
4770 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4771 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4772 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4773 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4774 a129376b 2019-03-28 stsp goto done;
4777 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4778 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4779 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4780 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4781 a129376b 2019-03-28 stsp goto done;
4784 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4785 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4786 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4787 a129376b 2019-03-28 stsp goto done;
4792 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4793 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
4794 945f9229 2022-04-16 thomas if (err)
4795 945f9229 2022-04-16 thomas goto done;
4797 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4798 a9fa2909 2019-07-27 stsp if (err) {
4799 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4800 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4801 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4802 a9fa2909 2019-07-27 stsp goto done;
4804 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4806 a9fa2909 2019-07-27 stsp goto done;
4808 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4810 a9fa2909 2019-07-27 stsp goto done;
4812 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4813 1233e6b6 2020-10-19 stsp free(te_name);
4814 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4815 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4816 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4817 a9fa2909 2019-07-27 stsp goto done;
4821 a129376b 2019-03-28 stsp switch (status) {
4822 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4823 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4824 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4825 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4826 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4828 33aa809d 2019-08-08 stsp goto done;
4829 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4832 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4833 1f1abb7e 2019-08-08 stsp ie->path);
4835 1ee397ad 2019-07-12 stsp goto done;
4836 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4837 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
4838 10604dce 2021-09-24 thomas if (asprintf(&ondisk_path, "%s/%s",
4839 10604dce 2021-09-24 thomas got_worktree_get_root_path(a->worktree),
4840 10604dce 2021-09-24 thomas relpath) == -1) {
4841 10604dce 2021-09-24 thomas err = got_error_from_errno("asprintf");
4842 10604dce 2021-09-24 thomas goto done;
4844 10604dce 2021-09-24 thomas if (unlink(ondisk_path) == -1) {
4845 10604dce 2021-09-24 thomas err = got_error_from_errno2("unlink",
4846 10604dce 2021-09-24 thomas ondisk_path);
4851 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4852 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4853 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4854 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4855 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4857 33aa809d 2019-08-08 stsp goto done;
4858 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4861 33aa809d 2019-08-08 stsp /* fall through */
4862 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4863 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4864 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4865 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4866 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4867 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4868 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
4869 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
4871 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
4872 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4873 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4874 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4875 f4ae6ddb 2022-07-01 thomas goto done;
4878 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4880 65084dad 2019-08-08 stsp goto done;
4882 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4883 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4884 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4885 a129376b 2019-03-28 stsp goto done;
4888 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4889 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4890 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4891 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4892 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4893 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4894 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4896 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4897 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4898 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4899 369fd7e5 2020-07-23 stsp path_content);
4902 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4903 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4904 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4905 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4907 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4908 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4909 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4910 369fd7e5 2020-07-23 stsp goto done;
4914 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4915 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4916 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4917 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4918 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4919 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4921 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4923 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4924 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4925 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4926 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4929 a129376b 2019-03-28 stsp goto done;
4930 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4931 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4932 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4933 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4934 437adc9d 2020-12-10 yzhong blob->id.sha1,
4935 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4937 2e1fa222 2020-07-23 stsp goto done;
4939 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4940 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4941 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4950 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4951 33aa809d 2019-08-08 stsp free(path_content);
4952 e20a8b6f 2019-06-04 stsp free(parent_path);
4953 a129376b 2019-03-28 stsp free(tree_path);
4954 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4955 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4957 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4959 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4960 a129376b 2019-03-28 stsp free(tree_id);
4961 945f9229 2022-04-16 thomas if (base_commit)
4962 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
4963 e20a8b6f 2019-06-04 stsp return err;
4966 e20a8b6f 2019-06-04 stsp const struct got_error *
4967 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4968 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4969 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4970 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4971 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4973 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4974 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4975 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4976 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4977 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4978 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4980 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4982 e20a8b6f 2019-06-04 stsp return err;
4984 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4986 e20a8b6f 2019-06-04 stsp goto done;
4988 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4989 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4990 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4991 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4992 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4993 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4994 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4995 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
4996 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4997 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4998 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
5002 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5003 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5004 e20a8b6f 2019-06-04 stsp err = sync_err;
5006 fb399478 2019-07-12 stsp free(fileindex_path);
5007 a129376b 2019-03-28 stsp if (fileindex)
5008 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5009 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5010 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5011 a129376b 2019-03-28 stsp err = unlockerr;
5012 c4296144 2019-05-09 stsp return err;
5015 cf066bf8 2019-05-09 stsp static void
5016 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5018 24519714 2019-05-09 stsp free(ct->path);
5019 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5020 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5021 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5022 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5023 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5024 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5028 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5029 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5030 24519714 2019-05-09 stsp struct got_repository *repo;
5031 24519714 2019-05-09 stsp struct got_worktree *worktree;
5032 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5033 5f8a88c6 2019-08-03 stsp int have_staged_files;
5034 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5035 ef899790 2022-11-01 thomas int diff_header_shown;
5036 be94c032 2023-02-20 thomas int commit_conflicts;
5037 ef899790 2022-11-01 thomas FILE *diff_outfile;
5038 ef899790 2022-11-01 thomas FILE *f1;
5039 ef899790 2022-11-01 thomas FILE *f2;
5043 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5044 ef899790 2022-11-01 thomas * it as content to the diff engine.
5046 ef899790 2022-11-01 thomas static const struct got_error *
5047 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5048 ef899790 2022-11-01 thomas const char *abspath)
5050 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5051 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5052 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5054 ef899790 2022-11-01 thomas *fd = -1;
5056 ef899790 2022-11-01 thomas if (dirfd != -1) {
5057 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5058 ef899790 2022-11-01 thomas if (target_len == -1)
5059 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5060 ef899790 2022-11-01 thomas } else {
5061 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5062 ef899790 2022-11-01 thomas if (target_len == -1)
5063 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5066 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5067 ef899790 2022-11-01 thomas if (*fd == -1)
5068 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5070 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5071 ef899790 2022-11-01 thomas if (outlen == -1) {
5072 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5073 ef899790 2022-11-01 thomas goto done;
5076 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5077 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5078 ef899790 2022-11-01 thomas goto done;
5081 ef899790 2022-11-01 thomas if (err) {
5082 ef899790 2022-11-01 thomas close(*fd);
5083 ef899790 2022-11-01 thomas *fd = -1;
5085 ef899790 2022-11-01 thomas return err;
5088 ef899790 2022-11-01 thomas static const struct got_error *
5089 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5090 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5091 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5093 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5094 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5095 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5096 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5097 ef899790 2022-11-01 thomas char *label1 = NULL;
5098 ef899790 2022-11-01 thomas struct stat sb;
5099 ef899790 2022-11-01 thomas off_t size1 = 0;
5100 ef899790 2022-11-01 thomas int f2_exists = 0;
5101 ef899790 2022-11-01 thomas char *id_str = NULL;
5103 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5105 d259e491 2022-11-01 thomas if (diff_staged) {
5106 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5107 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5108 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5109 d259e491 2022-11-01 thomas return NULL;
5110 d259e491 2022-11-01 thomas } else {
5111 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5112 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5113 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5114 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5115 d259e491 2022-11-01 thomas return NULL;
5118 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5119 ef899790 2022-11-01 thomas if (err)
5120 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5121 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5122 ef899790 2022-11-01 thomas if (err)
5123 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5125 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5126 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5127 ef899790 2022-11-01 thomas if (err)
5128 ef899790 2022-11-01 thomas return err;
5129 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5130 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5131 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5132 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5133 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5134 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5135 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5138 ef899790 2022-11-01 thomas if (diff_staged) {
5139 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5140 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5141 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5142 ef899790 2022-11-01 thomas label1 = ct->path;
5143 ef899790 2022-11-01 thomas label2 = ct->path;
5145 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5146 ef899790 2022-11-01 thomas label2 = ct->path;
5148 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5149 ef899790 2022-11-01 thomas label1 = ct->path;
5151 ef899790 2022-11-01 thomas default:
5152 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5154 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5155 ef899790 2022-11-01 thomas if (fd1 == -1) {
5156 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5157 ef899790 2022-11-01 thomas goto done;
5159 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5160 ef899790 2022-11-01 thomas if (fd2 == -1) {
5161 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5162 ef899790 2022-11-01 thomas goto done;
5164 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5165 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5166 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5167 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5168 ef899790 2022-11-01 thomas goto done;
5171 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5172 ef899790 2022-11-01 thomas if (fd1 == -1) {
5173 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5174 ef899790 2022-11-01 thomas goto done;
5177 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5178 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5179 ef899790 2022-11-01 thomas 8192, fd1);
5180 ef899790 2022-11-01 thomas if (err)
5181 ef899790 2022-11-01 thomas goto done;
5184 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5185 ef899790 2022-11-01 thomas if (dirfd != -1) {
5186 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5187 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5188 ef899790 2022-11-01 thomas if (fd == -1) {
5189 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5190 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5191 ef899790 2022-11-01 thomas ct->ondisk_path);
5192 ef899790 2022-11-01 thomas goto done;
5194 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5195 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5196 ef899790 2022-11-01 thomas if (err)
5197 ef899790 2022-11-01 thomas goto done;
5199 ef899790 2022-11-01 thomas } else {
5200 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5201 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5202 ef899790 2022-11-01 thomas if (fd == -1) {
5203 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5204 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5205 ef899790 2022-11-01 thomas ct->ondisk_path);
5206 ef899790 2022-11-01 thomas goto done;
5208 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5209 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5210 ef899790 2022-11-01 thomas if (err)
5211 ef899790 2022-11-01 thomas goto done;
5214 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5215 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5216 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5217 ef899790 2022-11-01 thomas goto done;
5219 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5220 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5221 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5222 ef899790 2022-11-01 thomas goto done;
5224 ef899790 2022-11-01 thomas fd = -1;
5225 ef899790 2022-11-01 thomas f2_exists = 1;
5228 ef899790 2022-11-01 thomas if (blob1) {
5229 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5230 ef899790 2022-11-01 thomas f1, blob1);
5231 ef899790 2022-11-01 thomas if (err)
5232 ef899790 2022-11-01 thomas goto done;
5235 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5236 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5237 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5239 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5240 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5241 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5242 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5243 ef899790 2022-11-01 thomas if (blob1)
5244 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5245 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5246 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5247 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5248 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5249 ef899790 2022-11-01 thomas return err;
5252 c4296144 2019-05-09 stsp static const struct got_error *
5253 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5254 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5255 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5256 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5258 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5259 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5260 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5261 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5262 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5263 768aea60 2019-05-09 stsp struct stat sb;
5265 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5266 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5267 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5268 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5269 5f8a88c6 2019-08-03 stsp return NULL;
5271 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5272 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5273 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5276 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5277 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5278 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5279 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5280 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5281 5f8a88c6 2019-08-03 stsp return NULL;
5284 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5285 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5286 036813ee 2019-05-09 stsp goto done;
5288 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5289 036813ee 2019-05-09 stsp parent_path = strdup("");
5290 69960a46 2019-05-09 stsp if (parent_path == NULL)
5291 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5293 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5295 69960a46 2019-05-09 stsp return err;
5298 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5299 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5300 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5301 c4296144 2019-05-09 stsp goto done;
5304 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5305 768aea60 2019-05-09 stsp relpath) == -1) {
5306 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5307 768aea60 2019-05-09 stsp goto done;
5310 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5311 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5312 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5313 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5314 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5315 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5316 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5317 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5319 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5320 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5323 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5324 0aeb8099 2020-07-23 stsp goto done;
5326 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5327 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5328 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5329 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5330 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5331 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5332 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5333 12463d8b 2019-12-13 stsp ct->ondisk_path);
5334 12463d8b 2019-12-13 stsp goto done;
5336 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5337 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5338 768aea60 2019-05-09 stsp goto done;
5340 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5343 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5344 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5345 44d03001 2019-05-09 stsp relpath) == -1) {
5346 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5347 44d03001 2019-05-09 stsp goto done;
5350 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5351 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5352 35213c7c 2020-07-23 stsp int is_bad_symlink;
5353 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5354 35213c7c 2020-07-23 stsp ssize_t target_len;
5355 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5356 35213c7c 2020-07-23 stsp sizeof(target_path));
5357 35213c7c 2020-07-23 stsp if (target_len == -1) {
5358 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5359 35213c7c 2020-07-23 stsp ct->ondisk_path);
5360 35213c7c 2020-07-23 stsp goto done;
5362 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5363 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5365 35213c7c 2020-07-23 stsp goto done;
5366 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5367 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5368 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5369 35213c7c 2020-07-23 stsp goto done;
5374 cf066bf8 2019-05-09 stsp ct->status = status;
5375 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5376 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5377 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5378 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5379 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5380 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5381 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5382 b416585c 2019-05-13 stsp goto done;
5384 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5385 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5386 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5387 f0b75401 2019-08-03 stsp goto done;
5390 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5391 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5392 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5393 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5394 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5395 036813ee 2019-05-09 stsp goto done;
5398 24519714 2019-05-09 stsp ct->path = strdup(path);
5399 24519714 2019-05-09 stsp if (ct->path == NULL) {
5400 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5401 24519714 2019-05-09 stsp goto done;
5403 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5404 ef899790 2022-11-01 thomas if (err)
5405 ef899790 2022-11-01 thomas goto done;
5407 ef899790 2022-11-01 thomas if (a->diff_outfile && ct && new != NULL) {
5408 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5409 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5410 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5411 ef899790 2022-11-01 thomas if (err)
5412 ef899790 2022-11-01 thomas goto done;
5415 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5416 ed175427 2019-05-09 stsp free_commitable(ct);
5417 24519714 2019-05-09 stsp free(parent_path);
5418 036813ee 2019-05-09 stsp free(path);
5419 a129376b 2019-03-28 stsp return err;
5422 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5423 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5424 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5425 036813ee 2019-05-09 stsp struct got_repository *);
5427 ed175427 2019-05-09 stsp static const struct got_error *
5428 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5429 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5430 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5431 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5432 afa376bf 2019-05-09 stsp struct got_repository *repo)
5434 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5435 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5436 036813ee 2019-05-09 stsp char *subpath;
5438 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5439 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5440 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5442 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5444 ed175427 2019-05-09 stsp return err;
5446 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5447 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5448 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5449 036813ee 2019-05-09 stsp free(subpath);
5450 036813ee 2019-05-09 stsp return err;
5453 036813ee 2019-05-09 stsp static const struct got_error *
5454 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5456 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5457 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5459 036813ee 2019-05-09 stsp *match = 0;
5461 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5462 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5463 0f63689d 2019-05-10 stsp return NULL;
5466 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5468 0f63689d 2019-05-10 stsp return err;
5469 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5470 036813ee 2019-05-09 stsp free(ct_parent_path);
5471 036813ee 2019-05-09 stsp return err;
5474 768aea60 2019-05-09 stsp static mode_t
5475 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5477 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5478 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5480 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5483 036813ee 2019-05-09 stsp static const struct got_error *
5484 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5485 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5487 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5489 036813ee 2019-05-09 stsp *new_te = NULL;
5491 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5493 036813ee 2019-05-09 stsp goto done;
5495 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5497 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5498 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5499 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5501 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5503 036813ee 2019-05-09 stsp if (err && *new_te) {
5504 56e0773d 2019-11-28 stsp free(*new_te);
5505 036813ee 2019-05-09 stsp *new_te = NULL;
5507 036813ee 2019-05-09 stsp return err;
5510 036813ee 2019-05-09 stsp static const struct got_error *
5511 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5512 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5514 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5515 102b254e 2020-10-19 stsp char *ct_name = NULL;
5517 036813ee 2019-05-09 stsp *new_te = NULL;
5519 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5520 036813ee 2019-05-09 stsp if (*new_te == NULL)
5521 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5523 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5525 036813ee 2019-05-09 stsp goto done;
5526 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5527 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5528 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5529 036813ee 2019-05-09 stsp goto done;
5532 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5534 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5535 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5536 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5538 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5540 102b254e 2020-10-19 stsp free(ct_name);
5541 036813ee 2019-05-09 stsp if (err && *new_te) {
5542 56e0773d 2019-11-28 stsp free(*new_te);
5543 036813ee 2019-05-09 stsp *new_te = NULL;
5545 036813ee 2019-05-09 stsp return err;
5548 036813ee 2019-05-09 stsp static const struct got_error *
5549 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5550 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5552 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5553 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5555 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5557 036813ee 2019-05-09 stsp return err;
5558 036813ee 2019-05-09 stsp if (new_pe == NULL)
5559 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5560 036813ee 2019-05-09 stsp return NULL;
5563 afa376bf 2019-05-09 stsp static const struct got_error *
5564 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5565 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5567 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5568 5f8a88c6 2019-08-03 stsp unsigned char status;
5570 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5571 ae1e948a 2021-09-28 thomas return NULL;
5573 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5574 afa376bf 2019-05-09 stsp ct_path++;
5576 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5577 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5579 5f8a88c6 2019-08-03 stsp status = ct->status;
5581 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5582 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5585 036813ee 2019-05-09 stsp static const struct got_error *
5586 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5587 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5589 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5590 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5591 44d03001 2019-05-09 stsp char *te_path;
5593 44d03001 2019-05-09 stsp *modified = 0;
5595 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5596 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5597 44d03001 2019-05-09 stsp te->name) == -1)
5598 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5600 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5601 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5602 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5603 44d03001 2019-05-09 stsp strlen(te_path));
5604 62d463ca 2020-10-20 naddy if (*modified)
5608 44d03001 2019-05-09 stsp free(te_path);
5609 44d03001 2019-05-09 stsp return err;
5612 44d03001 2019-05-09 stsp static const struct got_error *
5613 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5614 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5615 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5617 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5618 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5620 036813ee 2019-05-09 stsp *ctp = NULL;
5622 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5623 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5624 036813ee 2019-05-09 stsp char *ct_name = NULL;
5625 036813ee 2019-05-09 stsp int path_matches;
5627 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5628 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5629 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5630 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5631 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5634 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5635 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5639 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5642 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5644 036813ee 2019-05-09 stsp return err;
5645 036813ee 2019-05-09 stsp if (!path_matches)
5648 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5650 d34b633e 2020-10-19 stsp return err;
5652 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5653 d34b633e 2020-10-19 stsp free(ct_name);
5656 d34b633e 2020-10-19 stsp free(ct_name);
5658 036813ee 2019-05-09 stsp *ctp = ct;
5662 2b496619 2019-07-10 stsp return err;
5665 2b496619 2019-07-10 stsp static const struct got_error *
5666 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5667 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5668 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5669 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5670 2b496619 2019-07-10 stsp struct got_repository *repo)
5672 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5673 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5674 2b496619 2019-07-10 stsp char *subtree_path;
5675 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5676 ba580f68 2020-03-22 stsp int nentries;
5678 2b496619 2019-07-10 stsp *new_tep = NULL;
5680 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5681 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5682 2b496619 2019-07-10 stsp child_path) == -1)
5683 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5685 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5686 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5687 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5688 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5690 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5691 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5692 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5693 2b496619 2019-07-10 stsp goto done;
5695 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5696 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5697 2b496619 2019-07-10 stsp if (err) {
5698 56e0773d 2019-11-28 stsp free(new_te);
5699 2b496619 2019-07-10 stsp goto done;
5701 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5704 2b496619 2019-07-10 stsp free(subtree_path);
5705 2b496619 2019-07-10 stsp if (err == NULL)
5706 2b496619 2019-07-10 stsp *new_tep = new_te;
5707 036813ee 2019-05-09 stsp return err;
5710 036813ee 2019-05-09 stsp static const struct got_error *
5711 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5712 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5713 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5714 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5715 036813ee 2019-05-09 stsp struct got_repository *repo)
5717 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5718 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5719 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5720 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5722 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5723 ba580f68 2020-03-22 stsp *nentries = 0;
5725 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5726 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5727 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5728 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5730 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5731 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5732 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5735 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5736 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5739 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5740 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5742 036813ee 2019-05-09 stsp goto done;
5744 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5745 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5746 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5748 036813ee 2019-05-09 stsp goto done;
5749 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5751 afa376bf 2019-05-09 stsp goto done;
5752 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5753 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5755 2f51b5b3 2019-05-09 stsp goto done;
5756 ba580f68 2020-03-22 stsp (*nentries)++;
5758 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5759 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5760 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5761 2b496619 2019-07-10 stsp == NULL) {
5762 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5763 2b496619 2019-07-10 stsp child_path, path_base_tree,
5764 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5767 2b496619 2019-07-10 stsp goto done;
5768 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5770 2b496619 2019-07-10 stsp goto done;
5771 ba580f68 2020-03-22 stsp (*nentries)++;
5776 2f51b5b3 2019-05-09 stsp if (base_tree) {
5777 56e0773d 2019-11-28 stsp int i, nbase_entries;
5778 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5779 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5780 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5781 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5783 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5784 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5785 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5786 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5788 63c5ca5d 2019-08-24 stsp goto done;
5789 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5791 63c5ca5d 2019-08-24 stsp goto done;
5792 ba580f68 2020-03-22 stsp (*nentries)++;
5796 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5797 44d03001 2019-05-09 stsp int modified;
5798 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5800 036813ee 2019-05-09 stsp goto done;
5801 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5802 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5804 2f51b5b3 2019-05-09 stsp goto done;
5805 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5806 44d03001 2019-05-09 stsp if (modified) {
5807 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5808 ba580f68 2020-03-22 stsp int nsubentries;
5809 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5810 ba580f68 2020-03-22 stsp &nsubentries, te,
5811 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5812 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5814 44d03001 2019-05-09 stsp goto done;
5815 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5816 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5817 ba580f68 2020-03-22 stsp free(new_id);
5820 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5821 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5822 56e0773d 2019-11-28 stsp free(new_id);
5824 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5826 036813ee 2019-05-09 stsp goto done;
5827 ba580f68 2020-03-22 stsp (*nentries)++;
5831 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5832 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5834 f66c734c 2019-09-22 stsp goto done;
5836 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5837 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5838 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5839 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
5840 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5841 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5842 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5844 2f51b5b3 2019-05-09 stsp goto done;
5845 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5847 2f51b5b3 2019-05-09 stsp goto done;
5848 ba580f68 2020-03-22 stsp (*nentries)++;
5850 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5851 b416585c 2019-05-13 stsp status_arg);
5853 afa376bf 2019-05-09 stsp goto done;
5855 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5856 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5858 2f51b5b3 2019-05-09 stsp goto done;
5859 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5861 2f51b5b3 2019-05-09 stsp goto done;
5862 ba580f68 2020-03-22 stsp (*nentries)++;
5867 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5868 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5870 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5871 2e1fa222 2020-07-23 stsp return err;
5874 2e1fa222 2020-07-23 stsp static const struct got_error *
5875 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5876 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5877 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5878 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5880 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5881 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5882 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5884 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5885 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5886 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5888 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5890 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5891 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5892 437adc9d 2020-12-10 yzhong if (err)
5893 437adc9d 2020-12-10 yzhong goto done;
5896 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5897 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5898 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5899 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5900 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5901 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5902 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5903 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5905 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5906 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5907 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5908 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5909 72fd46fa 2019-09-06 stsp !have_staged_files);
5911 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5912 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5913 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5914 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5915 72fd46fa 2019-09-06 stsp !have_staged_files);
5917 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5919 437adc9d 2020-12-10 yzhong goto done;
5920 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5921 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5922 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5923 3969253a 2020-03-07 stsp if (err) {
5924 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5925 437adc9d 2020-12-10 yzhong goto done;
5927 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5928 3969253a 2020-03-07 stsp if (err) {
5929 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5930 437adc9d 2020-12-10 yzhong goto done;
5933 437adc9d 2020-12-10 yzhong free(relpath);
5934 437adc9d 2020-12-10 yzhong relpath = NULL;
5937 437adc9d 2020-12-10 yzhong free(relpath);
5938 d56d26ce 2019-05-10 stsp return err;
5942 d56d26ce 2019-05-10 stsp static const struct got_error *
5943 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5944 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5945 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5946 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5947 735ef5ac 2019-08-03 stsp int ood_errcode)
5949 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5950 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5951 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5953 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5954 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5955 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5956 1a36436d 2019-06-10 stsp return NULL;
5958 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5959 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5961 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5962 945f9229 2022-04-16 thomas if (err)
5963 945f9229 2022-04-16 thomas goto done;
5964 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5965 9bead371 2019-07-28 stsp if (err) {
5966 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5967 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5968 1a36436d 2019-06-10 stsp goto done;
5969 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5970 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5972 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5973 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5974 945f9229 2022-04-16 thomas if (err)
5975 945f9229 2022-04-16 thomas goto done;
5976 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5977 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5978 1a36436d 2019-06-10 stsp goto done;
5979 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5983 945f9229 2022-04-16 thomas if (commit)
5984 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5985 1a36436d 2019-06-10 stsp return err;
5988 ef20f542 2022-06-26 thomas static const struct got_error *
5989 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5990 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5991 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
5992 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
5993 10604dce 2021-09-24 thomas struct got_worktree *worktree,
5994 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
5995 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5996 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5997 afa376bf 2019-05-09 stsp struct got_repository *repo)
5999 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6000 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6001 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6002 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6003 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6004 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6005 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6006 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6007 10604dce 2021-09-24 thomas int nentries, nparents = 0;
6008 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6009 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6010 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6011 892eab0a 2022-07-22 thomas time_t timestamp;
6013 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6015 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6017 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6019 588edf97 2019-05-10 stsp goto done;
6021 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6023 588edf97 2019-05-10 stsp goto done;
6025 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6026 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
6027 ef899790 2022-11-01 thomas &logmsg, commit_arg);
6032 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6033 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6037 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6038 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6039 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6040 cf066bf8 2019-05-09 stsp char *ondisk_path;
6042 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6043 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6044 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6047 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6048 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6049 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6050 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6053 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6054 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6055 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6056 cf066bf8 2019-05-09 stsp goto done;
6058 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6059 2e1fa222 2020-07-23 stsp free(ondisk_path);
6061 cf066bf8 2019-05-09 stsp goto done;
6064 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6065 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6066 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6068 036813ee 2019-05-09 stsp goto done;
6070 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6072 de18fc63 2019-05-09 stsp goto done;
6073 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6074 10604dce 2021-09-24 thomas nparents++;
6075 10604dce 2021-09-24 thomas if (parent_id2) {
6076 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6077 10604dce 2021-09-24 thomas if (err)
6078 10604dce 2021-09-24 thomas goto done;
6079 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6080 10604dce 2021-09-24 thomas nparents++;
6082 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6083 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6084 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6085 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6086 33ad4cbe 2019-05-12 jcs free(logmsg);
6088 9d40349a 2019-05-09 stsp goto done;
6090 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6091 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6092 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6093 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6094 09f5bd90 2019-05-09 stsp goto done;
6096 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6097 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6099 09f5bd90 2019-05-09 stsp goto done;
6100 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6102 ebf99748 2019-05-09 stsp goto done;
6103 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6104 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6105 09f5bd90 2019-05-09 stsp goto done;
6107 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6108 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6110 f2c16586 2019-05-09 stsp goto done;
6111 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6113 f2c16586 2019-05-09 stsp goto done;
6115 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6117 09f5bd90 2019-05-09 stsp goto done;
6119 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6121 ebf99748 2019-05-09 stsp goto done;
6123 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6124 588edf97 2019-05-10 stsp if (head_tree)
6125 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6126 588edf97 2019-05-10 stsp if (head_commit)
6127 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6128 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6129 2f17228e 2019-05-12 stsp if (head_ref2) {
6130 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6131 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6132 2f17228e 2019-05-12 stsp err = unlockerr;
6133 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6135 39cd0ff6 2019-07-12 stsp return err;
6138 5c1e53bc 2019-07-28 stsp static const struct got_error *
6139 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6140 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6142 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6143 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6145 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6146 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6147 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6149 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6150 5c1e53bc 2019-07-28 stsp ct_path++;
6152 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6153 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6157 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6158 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6160 5c1e53bc 2019-07-28 stsp return NULL;
6163 f0b75401 2019-08-03 stsp static const struct got_error *
6164 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6166 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6168 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6169 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6170 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6173 f0b75401 2019-08-03 stsp return NULL;
6176 5f8a88c6 2019-08-03 stsp static const struct got_error *
6177 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6178 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6180 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6181 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6183 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6184 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6186 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6187 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6188 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6189 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6190 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6191 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6194 5f8a88c6 2019-08-03 stsp return NULL;
6197 39cd0ff6 2019-07-12 stsp const struct got_error *
6198 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6199 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6200 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6201 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6202 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6203 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6204 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6206 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6207 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6208 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6209 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6210 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6211 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6212 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6213 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6214 ef899790 2022-11-01 thomas char *diff_path = NULL;
6215 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6217 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6219 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6220 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6222 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6224 39cd0ff6 2019-07-12 stsp goto done;
6226 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6228 39cd0ff6 2019-07-12 stsp goto done;
6230 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6232 39cd0ff6 2019-07-12 stsp goto done;
6234 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6236 39cd0ff6 2019-07-12 stsp goto done;
6238 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6239 5f8a88c6 2019-08-03 stsp &have_staged_files);
6240 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6241 5f8a88c6 2019-08-03 stsp goto done;
6242 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6243 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6245 5f8a88c6 2019-08-03 stsp goto done;
6248 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6249 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6250 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6251 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6252 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6253 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6254 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6255 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6256 ef899790 2022-11-01 thomas if (show_diff) {
6257 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6258 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6259 ef899790 2022-11-01 thomas if (err)
6260 ef899790 2022-11-01 thomas goto done;
6261 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6262 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6263 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6264 ef899790 2022-11-01 thomas goto done;
6266 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6267 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6268 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6269 ef899790 2022-11-01 thomas goto done;
6273 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6274 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6275 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6277 5c1e53bc 2019-07-28 stsp goto done;
6280 ef899790 2022-11-01 thomas if (show_diff) {
6281 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6282 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6283 ef899790 2022-11-01 thomas goto done;
6287 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6288 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6289 39cd0ff6 2019-07-12 stsp goto done;
6292 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6293 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6295 5c1e53bc 2019-07-28 stsp goto done;
6298 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6299 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6300 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6302 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6303 f0b75401 2019-08-03 stsp ct_path++;
6304 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6305 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6306 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6308 b50cabdf 2019-07-12 stsp goto done;
6312 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6313 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6314 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6315 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6317 39cd0ff6 2019-07-12 stsp goto done;
6319 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6320 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6321 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6322 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6323 39cd0ff6 2019-07-12 stsp err = sync_err;
6325 39cd0ff6 2019-07-12 stsp if (fileindex)
6326 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6327 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6328 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6329 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6330 39cd0ff6 2019-07-12 stsp err = unlockerr;
6331 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6332 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6334 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6336 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6337 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6338 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6339 ef899790 2022-11-01 thomas free(diff_path);
6340 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6341 ef899790 2022-11-01 thomas err == NULL)
6342 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6343 c4296144 2019-05-09 stsp return err;
6346 8656d6c4 2019-05-20 stsp const char *
6347 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6349 8656d6c4 2019-05-20 stsp return ct->path;
6352 8656d6c4 2019-05-20 stsp unsigned int
6353 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6355 8656d6c4 2019-05-20 stsp return ct->status;
6358 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6359 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6360 818c7501 2019-07-11 stsp struct got_repository *repo;
6363 818c7501 2019-07-11 stsp static const struct got_error *
6364 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6366 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6367 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6368 818c7501 2019-07-11 stsp unsigned char status;
6369 818c7501 2019-07-11 stsp struct stat sb;
6370 818c7501 2019-07-11 stsp char *ondisk_path;
6372 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6373 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6374 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6375 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6377 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6379 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6381 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6382 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6383 818c7501 2019-07-11 stsp free(ondisk_path);
6385 818c7501 2019-07-11 stsp return err;
6387 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6388 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6389 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6390 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6392 818c7501 2019-07-11 stsp return NULL;
6395 818c7501 2019-07-11 stsp const struct got_error *
6396 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6397 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6398 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6399 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6401 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6402 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6403 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6404 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6405 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6406 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6407 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6409 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6410 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6411 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6413 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6415 818c7501 2019-07-11 stsp return err;
6417 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6419 818c7501 2019-07-11 stsp goto done;
6421 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6422 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6423 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6426 818c7501 2019-07-11 stsp goto done;
6428 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6430 818c7501 2019-07-11 stsp goto done;
6432 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6434 818c7501 2019-07-11 stsp goto done;
6436 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6438 818c7501 2019-07-11 stsp goto done;
6440 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6443 e51d7b55 2020-01-04 stsp goto done;
6445 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6447 818c7501 2019-07-11 stsp goto done;
6448 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6449 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6450 e51d7b55 2020-01-04 stsp goto done;
6453 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6454 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6456 818c7501 2019-07-11 stsp goto done;
6457 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6459 818c7501 2019-07-11 stsp goto done;
6461 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6463 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6465 818c7501 2019-07-11 stsp goto done;
6467 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6469 818c7501 2019-07-11 stsp goto done;
6471 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6472 818c7501 2019-07-11 stsp worktree->base_commit_id);
6474 818c7501 2019-07-11 stsp goto done;
6475 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6477 818c7501 2019-07-11 stsp goto done;
6479 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6481 818c7501 2019-07-11 stsp goto done;
6483 818c7501 2019-07-11 stsp free(fileindex_path);
6484 818c7501 2019-07-11 stsp free(tmp_branch_name);
6485 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6486 818c7501 2019-07-11 stsp free(branch_ref_name);
6487 818c7501 2019-07-11 stsp if (branch_ref)
6488 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6489 818c7501 2019-07-11 stsp if (wt_branch)
6490 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6491 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6492 818c7501 2019-07-11 stsp if (err) {
6493 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6494 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6495 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6497 818c7501 2019-07-11 stsp if (*tmp_branch) {
6498 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6499 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6501 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6502 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6503 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6505 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6507 818c7501 2019-07-11 stsp return err;
6510 818c7501 2019-07-11 stsp const struct got_error *
6511 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6512 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6513 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6514 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6516 818c7501 2019-07-11 stsp const struct got_error *err;
6517 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6518 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6519 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6520 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6521 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6523 818c7501 2019-07-11 stsp *commit_id = NULL;
6524 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6525 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6526 3e3a69f1 2019-07-25 stsp *branch = NULL;
6527 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6529 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6531 3e3a69f1 2019-07-25 stsp return err;
6533 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6535 f032f1f7 2019-08-04 stsp goto done;
6537 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6538 f032f1f7 2019-08-04 stsp &have_staged_files);
6539 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6540 f032f1f7 2019-08-04 stsp goto done;
6541 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6542 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6543 3e3a69f1 2019-07-25 stsp goto done;
6546 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6548 f032f1f7 2019-08-04 stsp goto done;
6550 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6552 818c7501 2019-07-11 stsp goto done;
6554 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6556 818c7501 2019-07-11 stsp goto done;
6558 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6560 818c7501 2019-07-11 stsp goto done;
6562 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6564 818c7501 2019-07-11 stsp goto done;
6566 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6567 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6569 818c7501 2019-07-11 stsp goto done;
6571 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6573 818c7501 2019-07-11 stsp goto done;
6575 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6577 818c7501 2019-07-11 stsp goto done;
6579 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6580 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6582 818c7501 2019-07-11 stsp goto done;
6584 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6586 818c7501 2019-07-11 stsp goto done;
6588 818c7501 2019-07-11 stsp free(commit_ref_name);
6589 818c7501 2019-07-11 stsp free(branch_ref_name);
6590 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6591 818c7501 2019-07-11 stsp if (commit_ref)
6592 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6593 818c7501 2019-07-11 stsp if (branch_ref)
6594 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6595 818c7501 2019-07-11 stsp if (err) {
6596 818c7501 2019-07-11 stsp free(*commit_id);
6597 818c7501 2019-07-11 stsp *commit_id = NULL;
6598 818c7501 2019-07-11 stsp if (*tmp_branch) {
6599 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6600 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6602 818c7501 2019-07-11 stsp if (*new_base_branch) {
6603 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6604 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6606 818c7501 2019-07-11 stsp if (*branch) {
6607 818c7501 2019-07-11 stsp got_ref_close(*branch);
6608 818c7501 2019-07-11 stsp *branch = NULL;
6610 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6611 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6612 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6614 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6616 818c7501 2019-07-11 stsp return err;
6619 818c7501 2019-07-11 stsp const struct got_error *
6620 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6622 818c7501 2019-07-11 stsp const struct got_error *err;
6623 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6625 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6627 818c7501 2019-07-11 stsp return err;
6629 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6630 818c7501 2019-07-11 stsp free(tmp_branch_name);
6631 818c7501 2019-07-11 stsp return NULL;
6634 818c7501 2019-07-11 stsp static const struct got_error *
6635 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6636 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
6638 0ebf8283 2019-07-24 stsp *logmsg = arg;
6639 818c7501 2019-07-11 stsp return NULL;
6642 818c7501 2019-07-11 stsp static const struct got_error *
6643 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6644 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6645 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6646 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6648 818c7501 2019-07-11 stsp return NULL;
6651 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6652 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6653 01757395 2019-07-12 stsp void *progress_arg;
6654 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6657 01757395 2019-07-12 stsp static const struct got_error *
6658 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6660 01757395 2019-07-12 stsp const struct got_error *err;
6661 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6663 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6665 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6667 01757395 2019-07-12 stsp return err;
6669 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6670 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6671 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6672 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6673 01757395 2019-07-12 stsp return NULL;
6675 01757395 2019-07-12 stsp p = strdup(path);
6676 01757395 2019-07-12 stsp if (p == NULL)
6677 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6679 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6680 01757395 2019-07-12 stsp if (err || new == NULL)
6682 01757395 2019-07-12 stsp return err;
6685 0ebf8283 2019-07-24 stsp static const struct got_error *
6686 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6687 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6689 818c7501 2019-07-11 stsp const struct got_error *err;
6690 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6692 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6693 818c7501 2019-07-11 stsp if (err) {
6694 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6695 818c7501 2019-07-11 stsp goto done;
6696 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6698 818c7501 2019-07-11 stsp goto done;
6699 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6701 818c7501 2019-07-11 stsp goto done;
6702 de05890f 2020-03-05 stsp } else if (is_rebase) {
6703 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6706 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6708 818c7501 2019-07-11 stsp goto done;
6709 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6710 818c7501 2019-07-11 stsp free(stored_id);
6711 818c7501 2019-07-11 stsp if (cmp != 0) {
6712 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6713 818c7501 2019-07-11 stsp goto done;
6717 0ebf8283 2019-07-24 stsp if (commit_ref)
6718 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6719 0ebf8283 2019-07-24 stsp return err;
6722 0ebf8283 2019-07-24 stsp static const struct got_error *
6723 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6724 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6725 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6726 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6727 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6728 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6730 0ebf8283 2019-07-24 stsp const struct got_error *err;
6731 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6732 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6733 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6735 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6737 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6739 0ebf8283 2019-07-24 stsp return err;
6741 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6742 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6743 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6744 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6745 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6746 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6747 818c7501 2019-07-11 stsp if (commit_ref)
6748 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6749 818c7501 2019-07-11 stsp return err;
6752 818c7501 2019-07-11 stsp const struct got_error *
6753 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6754 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6755 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6756 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6757 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6758 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6760 0ebf8283 2019-07-24 stsp const struct got_error *err;
6761 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6763 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6765 0ebf8283 2019-07-24 stsp return err;
6767 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6769 0ebf8283 2019-07-24 stsp goto done;
6771 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6772 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6773 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6775 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6776 0ebf8283 2019-07-24 stsp return err;
6779 0ebf8283 2019-07-24 stsp const struct got_error *
6780 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6781 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6782 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6783 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6784 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6785 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6787 0ebf8283 2019-07-24 stsp const struct got_error *err;
6788 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6790 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6792 0ebf8283 2019-07-24 stsp return err;
6794 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6796 0ebf8283 2019-07-24 stsp goto done;
6798 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6799 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6800 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6802 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6803 0ebf8283 2019-07-24 stsp return err;
6806 0ebf8283 2019-07-24 stsp static const struct got_error *
6807 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6808 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6809 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6810 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
6811 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
6812 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
6814 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6815 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6816 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6817 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6818 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6819 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6820 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6822 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6823 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6824 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6826 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6828 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6830 3e3a69f1 2019-07-25 stsp return err;
6832 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6833 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6834 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6835 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6836 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
6838 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6839 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6841 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6842 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6843 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6844 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6845 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6846 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6848 01757395 2019-07-12 stsp if (merged_paths) {
6849 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6850 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6851 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6852 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6855 01757395 2019-07-12 stsp goto done;
6858 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6859 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6861 01757395 2019-07-12 stsp goto done;
6864 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6865 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6866 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6868 a0e95631 2019-07-12 stsp goto done;
6869 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6870 a0e95631 2019-07-12 stsp goto done;
6873 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6875 edd02c5e 2019-07-11 stsp goto done;
6877 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6879 818c7501 2019-07-11 stsp goto done;
6881 5943eee2 2019-08-13 stsp if (new_logmsg) {
6882 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6883 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6884 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6885 5943eee2 2019-08-13 stsp goto done;
6888 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6890 5943eee2 2019-08-13 stsp goto done;
6893 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6894 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6895 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
6896 8db00f97 2022-07-22 thomas committer ? committer :
6897 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
6898 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6900 a0e95631 2019-07-12 stsp goto done;
6902 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6904 a0e95631 2019-07-12 stsp goto done;
6906 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6908 a0e95631 2019-07-12 stsp goto done;
6910 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6911 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6912 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6913 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6914 a0e95631 2019-07-12 stsp err = sync_err;
6916 a0e95631 2019-07-12 stsp free(fileindex_path);
6917 a0e95631 2019-07-12 stsp free(head_commit_id);
6918 a0e95631 2019-07-12 stsp if (head_ref)
6919 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6920 818c7501 2019-07-11 stsp if (err) {
6921 818c7501 2019-07-11 stsp free(*new_commit_id);
6922 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6924 818c7501 2019-07-11 stsp return err;
6927 818c7501 2019-07-11 stsp const struct got_error *
6928 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6929 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6930 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6931 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
6932 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
6933 be94c032 2023-02-20 thomas struct got_repository *repo)
6935 0ebf8283 2019-07-24 stsp const struct got_error *err;
6936 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6937 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6938 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6940 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6942 0ebf8283 2019-07-24 stsp return err;
6944 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6946 0ebf8283 2019-07-24 stsp goto done;
6947 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6949 0ebf8283 2019-07-24 stsp goto done;
6950 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6951 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6952 0ebf8283 2019-07-24 stsp goto done;
6955 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6956 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
6957 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
6959 0ebf8283 2019-07-24 stsp if (commit_ref)
6960 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6961 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6962 0ebf8283 2019-07-24 stsp free(commit_id);
6963 0ebf8283 2019-07-24 stsp return err;
6966 0ebf8283 2019-07-24 stsp const struct got_error *
6967 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6968 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6969 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6970 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
6971 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6972 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
6974 0ebf8283 2019-07-24 stsp const struct got_error *err;
6975 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6976 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6978 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6980 0ebf8283 2019-07-24 stsp return err;
6982 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6984 0ebf8283 2019-07-24 stsp goto done;
6986 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6987 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
6988 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
6990 0ebf8283 2019-07-24 stsp if (commit_ref)
6991 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6992 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6993 0ebf8283 2019-07-24 stsp return err;
6996 0ebf8283 2019-07-24 stsp const struct got_error *
6997 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6998 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7000 3e3a69f1 2019-07-25 stsp if (fileindex)
7001 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7002 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7005 69844fba 2019-07-11 stsp static const struct got_error *
7006 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7008 69844fba 2019-07-11 stsp const struct got_error *err;
7009 69844fba 2019-07-11 stsp struct got_reference *ref;
7011 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7012 69844fba 2019-07-11 stsp if (err) {
7013 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7014 69844fba 2019-07-11 stsp return NULL;
7015 69844fba 2019-07-11 stsp return err;
7018 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7019 69844fba 2019-07-11 stsp got_ref_close(ref);
7020 69844fba 2019-07-11 stsp return err;
7023 69844fba 2019-07-11 stsp static const struct got_error *
7024 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7026 69844fba 2019-07-11 stsp const struct got_error *err;
7027 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7028 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7030 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7032 69844fba 2019-07-11 stsp goto done;
7033 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7035 69844fba 2019-07-11 stsp goto done;
7037 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7039 69844fba 2019-07-11 stsp goto done;
7040 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7042 69844fba 2019-07-11 stsp goto done;
7044 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7046 69844fba 2019-07-11 stsp goto done;
7047 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7049 69844fba 2019-07-11 stsp goto done;
7051 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7053 69844fba 2019-07-11 stsp goto done;
7054 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7056 69844fba 2019-07-11 stsp goto done;
7059 69844fba 2019-07-11 stsp free(tmp_branch_name);
7060 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7061 69844fba 2019-07-11 stsp free(branch_ref_name);
7062 69844fba 2019-07-11 stsp free(commit_ref_name);
7063 e600f124 2021-03-21 stsp return err;
7066 ef20f542 2022-06-26 thomas static const struct got_error *
7067 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7068 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7070 e600f124 2021-03-21 stsp const struct got_error *err;
7071 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7072 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7073 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7074 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7075 e600f124 2021-03-21 stsp char *refname = NULL;
7077 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7078 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7079 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7080 e600f124 2021-03-21 stsp branch_name += 11;
7082 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7084 e600f124 2021-03-21 stsp return err;
7086 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7087 e600f124 2021-03-21 stsp new_id_str) == -1) {
7088 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7089 e600f124 2021-03-21 stsp goto done;
7092 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7094 e600f124 2021-03-21 stsp goto done;
7096 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7098 e600f124 2021-03-21 stsp goto done;
7100 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7102 e600f124 2021-03-21 stsp free(new_id_str);
7103 e600f124 2021-03-21 stsp free(refname);
7104 e600f124 2021-03-21 stsp free(old_commit_id);
7106 e600f124 2021-03-21 stsp got_ref_close(ref);
7107 69844fba 2019-07-11 stsp return err;
7110 818c7501 2019-07-11 stsp const struct got_error *
7111 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7112 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7113 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7114 f08f28be 2023-02-03 thomas int create_backup)
7116 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7117 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7118 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7120 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7122 818c7501 2019-07-11 stsp return err;
7124 e600f124 2021-03-21 stsp if (create_backup) {
7125 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7126 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7128 e600f124 2021-03-21 stsp goto done;
7131 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7133 818c7501 2019-07-11 stsp goto done;
7135 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7137 818c7501 2019-07-11 stsp goto done;
7139 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7141 818c7501 2019-07-11 stsp goto done;
7143 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7145 a615e0e7 2020-12-16 stsp goto done;
7147 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7149 a615e0e7 2020-12-16 stsp goto done;
7150 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7151 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7152 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7153 a615e0e7 2020-12-16 stsp err = sync_err;
7155 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7156 a615e0e7 2020-12-16 stsp free(fileindex_path);
7157 818c7501 2019-07-11 stsp free(new_head_commit_id);
7158 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7159 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7160 818c7501 2019-07-11 stsp err = unlockerr;
7161 818c7501 2019-07-11 stsp return err;
7164 818c7501 2019-07-11 stsp const struct got_error *
7165 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7166 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7167 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7168 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7170 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7171 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7172 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7173 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7174 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7175 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7176 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7178 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7180 818c7501 2019-07-11 stsp return err;
7182 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7183 945f9229 2022-04-16 thomas worktree->base_commit_id);
7184 945f9229 2022-04-16 thomas if (err)
7185 945f9229 2022-04-16 thomas goto done;
7187 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7188 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7190 818c7501 2019-07-11 stsp goto done;
7192 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7194 818c7501 2019-07-11 stsp goto done;
7197 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7198 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7199 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7201 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7203 818c7501 2019-07-11 stsp goto done;
7205 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7207 818c7501 2019-07-11 stsp goto done;
7209 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7210 945f9229 2022-04-16 thomas worktree->path_prefix);
7212 ca355955 2019-07-12 stsp goto done;
7214 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7216 ca355955 2019-07-12 stsp goto done;
7218 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7220 818c7501 2019-07-11 stsp goto done;
7222 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7223 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7224 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7225 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7226 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7227 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7228 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7229 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7230 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7231 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7233 1f1abb7e 2019-08-08 stsp goto sync;
7235 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7236 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7238 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7239 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7240 ca355955 2019-07-12 stsp err = sync_err;
7242 818c7501 2019-07-11 stsp got_ref_close(resolved);
7243 a3a2faf2 2019-07-12 stsp free(tree_id);
7244 818c7501 2019-07-11 stsp free(commit_id);
7245 945f9229 2022-04-16 thomas if (commit)
7246 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7247 0ebf8283 2019-07-24 stsp if (fileindex)
7248 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7249 0ebf8283 2019-07-24 stsp free(fileindex_path);
7251 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7252 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7253 0ebf8283 2019-07-24 stsp err = unlockerr;
7254 0ebf8283 2019-07-24 stsp return err;
7257 0ebf8283 2019-07-24 stsp const struct got_error *
7258 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7259 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7260 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7261 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7263 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7264 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7265 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7266 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7267 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7268 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7269 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7270 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7272 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7273 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7274 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7275 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7277 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7279 0ebf8283 2019-07-24 stsp return err;
7281 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7283 0ebf8283 2019-07-24 stsp goto done;
7285 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7286 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7287 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7290 0ebf8283 2019-07-24 stsp goto done;
7292 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7294 0ebf8283 2019-07-24 stsp goto done;
7296 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7298 0ebf8283 2019-07-24 stsp goto done;
7300 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7301 0ebf8283 2019-07-24 stsp worktree);
7303 0ebf8283 2019-07-24 stsp goto done;
7305 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7308 0ebf8283 2019-07-24 stsp goto done;
7310 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7312 0ebf8283 2019-07-24 stsp goto done;
7314 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7316 0ebf8283 2019-07-24 stsp goto done;
7318 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7319 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7321 0ebf8283 2019-07-24 stsp goto done;
7322 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7324 0ebf8283 2019-07-24 stsp goto done;
7325 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7326 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7327 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7328 0ebf8283 2019-07-24 stsp goto done;
7331 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7332 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7334 0ebf8283 2019-07-24 stsp goto done;
7335 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7337 0ebf8283 2019-07-24 stsp goto done;
7339 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7341 0ebf8283 2019-07-24 stsp goto done;
7343 0ebf8283 2019-07-24 stsp free(fileindex_path);
7344 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7345 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7346 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7347 0ebf8283 2019-07-24 stsp if (wt_branch)
7348 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7349 0ebf8283 2019-07-24 stsp if (err) {
7350 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7351 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7352 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7354 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7355 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7356 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7358 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7359 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7360 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7361 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7363 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7365 0ebf8283 2019-07-24 stsp return err;
7368 0ebf8283 2019-07-24 stsp const struct got_error *
7369 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7370 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7372 3e3a69f1 2019-07-25 stsp if (fileindex)
7373 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7374 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7377 0ebf8283 2019-07-24 stsp const struct got_error *
7378 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7379 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7381 0ebf8283 2019-07-24 stsp const struct got_error *err;
7382 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7384 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7386 0ebf8283 2019-07-24 stsp return err;
7388 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7389 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7390 0ebf8283 2019-07-24 stsp return NULL;
7393 0ebf8283 2019-07-24 stsp const struct got_error *
7394 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7395 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7396 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7397 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7399 0ebf8283 2019-07-24 stsp const struct got_error *err;
7400 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7401 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7402 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7403 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7404 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7405 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7407 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7408 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7409 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7410 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7412 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7414 3e3a69f1 2019-07-25 stsp return err;
7416 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7418 f032f1f7 2019-08-04 stsp goto done;
7420 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7421 f032f1f7 2019-08-04 stsp &have_staged_files);
7422 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7423 f032f1f7 2019-08-04 stsp goto done;
7424 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7425 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7426 3e3a69f1 2019-07-25 stsp goto done;
7429 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7431 f032f1f7 2019-08-04 stsp goto done;
7433 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7435 0ebf8283 2019-07-24 stsp goto done;
7437 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7439 0ebf8283 2019-07-24 stsp goto done;
7441 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7442 0ebf8283 2019-07-24 stsp worktree);
7444 0ebf8283 2019-07-24 stsp goto done;
7446 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7448 0ebf8283 2019-07-24 stsp goto done;
7450 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7452 0ebf8283 2019-07-24 stsp goto done;
7453 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7455 0ebf8283 2019-07-24 stsp goto done;
7457 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7459 0ebf8283 2019-07-24 stsp goto done;
7460 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7462 0ebf8283 2019-07-24 stsp goto done;
7464 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7466 0ebf8283 2019-07-24 stsp goto done;
7468 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7469 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7470 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7471 0ebf8283 2019-07-24 stsp if (commit_ref)
7472 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7473 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7474 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7475 0ebf8283 2019-07-24 stsp if (err) {
7476 0ebf8283 2019-07-24 stsp free(*commit_id);
7477 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7478 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7479 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7480 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7481 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7482 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7484 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7485 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7486 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7488 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7490 0ebf8283 2019-07-24 stsp return err;
7493 0ebf8283 2019-07-24 stsp static const struct got_error *
7494 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7496 0ebf8283 2019-07-24 stsp const struct got_error *err;
7497 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7498 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7500 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7502 0ebf8283 2019-07-24 stsp goto done;
7503 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7505 0ebf8283 2019-07-24 stsp goto done;
7507 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7508 0ebf8283 2019-07-24 stsp worktree);
7510 0ebf8283 2019-07-24 stsp goto done;
7511 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7513 0ebf8283 2019-07-24 stsp goto done;
7515 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7517 0ebf8283 2019-07-24 stsp goto done;
7518 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7520 0ebf8283 2019-07-24 stsp goto done;
7522 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7524 0ebf8283 2019-07-24 stsp goto done;
7525 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7527 0ebf8283 2019-07-24 stsp goto done;
7529 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7530 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7531 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7532 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7533 0ebf8283 2019-07-24 stsp return err;
7536 0ebf8283 2019-07-24 stsp const struct got_error *
7537 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7538 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7539 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7540 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7542 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7543 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7544 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7545 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7546 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7547 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7549 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7551 0ebf8283 2019-07-24 stsp return err;
7553 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7554 945f9229 2022-04-16 thomas worktree->base_commit_id);
7555 945f9229 2022-04-16 thomas if (err)
7556 945f9229 2022-04-16 thomas goto done;
7558 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7559 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7561 0ebf8283 2019-07-24 stsp goto done;
7563 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7565 0ebf8283 2019-07-24 stsp goto done;
7567 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7569 0ebf8283 2019-07-24 stsp goto done;
7571 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7572 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7574 0ebf8283 2019-07-24 stsp goto done;
7576 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7578 0ebf8283 2019-07-24 stsp goto done;
7580 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7582 0ebf8283 2019-07-24 stsp goto done;
7584 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7585 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7586 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7587 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7588 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7589 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7590 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7591 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7592 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7593 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7595 1f1abb7e 2019-08-08 stsp goto sync;
7597 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7598 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7600 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7601 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7602 0ebf8283 2019-07-24 stsp err = sync_err;
7604 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7605 0ebf8283 2019-07-24 stsp free(tree_id);
7606 818c7501 2019-07-11 stsp free(fileindex_path);
7608 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7609 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7610 818c7501 2019-07-11 stsp err = unlockerr;
7611 818c7501 2019-07-11 stsp return err;
7614 0ebf8283 2019-07-24 stsp const struct got_error *
7615 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7616 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7617 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7619 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7620 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7621 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7622 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7624 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7626 0ebf8283 2019-07-24 stsp return err;
7628 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7629 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7631 e600f124 2021-03-21 stsp goto done;
7633 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7634 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7636 0ebf8283 2019-07-24 stsp goto done;
7638 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7640 0ebf8283 2019-07-24 stsp goto done;
7642 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7644 0ebf8283 2019-07-24 stsp goto done;
7646 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7648 0ebf8283 2019-07-24 stsp goto done;
7650 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7652 a615e0e7 2020-12-16 stsp goto done;
7654 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7656 a615e0e7 2020-12-16 stsp goto done;
7657 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7658 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7659 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7660 a615e0e7 2020-12-16 stsp err = sync_err;
7662 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7663 a615e0e7 2020-12-16 stsp free(fileindex_path);
7664 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7665 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7666 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7667 0ebf8283 2019-07-24 stsp err = unlockerr;
7668 0ebf8283 2019-07-24 stsp return err;
7671 0ebf8283 2019-07-24 stsp const struct got_error *
7672 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7673 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7675 0ebf8283 2019-07-24 stsp const struct got_error *err;
7676 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7678 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7680 0ebf8283 2019-07-24 stsp return err;
7682 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7684 0ebf8283 2019-07-24 stsp goto done;
7686 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7688 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7689 2822a352 2019-10-15 stsp return err;
7692 2822a352 2019-10-15 stsp const struct got_error *
7693 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7694 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7695 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7696 2822a352 2019-10-15 stsp struct got_repository *repo)
7698 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7699 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7700 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7702 2822a352 2019-10-15 stsp *fileindex = NULL;
7703 2822a352 2019-10-15 stsp *branch_ref = NULL;
7704 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7706 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7708 2822a352 2019-10-15 stsp return err;
7710 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7711 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7712 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7713 2822a352 2019-10-15 stsp "update -b or different branch name required");
7714 2822a352 2019-10-15 stsp goto done;
7717 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7719 2822a352 2019-10-15 stsp goto done;
7721 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7722 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7723 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7724 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7727 2822a352 2019-10-15 stsp goto done;
7729 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7731 2822a352 2019-10-15 stsp goto done;
7733 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7734 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7736 2822a352 2019-10-15 stsp if (err) {
7737 2822a352 2019-10-15 stsp if (*branch_ref) {
7738 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7739 2822a352 2019-10-15 stsp *branch_ref = NULL;
7741 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7742 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7743 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7745 2822a352 2019-10-15 stsp if (*fileindex) {
7746 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7747 2822a352 2019-10-15 stsp *fileindex = NULL;
7749 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7751 0cb83759 2019-08-03 stsp return err;
7754 2822a352 2019-10-15 stsp const struct got_error *
7755 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7756 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7757 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7758 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7759 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7761 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7762 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7763 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7764 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7766 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7768 2822a352 2019-10-15 stsp goto done;
7770 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7772 2822a352 2019-10-15 stsp goto done;
7774 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
7775 945f9229 2022-04-16 thomas if (err)
7776 945f9229 2022-04-16 thomas goto done;
7778 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7779 2822a352 2019-10-15 stsp worktree->path_prefix);
7781 2822a352 2019-10-15 stsp goto done;
7783 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7785 2822a352 2019-10-15 stsp goto done;
7787 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7788 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7790 2822a352 2019-10-15 stsp goto sync;
7792 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7794 2822a352 2019-10-15 stsp goto sync;
7796 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7798 6e210706 2021-01-22 stsp goto sync;
7800 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7802 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7803 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7804 2822a352 2019-10-15 stsp err = sync_err;
7807 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7808 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7809 2822a352 2019-10-15 stsp err = unlockerr;
7810 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7812 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7813 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7814 2822a352 2019-10-15 stsp err = unlockerr;
7815 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7817 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7818 2822a352 2019-10-15 stsp free(fileindex_path);
7819 2822a352 2019-10-15 stsp free(tree_id);
7820 945f9229 2022-04-16 thomas if (commit)
7821 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7823 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7824 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7825 2822a352 2019-10-15 stsp err = unlockerr;
7826 2822a352 2019-10-15 stsp return err;
7829 2822a352 2019-10-15 stsp const struct got_error *
7830 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7831 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7832 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7834 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7836 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7838 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7840 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7841 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7842 8b692cd0 2019-10-21 stsp err = unlockerr;
7843 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7845 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7846 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7847 8b692cd0 2019-10-21 stsp err = unlockerr;
7848 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7850 10604dce 2021-09-24 thomas return err;
7853 10604dce 2021-09-24 thomas const struct got_error *
7854 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
7855 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
7857 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
7858 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7860 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7861 10604dce 2021-09-24 thomas if (err)
7862 10604dce 2021-09-24 thomas goto done;
7864 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7866 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
7867 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7868 10604dce 2021-09-24 thomas err = sync_err;
7870 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7871 10604dce 2021-09-24 thomas free(fileindex_path);
7872 8b692cd0 2019-10-21 stsp return err;
7875 10604dce 2021-09-24 thomas static const struct got_error *
7876 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7878 10604dce 2021-09-24 thomas const struct got_error *err;
7879 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7881 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7882 10604dce 2021-09-24 thomas if (err)
7883 10604dce 2021-09-24 thomas goto done;
7884 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
7885 10604dce 2021-09-24 thomas if (err)
7886 10604dce 2021-09-24 thomas goto done;
7888 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7889 10604dce 2021-09-24 thomas if (err)
7890 10604dce 2021-09-24 thomas goto done;
7891 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
7892 10604dce 2021-09-24 thomas if (err)
7893 10604dce 2021-09-24 thomas goto done;
7896 10604dce 2021-09-24 thomas free(branch_refname);
7897 10604dce 2021-09-24 thomas free(commit_refname);
7898 10604dce 2021-09-24 thomas return err;
7901 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
7902 10604dce 2021-09-24 thomas struct got_worktree *worktree;
7903 10604dce 2021-09-24 thomas const char *branch_name;
7906 10604dce 2021-09-24 thomas static const struct got_error *
7907 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7908 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
7910 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
7912 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7913 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
7914 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
7916 10604dce 2021-09-24 thomas return NULL;
7920 10604dce 2021-09-24 thomas const struct got_error *
7921 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
7922 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
7923 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
7924 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
7925 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7926 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7928 10604dce 2021-09-24 thomas const struct got_error *err;
7929 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7931 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7932 10604dce 2021-09-24 thomas if (err)
7933 10604dce 2021-09-24 thomas goto done;
7935 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7936 10604dce 2021-09-24 thomas worktree);
7937 10604dce 2021-09-24 thomas if (err)
7938 10604dce 2021-09-24 thomas goto done;
7940 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7941 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
7942 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
7944 10604dce 2021-09-24 thomas free(fileindex_path);
7945 10604dce 2021-09-24 thomas return err;
7948 10604dce 2021-09-24 thomas const struct got_error *
7949 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
7950 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
7951 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
7952 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
7953 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
7954 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
7957 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
7958 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
7959 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
7960 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
7961 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
7962 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
7963 10604dce 2021-09-24 thomas int have_staged_files = 0;
7964 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
7965 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7967 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7968 10604dce 2021-09-24 thomas *new_commit_id = NULL;
7970 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
7972 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7973 10604dce 2021-09-24 thomas if (err)
7974 10604dce 2021-09-24 thomas goto done;
7976 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7977 10604dce 2021-09-24 thomas if (err)
7978 10604dce 2021-09-24 thomas goto done;
7980 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
7981 10604dce 2021-09-24 thomas if (err)
7982 10604dce 2021-09-24 thomas goto done;
7984 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7985 10604dce 2021-09-24 thomas &have_staged_files);
7986 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7987 10604dce 2021-09-24 thomas goto done;
7988 10604dce 2021-09-24 thomas if (have_staged_files) {
7989 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7990 10604dce 2021-09-24 thomas goto done;
7993 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
7994 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
7995 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
7996 10604dce 2021-09-24 thomas cc_arg.repo = repo;
7997 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
7998 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7999 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
8000 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8001 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8002 10604dce 2021-09-24 thomas if (err)
8003 10604dce 2021-09-24 thomas goto done;
8005 10604dce 2021-09-24 thomas if (TAILQ_EMPTY(&commitable_paths)) {
8006 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8007 10604dce 2021-09-24 thomas "merge of %s cannot proceed", branch_name);
8008 10604dce 2021-09-24 thomas goto done;
8011 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8012 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8013 10604dce 2021-09-24 thomas const char *ct_path = ct->in_repo_path;
8015 10604dce 2021-09-24 thomas while (ct_path[0] == '/')
8016 10604dce 2021-09-24 thomas ct_path++;
8017 10604dce 2021-09-24 thomas err = check_out_of_date(ct_path, ct->status,
8018 10604dce 2021-09-24 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8019 10604dce 2021-09-24 thomas head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8020 10604dce 2021-09-24 thomas if (err)
8021 10604dce 2021-09-24 thomas goto done;
8025 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
8026 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
8027 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
8028 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
8029 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8030 10604dce 2021-09-24 thomas if (err)
8031 10604dce 2021-09-24 thomas goto done;
8033 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
8034 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
8035 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8036 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8037 10604dce 2021-09-24 thomas err = sync_err;
8039 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8040 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8042 10604dce 2021-09-24 thomas free_commitable(ct);
8044 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8045 10604dce 2021-09-24 thomas free(fileindex_path);
8046 10604dce 2021-09-24 thomas return err;
8049 10604dce 2021-09-24 thomas const struct got_error *
8050 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8051 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8053 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8054 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8056 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8057 10604dce 2021-09-24 thomas if (err)
8058 10604dce 2021-09-24 thomas goto done;
8060 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8061 10604dce 2021-09-24 thomas if (err)
8062 10604dce 2021-09-24 thomas goto done;
8063 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8064 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8065 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8066 10604dce 2021-09-24 thomas err = sync_err;
8068 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8069 10604dce 2021-09-24 thomas free(fileindex_path);
8070 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8071 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8072 10604dce 2021-09-24 thomas err = unlockerr;
8073 10604dce 2021-09-24 thomas return err;
8076 10604dce 2021-09-24 thomas const struct got_error *
8077 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8078 10604dce 2021-09-24 thomas struct got_repository *repo)
8080 10604dce 2021-09-24 thomas const struct got_error *err;
8081 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8082 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8084 10604dce 2021-09-24 thomas *in_progress = 0;
8086 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8087 10604dce 2021-09-24 thomas if (err)
8088 10604dce 2021-09-24 thomas return err;
8089 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8090 dfe86d1f 2021-09-24 thomas free(branch_refname);
8091 10604dce 2021-09-24 thomas if (err) {
8092 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8093 10604dce 2021-09-24 thomas return err;
8095 10604dce 2021-09-24 thomas *in_progress = 1;
8097 10604dce 2021-09-24 thomas return NULL;
8100 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8101 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8102 10604dce 2021-09-24 thomas struct got_reference *branch, struct got_repository *repo)
8104 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8105 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8106 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8107 10604dce 2021-09-24 thomas struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8108 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL;
8109 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8110 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8112 10604dce 2021-09-24 thomas *fileindex = NULL;
8114 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8115 10604dce 2021-09-24 thomas if (err)
8116 10604dce 2021-09-24 thomas return err;
8118 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8119 10604dce 2021-09-24 thomas if (err)
8120 10604dce 2021-09-24 thomas goto done;
8122 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8123 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8124 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8125 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8126 10604dce 2021-09-24 thomas &ok_arg);
8127 10604dce 2021-09-24 thomas if (err)
8128 10604dce 2021-09-24 thomas goto done;
8130 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8131 10604dce 2021-09-24 thomas if (err)
8132 10604dce 2021-09-24 thomas return err;
8134 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8135 10604dce 2021-09-24 thomas if (err)
8136 10604dce 2021-09-24 thomas return err;
8138 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8140 10604dce 2021-09-24 thomas if (err)
8141 10604dce 2021-09-24 thomas goto done;
8143 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8144 10604dce 2021-09-24 thomas if (err)
8145 10604dce 2021-09-24 thomas goto done;
8147 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8148 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8149 10604dce 2021-09-24 thomas goto done;
8152 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8153 10604dce 2021-09-24 thomas if (err)
8154 10604dce 2021-09-24 thomas goto done;
8156 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8157 10604dce 2021-09-24 thomas if (err)
8158 10604dce 2021-09-24 thomas goto done;
8159 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8160 10604dce 2021-09-24 thomas if (err)
8161 10604dce 2021-09-24 thomas goto done;
8163 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8164 10604dce 2021-09-24 thomas if (err)
8165 10604dce 2021-09-24 thomas goto done;
8166 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8167 10604dce 2021-09-24 thomas if (err)
8168 10604dce 2021-09-24 thomas goto done;
8171 10604dce 2021-09-24 thomas free(branch_refname);
8172 10604dce 2021-09-24 thomas free(commit_refname);
8173 10604dce 2021-09-24 thomas free(fileindex_path);
8174 10604dce 2021-09-24 thomas if (branch_ref)
8175 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8176 10604dce 2021-09-24 thomas if (commit_ref)
8177 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8178 10604dce 2021-09-24 thomas if (wt_branch)
8179 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
8180 10604dce 2021-09-24 thomas free(wt_branch_tip);
8181 10604dce 2021-09-24 thomas if (err) {
8182 10604dce 2021-09-24 thomas if (*fileindex) {
8183 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8184 10604dce 2021-09-24 thomas *fileindex = NULL;
8186 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8188 10604dce 2021-09-24 thomas return err;
8191 10604dce 2021-09-24 thomas const struct got_error *
8192 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8193 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8194 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8196 10604dce 2021-09-24 thomas const struct got_error *err;
8197 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8198 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8199 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8200 10604dce 2021-09-24 thomas int have_staged_files = 0;
8202 10604dce 2021-09-24 thomas *branch_name = NULL;
8203 10604dce 2021-09-24 thomas *branch_tip = NULL;
8204 10604dce 2021-09-24 thomas *fileindex = NULL;
8206 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8207 10604dce 2021-09-24 thomas if (err)
8208 10604dce 2021-09-24 thomas return err;
8210 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8211 10604dce 2021-09-24 thomas if (err)
8212 10604dce 2021-09-24 thomas goto done;
8214 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8215 10604dce 2021-09-24 thomas &have_staged_files);
8216 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8217 10604dce 2021-09-24 thomas goto done;
8218 10604dce 2021-09-24 thomas if (have_staged_files) {
8219 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8220 10604dce 2021-09-24 thomas goto done;
8223 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8224 10604dce 2021-09-24 thomas if (err)
8225 10604dce 2021-09-24 thomas goto done;
8227 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8228 10604dce 2021-09-24 thomas if (err)
8229 10604dce 2021-09-24 thomas goto done;
8231 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8232 10604dce 2021-09-24 thomas if (err)
8233 10604dce 2021-09-24 thomas goto done;
8235 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8236 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8237 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8238 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8239 10604dce 2021-09-24 thomas goto done;
8241 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8242 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8243 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8244 10604dce 2021-09-24 thomas goto done;
8247 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8248 10604dce 2021-09-24 thomas if (err)
8249 10604dce 2021-09-24 thomas goto done;
8251 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8252 10604dce 2021-09-24 thomas if (err)
8253 10604dce 2021-09-24 thomas goto done;
8255 10604dce 2021-09-24 thomas free(commit_refname);
8256 10604dce 2021-09-24 thomas free(branch_refname);
8257 10604dce 2021-09-24 thomas free(fileindex_path);
8258 10604dce 2021-09-24 thomas if (commit_ref)
8259 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8260 10604dce 2021-09-24 thomas if (branch_ref)
8261 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8262 10604dce 2021-09-24 thomas if (err) {
8263 10604dce 2021-09-24 thomas if (*branch_name) {
8264 10604dce 2021-09-24 thomas free(*branch_name);
8265 10604dce 2021-09-24 thomas *branch_name = NULL;
8267 10604dce 2021-09-24 thomas free(*branch_tip);
8268 10604dce 2021-09-24 thomas *branch_tip = NULL;
8269 10604dce 2021-09-24 thomas if (*fileindex) {
8270 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8271 10604dce 2021-09-24 thomas *fileindex = NULL;
8273 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8275 10604dce 2021-09-24 thomas return err;
8278 10604dce 2021-09-24 thomas const struct got_error *
8279 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8280 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8281 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8283 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8284 10604dce 2021-09-24 thomas struct got_object_id *commit_id = NULL;
8285 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8286 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8287 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8288 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8290 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8291 945f9229 2022-04-16 thomas worktree->base_commit_id);
8292 945f9229 2022-04-16 thomas if (err)
8293 945f9229 2022-04-16 thomas goto done;
8295 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8296 945f9229 2022-04-16 thomas worktree->path_prefix);
8297 10604dce 2021-09-24 thomas if (err)
8298 10604dce 2021-09-24 thomas goto done;
8300 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8301 10604dce 2021-09-24 thomas if (err)
8302 10604dce 2021-09-24 thomas goto done;
8304 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8305 10604dce 2021-09-24 thomas if (err)
8306 10604dce 2021-09-24 thomas goto done;
8308 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8309 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8310 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8311 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8312 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8313 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8314 10604dce 2021-09-24 thomas rfa.repo = repo;
8315 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8316 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8317 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8318 10604dce 2021-09-24 thomas if (err)
8319 10604dce 2021-09-24 thomas goto sync;
8321 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8322 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8324 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8325 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8326 10604dce 2021-09-24 thomas err = sync_err;
8328 10604dce 2021-09-24 thomas free(tree_id);
8329 10604dce 2021-09-24 thomas free(commit_id);
8330 945f9229 2022-04-16 thomas if (commit)
8331 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8332 10604dce 2021-09-24 thomas if (fileindex)
8333 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8334 10604dce 2021-09-24 thomas free(fileindex_path);
8336 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8337 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8338 10604dce 2021-09-24 thomas err = unlockerr;
8339 10604dce 2021-09-24 thomas return err;
8342 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8343 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8344 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8345 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8346 2db2652d 2019-08-07 stsp struct got_repository *repo;
8347 2db2652d 2019-08-07 stsp int have_changes;
8350 ef20f542 2022-06-26 thomas static const struct got_error *
8351 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8352 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8353 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8354 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8356 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8357 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8358 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8359 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8360 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8361 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8363 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8364 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8365 8b13ce36 2019-08-08 stsp return NULL;
8366 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8367 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8369 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8370 735ef5ac 2019-08-03 stsp if (ie == NULL)
8371 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8373 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8374 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8375 735ef5ac 2019-08-03 stsp relpath) == -1)
8376 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8378 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8379 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
8380 43010591 2023-02-17 thomas &base_commit_id, ie);
8383 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8384 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8385 3aa5969e 2019-08-06 stsp goto done;
8386 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8387 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8388 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8389 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8390 735ef5ac 2019-08-03 stsp goto done;
8393 2db2652d 2019-08-07 stsp a->have_changes = 1;
8395 735ef5ac 2019-08-03 stsp p = in_repo_path;
8396 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8398 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8399 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8400 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8402 735ef5ac 2019-08-03 stsp free(in_repo_path);
8403 dc424a06 2019-08-07 stsp return err;
8406 2db2652d 2019-08-07 stsp struct stage_path_arg {
8407 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8408 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8409 2db2652d 2019-08-07 stsp struct got_repository *repo;
8410 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8411 2db2652d 2019-08-07 stsp void *status_arg;
8412 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8413 2db2652d 2019-08-07 stsp void *patch_arg;
8414 7b5dc508 2019-10-28 stsp int staged_something;
8415 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8418 2db2652d 2019-08-07 stsp static const struct got_error *
8419 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8420 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8421 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8422 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8424 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8425 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8426 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8427 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8428 0cb83759 2019-08-03 stsp uint32_t stage;
8429 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8430 0aeb8099 2020-07-23 stsp struct stat sb;
8432 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8433 8b13ce36 2019-08-08 stsp return NULL;
8435 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8436 d3e7c587 2019-08-03 stsp if (ie == NULL)
8437 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8439 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8440 2db2652d 2019-08-07 stsp relpath)== -1)
8441 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8443 0cb83759 2019-08-03 stsp switch (status) {
8444 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8445 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8446 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8447 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8448 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8451 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8452 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8453 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8454 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8455 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8458 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8461 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8462 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8463 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8464 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8465 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8469 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8470 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8473 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8474 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8475 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8476 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8478 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8479 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8480 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8481 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8482 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8483 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8484 35213c7c 2020-07-23 stsp ssize_t target_len;
8485 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8486 35213c7c 2020-07-23 stsp sizeof(target_path));
8487 35213c7c 2020-07-23 stsp if (target_len == -1) {
8488 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8489 35213c7c 2020-07-23 stsp ondisk_path);
8492 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8493 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8494 35213c7c 2020-07-23 stsp a->worktree->root_path);
8497 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8498 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8499 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8503 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8504 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8505 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8507 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8508 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8510 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8511 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8513 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8514 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8516 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8517 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8518 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8519 f289c82c 2022-06-13 thomas if (err)
8522 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8523 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8525 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8526 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8527 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8528 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8531 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8532 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8534 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8535 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8536 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8537 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8540 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8542 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8543 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8547 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8548 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8549 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8550 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8552 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8553 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8556 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8558 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8559 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8561 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8562 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8565 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8569 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8570 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8571 dc424a06 2019-08-07 stsp free(path_content);
8572 2db2652d 2019-08-07 stsp free(ondisk_path);
8573 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8574 0ebf8283 2019-07-24 stsp return err;
8577 0cb83759 2019-08-03 stsp const struct got_error *
8578 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8579 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8580 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8581 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8582 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8584 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8585 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8586 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8587 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8588 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8589 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8590 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8591 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8593 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8595 0cb83759 2019-08-03 stsp return err;
8597 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8598 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8600 735ef5ac 2019-08-03 stsp goto done;
8601 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8603 735ef5ac 2019-08-03 stsp goto done;
8604 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8606 0cb83759 2019-08-03 stsp goto done;
8608 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8609 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8610 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8611 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8612 2db2652d 2019-08-07 stsp oka.repo = repo;
8613 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8614 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8615 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8616 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
8618 735ef5ac 2019-08-03 stsp goto done;
8620 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8621 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8622 2db2652d 2019-08-07 stsp goto done;
8625 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8626 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8627 2db2652d 2019-08-07 stsp spa.repo = repo;
8628 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8629 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8630 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8631 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8632 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8633 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8634 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8635 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8636 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
8638 2db2652d 2019-08-07 stsp goto done;
8640 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8641 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8642 7b5dc508 2019-10-28 stsp goto done;
8645 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8646 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8647 0cb83759 2019-08-03 stsp err = sync_err;
8649 735ef5ac 2019-08-03 stsp if (head_ref)
8650 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8651 735ef5ac 2019-08-03 stsp free(head_commit_id);
8652 0cb83759 2019-08-03 stsp free(fileindex_path);
8653 0cb83759 2019-08-03 stsp if (fileindex)
8654 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8655 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8656 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8657 0cb83759 2019-08-03 stsp err = unlockerr;
8658 0cb83759 2019-08-03 stsp return err;
8661 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8662 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8663 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8664 ad493afc 2019-08-03 stsp struct got_repository *repo;
8665 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8666 ad493afc 2019-08-03 stsp void *progress_arg;
8667 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8668 2e1f37b0 2019-08-08 stsp void *patch_arg;
8671 2e1f37b0 2019-08-08 stsp static const struct got_error *
8672 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8673 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8674 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8675 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8676 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8678 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8679 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8680 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8681 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8682 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8683 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8684 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8685 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8687 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8688 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8690 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8692 2e1f37b0 2019-08-08 stsp return err;
8694 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8695 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8696 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8697 f4ae6ddb 2022-07-01 thomas goto done;
8699 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8700 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8701 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8702 f4ae6ddb 2022-07-01 thomas goto done;
8705 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8707 2e1f37b0 2019-08-08 stsp goto done;
8709 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8711 2e1f37b0 2019-08-08 stsp goto done;
8713 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8715 2e1f37b0 2019-08-08 stsp goto done;
8717 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8720 2e1f37b0 2019-08-08 stsp goto done;
8722 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8724 2e1f37b0 2019-08-08 stsp goto done;
8726 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8728 2e1f37b0 2019-08-08 stsp goto done;
8730 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8731 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8733 2e1f37b0 2019-08-08 stsp goto done;
8735 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8736 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
8738 2e1f37b0 2019-08-08 stsp goto done;
8739 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8740 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
8742 2e1f37b0 2019-08-08 stsp goto done;
8744 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8745 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8746 2e1f37b0 2019-08-08 stsp goto done;
8748 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8749 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8750 2e1f37b0 2019-08-08 stsp goto done;
8752 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8753 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8754 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
8755 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8756 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8757 fe621944 2020-11-10 stsp nchanges++;
8759 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8760 2e1f37b0 2019-08-08 stsp int choice;
8761 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8762 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8763 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8764 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8766 2e1f37b0 2019-08-08 stsp goto done;
8767 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8768 2e1f37b0 2019-08-08 stsp have_content = 1;
8770 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8771 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8774 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8775 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8776 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8778 2e1f37b0 2019-08-08 stsp free(label1);
8779 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8780 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8782 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8783 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8784 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8785 2e1f37b0 2019-08-08 stsp if (staged_blob)
8786 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8787 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8788 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8789 fe621944 2020-11-10 stsp err = free_err;
8790 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8791 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8792 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8793 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8794 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8795 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8796 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8797 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8798 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8799 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8800 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8801 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8802 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8803 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8804 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8805 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8806 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8807 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8808 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8810 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8811 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8812 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8813 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8814 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8815 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8816 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8818 2e1f37b0 2019-08-08 stsp free(path1);
8819 2e1f37b0 2019-08-08 stsp free(path2);
8820 fda8017d 2020-07-23 stsp return err;
8823 fda8017d 2020-07-23 stsp static const struct got_error *
8824 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8825 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8826 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8827 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8828 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8829 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8830 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8832 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8833 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8834 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8835 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8836 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8837 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8838 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8839 fda8017d 2020-07-23 stsp struct stat sb;
8841 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8842 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8843 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8845 fda8017d 2020-07-23 stsp return err;
8847 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8848 fda8017d 2020-07-23 stsp return NULL;
8850 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8851 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8852 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8854 fda8017d 2020-07-23 stsp goto done;
8857 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
8858 fda8017d 2020-07-23 stsp if (f == NULL) {
8859 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8860 fda8017d 2020-07-23 stsp path_unstaged_content);
8861 fda8017d 2020-07-23 stsp goto done;
8863 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8864 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8865 fda8017d 2020-07-23 stsp goto done;
8867 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8868 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8869 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8871 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8872 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8873 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8874 fda8017d 2020-07-23 stsp goto done;
8876 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8877 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8878 fda8017d 2020-07-23 stsp goto done;
8880 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8881 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8882 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8883 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8884 fda8017d 2020-07-23 stsp progress_arg);
8886 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8888 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8890 67a66647 2021-05-31 stsp return err;
8892 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8893 67a66647 2021-05-31 stsp parent) == -1) {
8894 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8895 67a66647 2021-05-31 stsp base_path = NULL;
8896 67a66647 2021-05-31 stsp goto done;
8899 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
8900 fc2a50f2 2022-11-01 thomas base_path, "");
8902 67a66647 2021-05-31 stsp goto done;
8903 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8904 67a66647 2021-05-31 stsp blob_base);
8906 67a66647 2021-05-31 stsp goto done;
8909 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8910 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8912 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8913 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8914 eec2f5a9 2021-06-03 stsp ondisk_path);
8916 eec2f5a9 2021-06-03 stsp goto done;
8919 06340621 2021-12-31 thomas fd = open(ondisk_path,
8920 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8921 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8922 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8923 eec2f5a9 2021-06-03 stsp goto done;
8925 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8926 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8927 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8928 eec2f5a9 2021-06-03 stsp close(fd);
8929 eec2f5a9 2021-06-03 stsp goto done;
8933 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8934 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8935 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8936 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8937 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8940 fda8017d 2020-07-23 stsp goto done;
8942 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8943 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8944 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8946 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8947 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8950 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8951 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8952 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8953 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8954 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8955 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8956 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8957 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8958 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8959 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8960 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8961 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8962 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8963 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8964 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8965 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8966 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8967 67a66647 2021-05-31 stsp free(blob_base_path);
8968 67a66647 2021-05-31 stsp free(parent);
8969 67a66647 2021-05-31 stsp free(base_path);
8970 2e1f37b0 2019-08-08 stsp return err;
8973 ad493afc 2019-08-03 stsp static const struct got_error *
8974 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8975 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8976 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8977 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8979 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8980 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8981 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8982 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8983 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8984 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8985 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8986 9bc94a15 2019-08-03 stsp struct stat sb;
8987 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8989 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8990 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8991 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8992 2e1f37b0 2019-08-08 stsp return NULL;
8994 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8995 ad493afc 2019-08-03 stsp if (ie == NULL)
8996 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8998 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9000 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9002 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9003 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9005 f69721c3 2019-10-21 stsp goto done;
9006 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9007 f69721c3 2019-10-21 stsp id_str) == -1) {
9008 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9009 f4ae6ddb 2022-07-01 thomas goto done;
9012 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9013 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9014 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9015 f4ae6ddb 2022-07-01 thomas goto done;
9017 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9018 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9019 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9020 f69721c3 2019-10-21 stsp goto done;
9023 ad493afc 2019-08-03 stsp switch (staged_status) {
9024 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9025 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9026 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
9029 ad493afc 2019-08-03 stsp /* fall through */
9030 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9031 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9032 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9033 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9034 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9035 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9038 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9041 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9042 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9043 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9044 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9045 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9046 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9049 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9050 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9053 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9054 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9055 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9056 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9057 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9058 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9059 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9060 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9061 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9063 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9064 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9065 36bf999c 2020-07-23 stsp char *staged_target;
9066 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9067 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9069 36bf999c 2020-07-23 stsp goto done;
9070 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9071 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9072 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9073 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9074 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9075 36bf999c 2020-07-23 stsp free(staged_target);
9077 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9078 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9079 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9080 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9081 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9082 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9083 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9087 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9090 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9091 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9092 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9093 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9096 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9097 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9098 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9099 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9100 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9103 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9105 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9106 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9110 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9111 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9112 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9113 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9116 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9120 ad493afc 2019-08-03 stsp free(ondisk_path);
9121 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9122 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9123 ad493afc 2019-08-03 stsp if (blob_base)
9124 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9125 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9126 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9127 ad493afc 2019-08-03 stsp if (blob_staged)
9128 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9129 f69721c3 2019-10-21 stsp free(id_str);
9130 f69721c3 2019-10-21 stsp free(label_orig);
9131 ad493afc 2019-08-03 stsp return err;
9134 ad493afc 2019-08-03 stsp const struct got_error *
9135 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9136 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9137 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9138 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9139 ad493afc 2019-08-03 stsp struct got_repository *repo)
9141 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9142 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9143 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9144 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9145 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9147 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9149 ad493afc 2019-08-03 stsp return err;
9151 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9153 ad493afc 2019-08-03 stsp goto done;
9155 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9156 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9157 ad493afc 2019-08-03 stsp upa.repo = repo;
9158 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9159 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9160 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9161 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9162 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9163 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9164 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9166 ad493afc 2019-08-03 stsp goto done;
9169 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9170 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9171 ad493afc 2019-08-03 stsp err = sync_err;
9173 ad493afc 2019-08-03 stsp free(fileindex_path);
9174 ad493afc 2019-08-03 stsp if (fileindex)
9175 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9176 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9177 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9178 ad493afc 2019-08-03 stsp err = unlockerr;
9179 ad493afc 2019-08-03 stsp return err;
9182 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9183 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9184 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9185 b2118c49 2020-07-28 stsp void *info_arg;
9186 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9187 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9188 b2118c49 2020-07-28 stsp void *cancel_arg;
9191 b2118c49 2020-07-28 stsp static const struct got_error *
9192 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9194 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9195 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9196 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9197 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9198 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9199 b2118c49 2020-07-28 stsp int stage;
9201 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9202 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9204 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9205 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9206 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9209 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9210 b2118c49 2020-07-28 stsp return NULL;
9212 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9213 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9214 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9215 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9216 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9217 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9218 43010591 2023-02-17 thomas &staged_blob_id, ie);
9221 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9222 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9224 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9225 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9228 b2118c49 2020-07-28 stsp const struct got_error *
9229 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9230 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9231 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9232 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9235 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9236 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9237 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9238 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9240 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9242 b2118c49 2020-07-28 stsp return err;
9244 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9246 b2118c49 2020-07-28 stsp goto done;
9248 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9249 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9250 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9251 b2118c49 2020-07-28 stsp arg.paths = paths;
9252 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9253 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9254 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9257 b2118c49 2020-07-28 stsp free(fileindex_path);
9258 b2118c49 2020-07-28 stsp if (fileindex)
9259 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9260 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9261 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9262 b2118c49 2020-07-28 stsp err = unlockerr;
9263 b2118c49 2020-07-28 stsp return err;
9266 814624e7 2022-03-22 thomas static const struct got_error *
9267 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9268 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9269 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9271 814624e7 2022-03-22 thomas const struct got_error *err;
9272 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9273 814624e7 2022-03-22 thomas struct stat sb;
9274 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9276 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9277 814624e7 2022-03-22 thomas if (err)
9278 814624e7 2022-03-22 thomas return err;
9280 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9281 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9282 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9284 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9285 814624e7 2022-03-22 thomas if (ie) {
9286 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9287 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9289 814624e7 2022-03-22 thomas if (err)
9290 814624e7 2022-03-22 thomas goto done;
9291 814624e7 2022-03-22 thomas } else {
9292 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9293 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9294 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9295 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9296 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9297 814624e7 2022-03-22 thomas ondisk_path);
9298 814624e7 2022-03-22 thomas goto done;
9300 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9305 814624e7 2022-03-22 thomas free(ondisk_path);
9306 814624e7 2022-03-22 thomas return err;
9309 814624e7 2022-03-22 thomas static const struct got_error *
9310 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
9311 814624e7 2022-03-22 thomas unsigned char staged_status)
9313 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9314 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9315 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9316 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9317 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9318 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9319 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9320 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9321 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9322 814624e7 2022-03-22 thomas return NULL;
9325 814624e7 2022-03-22 thomas static const struct got_error *
9326 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9328 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9329 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9330 814624e7 2022-03-22 thomas return NULL;
9333 814624e7 2022-03-22 thomas static const struct got_error *
9334 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9335 814624e7 2022-03-22 thomas unsigned char staged_status)
9337 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9338 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9339 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9340 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9341 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9342 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9343 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9344 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9345 814624e7 2022-03-22 thomas return NULL;
9348 814624e7 2022-03-22 thomas const struct got_error *
9349 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9350 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9352 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9355 814624e7 2022-03-22 thomas const struct got_error *
9356 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9357 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9358 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9360 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9361 814624e7 2022-03-22 thomas int file_renamed = 0;
9362 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9363 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9365 814624e7 2022-03-22 thomas *oldpath = NULL;
9366 814624e7 2022-03-22 thomas *newpath = NULL;
9368 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9369 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9370 814624e7 2022-03-22 thomas if (err)
9371 814624e7 2022-03-22 thomas goto done;
9373 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9374 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9375 814624e7 2022-03-22 thomas if (err)
9376 814624e7 2022-03-22 thomas goto done;
9378 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9379 814624e7 2022-03-22 thomas file_renamed = 1;
9381 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9382 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9383 814624e7 2022-03-22 thomas else if (file_renamed) {
9384 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9385 814624e7 2022-03-22 thomas if (err == NULL)
9386 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9387 814624e7 2022-03-22 thomas } else if (old == NULL)
9388 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9390 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9393 814624e7 2022-03-22 thomas if (err) {
9394 814624e7 2022-03-22 thomas free(*oldpath);
9395 814624e7 2022-03-22 thomas *oldpath = NULL;
9396 814624e7 2022-03-22 thomas free(*newpath);
9397 814624e7 2022-03-22 thomas *newpath = NULL;
9399 814624e7 2022-03-22 thomas return err;
9402 814624e7 2022-03-22 thomas const struct got_error *
9403 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9404 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9405 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9407 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9409 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9410 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9411 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9412 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9413 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9414 5e22b737 2022-05-31 thomas saa.repo = repo;
9416 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9417 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9420 5e22b737 2022-05-31 thomas const struct got_error *
9421 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9422 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9423 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9425 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9427 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9428 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9429 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9430 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9431 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9432 5e22b737 2022-05-31 thomas sda.repo = repo;
9433 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9434 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9435 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9436 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9438 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9439 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9442 5e22b737 2022-05-31 thomas const struct got_error *
9443 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9444 36832a8e 2022-05-31 thomas const char *fileindex_path)
9446 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9448 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9449 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9451 5e22b737 2022-05-31 thomas return err;