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 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 5822e79e 2023-02-23 op #include <sha2.h>
33 9d31a1d8 2018-03-11 stsp #include <zlib.h>
34 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
35 512f0d0e 2019-01-02 stsp #include <libgen.h>
36 ec22038e 2019-03-10 stsp #include <uuid.h>
37 7154f6ce 2019-03-27 stsp #include <util.h>
39 86c3caaf 2018-03-09 stsp #include "got_error.h"
40 86c3caaf 2018-03-09 stsp #include "got_repository.h"
41 5261c201 2018-04-01 stsp #include "got_reference.h"
42 9d31a1d8 2018-03-11 stsp #include "got_object.h"
43 1dd54920 2019-05-11 stsp #include "got_path.h"
44 e6209546 2019-08-22 stsp #include "got_cancel.h"
45 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
46 511a516b 2018-05-19 stsp #include "got_opentemp.h"
47 234035bc 2019-06-01 stsp #include "got_diff.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
50 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
52 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
54 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
55 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
56 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
57 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
58 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
59 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
62 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
66 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
68 b2b3fce1 2022-10-29 op static mode_t apply_umask(mode_t);
70 99724ed4 2018-03-10 stsp static const struct got_error *
71 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
73 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
76 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
77 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
79 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
84 507dc3bb 2018-12-29 stsp static const struct got_error *
85 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
87 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
88 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
89 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
90 507dc3bb 2018-12-29 stsp char *path = NULL;
92 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
93 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
94 507dc3bb 2018-12-29 stsp path = NULL;
98 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &tmpfile, path, "");
102 507dc3bb 2018-12-29 stsp if (content) {
103 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
104 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
105 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
110 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
111 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
112 2a57020b 2019-02-20 stsp unlink(tmppath);
117 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
118 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
119 230a42bd 2019-05-11 jcs free(tmppath);
120 09fe317a 2018-03-11 stsp return err;
123 024e9686 2019-05-14 stsp static const struct got_error *
124 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
126 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
127 024e9686 2019-05-14 stsp char *refstr = NULL;
129 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
130 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
131 024e9686 2019-05-14 stsp if (refstr == NULL)
132 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
134 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
135 024e9686 2019-05-14 stsp if (refstr == NULL)
136 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
138 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
139 024e9686 2019-05-14 stsp free(refstr);
140 024e9686 2019-05-14 stsp return err;
143 86c3caaf 2018-03-09 stsp const struct got_error *
144 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
145 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
147 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
148 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
149 ec22038e 2019-03-10 stsp uuid_t uuid;
150 ec22038e 2019-03-10 stsp uint32_t uuid_status;
151 65596e15 2018-12-24 stsp int obj_type;
152 7ac97322 2018-03-11 stsp char *path_got = NULL;
153 1451e70d 2018-03-10 stsp char *formatstr = NULL;
154 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
155 65596e15 2018-12-24 stsp char *basestr = NULL;
156 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
158 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
159 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
163 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
165 65596e15 2018-12-24 stsp return err;
166 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
168 65596e15 2018-12-24 stsp return err;
169 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
170 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
172 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
173 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
174 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
177 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
178 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
179 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
183 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
184 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
185 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
188 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
189 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
193 056e7441 2018-03-11 stsp /* Create an empty lock file. */
194 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
198 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
199 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
203 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
204 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
208 65596e15 2018-12-24 stsp /* Record our base commit. */
209 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
212 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
216 1451e70d 2018-03-10 stsp /* Store path to repository. */
217 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
218 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
222 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
223 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
224 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
228 ec22038e 2019-03-10 stsp /* Generate UUID. */
229 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
230 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
231 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
234 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
235 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
236 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
239 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
243 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
244 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
245 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
248 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
253 65596e15 2018-12-24 stsp free(commit_id);
254 7ac97322 2018-03-11 stsp free(path_got);
255 1451e70d 2018-03-10 stsp free(formatstr);
256 0bb8a95e 2018-03-12 stsp free(absprefix);
257 65596e15 2018-12-24 stsp free(basestr);
258 ec22038e 2019-03-10 stsp free(uuidstr);
259 86c3caaf 2018-03-09 stsp return err;
262 247140b2 2019-02-05 stsp const struct got_error *
263 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
264 e5dc7198 2018-12-29 stsp const char *path_prefix)
266 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
268 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
269 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
270 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
272 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
273 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
274 e5dc7198 2018-12-29 stsp free(absprefix);
275 e5dc7198 2018-12-29 stsp return NULL;
278 bc70eb79 2019-05-09 stsp const char *
279 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
281 36a38700 2019-05-10 stsp return worktree->head_ref_name;
284 024e9686 2019-05-14 stsp const struct got_error *
285 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
286 024e9686 2019-05-14 stsp struct got_reference *head_ref)
288 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
289 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
291 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
292 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
293 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
294 024e9686 2019-05-14 stsp path_got = NULL;
298 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
299 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
300 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
304 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
308 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
309 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
311 024e9686 2019-05-14 stsp free(path_got);
313 024e9686 2019-05-14 stsp free(head_ref_name);
314 024e9686 2019-05-14 stsp return err;
317 b72f483a 2019-02-05 stsp struct got_object_id *
318 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
320 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
323 507dc3bb 2018-12-29 stsp const struct got_error *
324 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
325 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
327 507dc3bb 2018-12-29 stsp const struct got_error *err;
328 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
329 507dc3bb 2018-12-29 stsp char *id_str = NULL;
330 507dc3bb 2018-12-29 stsp char *path_got = NULL;
332 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
333 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
334 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
335 507dc3bb 2018-12-29 stsp path_got = NULL;
339 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
341 507dc3bb 2018-12-29 stsp return err;
343 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
344 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
348 507dc3bb 2018-12-29 stsp /* Record our base commit. */
349 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
352 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
356 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
357 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
358 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
359 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
364 507dc3bb 2018-12-29 stsp got_object_close(obj);
365 507dc3bb 2018-12-29 stsp free(id_str);
366 507dc3bb 2018-12-29 stsp free(path_got);
367 507dc3bb 2018-12-29 stsp return err;
370 50b0790e 2020-09-11 stsp const struct got_gotconfig *
371 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
373 50b0790e 2020-09-11 stsp return worktree->gotconfig;
376 9d31a1d8 2018-03-11 stsp static const struct got_error *
377 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
379 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
380 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
381 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
382 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
383 86c3caaf 2018-03-09 stsp return NULL;
386 21908da4 2019-01-13 stsp static const struct got_error *
387 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
389 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
390 4a1ddfc2 2019-01-12 stsp char *abspath;
392 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
393 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
395 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
396 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
397 ddcd8544 2019-03-11 stsp struct stat sb;
398 ddcd8544 2019-03-11 stsp err = NULL;
399 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
400 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
401 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
402 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
403 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
406 4a1ddfc2 2019-01-12 stsp free(abspath);
407 68c76935 2019-02-19 stsp return err;
410 68c76935 2019-02-19 stsp static const struct got_error *
411 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
413 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
414 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
415 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
416 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
421 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
422 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
423 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
426 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
427 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
428 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
431 68c76935 2019-02-19 stsp if (flen1 == 0) {
432 68c76935 2019-02-19 stsp if (flen2 != 0)
435 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
436 68c76935 2019-02-19 stsp if (flen1 != 0)
439 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
440 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
450 68c76935 2019-02-19 stsp return err;
453 68c76935 2019-02-19 stsp static const struct got_error *
454 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
456 68c76935 2019-02-19 stsp struct stat sb;
457 68c76935 2019-02-19 stsp size_t size1, size2;
461 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
462 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
463 68c76935 2019-02-19 stsp size1 = sb.st_size;
465 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
466 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
467 68c76935 2019-02-19 stsp size2 = sb.st_size;
469 68c76935 2019-02-19 stsp if (size1 != size2) {
471 68c76935 2019-02-19 stsp return NULL;
474 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
475 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
476 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
477 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
479 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
482 0e039681 2021-11-15 stsp static const struct got_error *
483 0e039681 2021-11-15 stsp copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
485 0e039681 2021-11-15 stsp uint8_t fbuf[65536];
486 0e039681 2021-11-15 stsp size_t flen;
487 0e039681 2021-11-15 stsp ssize_t outlen;
489 0e039681 2021-11-15 stsp *outsize = 0;
491 0e039681 2021-11-15 stsp if (fseek(f, 0L, SEEK_SET) == -1)
492 0e039681 2021-11-15 stsp return got_ferror(f, GOT_ERR_IO);
495 0e039681 2021-11-15 stsp flen = fread(fbuf, 1, sizeof(fbuf), f);
496 0e039681 2021-11-15 stsp if (flen == 0) {
497 0e039681 2021-11-15 stsp if (ferror(f))
498 0e039681 2021-11-15 stsp return got_error_from_errno("fread");
499 0e039681 2021-11-15 stsp if (feof(f))
502 0e039681 2021-11-15 stsp outlen = write(outfd, fbuf, flen);
503 0e039681 2021-11-15 stsp if (outlen == -1)
504 0e039681 2021-11-15 stsp return got_error_from_errno("write");
505 0e039681 2021-11-15 stsp if (outlen != flen)
506 0e039681 2021-11-15 stsp return got_error(GOT_ERR_IO);
507 0e039681 2021-11-15 stsp *outsize += outlen;
510 0e039681 2021-11-15 stsp return NULL;
513 0e039681 2021-11-15 stsp static const struct got_error *
514 0e039681 2021-11-15 stsp merge_binary_file(int *overlapcnt, int merged_fd,
515 0e039681 2021-11-15 stsp FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
516 0e039681 2021-11-15 stsp const char *label_deriv, const char *label_orig, const char *label_deriv2,
517 0e039681 2021-11-15 stsp const char *ondisk_path)
519 0e039681 2021-11-15 stsp const struct got_error *err = NULL;
520 0e039681 2021-11-15 stsp int same_content, changed_deriv, changed_deriv2;
521 0e039681 2021-11-15 stsp int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
522 0e039681 2021-11-15 stsp off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
523 0e039681 2021-11-15 stsp char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
524 0e039681 2021-11-15 stsp char *base_path_orig = NULL, *base_path_deriv = NULL;
525 0e039681 2021-11-15 stsp char *base_path_deriv2 = NULL;
527 0e039681 2021-11-15 stsp *overlapcnt = 0;
529 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_deriv2);
531 0e039681 2021-11-15 stsp return err;
533 0e039681 2021-11-15 stsp if (same_content)
534 0e039681 2021-11-15 stsp return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
536 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_orig);
538 0e039681 2021-11-15 stsp return err;
539 0e039681 2021-11-15 stsp changed_deriv = !same_content;
540 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv2, f_orig);
542 0e039681 2021-11-15 stsp return err;
543 0e039681 2021-11-15 stsp changed_deriv2 = !same_content;
545 0e039681 2021-11-15 stsp if (changed_deriv && changed_deriv2) {
546 0e039681 2021-11-15 stsp *overlapcnt = 1;
547 0e039681 2021-11-15 stsp if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
548 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
551 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
552 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
555 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
556 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
559 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_orig, &fd_orig,
560 b90054ed 2022-11-01 stsp base_path_orig, "");
563 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
564 b90054ed 2022-11-01 stsp base_path_deriv, "");
567 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
568 b90054ed 2022-11-01 stsp base_path_deriv2, "");
571 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
574 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
577 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
580 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "Binary files differ and cannot be "
581 0e039681 2021-11-15 stsp "merged automatically:\n") < 0) {
582 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
585 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
586 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
587 0e039681 2021-11-15 stsp label_deriv ? " " : "",
588 0e039681 2021-11-15 stsp label_deriv ? label_deriv : "",
589 0e039681 2021-11-15 stsp path_deriv) < 0) {
590 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
593 0e039681 2021-11-15 stsp if (size_orig > 0) {
594 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
595 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_ORIG,
596 0e039681 2021-11-15 stsp label_orig ? " " : "",
597 0e039681 2021-11-15 stsp label_orig ? label_orig : "",
598 0e039681 2021-11-15 stsp path_orig) < 0) {
599 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
603 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
604 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
605 0e039681 2021-11-15 stsp path_deriv2,
606 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_END,
607 0e039681 2021-11-15 stsp label_deriv2 ? " " : "",
608 0e039681 2021-11-15 stsp label_deriv2 ? label_deriv2 : "") < 0) {
609 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
612 0e039681 2021-11-15 stsp } else if (changed_deriv)
613 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
614 0e039681 2021-11-15 stsp else if (changed_deriv2)
615 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
617 0e039681 2021-11-15 stsp if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
618 0e039681 2021-11-15 stsp err == NULL)
619 0e039681 2021-11-15 stsp err = got_error_from_errno2("unlink", path_orig);
620 0e039681 2021-11-15 stsp if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
621 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_orig);
622 0e039681 2021-11-15 stsp if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
623 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv);
624 0e039681 2021-11-15 stsp if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
625 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv2);
626 0e039681 2021-11-15 stsp free(path_orig);
627 0e039681 2021-11-15 stsp free(path_deriv);
628 0e039681 2021-11-15 stsp free(path_deriv2);
629 0e039681 2021-11-15 stsp free(base_path_orig);
630 0e039681 2021-11-15 stsp free(base_path_deriv);
631 0e039681 2021-11-15 stsp free(base_path_deriv2);
632 0e039681 2021-11-15 stsp return err;
636 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
637 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
638 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
639 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
640 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
642 6353ad76 2019-02-08 stsp static const struct got_error *
643 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
644 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
645 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
646 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
647 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
648 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
650 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
651 6353ad76 2019-02-08 stsp int merged_fd = -1;
652 db590691 2021-06-02 stsp FILE *f_merged = NULL;
653 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
654 234035bc 2019-06-01 stsp int overlapcnt = 0;
655 3524bbf9 2020-10-20 stsp char *parent = NULL;
657 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
659 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
661 3524bbf9 2020-10-20 stsp return err;
663 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
664 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
668 b90054ed 2022-11-01 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
672 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
673 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
675 0e039681 2021-11-15 stsp if (err->code != GOT_ERR_FILE_BINARY)
677 0e039681 2021-11-15 stsp err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
678 0e039681 2021-11-15 stsp f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
679 0e039681 2021-11-15 stsp ondisk_path);
684 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
685 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
689 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
690 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
694 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
695 db590691 2021-06-02 stsp if (f_merged == NULL) {
696 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
699 db590691 2021-06-02 stsp merged_fd = -1;
701 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
702 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
703 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
709 b2b3fce1 2022-10-29 op if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
710 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
714 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
715 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
716 230a42bd 2019-05-11 jcs ondisk_path);
721 fdcb7daf 2019-12-15 stsp if (merged_path)
722 fdcb7daf 2019-12-15 stsp unlink(merged_path);
724 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
725 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
726 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
727 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
728 6353ad76 2019-02-08 stsp free(merged_path);
729 af54ae4a 2019-02-19 stsp free(base_path);
730 3524bbf9 2020-10-20 stsp free(parent);
731 af57b12a 2020-07-23 stsp return err;
734 af57b12a 2020-07-23 stsp static const struct got_error *
735 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
736 af57b12a 2020-07-23 stsp size_t target_len)
738 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
739 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
740 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
741 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
742 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
743 af57b12a 2020-07-23 stsp ondisk_path);
744 af57b12a 2020-07-23 stsp return NULL;
748 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
749 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
750 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
751 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
753 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
754 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
756 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
757 993e2a1b 2020-07-23 stsp * has deleted this symlink.
759 11cc08c1 2020-07-23 stsp static const struct got_error *
760 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
761 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
762 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
764 11cc08c1 2020-07-23 stsp const struct got_error *err;
765 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
766 11cc08c1 2020-07-23 stsp FILE *f = NULL;
768 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
770 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
772 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
773 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
774 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
778 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
780 3818e3c4 2020-11-01 naddy goto done;
782 b2b3fce1 2022-10-29 op if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
783 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
787 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
788 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
789 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
790 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
791 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
792 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
793 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
794 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
795 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
796 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
800 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
801 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
804 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
805 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
809 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
810 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
811 11cc08c1 2020-07-23 stsp free(path);
812 11cc08c1 2020-07-23 stsp free(id_str);
813 11cc08c1 2020-07-23 stsp free(label_deriv);
814 11cc08c1 2020-07-23 stsp return err;
817 d219f183 2020-07-23 stsp /* forward declaration */
818 d219f183 2020-07-23 stsp static const struct got_error *
819 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
820 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
821 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
822 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
825 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
826 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
827 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
828 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
830 af57b12a 2020-07-23 stsp static const struct got_error *
831 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
832 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
833 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
834 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
835 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
837 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
838 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
839 af57b12a 2020-07-23 stsp struct stat sb;
840 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
841 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
842 11cc08c1 2020-07-23 stsp int have_local_change = 0;
843 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
845 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
846 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
848 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
849 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
850 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
851 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
852 af57b12a 2020-07-23 stsp ondisk_path);
855 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
857 526a746f 2020-07-23 stsp if (blob_orig) {
858 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
863 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
864 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
865 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
866 11cc08c1 2020-07-23 stsp have_local_change = 1;
868 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
869 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
870 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
871 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
872 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
874 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
875 11cc08c1 2020-07-23 stsp if (ancestor_target) {
876 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
877 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
879 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
880 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
881 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
882 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
885 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
886 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
887 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
888 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
891 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
894 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
895 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
896 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
897 af57b12a 2020-07-23 stsp strlen(deriv_target));
900 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
901 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
902 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
903 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
904 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
905 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
908 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
909 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
910 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
913 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
919 af57b12a 2020-07-23 stsp free(ancestor_target);
920 eec2f5a9 2021-06-03 stsp return err;
923 eec2f5a9 2021-06-03 stsp static const struct got_error *
924 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
926 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
927 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
928 eec2f5a9 2021-06-03 stsp ssize_t target_len;
932 eec2f5a9 2021-06-03 stsp *outfile = NULL;
934 eec2f5a9 2021-06-03 stsp f = got_opentemp();
935 eec2f5a9 2021-06-03 stsp if (f == NULL)
936 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
937 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
938 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
939 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
942 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
943 eec2f5a9 2021-06-03 stsp if (n != target_len) {
944 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
947 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
948 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
951 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
952 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
959 eec2f5a9 2021-06-03 stsp *outfile = f;
960 14c901f1 2019-08-08 stsp return err;
964 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
965 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
966 14c901f1 2019-08-08 stsp * acts as the second derived version.
968 14c901f1 2019-08-08 stsp static const struct got_error *
969 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
970 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
971 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
972 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
973 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
974 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
976 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
977 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
978 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
979 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
980 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
982 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
984 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
986 ed6b5030 2020-10-20 stsp return err;
988 67a66647 2021-05-31 stsp if (blob_orig) {
989 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
990 67a66647 2021-05-31 stsp parent) == -1) {
991 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
992 67a66647 2021-05-31 stsp base_path = NULL;
996 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_orig_path, &f_orig,
997 b90054ed 2022-11-01 stsp base_path, "");
1000 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1001 67a66647 2021-05-31 stsp blob_orig);
1003 67a66647 2021-05-31 stsp goto done;
1004 67a66647 2021-05-31 stsp free(base_path);
1007 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1008 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1009 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1011 db590691 2021-06-02 stsp f_orig = got_opentemp();
1012 db590691 2021-06-02 stsp if (f_orig == NULL) {
1013 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1014 db590691 2021-06-02 stsp goto done;
1018 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1019 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1020 14c901f1 2019-08-08 stsp base_path = NULL;
1021 14c901f1 2019-08-08 stsp goto done;
1024 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1026 14c901f1 2019-08-08 stsp goto done;
1027 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1028 14c901f1 2019-08-08 stsp blob_deriv);
1030 14c901f1 2019-08-08 stsp goto done;
1032 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1034 14c901f1 2019-08-08 stsp goto done;
1035 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1036 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1037 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1038 14c901f1 2019-08-08 stsp goto done;
1042 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1043 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1045 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1046 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1048 eec2f5a9 2021-06-03 stsp goto done;
1051 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1052 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1053 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1054 eec2f5a9 2021-06-03 stsp goto done;
1056 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1057 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1058 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1059 eec2f5a9 2021-06-03 stsp close(fd);
1060 eec2f5a9 2021-06-03 stsp goto done;
1064 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1065 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1066 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1068 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1069 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1070 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1071 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1072 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1073 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1074 14c901f1 2019-08-08 stsp free(base_path);
1075 67a66647 2021-05-31 stsp if (blob_orig_path) {
1076 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1077 67a66647 2021-05-31 stsp free(blob_orig_path);
1079 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1080 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1081 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1083 6353ad76 2019-02-08 stsp free(id_str);
1084 818c7501 2019-07-11 stsp free(label_deriv);
1085 ed6b5030 2020-10-20 stsp free(parent);
1086 4a1ddfc2 2019-01-12 stsp return err;
1089 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1090 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1091 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1092 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1094 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1095 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1097 65b05cec 2020-07-23 stsp *new_iep = NULL;
1099 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1101 054041d0 2020-06-24 stsp return err;
1103 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1104 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1106 054041d0 2020-06-24 stsp goto done;
1108 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1111 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1113 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1114 13d9040b 2019-03-27 stsp return err;
1117 1ebedb77 2019-10-19 stsp static mode_t
1118 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1120 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1122 1ebedb77 2019-10-19 stsp if (executable) {
1123 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1124 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1125 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1126 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1127 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1128 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1131 b2b3fce1 2022-10-29 op return st_mode;
1134 b2b3fce1 2022-10-29 op static mode_t
1135 b2b3fce1 2022-10-29 op apply_umask(mode_t mode)
1139 b2b3fce1 2022-10-29 op um = umask(000);
1141 b2b3fce1 2022-10-29 op return mode & ~um;
1144 8ba819a3 2020-07-23 stsp /* forward declaration */
1145 13d9040b 2019-03-27 stsp static const struct got_error *
1146 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1147 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1148 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1149 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1150 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1151 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1154 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1155 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1157 8ba819a3 2020-07-23 stsp static const struct got_error *
1158 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1159 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1161 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1162 5a1fbc73 2020-07-23 stsp ssize_t elen;
1163 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1166 c6e8a826 2021-04-05 stsp *did_something = 0;
1169 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1170 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1171 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1172 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1173 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1174 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1176 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1177 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1178 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
1179 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1181 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1182 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1183 5a1fbc73 2020-07-23 stsp if (elen == -1)
1184 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1186 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1187 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1188 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1191 c6e8a826 2021-04-05 stsp *did_something = 1;
1192 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1193 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1194 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1195 5a1fbc73 2020-07-23 stsp return err;
1198 3c1ec782 2020-07-23 stsp static const struct got_error *
1199 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1200 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1202 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1203 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1204 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1206 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1208 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1209 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1210 3c1ec782 2020-07-23 stsp return NULL;
1214 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1215 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1216 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1217 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1218 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1220 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1221 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1222 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1224 ce031e9e 2020-10-20 stsp return err;
1225 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1226 ce031e9e 2020-10-20 stsp free(parent);
1227 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1229 ce031e9e 2020-10-20 stsp free(parent);
1230 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1231 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1232 3c1ec782 2020-07-23 stsp free(abspath);
1233 3c1ec782 2020-07-23 stsp return err;
1235 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1236 3c1ec782 2020-07-23 stsp free(abspath);
1238 3c1ec782 2020-07-23 stsp return err;
1240 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1242 3c1ec782 2020-07-23 stsp return err;
1245 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1246 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1247 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1248 3c1ec782 2020-07-23 stsp return NULL;
1251 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1252 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1253 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1254 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1255 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1256 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1258 3c1ec782 2020-07-23 stsp free(path_got);
1259 3c1ec782 2020-07-23 stsp return NULL;
1262 5a1fbc73 2020-07-23 stsp static const struct got_error *
1263 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1264 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1265 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1266 5267b9e4 2021-09-26 stsp int path_is_unversioned, int allow_bad_symlinks,
1267 5267b9e4 2021-09-26 stsp struct got_repository *repo,
1268 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1270 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1271 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1272 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1273 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1274 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1276 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1279 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1280 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1281 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1282 8ba819a3 2020-07-23 stsp * in the blob object.
1285 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1286 538b6881 2022-07-21 florian if (err)
1287 538b6881 2022-07-21 florian return err;
1289 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1290 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1291 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1292 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1293 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1294 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1295 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1296 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1297 3b9f0f87 2020-07-23 stsp progress_arg);
1299 8ba819a3 2020-07-23 stsp if (len > 0) {
1300 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1301 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1302 8ba819a3 2020-07-23 stsp len - hdrlen);
1303 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1304 8ba819a3 2020-07-23 stsp hdrlen = 0;
1306 8ba819a3 2020-07-23 stsp } while (len != 0);
1307 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1309 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1310 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1312 3c1ec782 2020-07-23 stsp return err;
1314 5267b9e4 2021-09-26 stsp if (*is_bad_symlink && !allow_bad_symlinks) {
1315 906c123b 2020-07-23 stsp /* install as a regular file */
1316 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1317 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1318 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1319 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1320 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1321 3c29341b 2022-07-21 florian return err;
1324 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1325 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1326 c6e8a826 2021-04-05 stsp int symlink_replaced;
1327 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1328 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1329 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1330 3c29341b 2022-07-21 florian return err;
1332 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1333 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1335 3c29341b 2022-07-21 florian return err;
1336 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1337 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1338 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1339 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1340 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1341 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1343 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1344 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1347 3c29341b 2022-07-21 florian return err; /* Nothing else to do. */
1350 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1351 f4994adc 2020-10-20 stsp char *parent;
1352 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1354 3c29341b 2022-07-21 florian return err;
1355 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1356 f4994adc 2020-10-20 stsp free(parent);
1358 3c29341b 2022-07-21 florian return err;
1360 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1361 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1363 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1364 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1365 3c29341b 2022-07-21 florian return err;
1369 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1370 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1371 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1372 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1373 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1374 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1375 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1376 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1377 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1378 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1379 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1380 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1381 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1383 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1384 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1386 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1387 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1388 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1389 8ba819a3 2020-07-23 stsp return err;
1392 8ba819a3 2020-07-23 stsp static const struct got_error *
1393 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1394 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1395 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1396 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1397 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1398 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1400 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1401 507dc3bb 2018-12-29 stsp int fd = -1;
1402 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1403 507dc3bb 2018-12-29 stsp int update = 0;
1404 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1405 b2b3fce1 2022-10-29 op mode_t mode;
1407 b2b3fce1 2022-10-29 op mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1408 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1409 b2b3fce1 2022-10-29 op O_CLOEXEC, mode);
1410 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1411 07fa9365 2023-03-10 stsp if (errno == ENOENT || errno == ENOTDIR) {
1412 f5375317 2020-10-20 stsp char *parent;
1413 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1415 f5375317 2020-10-20 stsp return err;
1416 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1417 07fa9365 2023-03-10 stsp if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1418 07fa9365 2023-03-10 stsp err = got_error_path(path, err->code);
1419 f5375317 2020-10-20 stsp free(parent);
1421 21908da4 2019-01-13 stsp return err;
1422 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1423 8bd0cdad 2021-12-31 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1425 21908da4 2019-01-13 stsp if (fd == -1)
1426 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1427 230a42bd 2019-05-11 jcs ondisk_path);
1428 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1429 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1430 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1431 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1432 3b9f0f87 2020-07-23 stsp goto done;
1434 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1435 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1436 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1437 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1438 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1439 507dc3bb 2018-12-29 stsp goto done;
1441 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1442 b90054ed 2022-11-01 stsp ondisk_path, "");
1444 507dc3bb 2018-12-29 stsp goto done;
1445 507dc3bb 2018-12-29 stsp update = 1;
1447 b2b3fce1 2022-10-29 op if (fchmod(fd, apply_umask(mode)) == -1) {
1448 b2b3fce1 2022-10-29 op err = got_error_from_errno2("fchmod",
1454 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1457 bd6aa359 2020-07-23 stsp if (progress_cb) {
1458 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1459 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1461 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1462 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1465 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1466 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1468 bd6aa359 2020-07-23 stsp goto done;
1471 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1473 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1474 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1477 9d31a1d8 2018-03-11 stsp if (len > 0) {
1478 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1479 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1480 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1481 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1482 b87c6f83 2018-12-24 stsp goto done;
1483 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1484 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1485 b87c6f83 2018-12-24 stsp goto done;
1487 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1489 9d31a1d8 2018-03-11 stsp } while (len != 0);
1491 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1492 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1493 816dc654 2019-02-16 stsp goto done;
1496 507dc3bb 2018-12-29 stsp if (update) {
1497 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1498 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1499 f6d8c0ac 2020-11-09 stsp goto done;
1501 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1502 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1503 230a42bd 2019-05-11 jcs ondisk_path);
1504 68ed9ba5 2019-02-10 stsp goto done;
1506 63df146d 2020-11-09 stsp free(tmppath);
1507 63df146d 2020-11-09 stsp tmppath = NULL;
1511 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1512 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1513 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1514 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1515 507dc3bb 2018-12-29 stsp free(tmppath);
1516 6353ad76 2019-02-08 stsp return err;
1520 12383673 2023-02-18 mark * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1521 12383673 2023-02-18 mark * conflict marker is found in newly added lines only.
1523 6353ad76 2019-02-08 stsp static const struct got_error *
1524 12383673 2023-02-18 mark get_modified_file_content_status(unsigned char *status,
1525 12383673 2023-02-18 mark struct got_blob_object *blob, const char *path, struct stat *sb,
1526 12383673 2023-02-18 mark FILE *ondisk_file)
1528 d952957d 2023-02-19 mark const struct got_error *err, *free_err;
1529 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1530 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1531 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1532 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1534 d952957d 2023-02-19 mark FILE *f1 = NULL;
1535 d952957d 2023-02-19 mark struct got_diffreg_result *diffreg_result = NULL;
1536 d952957d 2023-02-19 mark struct diff_result *r;
1537 d952957d 2023-02-19 mark int nchunks_parsed, n, i = 0, ln = 0;
1538 9bdd68dd 2020-01-02 naddy char *line = NULL;
1539 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1540 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1542 d952957d 2023-02-19 mark if (*status != GOT_STATUS_MODIFY)
1543 d952957d 2023-02-19 mark return NULL;
1545 d952957d 2023-02-19 mark f1 = got_opentemp();
1546 d952957d 2023-02-19 mark if (f1 == NULL)
1547 d952957d 2023-02-19 mark return got_error_from_errno("got_opentemp");
1549 d952957d 2023-02-19 mark if (blob) {
1550 d952957d 2023-02-19 mark got_object_blob_rewind(blob);
1551 d952957d 2023-02-19 mark err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1553 12383673 2023-02-18 mark goto done;
1556 d952957d 2023-02-19 mark err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1557 d952957d 2023-02-19 mark 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1559 d952957d 2023-02-19 mark goto done;
1561 d952957d 2023-02-19 mark r = diffreg_result->result;
1563 d952957d 2023-02-19 mark for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1564 d952957d 2023-02-19 mark struct diff_chunk *c;
1565 d952957d 2023-02-19 mark struct diff_chunk_context cc = {};
1566 7783f73c 2023-02-20 mark off_t pos;
1569 d952957d 2023-02-19 mark * We can optimise a little by advancing straight
1570 d952957d 2023-02-19 mark * to the next chunk if this one has no added lines.
1572 d952957d 2023-02-19 mark c = diff_chunk_get(r, n);
1574 45e6b2f4 2023-02-20 mark if (diff_chunk_type(c) != CHUNK_PLUS) {
1575 d952957d 2023-02-19 mark nchunks_parsed = 1;
1576 7783f73c 2023-02-20 mark continue; /* removed or unchanged lines */
1579 7783f73c 2023-02-20 mark pos = diff_chunk_get_right_start_pos(c);
1580 7783f73c 2023-02-20 mark if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1581 7783f73c 2023-02-20 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1582 7783f73c 2023-02-20 mark goto done;
1585 7783f73c 2023-02-20 mark diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1586 7783f73c 2023-02-20 mark ln = cc.right.start;
1588 d952957d 2023-02-19 mark while (ln < cc.right.end) {
1589 d952957d 2023-02-19 mark linelen = getline(&line, &linesize, ondisk_file);
1590 d952957d 2023-02-19 mark if (linelen == -1) {
1591 d952957d 2023-02-19 mark if (feof(ondisk_file))
1593 d952957d 2023-02-19 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1597 d952957d 2023-02-19 mark if (line && strncmp(line, markers[i],
1598 d952957d 2023-02-19 mark strlen(markers[i])) == 0) {
1599 d952957d 2023-02-19 mark if (strcmp(markers[i],
1600 d952957d 2023-02-19 mark GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1601 d952957d 2023-02-19 mark *status = GOT_STATUS_CONFLICT;
1602 d952957d 2023-02-19 mark goto done;
1611 9bdd68dd 2020-01-02 naddy free(line);
1612 12383673 2023-02-18 mark if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1613 12383673 2023-02-18 mark err = got_error_from_errno("fclose");
1614 d952957d 2023-02-19 mark free_err = got_diffreg_result_free(diffreg_result);
1615 d952957d 2023-02-19 mark if (err == NULL)
1616 d952957d 2023-02-19 mark err = free_err;
1618 7154f6ce 2019-03-27 stsp return err;
1621 e2b1e152 2019-07-13 stsp static int
1622 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1624 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1625 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1628 1ebedb77 2019-10-19 stsp static int
1629 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1631 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1632 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1633 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1634 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1635 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1636 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1639 c363b2c1 2019-08-03 stsp static unsigned char
1640 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1642 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1643 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1644 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1645 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1646 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1647 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1648 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1650 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1654 a919d5c4 2020-07-23 stsp static const struct got_error *
1655 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1656 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1657 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1659 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1660 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1661 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1662 a919d5c4 2020-07-23 stsp ssize_t elen;
1663 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1664 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1665 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1667 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1669 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1671 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1673 a919d5c4 2020-07-23 stsp return err;
1674 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1676 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1677 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1679 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1681 a919d5c4 2020-07-23 stsp if (len > 0) {
1682 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1683 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1684 a919d5c4 2020-07-23 stsp len - hdrlen);
1685 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1686 a919d5c4 2020-07-23 stsp hdrlen = 0;
1688 a919d5c4 2020-07-23 stsp } while (len != 0);
1689 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1691 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1692 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1693 a919d5c4 2020-07-23 stsp if (elen == -1)
1694 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1696 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1697 a919d5c4 2020-07-23 stsp if (elen == -1)
1698 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1701 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1702 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1704 a919d5c4 2020-07-23 stsp return NULL;
1707 7154f6ce 2019-03-27 stsp static const struct got_error *
1708 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1709 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1710 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1712 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1713 6353ad76 2019-02-08 stsp struct got_object_id id;
1714 6353ad76 2019-02-08 stsp size_t hdrlen;
1715 eb81bc23 2022-06-28 tracey int fd = -1, fd1 = -1;
1716 6353ad76 2019-02-08 stsp FILE *f = NULL;
1717 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1718 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1719 6353ad76 2019-02-08 stsp size_t flen, blen;
1720 2c4740ad 2023-02-12 mark unsigned char staged_status;
1722 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
1723 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1724 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1727 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1728 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1729 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1731 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1732 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1733 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1734 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1735 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1737 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1738 882ef1b9 2019-12-13 stsp goto done;
1740 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1741 3d35a492 2019-12-13 stsp goto done;
1744 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1745 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1746 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1747 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1748 5c02d2a5 2021-09-26 stsp else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1749 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1750 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1751 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1752 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1753 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1754 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1756 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1757 3d35a492 2019-12-13 stsp goto done;
1759 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1760 1338848f 2019-12-13 stsp goto done;
1764 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1765 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1766 1338848f 2019-12-13 stsp goto done;
1769 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1770 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1771 1338848f 2019-12-13 stsp goto done;
1772 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1773 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1774 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1775 1338848f 2019-12-13 stsp goto done;
1778 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1779 f179e66d 2020-07-23 stsp goto done;
1781 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1782 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1783 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1784 1338848f 2019-12-13 stsp goto done;
1787 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1788 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1789 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
1791 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
1793 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1794 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1795 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1796 eb81bc23 2022-06-28 tracey goto done;
1798 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1800 1338848f 2019-12-13 stsp goto done;
1802 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1803 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1804 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1805 a919d5c4 2020-07-23 stsp goto done;
1808 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1809 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1810 ab0d4361 2019-12-13 stsp if (fd == -1) {
1811 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1812 ab0d4361 2019-12-13 stsp goto done;
1816 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1817 6353ad76 2019-02-08 stsp if (f == NULL) {
1818 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1819 6353ad76 2019-02-08 stsp goto done;
1822 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1824 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1825 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1827 7154f6ce 2019-03-27 stsp goto done;
1828 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1829 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1830 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1831 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1832 7154f6ce 2019-03-27 stsp goto done;
1834 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1835 6353ad76 2019-02-08 stsp if (flen != 0)
1836 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1838 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1839 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1840 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1842 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1843 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1844 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1845 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1849 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1852 6353ad76 2019-02-08 stsp hdrlen = 0;
1855 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1856 7154f6ce 2019-03-27 stsp rewind(f);
1857 12383673 2023-02-18 mark err = get_modified_file_content_status(status, blob, ie->path,
1859 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1860 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1862 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1863 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1865 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1866 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1867 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1868 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1869 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1870 9d31a1d8 2018-03-11 stsp return err;
1874 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1875 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1877 e2b1e152 2019-07-13 stsp static const struct got_error *
1878 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1879 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1881 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1882 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1883 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1885 e2b1e152 2019-07-13 stsp return NULL;
1888 07fa9365 2023-03-10 stsp static const struct got_error *remove_ondisk_file(const char *, const char *);
1890 9d31a1d8 2018-03-11 stsp static const struct got_error *
1891 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1892 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1893 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1894 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1895 0584f854 2019-04-06 stsp void *progress_arg)
1897 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1898 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1899 eb81bc23 2022-06-28 tracey char *ondisk_path = NULL;
1900 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1901 b8f41171 2019-02-10 stsp struct stat sb;
1902 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
1904 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1905 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1908 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1909 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1910 a76c42e6 2019-08-03 stsp goto done;
1912 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1915 abb4604f 2019-07-27 stsp goto done;
1916 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1917 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1919 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1920 07fa9365 2023-03-10 stsp if (errno != ENOENT && errno != ENOTDIR) {
1921 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1922 f6764181 2021-09-24 stsp ondisk_path);
1923 f6764181 2021-09-24 stsp goto done;
1925 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1926 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1928 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1929 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1931 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1935 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1937 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1938 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1939 b8f41171 2019-02-10 stsp goto done;
1941 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1943 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1944 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1946 5036ab18 2020-04-18 stsp goto done;
1949 07fa9365 2023-03-10 stsp if (S_ISDIR(te->mode)) { /* file changing into a directory */
1950 07fa9365 2023-03-10 stsp if (status == GOT_STATUS_UNVERSIONED) {
1951 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg, status, path);
1952 07fa9365 2023-03-10 stsp } else if (status != GOT_STATUS_NO_CHANGE &&
1953 07fa9365 2023-03-10 stsp status != GOT_STATUS_DELETE &&
1954 07fa9365 2023-03-10 stsp status != GOT_STATUS_NONEXISTENT &&
1955 07fa9365 2023-03-10 stsp status != GOT_STATUS_MISSING) {
1956 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg,
1957 07fa9365 2023-03-10 stsp GOT_STATUS_CANNOT_DELETE, path);
1958 07fa9365 2023-03-10 stsp } else if (ie) {
1959 07fa9365 2023-03-10 stsp if (status != GOT_STATUS_DELETE &&
1960 07fa9365 2023-03-10 stsp status != GOT_STATUS_NONEXISTENT &&
1961 07fa9365 2023-03-10 stsp status != GOT_STATUS_MISSING) {
1962 07fa9365 2023-03-10 stsp err = remove_ondisk_file(worktree->root_path,
1963 07fa9365 2023-03-10 stsp ie->path);
1964 07fa9365 2023-03-10 stsp if (err && !(err->code == GOT_ERR_ERRNO &&
1965 07fa9365 2023-03-10 stsp errno == ENOENT))
1966 07fa9365 2023-03-10 stsp goto done;
1968 07fa9365 2023-03-10 stsp got_fileindex_entry_remove(fileindex, ie);
1969 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1970 07fa9365 2023-03-10 stsp ie->path);
1972 07fa9365 2023-03-10 stsp goto done; /* nothing else to do */
1975 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1976 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1977 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1979 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1980 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1981 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1982 c6e8a826 2021-04-05 stsp * updating contents of this file.
1984 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1985 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1986 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1987 c6e8a826 2021-04-05 stsp /* Same commit. */
1988 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1989 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1991 e2b1e152 2019-07-13 stsp goto done;
1992 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1994 6353ad76 2019-02-08 stsp goto done;
1996 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1997 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1998 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1999 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
2000 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2001 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2003 0f58026f 2021-04-05 stsp goto done;
2004 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2006 b8f41171 2019-02-10 stsp goto done;
2010 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
2011 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
2012 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
2013 eb81bc23 2022-06-28 tracey goto done;
2015 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2017 6353ad76 2019-02-08 stsp goto done;
2019 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2020 234035bc 2019-06-01 stsp int update_timestamps;
2021 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2022 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2023 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2024 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
2025 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
2026 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
2027 eb81bc23 2022-06-28 tracey goto done;
2029 234035bc 2019-06-01 stsp struct got_object_id id2;
2030 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id2, ie);
2031 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2034 f69721c3 2019-10-21 stsp goto done;
2036 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2037 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2038 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2039 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2040 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2041 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2042 f69721c3 2019-10-21 stsp goto done;
2044 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2045 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2046 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2047 234035bc 2019-06-01 stsp goto done;
2050 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2051 36bf999c 2020-07-23 stsp char *link_target;
2052 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2054 36bf999c 2020-07-23 stsp goto done;
2055 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2056 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2057 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2058 36bf999c 2020-07-23 stsp free(link_target);
2060 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2061 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2062 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2063 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2065 f69721c3 2019-10-21 stsp free(label_orig);
2066 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2067 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2068 eb81bc23 2022-06-28 tracey goto done;
2070 234035bc 2019-06-01 stsp if (blob2)
2071 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2073 909d120e 2019-09-22 stsp goto done;
2075 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2076 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2077 234035bc 2019-06-01 stsp * unmodified files again.
2079 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2080 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2081 234035bc 2019-06-01 stsp update_timestamps);
2082 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2083 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2084 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2085 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2086 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2088 1ee397ad 2019-07-12 stsp goto done;
2089 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2090 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2092 13d9040b 2019-03-27 stsp goto done;
2094 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2095 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2096 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2097 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2098 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2099 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
2100 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
2102 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2103 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2104 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2105 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2106 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2109 13d9040b 2019-03-27 stsp goto done;
2112 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2113 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2114 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2116 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2117 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2118 054041d0 2020-06-24 stsp &blob->id);
2121 13d9040b 2019-03-27 stsp goto done;
2123 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2124 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2125 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2129 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2130 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2131 eb81bc23 2022-06-28 tracey goto done;
2133 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2135 6353ad76 2019-02-08 stsp free(ondisk_path);
2136 8da9e5f4 2019-01-12 stsp return err;
2139 90285c3b 2019-01-08 stsp static const struct got_error *
2140 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2142 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2143 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2145 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2146 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2148 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2149 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2150 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2152 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2153 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2155 1c4cdd89 2021-06-20 stsp goto done;
2156 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2157 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2158 fddefe3b 2020-10-20 stsp free(ondisk_path);
2159 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2160 1c4cdd89 2021-06-20 stsp parent = NULL;
2161 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2162 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2163 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2164 fddefe3b 2020-10-20 stsp ondisk_path);
2167 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2173 90285c3b 2019-01-08 stsp free(ondisk_path);
2174 1c4cdd89 2021-06-20 stsp free(parent);
2175 90285c3b 2019-01-08 stsp return err;
2178 708d8e67 2019-03-27 stsp static const struct got_error *
2179 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2180 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2181 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2183 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2184 708d8e67 2019-03-27 stsp unsigned char status;
2185 708d8e67 2019-03-27 stsp struct stat sb;
2186 708d8e67 2019-03-27 stsp char *ondisk_path;
2188 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2189 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2191 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2193 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2195 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2197 5a58a424 2020-06-23 stsp goto done;
2199 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2200 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2201 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2202 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2203 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2204 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2205 993e2a1b 2020-07-23 stsp goto done;
2207 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2208 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2209 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2210 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2212 993e2a1b 2020-07-23 stsp goto done;
2213 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2214 993e2a1b 2020-07-23 stsp ie->path);
2215 993e2a1b 2020-07-23 stsp goto done;
2218 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2219 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2220 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2222 5a58a424 2020-06-23 stsp goto done;
2224 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2225 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2227 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2228 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2230 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2232 5a58a424 2020-06-23 stsp goto done;
2233 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2234 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2236 5a58a424 2020-06-23 stsp goto done;
2238 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2241 5a58a424 2020-06-23 stsp free(ondisk_path);
2242 fc6346c4 2019-03-27 stsp return err;
2245 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2246 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2247 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2248 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2249 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2250 8da9e5f4 2019-01-12 stsp void *progress_arg;
2251 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2252 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2255 512f0d0e 2019-01-02 stsp static const struct got_error *
2256 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2257 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2259 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2261 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2262 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2264 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2265 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2268 8da9e5f4 2019-01-12 stsp static const struct got_error *
2269 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2271 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2273 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2274 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2276 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2277 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2280 9d31a1d8 2018-03-11 stsp static const struct got_error *
2281 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2283 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2284 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2285 8da9e5f4 2019-01-12 stsp char *path;
2287 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2288 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2290 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2291 63c5ca5d 2019-08-24 stsp return NULL;
2293 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2294 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2296 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2298 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2299 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2301 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2302 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2304 8da9e5f4 2019-01-12 stsp free(path);
2305 0cd1c46a 2019-03-11 stsp return err;
2308 b2118c49 2020-07-28 stsp const struct got_error *
2309 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2311 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2313 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2314 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2315 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2316 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2319 b2118c49 2020-07-28 stsp return NULL;
2322 818c7501 2019-07-11 stsp static const struct got_error *
2323 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2325 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2326 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2328 517bab73 2019-03-11 stsp *refname = NULL;
2330 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2332 b2118c49 2020-07-28 stsp return err;
2334 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2335 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2336 0647c563 2019-03-11 stsp *refname = NULL;
2338 517bab73 2019-03-11 stsp free(uuidstr);
2339 517bab73 2019-03-11 stsp return err;
2342 818c7501 2019-07-11 stsp const struct got_error *
2343 9587e6cc 2023-01-28 mark got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2344 9587e6cc 2023-01-28 mark const char *prefix)
2346 9587e6cc 2023-01-28 mark return get_ref_name(refname, worktree, prefix);
2349 9587e6cc 2023-01-28 mark const struct got_error *
2350 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2352 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2355 818c7501 2019-07-11 stsp static const struct got_error *
2356 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2358 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2359 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2362 818c7501 2019-07-11 stsp static const struct got_error *
2363 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2365 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2368 818c7501 2019-07-11 stsp static const struct got_error *
2369 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2371 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2372 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2375 818c7501 2019-07-11 stsp static const struct got_error *
2376 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2378 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2379 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2382 0ebf8283 2019-07-24 stsp static const struct got_error *
2383 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2385 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2386 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2389 0ebf8283 2019-07-24 stsp static const struct got_error *
2390 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2392 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2393 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2396 0ebf8283 2019-07-24 stsp static const struct got_error *
2397 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2399 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2400 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2403 0ebf8283 2019-07-24 stsp static const struct got_error *
2404 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2406 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2407 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2410 0ebf8283 2019-07-24 stsp const struct got_error *
2411 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2412 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2414 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2415 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2416 0ebf8283 2019-07-24 stsp *path = NULL;
2417 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2419 0ebf8283 2019-07-24 stsp return NULL;
2422 f259c4c1 2021-09-24 stsp static const struct got_error *
2423 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2425 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2426 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2429 f259c4c1 2021-09-24 stsp static const struct got_error *
2430 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2432 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2433 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2437 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2438 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2440 517bab73 2019-03-11 stsp static const struct got_error *
2441 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2443 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2444 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2445 517bab73 2019-03-11 stsp char *refname;
2447 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2449 517bab73 2019-03-11 stsp return err;
2451 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2453 0cd1c46a 2019-03-11 stsp goto done;
2455 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2457 0cd1c46a 2019-03-11 stsp free(refname);
2459 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2460 3e3a69f1 2019-07-25 stsp return err;
2463 3e3a69f1 2019-07-25 stsp static const struct got_error *
2464 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2466 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2468 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2469 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2470 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2471 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2473 9d31a1d8 2018-03-11 stsp return err;
2477 ebf99748 2019-05-09 stsp static const struct got_error *
2478 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2479 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2481 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2482 ebf99748 2019-05-09 stsp FILE *index = NULL;
2484 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2485 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2486 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2487 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2489 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2491 ebf99748 2019-05-09 stsp goto done;
2493 00fe21f2 2021-12-31 stsp index = fopen(*fileindex_path, "rbe");
2494 ebf99748 2019-05-09 stsp if (index == NULL) {
2495 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2496 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2498 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2499 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2500 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2503 ebf99748 2019-05-09 stsp if (err) {
2504 ebf99748 2019-05-09 stsp free(*fileindex_path);
2505 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2506 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2507 ebf99748 2019-05-09 stsp *fileindex = NULL;
2509 ebf99748 2019-05-09 stsp return err;
2512 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2513 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2514 c932eeeb 2019-05-22 stsp const char *path;
2515 c932eeeb 2019-05-22 stsp size_t path_len;
2516 c932eeeb 2019-05-22 stsp const char *entry_name;
2517 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2518 a484d721 2019-06-10 stsp void *progress_arg;
2521 c932eeeb 2019-05-22 stsp static const struct got_error *
2522 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2524 1ee397ad 2019-07-12 stsp const struct got_error *err;
2525 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2527 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2528 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2529 c932eeeb 2019-05-22 stsp return NULL;
2530 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2531 102ff934 2019-06-11 stsp return NULL;
2533 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2534 a769b60b 2021-06-27 stsp return NULL;
2536 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2537 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2538 c932eeeb 2019-05-22 stsp return NULL;
2540 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2541 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2542 edd02c5e 2019-07-11 stsp ie->path);
2544 1ee397ad 2019-07-12 stsp return err;
2546 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2547 c932eeeb 2019-05-22 stsp return NULL;
2550 b0a0c9ed 2023-02-06 op /* Bump base commit ID of all files within an updated part of the work tree. */
2551 a615e0e7 2020-12-16 stsp static const struct got_error *
2552 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2553 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2554 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2556 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2558 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2559 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2560 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2561 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2562 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2563 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2565 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2566 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2569 9c6338c4 2019-06-02 stsp static const struct got_error *
2570 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2572 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2573 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2574 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2575 867630bb 2020-01-17 stsp struct timespec timeout;
2577 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2578 b90054ed 2022-11-01 stsp fileindex_path, "");
2580 9c6338c4 2019-06-02 stsp goto done;
2582 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2584 9c6338c4 2019-06-02 stsp goto done;
2586 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2587 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2588 9c6338c4 2019-06-02 stsp fileindex_path);
2589 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2593 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2594 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2595 867630bb 2020-01-17 stsp * was recorded in the file index.
2597 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2598 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2599 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2601 9c6338c4 2019-06-02 stsp if (new_index)
2602 9c6338c4 2019-06-02 stsp fclose(new_index);
2603 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2604 9c6338c4 2019-06-02 stsp return err;
2607 6ced4176 2019-07-12 stsp static const struct got_error *
2608 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2609 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2610 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit, struct got_worktree *worktree,
2611 a44927cc 2022-04-07 stsp struct got_repository *repo)
2613 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2614 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2615 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2616 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2618 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2619 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2620 6ced4176 2019-07-12 stsp *tree_id = NULL;
2622 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2623 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2624 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2625 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2626 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2627 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2628 6ced4176 2019-07-12 stsp goto done;
2630 a44927cc 2022-04-07 stsp err = got_object_id_by_path(tree_id, repo, base_commit,
2631 a44927cc 2022-04-07 stsp worktree->path_prefix);
2633 6ced4176 2019-07-12 stsp goto done;
2634 6ced4176 2019-07-12 stsp return NULL;
2637 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2639 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2640 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2641 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2642 6ced4176 2019-07-12 stsp goto done;
2645 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2647 6ced4176 2019-07-12 stsp goto done;
2649 6ced4176 2019-07-12 stsp free(in_repo_path);
2650 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2652 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2654 6ced4176 2019-07-12 stsp goto done;
2656 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2657 6ced4176 2019-07-12 stsp /* Check out a single file. */
2658 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2659 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2660 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2661 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2662 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2663 6ced4176 2019-07-12 stsp goto done;
2665 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2666 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2667 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2668 6ced4176 2019-07-12 stsp goto done;
2671 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2672 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2674 6ced4176 2019-07-12 stsp return err;
2675 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2676 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2677 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2678 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2679 6ced4176 2019-07-12 stsp goto done;
2682 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2683 a44927cc 2022-04-07 stsp base_commit, in_repo_path);
2685 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2686 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2687 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2688 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2689 6ced4176 2019-07-12 stsp goto done;
2691 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2692 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2693 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2694 6ced4176 2019-07-12 stsp goto done;
2699 6ced4176 2019-07-12 stsp free(in_repo_path);
2700 6ced4176 2019-07-12 stsp if (err) {
2701 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2702 6ced4176 2019-07-12 stsp free(*tree_relpath);
2703 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2704 6ced4176 2019-07-12 stsp free(*tree_id);
2705 6ced4176 2019-07-12 stsp *tree_id = NULL;
2707 07ed472d 2019-07-12 stsp return err;
2710 07ed472d 2019-07-12 stsp static const struct got_error *
2711 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2712 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2713 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2714 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2716 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2717 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2718 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2719 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2720 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2722 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2723 7f47418f 2019-12-20 stsp if (err) {
2724 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2725 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2726 7f47418f 2019-12-20 stsp goto done;
2727 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2728 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2730 7f47418f 2019-12-20 stsp return err;
2733 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2734 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2736 07ed472d 2019-07-12 stsp goto done;
2738 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2740 07ed472d 2019-07-12 stsp goto done;
2742 07ed472d 2019-07-12 stsp if (entry_name &&
2743 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2744 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2745 07ed472d 2019-07-12 stsp goto done;
2748 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2749 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2750 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2751 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2752 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2753 07ed472d 2019-07-12 stsp arg.repo = repo;
2754 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2755 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2756 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2757 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2758 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2759 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2762 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2763 07ed472d 2019-07-12 stsp if (commit)
2764 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2765 6ced4176 2019-07-12 stsp return err;
2768 9d31a1d8 2018-03-11 stsp const struct got_error *
2769 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2770 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2771 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2772 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2774 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2775 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2776 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2777 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2778 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2779 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2780 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2781 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2782 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2783 f2ea84fa 2019-07-27 stsp int entry_type;
2784 f2ea84fa 2019-07-27 stsp char *relpath;
2785 f2ea84fa 2019-07-27 stsp char *entry_name;
2786 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2787 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2789 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2791 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2793 9d31a1d8 2018-03-11 stsp return err;
2795 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
2796 a44927cc 2022-04-07 stsp worktree->base_commit_id);
2798 a44927cc 2022-04-07 stsp goto done;
2800 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2801 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2802 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2803 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2804 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2805 f2ea84fa 2019-07-27 stsp goto done;
2808 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2809 a44927cc 2022-04-07 stsp &tpd->relpath, &tpd->tree_id, pe->path, commit,
2810 a44927cc 2022-04-07 stsp worktree, repo);
2811 f2ea84fa 2019-07-27 stsp if (err) {
2812 f2ea84fa 2019-07-27 stsp free(tpd);
2813 6ced4176 2019-07-12 stsp goto done;
2816 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2817 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2818 f2ea84fa 2019-07-27 stsp if (err) {
2819 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2820 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2821 f2ea84fa 2019-07-27 stsp free(tpd);
2822 f2ea84fa 2019-07-27 stsp goto done;
2825 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2827 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2831 51514078 2018-12-25 stsp * Read the file index.
2832 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2833 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2835 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2837 c4cdcb68 2019-04-03 stsp goto done;
2839 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2840 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2841 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2843 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2844 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2845 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2849 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2850 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2851 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2852 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2853 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2854 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2855 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2856 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2860 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2862 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2863 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2864 af12c6b9 2019-06-04 stsp err = sync_err;
2866 9c6338c4 2019-06-02 stsp free(fileindex_path);
2868 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2869 9d31a1d8 2018-03-11 stsp if (commit)
2870 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2871 5ade8233 2019-07-12 stsp if (fileindex)
2872 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2873 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2874 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2875 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2876 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2877 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2878 f2ea84fa 2019-07-27 stsp free(tpd);
2880 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2881 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2882 9d31a1d8 2018-03-11 stsp err = unlockerr;
2883 f8d1f275 2019-02-04 stsp return err;
2886 9a298e5c 2023-03-10 stsp static const struct got_error *
2887 9a298e5c 2023-03-10 stsp add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2888 9a298e5c 2023-03-10 stsp struct got_fileindex_entry *ie, const char *ondisk_path,
2889 9a298e5c 2023-03-10 stsp const char *path2, struct got_blob_object *blob2, mode_t mode2,
2890 9a298e5c 2023-03-10 stsp int restoring_missing_file, int reverting_versioned_file,
2891 9a298e5c 2023-03-10 stsp int path_is_unversioned, int allow_bad_symlinks,
2892 9a298e5c 2023-03-10 stsp struct got_repository *repo,
2893 9a298e5c 2023-03-10 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2895 9a298e5c 2023-03-10 stsp const struct got_error *err = NULL;
2896 9a298e5c 2023-03-10 stsp int is_bad_symlink = 0;
2898 9a298e5c 2023-03-10 stsp if (S_ISLNK(mode2)) {
2899 9a298e5c 2023-03-10 stsp err = install_symlink(&is_bad_symlink,
2900 9a298e5c 2023-03-10 stsp worktree, ondisk_path, path2, blob2,
2901 9a298e5c 2023-03-10 stsp restoring_missing_file,
2902 9a298e5c 2023-03-10 stsp reverting_versioned_file,
2903 9a298e5c 2023-03-10 stsp path_is_unversioned, allow_bad_symlinks,
2904 9a298e5c 2023-03-10 stsp repo, progress_cb, progress_arg);
2906 9a298e5c 2023-03-10 stsp err = install_blob(worktree, ondisk_path, path2,
2907 9a298e5c 2023-03-10 stsp mode2, GOT_DEFAULT_FILE_MODE, blob2,
2908 9a298e5c 2023-03-10 stsp restoring_missing_file, reverting_versioned_file, 0,
2909 9a298e5c 2023-03-10 stsp path_is_unversioned, repo, progress_cb, progress_arg);
2912 9a298e5c 2023-03-10 stsp return err;
2913 9a298e5c 2023-03-10 stsp if (ie == NULL) {
2914 9a298e5c 2023-03-10 stsp /* Adding an unversioned file. */
2915 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_alloc(&ie, path2);
2917 9a298e5c 2023-03-10 stsp return err;
2918 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_update(ie,
2919 9a298e5c 2023-03-10 stsp worktree->root_fd, path2, NULL, NULL, 1);
2920 9a298e5c 2023-03-10 stsp if (err) {
2921 9a298e5c 2023-03-10 stsp got_fileindex_entry_free(ie);
2922 9a298e5c 2023-03-10 stsp return err;
2924 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_add(fileindex, ie);
2925 9a298e5c 2023-03-10 stsp if (err) {
2926 9a298e5c 2023-03-10 stsp got_fileindex_entry_free(ie);
2927 9a298e5c 2023-03-10 stsp return err;
2930 9a298e5c 2023-03-10 stsp /* Re-adding a locally deleted file. */
2931 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_update(ie,
2932 9a298e5c 2023-03-10 stsp worktree->root_fd, path2, ie->blob_sha1,
2933 9a298e5c 2023-03-10 stsp worktree->base_commit_id->sha1, 0);
2935 9a298e5c 2023-03-10 stsp return err;
2938 9a298e5c 2023-03-10 stsp if (is_bad_symlink) {
2939 9a298e5c 2023-03-10 stsp got_fileindex_entry_filetype_set(ie,
2940 9a298e5c 2023-03-10 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2943 9a298e5c 2023-03-10 stsp return NULL;
2946 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2947 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2948 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2949 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2950 234035bc 2019-06-01 stsp void *progress_arg;
2951 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2952 234035bc 2019-06-01 stsp void *cancel_arg;
2953 f69721c3 2019-10-21 stsp const char *label_orig;
2954 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2955 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2958 234035bc 2019-06-01 stsp static const struct got_error *
2959 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2960 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
2961 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
2962 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
2963 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2965 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2966 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2967 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2968 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2969 234035bc 2019-06-01 stsp struct stat sb;
2970 234035bc 2019-06-01 stsp unsigned char status;
2971 234035bc 2019-06-01 stsp int local_changes_subsumed;
2972 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2973 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2975 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2976 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2977 d6c87207 2019-08-02 stsp strlen(path2));
2978 1ee397ad 2019-07-12 stsp if (ie == NULL)
2979 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2980 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2982 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2983 234035bc 2019-06-01 stsp path2) == -1)
2984 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2986 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2989 234035bc 2019-06-01 stsp goto done;
2991 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2992 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2993 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2994 234035bc 2019-06-01 stsp goto done;
2996 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2997 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2998 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2999 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3000 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3001 234035bc 2019-06-01 stsp goto done;
3004 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3005 36bf999c 2020-07-23 stsp char *link_target2;
3006 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3008 36bf999c 2020-07-23 stsp goto done;
3009 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3010 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3011 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3012 36bf999c 2020-07-23 stsp free(link_target2);
3016 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3017 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3018 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3019 1af628f4 2021-06-03 stsp goto done;
3021 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3022 1af628f4 2021-06-03 stsp f_orig, blob1);
3024 1af628f4 2021-06-03 stsp goto done;
3026 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3027 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3028 1af628f4 2021-06-03 stsp goto done;
3029 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3030 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3032 1af628f4 2021-06-03 stsp goto done;
3034 c0df5966 2021-12-31 stsp fd = open(ondisk_path,
3035 c0df5966 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3036 1af628f4 2021-06-03 stsp if (fd == -1) {
3037 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3038 1af628f4 2021-06-03 stsp ondisk_path);
3039 1af628f4 2021-06-03 stsp goto done;
3041 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3042 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3043 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3044 1af628f4 2021-06-03 stsp ondisk_path);
3045 1af628f4 2021-06-03 stsp close(fd);
3046 1af628f4 2021-06-03 stsp goto done;
3048 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3050 1af628f4 2021-06-03 stsp goto done;
3051 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3052 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3053 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3054 1af628f4 2021-06-03 stsp goto done;
3056 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3057 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3058 c48f94a4 2023-01-29 stsp mode2, a->label_orig, NULL, label_deriv2,
3059 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3060 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3062 234035bc 2019-06-01 stsp } else if (blob1) {
3063 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3064 d6c87207 2019-08-02 stsp strlen(path1));
3065 1ee397ad 2019-07-12 stsp if (ie == NULL)
3066 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3067 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3069 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3070 2b92fad7 2019-06-02 stsp path1) == -1)
3071 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3073 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3076 2b92fad7 2019-06-02 stsp goto done;
3078 2b92fad7 2019-06-02 stsp switch (status) {
3079 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3080 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3081 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3083 1ee397ad 2019-07-12 stsp goto done;
3084 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3086 2b92fad7 2019-06-02 stsp goto done;
3088 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3090 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3091 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3092 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3093 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3095 1ee397ad 2019-07-12 stsp goto done;
3097 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3099 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3100 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3101 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3102 72840534 2022-01-19 stsp off_t blob1_size;
3104 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3105 0a22ca1a 2020-09-23 stsp * exists in the repository.
3107 72840534 2022-01-19 stsp err = got_object_blob_file_create(&id, &blob1_f,
3108 72840534 2022-01-19 stsp &blob1_size, path1);
3110 0a22ca1a 2020-09-23 stsp goto done;
3111 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3112 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3113 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3115 0a22ca1a 2020-09-23 stsp goto done;
3116 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3119 0a22ca1a 2020-09-23 stsp goto done;
3121 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3124 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3125 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3127 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3128 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3131 0a22ca1a 2020-09-23 stsp goto done;
3134 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3135 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3136 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3137 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3139 1ee397ad 2019-07-12 stsp goto done;
3141 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3142 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3144 1ee397ad 2019-07-12 stsp goto done;
3149 234035bc 2019-06-01 stsp } else if (blob2) {
3150 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3151 234035bc 2019-06-01 stsp path2) == -1)
3152 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3153 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3154 d6c87207 2019-08-02 stsp strlen(path2));
3156 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3157 7f91a133 2019-12-13 stsp -1, NULL, repo);
3159 234035bc 2019-06-01 stsp goto done;
3160 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3161 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3162 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3163 9a298e5c 2023-03-10 stsp status != GOT_STATUS_ADD &&
3164 9a298e5c 2023-03-10 stsp status != GOT_STATUS_DELETE) {
3165 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3166 1ee397ad 2019-07-12 stsp status, path2);
3167 234035bc 2019-06-01 stsp goto done;
3169 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3170 36bf999c 2020-07-23 stsp char *link_target2;
3171 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3174 36bf999c 2020-07-23 stsp goto done;
3175 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3176 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3177 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3178 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3179 36bf999c 2020-07-23 stsp free(link_target2);
3180 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3181 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3182 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3183 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3184 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3185 526a746f 2020-07-23 stsp a->progress_arg);
3186 9a298e5c 2023-03-10 stsp } else if (status != GOT_STATUS_DELETE) {
3187 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3188 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3191 dfe9fba0 2020-07-23 stsp goto done;
3192 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3193 9a298e5c 2023-03-10 stsp /* Re-add file with content from new blob. */
3194 9a298e5c 2023-03-10 stsp err = add_file(a->worktree, a->fileindex, ie,
3195 9a298e5c 2023-03-10 stsp ondisk_path, path2, blob2, mode2,
3196 9a298e5c 2023-03-10 stsp 0, 0, 0, a->allow_bad_symlinks,
3197 9a298e5c 2023-03-10 stsp repo, a->progress_cb, a->progress_arg);
3199 234035bc 2019-06-01 stsp goto done;
3202 9a298e5c 2023-03-10 stsp err = add_file(a->worktree, a->fileindex, NULL,
3203 9a298e5c 2023-03-10 stsp ondisk_path, path2, blob2, mode2,
3204 9a298e5c 2023-03-10 stsp 0, 0, 1, a->allow_bad_symlinks,
3205 9a298e5c 2023-03-10 stsp repo, a->progress_cb, a->progress_arg);
3207 2b92fad7 2019-06-02 stsp goto done;
3211 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3212 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3213 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3214 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3215 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3216 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3217 1af628f4 2021-06-03 stsp free(id_str);
3218 54d5be07 2021-06-03 stsp free(label_deriv2);
3219 234035bc 2019-06-01 stsp free(ondisk_path);
3220 234035bc 2019-06-01 stsp return err;
3223 69de9dd4 2021-09-03 stsp static const struct got_error *
3224 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3226 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3228 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3229 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3230 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3231 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3232 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3234 69de9dd4 2021-09-03 stsp return NULL;
3237 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3238 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3239 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3240 234035bc 2019-06-01 stsp struct got_repository *repo;
3243 234035bc 2019-06-01 stsp static const struct got_error *
3244 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3245 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3246 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3247 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3248 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3250 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3251 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3252 234035bc 2019-06-01 stsp unsigned char status;
3253 234035bc 2019-06-01 stsp struct stat sb;
3254 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3255 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3256 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3257 234035bc 2019-06-01 stsp char *ondisk_path;
3259 69de9dd4 2021-09-03 stsp if (id == NULL)
3260 69de9dd4 2021-09-03 stsp return NULL;
3262 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3263 69de9dd4 2021-09-03 stsp if (ie == NULL)
3264 69de9dd4 2021-09-03 stsp return NULL;
3266 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3268 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3270 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3271 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3272 5546d466 2021-09-02 stsp free(ondisk_path);
3274 234035bc 2019-06-01 stsp return err;
3275 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3276 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3278 234035bc 2019-06-01 stsp return NULL;
3281 818c7501 2019-07-11 stsp static const struct got_error *
3282 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3283 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3284 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3285 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3286 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3288 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3289 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3290 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3291 a44927cc 2022-04-07 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3292 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3293 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3294 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3295 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
3296 f9d37699 2022-06-28 stsp int fd1 = -1, fd2 = -1;
3298 03415a1a 2019-06-02 stsp if (commit_id1) {
3299 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit1, repo, commit_id1);
3301 a44927cc 2022-04-07 stsp goto done;
3302 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id1, repo, commit1,
3303 03415a1a 2019-06-02 stsp worktree->path_prefix);
3304 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3305 03415a1a 2019-06-02 stsp goto done;
3307 69d57f3d 2020-07-31 stsp if (tree_id1) {
3308 69d57f3d 2020-07-31 stsp char *id_str;
3310 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3312 03415a1a 2019-06-02 stsp goto done;
3314 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3316 f69721c3 2019-10-21 stsp goto done;
3318 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3319 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3320 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3321 f69721c3 2019-10-21 stsp free(id_str);
3322 f69721c3 2019-10-21 stsp goto done;
3324 f69721c3 2019-10-21 stsp free(id_str);
3326 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3327 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3328 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3329 b72706c3 2022-06-01 stsp goto done;
3333 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit2, repo, commit_id2);
3335 a44927cc 2022-04-07 stsp goto done;
3337 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id2, repo, commit2,
3338 234035bc 2019-06-01 stsp worktree->path_prefix);
3340 234035bc 2019-06-01 stsp goto done;
3342 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3344 234035bc 2019-06-01 stsp goto done;
3346 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3347 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3348 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3349 f9d37699 2022-06-28 stsp goto done;
3352 f9d37699 2022-06-28 stsp fd1 = got_opentempfd();
3353 f9d37699 2022-06-28 stsp if (fd1 == -1) {
3354 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3355 b72706c3 2022-06-01 stsp goto done;
3358 f9d37699 2022-06-28 stsp fd2 = got_opentempfd();
3359 f9d37699 2022-06-28 stsp if (fd2 == -1) {
3360 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3361 f9d37699 2022-06-28 stsp goto done;
3364 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3365 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3366 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3367 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3368 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3370 69de9dd4 2021-09-03 stsp goto done;
3372 234035bc 2019-06-01 stsp arg.worktree = worktree;
3373 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3374 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3375 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3376 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3377 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3378 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3379 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3380 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3381 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3382 b72706c3 2022-06-01 stsp merge_file_cb, &arg, 1);
3383 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3384 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3385 af12c6b9 2019-06-04 stsp err = sync_err;
3387 a44927cc 2022-04-07 stsp if (commit1)
3388 a44927cc 2022-04-07 stsp got_object_commit_close(commit1);
3389 a44927cc 2022-04-07 stsp if (commit2)
3390 a44927cc 2022-04-07 stsp got_object_commit_close(commit2);
3391 234035bc 2019-06-01 stsp if (tree1)
3392 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3393 234035bc 2019-06-01 stsp if (tree2)
3394 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3395 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3396 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3397 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3398 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3399 f9d37699 2022-06-28 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3400 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3401 f9d37699 2022-06-28 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3402 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3403 f69721c3 2019-10-21 stsp free(label_orig);
3404 818c7501 2019-07-11 stsp return err;
3407 818c7501 2019-07-11 stsp const struct got_error *
3408 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3409 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3410 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3411 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3413 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3414 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3415 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3417 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3419 818c7501 2019-07-11 stsp return err;
3421 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3423 818c7501 2019-07-11 stsp goto done;
3425 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3426 69de9dd4 2021-09-03 stsp worktree);
3428 818c7501 2019-07-11 stsp goto done;
3430 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3431 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3432 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3434 818c7501 2019-07-11 stsp if (fileindex)
3435 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3436 818c7501 2019-07-11 stsp free(fileindex_path);
3437 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3438 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3439 234035bc 2019-06-01 stsp err = unlockerr;
3440 234035bc 2019-06-01 stsp return err;
3443 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3444 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3445 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3446 927df6b7 2019-02-10 stsp const char *status_path;
3447 927df6b7 2019-02-10 stsp size_t status_path_len;
3448 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3449 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3450 f8d1f275 2019-02-04 stsp void *status_arg;
3451 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3452 f8d1f275 2019-02-04 stsp void *cancel_arg;
3453 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3454 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3455 f2a9dc41 2019-12-13 tracey int report_unchanged;
3456 3143d852 2020-06-25 stsp int no_ignores;
3459 f8d1f275 2019-02-04 stsp static const struct got_error *
3460 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3461 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3462 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3463 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3465 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3466 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3467 2c4740ad 2023-02-12 mark unsigned char staged_status;
3468 927df6b7 2019-02-10 stsp struct stat sb;
3469 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3470 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3471 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3473 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
3474 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3476 98eaaa12 2019-08-03 stsp return err;
3478 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3479 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3480 98eaaa12 2019-08-03 stsp return NULL;
3482 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
3483 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3484 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
3485 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3486 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3487 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3488 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3489 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
3492 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3493 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3496 927df6b7 2019-02-10 stsp static const struct got_error *
3497 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3498 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3500 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3501 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3502 f8d1f275 2019-02-04 stsp char *abspath;
3504 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3505 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3507 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3508 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3509 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3510 927df6b7 2019-02-10 stsp return NULL;
3512 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3513 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3514 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3515 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3517 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3518 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3519 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3522 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3523 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3524 f8d1f275 2019-02-04 stsp free(abspath);
3525 9d31a1d8 2018-03-11 stsp return err;
3528 f8d1f275 2019-02-04 stsp static const struct got_error *
3529 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3531 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3532 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3533 2ec1f75b 2019-03-26 stsp unsigned char status;
3535 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3536 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3538 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3539 927df6b7 2019-02-10 stsp return NULL;
3541 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&blob_id, ie);
3542 b4b2adf5 2023-02-09 op got_fileindex_entry_get_commit_id(&commit_id, ie);
3543 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3544 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3546 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3547 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3548 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3552 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3554 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3556 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3557 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3559 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3561 d8bacb93 2023-01-10 mark got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3564 6841da00 2019-08-08 stsp static const struct got_error *
3565 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3567 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3568 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3569 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3570 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3571 6841da00 2019-08-08 stsp size_t linesize = 0;
3572 6841da00 2019-08-08 stsp ssize_t linelen;
3574 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3575 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3576 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3577 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3579 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3580 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3581 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3583 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3584 bd8de430 2019-10-04 stsp if (line[0] == '#')
3587 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3588 bd8de430 2019-10-04 stsp if (line[0] == '!')
3591 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3592 6841da00 2019-08-08 stsp line) == -1) {
3593 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3594 6841da00 2019-08-08 stsp goto done;
3596 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3598 6841da00 2019-08-08 stsp goto done;
3600 6841da00 2019-08-08 stsp if (ferror(f)) {
3601 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3602 6841da00 2019-08-08 stsp goto done;
3605 6841da00 2019-08-08 stsp dirpath = strdup(path);
3606 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3607 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3608 6841da00 2019-08-08 stsp goto done;
3610 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3612 6841da00 2019-08-08 stsp free(line);
3613 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3614 6841da00 2019-08-08 stsp free(dirpath);
3615 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3617 6841da00 2019-08-08 stsp return err;
3620 249b637c 2023-02-20 stsp static int
3621 249b637c 2023-02-20 stsp match_path(const char *pattern, size_t pattern_len, const char *path,
3622 249b637c 2023-02-20 stsp int flags)
3624 249b637c 2023-02-20 stsp char buf[PATH_MAX];
3627 249b637c 2023-02-20 stsp * Trailing slashes signify directories.
3628 249b637c 2023-02-20 stsp * Append a * to make such patterns conform to fnmatch rules.
3630 249b637c 2023-02-20 stsp if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3631 249b637c 2023-02-20 stsp if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3632 249b637c 2023-02-20 stsp return FNM_NOMATCH; /* XXX */
3634 249b637c 2023-02-20 stsp return fnmatch(buf, path, flags);
3637 249b637c 2023-02-20 stsp return fnmatch(pattern, path, flags);
3641 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3643 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3645 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3646 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3647 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3648 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3650 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3651 249b637c 2023-02-20 stsp const char *p;
3653 249b637c 2023-02-20 stsp if (pi->path_len < 3 ||
3654 249b637c 2023-02-20 stsp strncmp(pi->path, "**/", 3) != 0)
3657 bd8de430 2019-10-04 stsp while (*p) {
3658 249b637c 2023-02-20 stsp if (match_path(pi->path + 3,
3659 249b637c 2023-02-20 stsp pi->path_len - 3, p,
3660 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3661 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3662 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3664 bd8de430 2019-10-04 stsp while (*p == '/')
3674 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3675 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3676 6841da00 2019-08-08 stsp * ignores backwards.
3678 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3679 6841da00 2019-08-08 stsp while (pe) {
3680 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3681 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3682 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3683 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3684 249b637c 2023-02-20 stsp int flags = FNM_LEADING_DIR;
3685 249b637c 2023-02-20 stsp if (strstr(pi->path, "/**/") == NULL)
3686 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3687 249b637c 2023-02-20 stsp if (match_path(pi->path, pi->path_len,
3688 249b637c 2023-02-20 stsp path, flags))
3693 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3699 6841da00 2019-08-08 stsp static const struct got_error *
3700 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3701 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3703 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3704 6841da00 2019-08-08 stsp char *ignorespath;
3705 886cec17 2019-12-15 stsp int fd = -1;
3706 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3708 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3709 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3710 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3712 886cec17 2019-12-15 stsp if (dirfd != -1) {
3713 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3714 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3715 886cec17 2019-12-15 stsp if (fd == -1) {
3716 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3717 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3718 886cec17 2019-12-15 stsp ignorespath);
3720 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3721 5e91dae4 2022-08-30 stsp if (ignoresfile == NULL)
3722 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3723 886cec17 2019-12-15 stsp ignorespath);
3726 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3730 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3731 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3732 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3733 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3734 886cec17 2019-12-15 stsp ignorespath);
3736 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3739 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3740 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3741 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3742 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3743 6841da00 2019-08-08 stsp free(ignorespath);
3744 6841da00 2019-08-08 stsp return err;
3747 f8d1f275 2019-02-04 stsp static const struct got_error *
3748 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3749 62da3196 2021-10-01 stsp int dirfd)
3751 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3752 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3753 f8d1f275 2019-02-04 stsp char *path = NULL;
3755 62da3196 2021-10-01 stsp if (ignore != NULL)
3756 62da3196 2021-10-01 stsp *ignore = 0;
3758 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3759 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3761 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3762 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3763 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3765 f8d1f275 2019-02-04 stsp path = de->d_name;
3768 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3769 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3770 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3771 62da3196 2021-10-01 stsp *ignore = 1;
3772 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3773 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3774 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3775 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3776 f8d1f275 2019-02-04 stsp if (parent_path[0])
3777 f8d1f275 2019-02-04 stsp free(path);
3778 b72f483a 2019-02-05 stsp return err;
3781 347d1d3e 2019-07-12 stsp static const struct got_error *
3782 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3784 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3785 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3787 3143d852 2020-06-25 stsp if (a->no_ignores)
3788 3143d852 2020-06-25 stsp return NULL;
3790 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3791 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3793 3143d852 2020-06-25 stsp return err;
3795 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3796 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3798 3143d852 2020-06-25 stsp return err;
3801 3143d852 2020-06-25 stsp static const struct got_error *
3802 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3803 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3804 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3805 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3807 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3808 abb4604f 2019-07-27 stsp struct stat sb;
3810 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3812 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3813 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3815 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3816 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3817 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3818 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3819 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3822 916237f3 2022-02-11 stsp if (!no_ignores && match_ignores(ignores, path))
3823 916237f3 2022-02-11 stsp return NULL;
3825 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3826 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3827 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3829 abb4604f 2019-07-27 stsp return NULL;
3832 3143d852 2020-06-25 stsp static const struct got_error *
3833 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3834 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3836 3143d852 2020-06-25 stsp const struct got_error *err;
3837 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3839 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3840 3143d852 2020-06-25 stsp ".cvsignore");
3842 3143d852 2020-06-25 stsp return err;
3844 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3845 3143d852 2020-06-25 stsp ".gitignore");
3847 3143d852 2020-06-25 stsp return err;
3849 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3850 3143d852 2020-06-25 stsp if (err) {
3851 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3852 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3853 3143d852 2020-06-25 stsp return err;
3855 3143d852 2020-06-25 stsp for (;;) {
3856 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3857 3143d852 2020-06-25 stsp ".cvsignore");
3860 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3861 3143d852 2020-06-25 stsp ".gitignore");
3864 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3865 3143d852 2020-06-25 stsp if (err) {
3866 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3867 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3870 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3872 b737c85a 2020-06-26 stsp free(parent_path);
3873 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3874 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3877 b737c85a 2020-06-26 stsp free(parent_path);
3878 b737c85a 2020-06-26 stsp free(next_parent_path);
3879 3143d852 2020-06-25 stsp return err;
3882 abb4604f 2019-07-27 stsp static const struct got_error *
3883 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3884 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3885 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3886 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3887 f2a9dc41 2019-12-13 tracey int report_unchanged)
3889 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3890 6fc93f37 2019-12-13 stsp int fd = -1;
3891 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3892 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3893 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3894 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3895 a84c0d30 2022-03-12 stsp struct got_fileindex_entry *ie;
3897 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3899 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3900 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3901 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3903 a84c0d30 2022-03-12 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3905 a84c0d30 2022-03-12 stsp err = report_single_file_status(path, ondisk_path,
3906 a84c0d30 2022-03-12 stsp fileindex, status_cb, status_arg, repo,
3907 a84c0d30 2022-03-12 stsp report_unchanged, &ignores, no_ignores);
3908 a84c0d30 2022-03-12 stsp goto done;
3911 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3912 6fc93f37 2019-12-13 stsp if (fd == -1) {
3913 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3914 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
3915 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3917 ff56836b 2021-07-08 stsp if (!no_ignores) {
3918 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3919 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3921 ff56836b 2021-07-08 stsp goto done;
3923 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3924 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3925 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3928 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3929 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3930 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3931 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3932 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3933 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3934 abb4604f 2019-07-27 stsp arg.status_path = path;
3935 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3936 abb4604f 2019-07-27 stsp arg.repo = repo;
3937 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3938 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3939 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3940 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3941 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3942 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3943 022fae89 2019-12-06 tracey if (!no_ignores) {
3944 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3945 3143d852 2020-06-25 stsp worktree->root_path, path);
3947 3143d852 2020-06-25 stsp goto done;
3949 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3950 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3951 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3954 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3955 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3956 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3957 927df6b7 2019-02-10 stsp free(ondisk_path);
3958 347d1d3e 2019-07-12 stsp return err;
3961 347d1d3e 2019-07-12 stsp const struct got_error *
3962 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3963 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3964 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3965 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3967 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3968 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3969 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3970 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3972 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3974 347d1d3e 2019-07-12 stsp return err;
3976 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3977 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3978 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3979 f6343036 2021-06-22 stsp no_ignores, 0);
3983 f8d1f275 2019-02-04 stsp free(fileindex_path);
3984 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3985 f8d1f275 2019-02-04 stsp return err;
3988 6c7ab921 2019-03-18 stsp const struct got_error *
3989 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3990 6c7ab921 2019-03-18 stsp const char *arg)
3992 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3993 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3994 6c7ab921 2019-03-18 stsp size_t len;
3995 00bb5ea0 2020-07-23 stsp struct stat sb;
3996 01740607 2020-11-04 stsp char *abspath = NULL;
3997 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3999 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4001 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4002 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4003 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4005 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4006 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4007 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4008 00bb5ea0 2020-07-23 stsp goto done;
4010 727173c3 2020-11-06 stsp sb.st_mode = 0;
4012 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4014 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4015 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4016 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4017 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4019 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4020 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4021 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4022 00bb5ea0 2020-07-23 stsp goto done;
4026 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4027 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4029 d0710d08 2019-07-22 stsp goto done;
4030 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4031 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4032 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4033 00bb5ea0 2020-07-23 stsp goto done;
4036 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4037 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4038 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4039 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4040 00bb5ea0 2020-07-23 stsp goto done;
4042 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4043 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4044 01740607 2020-11-04 stsp goto done;
4046 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4047 01740607 2020-11-04 stsp sizeof(canonpath));
4049 00bb5ea0 2020-07-23 stsp goto done;
4050 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4051 01740607 2020-11-04 stsp if (resolved == NULL) {
4052 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4053 01740607 2020-11-04 stsp goto done;
4058 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4059 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4060 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4061 6c7ab921 2019-03-18 stsp goto done;
4064 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4065 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4066 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4068 f6d88e1a 2019-05-29 stsp goto done;
4070 f6d88e1a 2019-05-29 stsp path = strdup("");
4071 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4072 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4073 f6d88e1a 2019-05-29 stsp goto done;
4077 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4078 6c7ab921 2019-03-18 stsp len = strlen(path);
4079 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4080 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4084 01740607 2020-11-04 stsp free(abspath);
4085 6c7ab921 2019-03-18 stsp free(resolved);
4086 d0710d08 2019-07-22 stsp free(cwd);
4087 6c7ab921 2019-03-18 stsp if (err == NULL)
4088 6c7ab921 2019-03-18 stsp *wt_path = path;
4090 6c7ab921 2019-03-18 stsp free(path);
4091 d00136be 2019-03-26 stsp return err;
4094 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4095 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4096 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4097 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4098 4e68cba3 2019-11-23 stsp void *progress_arg;
4099 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4102 4e68cba3 2019-11-23 stsp static const struct got_error *
4103 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4104 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4105 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4106 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4108 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4109 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4110 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4111 6d022e97 2019-08-04 stsp struct stat sb;
4112 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4114 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4115 dbb83fbd 2019-12-12 stsp relpath) == -1)
4116 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4118 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4120 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4121 12463d8b 2019-12-13 stsp de_name, a->repo);
4123 dbb83fbd 2019-12-12 stsp goto done;
4124 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4125 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
4126 dbb83fbd 2019-12-12 stsp goto done;
4127 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4129 dbb83fbd 2019-12-12 stsp goto done;
4132 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4133 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
4134 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
4136 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4137 dbb83fbd 2019-12-12 stsp goto done;
4140 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4142 4e68cba3 2019-11-23 stsp goto done;
4143 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4144 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4145 3969253a 2020-03-07 stsp if (err) {
4146 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4147 3969253a 2020-03-07 stsp goto done;
4149 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4150 07f5b47a 2019-06-02 stsp if (err) {
4151 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4152 4e68cba3 2019-11-23 stsp goto done;
4155 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4157 dbb83fbd 2019-12-12 stsp return err;
4158 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4159 dbb83fbd 2019-12-12 stsp return NULL;
4160 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4163 d00136be 2019-03-26 stsp const struct got_error *
4164 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4165 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4166 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4167 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4169 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4170 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4171 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4172 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4173 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4175 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4177 d00136be 2019-03-26 stsp return err;
4179 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4181 d00136be 2019-03-26 stsp goto done;
4183 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4184 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4185 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4186 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4187 4e68cba3 2019-11-23 stsp saa.repo = repo;
4189 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4190 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4191 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4195 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4196 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4197 af12c6b9 2019-06-04 stsp err = sync_err;
4199 fb399478 2019-07-12 stsp free(fileindex_path);
4200 d00136be 2019-03-26 stsp if (fileindex)
4201 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4202 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4203 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4204 d00136be 2019-03-26 stsp err = unlockerr;
4205 6c7ab921 2019-03-18 stsp return err;
4208 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4209 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4210 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4211 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4212 f2a9dc41 2019-12-13 tracey void *progress_arg;
4213 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4214 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4215 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4216 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4217 766841c2 2020-08-13 stsp const char *status_codes;
4220 f2a9dc41 2019-12-13 tracey static const struct got_error *
4221 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4222 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4223 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4224 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4226 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4227 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4228 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4229 17ed4618 2019-06-02 stsp struct stat sb;
4230 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4232 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4233 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4234 4e12cd97 2022-01-25 stsp return NULL;
4235 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4238 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4239 17ed4618 2019-06-02 stsp if (ie == NULL)
4240 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4242 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4243 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4244 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4245 9acbc4fa 2019-08-03 stsp return NULL;
4246 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4249 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4250 f2a9dc41 2019-12-13 tracey relpath) == -1)
4251 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4253 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4256 f2a9dc41 2019-12-13 tracey goto done;
4258 766841c2 2020-08-13 stsp if (a->status_codes) {
4259 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4261 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4262 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4265 5e91dae4 2022-08-30 stsp if (i == ncodes) {
4266 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4267 766841c2 2020-08-13 stsp free(ondisk_path);
4268 766841c2 2020-08-13 stsp return NULL;
4270 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4271 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4272 766841c2 2020-08-13 stsp static char msg[64];
4273 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4274 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4275 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4276 766841c2 2020-08-13 stsp goto done;
4280 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4281 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4282 f2a9dc41 2019-12-13 tracey goto done;
4283 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4284 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4285 f2a9dc41 2019-12-13 tracey goto done;
4287 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4288 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4289 4e12cd97 2022-01-25 stsp goto done;
4291 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4292 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4293 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4294 f2a9dc41 2019-12-13 tracey goto done;
4298 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4299 20a2ad1c 2020-10-20 stsp size_t root_len;
4301 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4302 a06ca3f7 2022-10-15 stsp if (unlinkat(dirfd, de_name, 0) == -1) {
4303 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4304 12463d8b 2019-12-13 stsp ondisk_path);
4305 12463d8b 2019-12-13 stsp goto done;
4307 a06ca3f7 2022-10-15 stsp } else if (unlink(ondisk_path) == -1) {
4308 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4309 12463d8b 2019-12-13 stsp goto done;
4312 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4314 20a2ad1c 2020-10-20 stsp char *parent;
4315 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4317 2513f20a 2020-10-20 stsp goto done;
4318 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4319 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4320 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4321 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4322 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4323 20a2ad1c 2020-10-20 stsp ondisk_path);
4326 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4327 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4330 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4332 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4333 f2a9dc41 2019-12-13 tracey if (err)
4334 f2a9dc41 2019-12-13 tracey return err;
4335 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4336 f2a9dc41 2019-12-13 tracey return NULL;
4337 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4338 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4341 2ec1f75b 2019-03-26 stsp const struct got_error *
4342 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4343 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4344 766841c2 2020-08-13 stsp const char *status_codes,
4345 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4346 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4348 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4349 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4350 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4351 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4352 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4354 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4356 2ec1f75b 2019-03-26 stsp return err;
4358 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4360 2ec1f75b 2019-03-26 stsp goto done;
4362 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4363 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4364 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4365 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4366 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4367 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4368 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4369 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4370 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4372 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4373 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4374 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4378 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4379 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4380 af12c6b9 2019-06-04 stsp err = sync_err;
4382 fb399478 2019-07-12 stsp free(fileindex_path);
4383 2ec1f75b 2019-03-26 stsp if (fileindex)
4384 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4385 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4386 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4387 2ec1f75b 2019-03-26 stsp err = unlockerr;
4388 33aa809d 2019-08-08 stsp return err;
4391 33aa809d 2019-08-08 stsp static const struct got_error *
4392 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4394 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4395 33aa809d 2019-08-08 stsp char *line = NULL;
4396 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4397 33aa809d 2019-08-08 stsp ssize_t linelen;
4399 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4400 33aa809d 2019-08-08 stsp if (linelen == -1) {
4401 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4402 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4403 33aa809d 2019-08-08 stsp goto done;
4405 33aa809d 2019-08-08 stsp return NULL;
4407 33aa809d 2019-08-08 stsp if (outfile) {
4408 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4409 33aa809d 2019-08-08 stsp if (n != linelen) {
4410 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4411 33aa809d 2019-08-08 stsp goto done;
4414 33aa809d 2019-08-08 stsp if (rejectfile) {
4415 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4416 33aa809d 2019-08-08 stsp if (n != linelen)
4417 910d235d 2023-01-10 mark err = got_ferror(rejectfile, GOT_ERR_IO);
4420 33aa809d 2019-08-08 stsp free(line);
4421 2ec1f75b 2019-03-26 stsp return err;
4424 33aa809d 2019-08-08 stsp static const struct got_error *
4425 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4427 33aa809d 2019-08-08 stsp char *line = NULL;
4428 33aa809d 2019-08-08 stsp size_t linesize = 0;
4429 33aa809d 2019-08-08 stsp ssize_t linelen;
4431 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4432 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4433 500467ff 2019-09-25 hiltjo if (ferror(f))
4434 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4435 500467ff 2019-09-25 hiltjo return NULL;
4437 33aa809d 2019-08-08 stsp free(line);
4438 33aa809d 2019-08-08 stsp return NULL;
4441 33aa809d 2019-08-08 stsp static const struct got_error *
4442 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4443 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4444 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4446 33aa809d 2019-08-08 stsp const struct got_error *err;
4448 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4449 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4450 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4452 33aa809d 2019-08-08 stsp return err;
4453 33aa809d 2019-08-08 stsp (*line_cur1)++;
4455 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4456 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4457 33aa809d 2019-08-08 stsp if (rejectfile)
4458 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4460 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4462 33aa809d 2019-08-08 stsp return err;
4463 33aa809d 2019-08-08 stsp (*line_cur2)++;
4465 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4466 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4467 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4469 33aa809d 2019-08-08 stsp return err;
4470 33aa809d 2019-08-08 stsp (*line_cur2)++;
4472 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4473 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4474 33aa809d 2019-08-08 stsp if (rejectfile)
4475 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4477 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4479 33aa809d 2019-08-08 stsp return err;
4480 33aa809d 2019-08-08 stsp (*line_cur1)++;
4483 f1e81a05 2019-08-10 stsp return NULL;
4486 f1e81a05 2019-08-10 stsp static const struct got_error *
4487 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4488 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4490 f1e81a05 2019-08-10 stsp const struct got_error *err;
4492 f1e81a05 2019-08-10 stsp if (outfile) {
4493 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4494 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4495 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4497 f1e81a05 2019-08-10 stsp return err;
4498 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4501 f1e81a05 2019-08-10 stsp if (rejectfile) {
4502 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4503 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4504 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4506 f1e81a05 2019-08-10 stsp return err;
4507 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4511 33aa809d 2019-08-08 stsp return NULL;
4514 33aa809d 2019-08-08 stsp static const struct got_error *
4515 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4516 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4517 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4518 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4519 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4521 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4522 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4523 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4524 33aa809d 2019-08-08 stsp FILE *hunkfile;
4525 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4526 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4529 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4531 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4532 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4533 fe621944 2020-11-10 stsp start_old = cc.left.start;
4534 fe621944 2020-11-10 stsp end_old = cc.left.end;
4535 fe621944 2020-11-10 stsp start_new = cc.right.start;
4536 fe621944 2020-11-10 stsp end_new = cc.right.end;
4538 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4539 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4540 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4542 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4543 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4544 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4546 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4547 fe621944 2020-11-10 stsp if (diff_state == NULL)
4548 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4549 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4551 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4552 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4553 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4554 33aa809d 2019-08-08 stsp goto done;
4557 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4558 fe621944 2020-11-10 stsp diff_result, &cc);
4559 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4560 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4561 33aa809d 2019-08-08 stsp goto done;
4564 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4565 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4566 33aa809d 2019-08-08 stsp goto done;
4569 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4570 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4572 33aa809d 2019-08-08 stsp goto done;
4574 33aa809d 2019-08-08 stsp switch (*choice) {
4575 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4576 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4577 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4579 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4580 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4581 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4583 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4586 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4590 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4591 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4592 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4593 33aa809d 2019-08-08 stsp return err;
4596 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4597 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4598 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4599 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4600 1f1abb7e 2019-08-08 stsp void *progress_arg;
4601 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4602 33aa809d 2019-08-08 stsp void *patch_arg;
4603 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4604 f259c4c1 2021-09-24 stsp int unlink_added_files;
4607 e20a8b6f 2019-06-04 stsp static const struct got_error *
4608 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4609 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4610 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4611 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4612 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4614 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4615 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4616 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4617 eb81bc23 2022-06-28 tracey int fd = -1, fd2 = -1;
4618 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4619 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4620 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4621 fe621944 2020-11-10 stsp struct stat sb2;
4622 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4623 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4624 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4626 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4628 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4630 33aa809d 2019-08-08 stsp return err;
4632 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4633 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4634 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4635 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4636 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4637 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4638 fa3cef63 2020-07-23 stsp goto done;
4640 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4641 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4642 c0df5966 2021-12-31 stsp if (link_len == -1) {
4643 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4646 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4647 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4650 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4651 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4652 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4653 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4654 fa3cef63 2020-07-23 stsp goto done;
4656 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4657 fa3cef63 2020-07-23 stsp sizeof(link_target));
4658 fa3cef63 2020-07-23 stsp if (link_len == -1)
4659 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4660 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4661 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4664 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4665 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4666 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4667 fa3cef63 2020-07-23 stsp goto done;
4670 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4671 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4672 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4673 fa3cef63 2020-07-23 stsp goto done;
4678 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4679 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4680 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4681 fa3cef63 2020-07-23 stsp goto done;
4683 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4684 fa3cef63 2020-07-23 stsp if (n != link_len) {
4685 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4686 fa3cef63 2020-07-23 stsp goto done;
4688 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4689 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4690 fa3cef63 2020-07-23 stsp goto done;
4692 fa3cef63 2020-07-23 stsp rewind(f2);
4695 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4696 eb81bc23 2022-06-28 tracey if (fd == -1) {
4697 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4698 eb81bc23 2022-06-28 tracey goto done;
4701 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4703 33aa809d 2019-08-08 stsp goto done;
4705 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4707 33aa809d 2019-08-08 stsp goto done;
4709 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4711 33aa809d 2019-08-08 stsp goto done;
4713 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4714 4b752015 2022-06-30 stsp 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4716 33aa809d 2019-08-08 stsp goto done;
4718 b90054ed 2022-11-01 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4721 33aa809d 2019-08-08 stsp goto done;
4723 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4724 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4725 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4726 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4728 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4729 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4730 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4731 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4732 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4733 fe621944 2020-11-10 stsp nchanges++;
4735 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4736 33aa809d 2019-08-08 stsp int choice;
4737 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4738 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4739 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4740 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4741 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4742 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4744 33aa809d 2019-08-08 stsp goto done;
4745 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4746 33aa809d 2019-08-08 stsp have_content = 1;
4747 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4750 1ebedb77 2019-10-19 stsp if (have_content) {
4751 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4752 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4753 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4755 1ebedb77 2019-10-19 stsp goto done;
4757 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4758 b2b3fce1 2022-10-29 op mode_t mode;
4760 b2b3fce1 2022-10-29 op mode = apply_umask(sb2.st_mode);
4761 b2b3fce1 2022-10-29 op if (fchmod(fileno(outfile), mode) == -1) {
4762 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4763 fa3cef63 2020-07-23 stsp goto done;
4768 33aa809d 2019-08-08 stsp free(id_str);
4769 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4770 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4772 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4773 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4774 fe621944 2020-11-10 stsp if (err == NULL)
4775 fe621944 2020-11-10 stsp err = free_err;
4776 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4777 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4778 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4779 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4780 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4781 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4782 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4783 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4784 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4785 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4786 33aa809d 2019-08-08 stsp if (err || !have_content) {
4787 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4788 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4789 33aa809d 2019-08-08 stsp free(*path_outfile);
4790 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4792 33aa809d 2019-08-08 stsp free(path1);
4793 33aa809d 2019-08-08 stsp return err;
4796 33aa809d 2019-08-08 stsp static const struct got_error *
4797 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4798 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4799 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4800 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4802 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4803 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4804 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4805 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4806 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
4807 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4808 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4809 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4810 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4811 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4812 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4813 eb81bc23 2022-06-28 tracey int fd = -1;
4815 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4816 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4817 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4818 d3bcc3d1 2019-08-08 stsp return NULL;
4820 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4821 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4822 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4824 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4825 65084dad 2019-08-08 stsp if (ie == NULL)
4826 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4828 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4829 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4830 a129376b 2019-03-28 stsp if (err) {
4831 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4832 a129376b 2019-03-28 stsp goto done;
4833 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4834 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4835 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4836 e20a8b6f 2019-06-04 stsp goto done;
4839 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4840 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4841 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4842 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4843 a129376b 2019-03-28 stsp goto done;
4846 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4847 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4848 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4849 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4850 a129376b 2019-03-28 stsp goto done;
4853 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4854 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4855 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4856 a129376b 2019-03-28 stsp goto done;
4861 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
4862 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
4864 a44927cc 2022-04-07 stsp goto done;
4866 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4867 a9fa2909 2019-07-27 stsp if (err) {
4868 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4869 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4870 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4871 a9fa2909 2019-07-27 stsp goto done;
4873 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4875 a9fa2909 2019-07-27 stsp goto done;
4877 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4879 a9fa2909 2019-07-27 stsp goto done;
4881 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4882 1233e6b6 2020-10-19 stsp free(te_name);
4883 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4884 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4885 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4886 a9fa2909 2019-07-27 stsp goto done;
4890 a129376b 2019-03-28 stsp switch (status) {
4891 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4892 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4893 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4894 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4895 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4897 33aa809d 2019-08-08 stsp goto done;
4898 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4901 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4902 1f1abb7e 2019-08-08 stsp ie->path);
4904 1ee397ad 2019-07-12 stsp goto done;
4905 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4906 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
4907 f259c4c1 2021-09-24 stsp if (asprintf(&ondisk_path, "%s/%s",
4908 f259c4c1 2021-09-24 stsp got_worktree_get_root_path(a->worktree),
4909 f259c4c1 2021-09-24 stsp relpath) == -1) {
4910 f259c4c1 2021-09-24 stsp err = got_error_from_errno("asprintf");
4911 f259c4c1 2021-09-24 stsp goto done;
4913 f259c4c1 2021-09-24 stsp if (unlink(ondisk_path) == -1) {
4914 f259c4c1 2021-09-24 stsp err = got_error_from_errno2("unlink",
4915 f259c4c1 2021-09-24 stsp ondisk_path);
4920 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4921 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4922 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4923 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4924 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4926 33aa809d 2019-08-08 stsp goto done;
4927 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4930 33aa809d 2019-08-08 stsp /* fall through */
4931 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4932 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4933 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4934 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4935 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4936 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4937 b4b2adf5 2023-02-09 op staged_status == GOT_STATUS_MODIFY)
4938 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
4940 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
4941 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4942 eb81bc23 2022-06-28 tracey if (fd == -1) {
4943 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4944 eb81bc23 2022-06-28 tracey goto done;
4947 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4949 65084dad 2019-08-08 stsp goto done;
4951 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4952 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4953 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4954 a129376b 2019-03-28 stsp goto done;
4957 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4958 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4959 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4960 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4961 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4962 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4963 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4965 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4966 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4967 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4968 369fd7e5 2020-07-23 stsp path_content);
4971 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4972 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4973 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4974 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4976 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4977 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4978 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4979 369fd7e5 2020-07-23 stsp goto done;
4983 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4984 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4985 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4986 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4987 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4988 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4990 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4992 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4993 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4994 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4995 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4998 a129376b 2019-03-28 stsp goto done;
4999 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5000 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5001 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5002 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5003 437adc9d 2020-12-10 yzhong blob->id.sha1,
5004 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5006 2e1fa222 2020-07-23 stsp goto done;
5008 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5009 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5010 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5019 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5020 33aa809d 2019-08-08 stsp free(path_content);
5021 e20a8b6f 2019-06-04 stsp free(parent_path);
5022 a129376b 2019-03-28 stsp free(tree_path);
5023 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5024 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5026 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5028 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5029 a129376b 2019-03-28 stsp free(tree_id);
5030 a44927cc 2022-04-07 stsp if (base_commit)
5031 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
5032 e20a8b6f 2019-06-04 stsp return err;
5035 e20a8b6f 2019-06-04 stsp const struct got_error *
5036 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5037 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5038 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5039 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5040 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5042 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5043 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5044 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5045 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5046 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5047 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5049 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5051 e20a8b6f 2019-06-04 stsp return err;
5053 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5055 e20a8b6f 2019-06-04 stsp goto done;
5057 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5058 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5059 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5060 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5061 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5062 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5063 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5064 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
5065 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5066 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5067 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
5071 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5072 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5073 e20a8b6f 2019-06-04 stsp err = sync_err;
5075 fb399478 2019-07-12 stsp free(fileindex_path);
5076 a129376b 2019-03-28 stsp if (fileindex)
5077 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5078 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5079 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5080 a129376b 2019-03-28 stsp err = unlockerr;
5081 c4296144 2019-05-09 stsp return err;
5084 cf066bf8 2019-05-09 stsp static void
5085 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5087 24519714 2019-05-09 stsp free(ct->path);
5088 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5089 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5090 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5091 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5092 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5093 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5097 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5098 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5099 24519714 2019-05-09 stsp struct got_repository *repo;
5100 24519714 2019-05-09 stsp struct got_worktree *worktree;
5101 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5102 5f8a88c6 2019-08-03 stsp int have_staged_files;
5103 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5104 2a47b1e5 2022-11-01 stsp int diff_header_shown;
5105 12383673 2023-02-18 mark int commit_conflicts;
5106 2a47b1e5 2022-11-01 stsp FILE *diff_outfile;
5112 2a47b1e5 2022-11-01 stsp * Create a file which contains the target path of a symlink so we can feed
5113 2a47b1e5 2022-11-01 stsp * it as content to the diff engine.
5115 2a47b1e5 2022-11-01 stsp static const struct got_error *
5116 2a47b1e5 2022-11-01 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5117 2a47b1e5 2022-11-01 stsp const char *abspath)
5119 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5120 2a47b1e5 2022-11-01 stsp char target_path[PATH_MAX];
5121 2a47b1e5 2022-11-01 stsp ssize_t target_len, outlen;
5125 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5126 2a47b1e5 2022-11-01 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5127 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5128 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlinkat", abspath);
5130 2a47b1e5 2022-11-01 stsp target_len = readlink(abspath, target_path, PATH_MAX);
5131 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5132 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlink", abspath);
5135 2a47b1e5 2022-11-01 stsp *fd = got_opentempfd();
5136 2a47b1e5 2022-11-01 stsp if (*fd == -1)
5137 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentempfd");
5139 2a47b1e5 2022-11-01 stsp outlen = write(*fd, target_path, target_len);
5140 2a47b1e5 2022-11-01 stsp if (outlen == -1) {
5141 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5142 2a47b1e5 2022-11-01 stsp goto done;
5145 2a47b1e5 2022-11-01 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
5146 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("lseek", abspath);
5147 2a47b1e5 2022-11-01 stsp goto done;
5150 2a47b1e5 2022-11-01 stsp if (err) {
5151 2a47b1e5 2022-11-01 stsp close(*fd);
5154 2a47b1e5 2022-11-01 stsp return err;
5157 2a47b1e5 2022-11-01 stsp static const struct got_error *
5158 2a47b1e5 2022-11-01 stsp append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5159 2a47b1e5 2022-11-01 stsp FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5160 6d15dc69 2022-11-01 stsp int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5162 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5163 2a47b1e5 2022-11-01 stsp struct got_blob_object *blob1 = NULL;
5164 2a47b1e5 2022-11-01 stsp int fd = -1, fd1 = -1, fd2 = -1;
5165 2a47b1e5 2022-11-01 stsp FILE *ondisk_file = NULL;
5166 2a47b1e5 2022-11-01 stsp char *label1 = NULL;
5167 2a47b1e5 2022-11-01 stsp struct stat sb;
5168 2a47b1e5 2022-11-01 stsp off_t size1 = 0;
5169 2a47b1e5 2022-11-01 stsp int f2_exists = 0;
5170 2a47b1e5 2022-11-01 stsp char *id_str = NULL;
5172 2a47b1e5 2022-11-01 stsp memset(&sb, 0, sizeof(sb));
5174 4ba5cca9 2022-11-01 stsp if (diff_staged) {
5175 4ba5cca9 2022-11-01 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5176 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_ADD &&
5177 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_DELETE)
5178 4ba5cca9 2022-11-01 stsp return NULL;
5180 4ba5cca9 2022-11-01 stsp if (ct->status != GOT_STATUS_MODIFY &&
5181 4ba5cca9 2022-11-01 stsp ct->status != GOT_STATUS_ADD &&
5182 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5183 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5184 4ba5cca9 2022-11-01 stsp return NULL;
5187 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f1);
5189 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5190 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f2);
5192 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5194 2a47b1e5 2022-11-01 stsp if (!*diff_header_shown) {
5195 2a47b1e5 2022-11-01 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
5197 2a47b1e5 2022-11-01 stsp return err;
5198 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5199 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree));
5200 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "commit - %s\n", id_str);
5201 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "path + %s%s\n",
5202 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree),
5203 2a47b1e5 2022-11-01 stsp diff_staged ? " (staged changes)" : "");
5204 2a47b1e5 2022-11-01 stsp *diff_header_shown = 1;
5207 2a47b1e5 2022-11-01 stsp if (diff_staged) {
5208 2a47b1e5 2022-11-01 stsp const char *label1 = NULL, *label2 = NULL;
5209 2a47b1e5 2022-11-01 stsp switch (ct->staged_status) {
5210 2a47b1e5 2022-11-01 stsp case GOT_STATUS_MODIFY:
5211 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5212 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5214 2a47b1e5 2022-11-01 stsp case GOT_STATUS_ADD:
5215 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5217 2a47b1e5 2022-11-01 stsp case GOT_STATUS_DELETE:
5218 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5221 2a47b1e5 2022-11-01 stsp return got_error(GOT_ERR_FILE_STATUS);
5223 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5224 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5225 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5226 2a47b1e5 2022-11-01 stsp goto done;
5228 2a47b1e5 2022-11-01 stsp fd2 = got_opentempfd();
5229 2a47b1e5 2022-11-01 stsp if (fd2 == -1) {
5230 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5231 2a47b1e5 2022-11-01 stsp goto done;
5233 2a47b1e5 2022-11-01 stsp err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5234 2a47b1e5 2022-11-01 stsp fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5235 1f3405c9 2023-01-17 mark label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5236 a76e88e5 2023-01-10 mark NULL, repo, diff_outfile);
5237 2a47b1e5 2022-11-01 stsp goto done;
5240 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5241 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5242 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5243 2a47b1e5 2022-11-01 stsp goto done;
5246 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_ADD) {
5247 2a47b1e5 2022-11-01 stsp err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5248 2a47b1e5 2022-11-01 stsp 8192, fd1);
5250 2a47b1e5 2022-11-01 stsp goto done;
5253 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_DELETE) {
5254 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5255 2a47b1e5 2022-11-01 stsp fd = openat(dirfd, de_name,
5256 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5257 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5258 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5259 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("openat",
5260 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5261 2a47b1e5 2022-11-01 stsp goto done;
5263 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5264 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5266 2a47b1e5 2022-11-01 stsp goto done;
5269 2a47b1e5 2022-11-01 stsp fd = open(ct->ondisk_path,
5270 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5271 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5272 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5273 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("open",
5274 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5275 2a47b1e5 2022-11-01 stsp goto done;
5277 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5278 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5280 2a47b1e5 2022-11-01 stsp goto done;
5283 2a47b1e5 2022-11-01 stsp if (fstatat(fd, ct->ondisk_path, &sb,
5284 2a47b1e5 2022-11-01 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5285 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fstatat", ct->ondisk_path);
5286 2a47b1e5 2022-11-01 stsp goto done;
5288 2a47b1e5 2022-11-01 stsp ondisk_file = fdopen(fd, "r");
5289 2a47b1e5 2022-11-01 stsp if (ondisk_file == NULL) {
5290 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fdopen", ct->ondisk_path);
5291 2a47b1e5 2022-11-01 stsp goto done;
5294 2a47b1e5 2022-11-01 stsp f2_exists = 1;
5297 2a47b1e5 2022-11-01 stsp if (blob1) {
5298 2a47b1e5 2022-11-01 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5299 2a47b1e5 2022-11-01 stsp f1, blob1);
5301 2a47b1e5 2022-11-01 stsp goto done;
5304 2a47b1e5 2022-11-01 stsp err = got_diff_blob_file(blob1, f1, size1, label1,
5305 2a47b1e5 2022-11-01 stsp ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5306 1f3405c9 2023-01-17 mark GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5308 2a47b1e5 2022-11-01 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5309 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5310 2a47b1e5 2022-11-01 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5311 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5312 2a47b1e5 2022-11-01 stsp if (blob1)
5313 2a47b1e5 2022-11-01 stsp got_object_blob_close(blob1);
5314 2a47b1e5 2022-11-01 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5315 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5316 2a47b1e5 2022-11-01 stsp if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5317 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
5318 2a47b1e5 2022-11-01 stsp return err;
5321 c4296144 2019-05-09 stsp static const struct got_error *
5322 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5323 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5324 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5325 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5327 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5328 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5329 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5330 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5331 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5332 768aea60 2019-05-09 stsp struct stat sb;
5334 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5335 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5336 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5337 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5338 5f8a88c6 2019-08-03 stsp return NULL;
5340 12383673 2023-02-18 mark if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5341 12383673 2023-02-18 mark printf("C %s\n", relpath);
5342 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5345 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5346 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5347 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5348 12383673 2023-02-18 mark status != GOT_STATUS_DELETE &&
5349 12383673 2023-02-18 mark status != GOT_STATUS_CONFLICT)
5350 5f8a88c6 2019-08-03 stsp return NULL;
5353 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5354 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5355 036813ee 2019-05-09 stsp goto done;
5357 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5358 036813ee 2019-05-09 stsp parent_path = strdup("");
5359 69960a46 2019-05-09 stsp if (parent_path == NULL)
5360 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5362 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5364 69960a46 2019-05-09 stsp return err;
5367 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5368 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5369 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5370 c4296144 2019-05-09 stsp goto done;
5373 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5374 768aea60 2019-05-09 stsp relpath) == -1) {
5375 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5376 768aea60 2019-05-09 stsp goto done;
5379 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5380 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5381 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5382 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5383 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5384 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5385 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5386 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5388 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5389 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5392 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5393 0aeb8099 2020-07-23 stsp goto done;
5395 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5396 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5397 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5398 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5399 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5400 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5401 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5402 12463d8b 2019-12-13 stsp ct->ondisk_path);
5403 12463d8b 2019-12-13 stsp goto done;
5405 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5406 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5407 768aea60 2019-05-09 stsp goto done;
5409 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5412 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5413 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5414 44d03001 2019-05-09 stsp relpath) == -1) {
5415 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5416 44d03001 2019-05-09 stsp goto done;
5419 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5420 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5421 35213c7c 2020-07-23 stsp int is_bad_symlink;
5422 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5423 35213c7c 2020-07-23 stsp ssize_t target_len;
5424 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5425 35213c7c 2020-07-23 stsp sizeof(target_path));
5426 35213c7c 2020-07-23 stsp if (target_len == -1) {
5427 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5428 35213c7c 2020-07-23 stsp ct->ondisk_path);
5429 35213c7c 2020-07-23 stsp goto done;
5431 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5432 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5434 35213c7c 2020-07-23 stsp goto done;
5435 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5436 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5437 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5438 35213c7c 2020-07-23 stsp goto done;
5443 cf066bf8 2019-05-09 stsp ct->status = status;
5444 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5445 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5446 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5447 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5448 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5449 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5450 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5451 b416585c 2019-05-13 stsp goto done;
5453 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5454 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5455 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5456 f0b75401 2019-08-03 stsp goto done;
5459 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5460 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5461 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5462 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5463 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5464 036813ee 2019-05-09 stsp goto done;
5467 24519714 2019-05-09 stsp ct->path = strdup(path);
5468 24519714 2019-05-09 stsp if (ct->path == NULL) {
5469 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5470 24519714 2019-05-09 stsp goto done;
5472 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5474 2a47b1e5 2022-11-01 stsp goto done;
5476 2a47b1e5 2022-11-01 stsp if (a->diff_outfile && ct && new != NULL) {
5477 2a47b1e5 2022-11-01 stsp err = append_ct_diff(ct, &a->diff_header_shown,
5478 2a47b1e5 2022-11-01 stsp a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5479 6d15dc69 2022-11-01 stsp a->have_staged_files, a->repo, a->worktree);
5481 2a47b1e5 2022-11-01 stsp goto done;
5484 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5485 ed175427 2019-05-09 stsp free_commitable(ct);
5486 24519714 2019-05-09 stsp free(parent_path);
5487 036813ee 2019-05-09 stsp free(path);
5488 a129376b 2019-03-28 stsp return err;
5491 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5492 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5493 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5494 036813ee 2019-05-09 stsp struct got_repository *);
5496 ed175427 2019-05-09 stsp static const struct got_error *
5497 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5498 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5499 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5500 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5501 afa376bf 2019-05-09 stsp struct got_repository *repo)
5503 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5504 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5505 036813ee 2019-05-09 stsp char *subpath;
5507 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5508 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5509 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5511 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5513 ed175427 2019-05-09 stsp return err;
5515 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5516 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5517 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5518 036813ee 2019-05-09 stsp free(subpath);
5519 036813ee 2019-05-09 stsp return err;
5522 036813ee 2019-05-09 stsp static const struct got_error *
5523 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5525 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5526 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5528 036813ee 2019-05-09 stsp *match = 0;
5530 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5531 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5532 0f63689d 2019-05-10 stsp return NULL;
5535 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5537 0f63689d 2019-05-10 stsp return err;
5538 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5539 036813ee 2019-05-09 stsp free(ct_parent_path);
5540 036813ee 2019-05-09 stsp return err;
5543 768aea60 2019-05-09 stsp static mode_t
5544 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5546 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5547 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5549 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5552 036813ee 2019-05-09 stsp static const struct got_error *
5553 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5554 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5556 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5558 036813ee 2019-05-09 stsp *new_te = NULL;
5560 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5562 036813ee 2019-05-09 stsp goto done;
5564 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5566 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5567 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5568 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5570 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5572 036813ee 2019-05-09 stsp if (err && *new_te) {
5573 56e0773d 2019-11-28 stsp free(*new_te);
5574 036813ee 2019-05-09 stsp *new_te = NULL;
5576 036813ee 2019-05-09 stsp return err;
5579 036813ee 2019-05-09 stsp static const struct got_error *
5580 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5581 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5583 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5584 102b254e 2020-10-19 stsp char *ct_name = NULL;
5586 036813ee 2019-05-09 stsp *new_te = NULL;
5588 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5589 036813ee 2019-05-09 stsp if (*new_te == NULL)
5590 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5592 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5594 036813ee 2019-05-09 stsp goto done;
5595 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5596 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5597 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5598 036813ee 2019-05-09 stsp goto done;
5601 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5603 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5604 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5605 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5607 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5609 102b254e 2020-10-19 stsp free(ct_name);
5610 036813ee 2019-05-09 stsp if (err && *new_te) {
5611 56e0773d 2019-11-28 stsp free(*new_te);
5612 036813ee 2019-05-09 stsp *new_te = NULL;
5614 036813ee 2019-05-09 stsp return err;
5617 036813ee 2019-05-09 stsp static const struct got_error *
5618 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5619 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5621 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5622 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5624 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5626 036813ee 2019-05-09 stsp return err;
5627 036813ee 2019-05-09 stsp if (new_pe == NULL)
5628 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5629 036813ee 2019-05-09 stsp return NULL;
5632 afa376bf 2019-05-09 stsp static const struct got_error *
5633 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5634 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5636 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5637 5f8a88c6 2019-08-03 stsp unsigned char status;
5639 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5640 0ff8d236 2021-09-28 stsp return NULL;
5642 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5643 afa376bf 2019-05-09 stsp ct_path++;
5645 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5646 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5648 5f8a88c6 2019-08-03 stsp status = ct->status;
5650 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5651 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5654 036813ee 2019-05-09 stsp static const struct got_error *
5655 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5656 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5658 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5659 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5660 44d03001 2019-05-09 stsp char *te_path;
5662 44d03001 2019-05-09 stsp *modified = 0;
5664 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5665 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5666 44d03001 2019-05-09 stsp te->name) == -1)
5667 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5669 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5670 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5671 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5672 44d03001 2019-05-09 stsp strlen(te_path));
5673 62d463ca 2020-10-20 naddy if (*modified)
5677 44d03001 2019-05-09 stsp free(te_path);
5678 44d03001 2019-05-09 stsp return err;
5681 44d03001 2019-05-09 stsp static const struct got_error *
5682 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5683 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5684 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5686 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5687 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5689 036813ee 2019-05-09 stsp *ctp = NULL;
5691 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5692 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5693 036813ee 2019-05-09 stsp char *ct_name = NULL;
5694 036813ee 2019-05-09 stsp int path_matches;
5696 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5697 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5698 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5699 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5700 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5703 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5704 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5708 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5711 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5713 036813ee 2019-05-09 stsp return err;
5714 036813ee 2019-05-09 stsp if (!path_matches)
5717 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5719 d34b633e 2020-10-19 stsp return err;
5721 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5722 d34b633e 2020-10-19 stsp free(ct_name);
5725 d34b633e 2020-10-19 stsp free(ct_name);
5727 036813ee 2019-05-09 stsp *ctp = ct;
5731 2b496619 2019-07-10 stsp return err;
5734 2b496619 2019-07-10 stsp static const struct got_error *
5735 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5736 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5737 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5738 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5739 2b496619 2019-07-10 stsp struct got_repository *repo)
5741 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5742 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5743 2b496619 2019-07-10 stsp char *subtree_path;
5744 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5745 ba580f68 2020-03-22 stsp int nentries;
5747 2b496619 2019-07-10 stsp *new_tep = NULL;
5749 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5750 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5751 2b496619 2019-07-10 stsp child_path) == -1)
5752 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5754 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5755 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5756 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5757 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5759 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5760 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5761 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5762 2b496619 2019-07-10 stsp goto done;
5764 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5765 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5766 2b496619 2019-07-10 stsp if (err) {
5767 56e0773d 2019-11-28 stsp free(new_te);
5768 2b496619 2019-07-10 stsp goto done;
5770 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5773 2b496619 2019-07-10 stsp free(subtree_path);
5774 2b496619 2019-07-10 stsp if (err == NULL)
5775 2b496619 2019-07-10 stsp *new_tep = new_te;
5776 036813ee 2019-05-09 stsp return err;
5779 036813ee 2019-05-09 stsp static const struct got_error *
5780 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5781 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5782 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5783 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5784 036813ee 2019-05-09 stsp struct got_repository *repo)
5786 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5787 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5788 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5789 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5791 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5792 ba580f68 2020-03-22 stsp *nentries = 0;
5794 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5795 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5796 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5797 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5799 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5800 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5801 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5804 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5805 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5808 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5809 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5811 036813ee 2019-05-09 stsp goto done;
5813 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5814 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5815 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5817 036813ee 2019-05-09 stsp goto done;
5818 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5820 afa376bf 2019-05-09 stsp goto done;
5821 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5822 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5824 2f51b5b3 2019-05-09 stsp goto done;
5825 ba580f68 2020-03-22 stsp (*nentries)++;
5827 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5828 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5829 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5830 2b496619 2019-07-10 stsp == NULL) {
5831 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5832 2b496619 2019-07-10 stsp child_path, path_base_tree,
5833 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5836 2b496619 2019-07-10 stsp goto done;
5837 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5839 2b496619 2019-07-10 stsp goto done;
5840 ba580f68 2020-03-22 stsp (*nentries)++;
5845 2f51b5b3 2019-05-09 stsp if (base_tree) {
5846 56e0773d 2019-11-28 stsp int i, nbase_entries;
5847 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5848 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5849 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5850 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5852 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5853 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5854 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5855 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5857 63c5ca5d 2019-08-24 stsp goto done;
5858 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5860 63c5ca5d 2019-08-24 stsp goto done;
5861 ba580f68 2020-03-22 stsp (*nentries)++;
5865 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5866 44d03001 2019-05-09 stsp int modified;
5867 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5869 036813ee 2019-05-09 stsp goto done;
5870 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5871 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5873 2f51b5b3 2019-05-09 stsp goto done;
5874 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5875 44d03001 2019-05-09 stsp if (modified) {
5876 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5877 ba580f68 2020-03-22 stsp int nsubentries;
5878 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5879 ba580f68 2020-03-22 stsp &nsubentries, te,
5880 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5881 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5883 44d03001 2019-05-09 stsp goto done;
5884 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5885 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5886 ba580f68 2020-03-22 stsp free(new_id);
5889 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5890 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5891 56e0773d 2019-11-28 stsp free(new_id);
5893 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5895 036813ee 2019-05-09 stsp goto done;
5896 ba580f68 2020-03-22 stsp (*nentries)++;
5900 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5901 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5903 f66c734c 2019-09-22 stsp goto done;
5905 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5906 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5907 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5908 12383673 2023-02-18 mark ct->status == GOT_STATUS_CONFLICT ||
5909 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5910 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5911 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5913 2f51b5b3 2019-05-09 stsp goto done;
5914 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5916 2f51b5b3 2019-05-09 stsp goto done;
5917 ba580f68 2020-03-22 stsp (*nentries)++;
5919 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5920 b416585c 2019-05-13 stsp status_arg);
5922 afa376bf 2019-05-09 stsp goto done;
5924 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5925 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5927 2f51b5b3 2019-05-09 stsp goto done;
5928 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5930 2f51b5b3 2019-05-09 stsp goto done;
5931 ba580f68 2020-03-22 stsp (*nentries)++;
5936 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5937 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5939 d8bacb93 2023-01-10 mark got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5940 2e1fa222 2020-07-23 stsp return err;
5943 2e1fa222 2020-07-23 stsp static const struct got_error *
5944 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5945 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5946 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5947 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5949 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5950 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5951 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5953 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5954 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5955 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5957 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5959 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5960 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5961 437adc9d 2020-12-10 yzhong if (err)
5962 437adc9d 2020-12-10 yzhong goto done;
5965 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5966 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5967 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5968 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5969 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5970 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5971 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5972 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5974 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5975 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5976 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5977 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5978 72fd46fa 2019-09-06 stsp !have_staged_files);
5980 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5981 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5982 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5983 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5984 72fd46fa 2019-09-06 stsp !have_staged_files);
5986 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5988 437adc9d 2020-12-10 yzhong goto done;
5989 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5990 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5991 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5992 3969253a 2020-03-07 stsp if (err) {
5993 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5994 437adc9d 2020-12-10 yzhong goto done;
5996 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5997 3969253a 2020-03-07 stsp if (err) {
5998 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5999 437adc9d 2020-12-10 yzhong goto done;
6002 437adc9d 2020-12-10 yzhong free(relpath);
6003 437adc9d 2020-12-10 yzhong relpath = NULL;
6006 437adc9d 2020-12-10 yzhong free(relpath);
6007 d56d26ce 2019-05-10 stsp return err;
6011 d56d26ce 2019-05-10 stsp static const struct got_error *
6012 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6013 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6014 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6015 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6016 735ef5ac 2019-08-03 stsp int ood_errcode)
6018 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6019 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6020 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6022 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6023 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6024 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6025 1a36436d 2019-06-10 stsp return NULL;
6027 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6028 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6030 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
6032 a44927cc 2022-04-07 stsp goto done;
6033 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6034 9bead371 2019-07-28 stsp if (err) {
6035 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6036 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6037 1a36436d 2019-06-10 stsp goto done;
6038 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6039 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6041 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6042 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
6044 a44927cc 2022-04-07 stsp goto done;
6045 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6046 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6047 1a36436d 2019-06-10 stsp goto done;
6048 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6052 a44927cc 2022-04-07 stsp if (commit)
6053 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6054 1a36436d 2019-06-10 stsp return err;
6057 336075a4 2022-06-25 op static const struct got_error *
6058 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6059 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6060 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
6061 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
6062 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
6063 2a47b1e5 2022-11-01 stsp const char *author, const char *committer, char *diff_path,
6064 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6065 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6066 afa376bf 2019-05-09 stsp struct got_repository *repo)
6068 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6069 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6070 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6071 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6072 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6073 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6074 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6075 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6076 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
6077 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6078 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6079 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6080 f8399b8f 2022-07-22 stsp time_t timestamp;
6082 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6084 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6086 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6088 588edf97 2019-05-10 stsp goto done;
6090 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6092 588edf97 2019-05-10 stsp goto done;
6094 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6095 2a47b1e5 2022-11-01 stsp err = commit_msg_cb(commitable_paths, diff_path,
6096 2a47b1e5 2022-11-01 stsp &logmsg, commit_arg);
6101 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6102 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6106 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6107 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6108 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6109 cf066bf8 2019-05-09 stsp char *ondisk_path;
6111 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6112 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6113 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6116 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6117 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6118 12383673 2023-02-18 mark ct->status != GOT_STATUS_MODE_CHANGE &&
6119 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
6122 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6123 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6124 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6125 cf066bf8 2019-05-09 stsp goto done;
6127 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6128 2e1fa222 2020-07-23 stsp free(ondisk_path);
6130 cf066bf8 2019-05-09 stsp goto done;
6133 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6134 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6135 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6137 036813ee 2019-05-09 stsp goto done;
6139 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6141 de18fc63 2019-05-09 stsp goto done;
6142 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6143 f259c4c1 2021-09-24 stsp nparents++;
6144 f259c4c1 2021-09-24 stsp if (parent_id2) {
6145 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
6147 f259c4c1 2021-09-24 stsp goto done;
6148 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6149 f259c4c1 2021-09-24 stsp nparents++;
6151 f8399b8f 2022-07-22 stsp timestamp = time(NULL);
6152 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6153 f8399b8f 2022-07-22 stsp nparents, author, timestamp, committer, timestamp, logmsg, repo);
6154 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6155 33ad4cbe 2019-05-12 jcs free(logmsg);
6157 9d40349a 2019-05-09 stsp goto done;
6159 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6160 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6161 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6162 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6163 09f5bd90 2019-05-09 stsp goto done;
6165 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6166 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6168 09f5bd90 2019-05-09 stsp goto done;
6169 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6171 ebf99748 2019-05-09 stsp goto done;
6172 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6173 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6174 09f5bd90 2019-05-09 stsp goto done;
6176 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6177 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6179 f2c16586 2019-05-09 stsp goto done;
6180 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6182 f2c16586 2019-05-09 stsp goto done;
6184 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6186 09f5bd90 2019-05-09 stsp goto done;
6188 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6190 ebf99748 2019-05-09 stsp goto done;
6192 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
6193 588edf97 2019-05-10 stsp if (head_tree)
6194 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6195 588edf97 2019-05-10 stsp if (head_commit)
6196 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6197 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6198 2f17228e 2019-05-12 stsp if (head_ref2) {
6199 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6200 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6201 2f17228e 2019-05-12 stsp err = unlockerr;
6202 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6204 39cd0ff6 2019-07-12 stsp return err;
6207 5c1e53bc 2019-07-28 stsp static const struct got_error *
6208 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6209 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6211 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6212 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6214 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6215 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6216 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6218 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6219 5c1e53bc 2019-07-28 stsp ct_path++;
6221 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6222 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6226 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6227 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6229 5c1e53bc 2019-07-28 stsp return NULL;
6232 f0b75401 2019-08-03 stsp static const struct got_error *
6233 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6235 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6237 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6238 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6239 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6242 f0b75401 2019-08-03 stsp return NULL;
6245 5f8a88c6 2019-08-03 stsp static const struct got_error *
6246 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6247 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6249 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6250 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6252 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6253 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6255 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6256 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6257 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6258 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6259 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6260 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6263 5f8a88c6 2019-08-03 stsp return NULL;
6266 39cd0ff6 2019-07-12 stsp const struct got_error *
6267 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6268 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6269 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6270 12383673 2023-02-18 mark int show_diff, int commit_conflicts,
6271 12383673 2023-02-18 mark got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6272 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6273 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6275 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6276 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6277 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6278 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6279 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6280 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6281 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6282 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6283 2a47b1e5 2022-11-01 stsp char *diff_path = NULL;
6284 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6286 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6288 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6289 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6291 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6293 39cd0ff6 2019-07-12 stsp goto done;
6295 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6297 39cd0ff6 2019-07-12 stsp goto done;
6299 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6301 39cd0ff6 2019-07-12 stsp goto done;
6303 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6305 39cd0ff6 2019-07-12 stsp goto done;
6307 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6308 5f8a88c6 2019-08-03 stsp &have_staged_files);
6309 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6310 5f8a88c6 2019-08-03 stsp goto done;
6311 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6312 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6314 5f8a88c6 2019-08-03 stsp goto done;
6317 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6318 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6319 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6320 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6321 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6322 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6323 2a47b1e5 2022-11-01 stsp cc_arg.diff_header_shown = 0;
6324 12383673 2023-02-18 mark cc_arg.commit_conflicts = commit_conflicts;
6325 2a47b1e5 2022-11-01 stsp if (show_diff) {
6326 2a47b1e5 2022-11-01 stsp err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6327 b90054ed 2022-11-01 stsp GOT_TMPDIR_STR "/got", ".diff");
6329 2a47b1e5 2022-11-01 stsp goto done;
6330 2a47b1e5 2022-11-01 stsp cc_arg.f1 = got_opentemp();
6331 2a47b1e5 2022-11-01 stsp if (cc_arg.f1 == NULL) {
6332 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6333 2a47b1e5 2022-11-01 stsp goto done;
6335 2a47b1e5 2022-11-01 stsp cc_arg.f2 = got_opentemp();
6336 2a47b1e5 2022-11-01 stsp if (cc_arg.f2 == NULL) {
6337 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6338 2a47b1e5 2022-11-01 stsp goto done;
6342 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6343 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6344 4ba2e955 2022-09-02 sh+got collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6346 5c1e53bc 2019-07-28 stsp goto done;
6349 2a47b1e5 2022-11-01 stsp if (show_diff) {
6350 2a47b1e5 2022-11-01 stsp if (fflush(cc_arg.diff_outfile) == EOF) {
6351 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fflush");
6352 2a47b1e5 2022-11-01 stsp goto done;
6356 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6357 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6358 39cd0ff6 2019-07-12 stsp goto done;
6361 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6362 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6364 5c1e53bc 2019-07-28 stsp goto done;
6367 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6368 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6369 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6371 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6372 f0b75401 2019-08-03 stsp ct_path++;
6373 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6374 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6375 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6377 b50cabdf 2019-07-12 stsp goto done;
6381 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6382 210c2321 2022-11-01 stsp head_commit_id, NULL, worktree, author, committer,
6383 210c2321 2022-11-01 stsp (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6384 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6386 39cd0ff6 2019-07-12 stsp goto done;
6388 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6389 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6390 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6391 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6392 39cd0ff6 2019-07-12 stsp err = sync_err;
6394 39cd0ff6 2019-07-12 stsp if (fileindex)
6395 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6396 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6397 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6398 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6399 39cd0ff6 2019-07-12 stsp err = unlockerr;
6400 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6401 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6403 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6405 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6406 2a47b1e5 2022-11-01 stsp if (diff_path && unlink(diff_path) == -1 && err == NULL)
6407 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("unlink", diff_path);
6408 2a47b1e5 2022-11-01 stsp free(diff_path);
6409 2a47b1e5 2022-11-01 stsp if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6410 2a47b1e5 2022-11-01 stsp err == NULL)
6411 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
6412 c4296144 2019-05-09 stsp return err;
6415 8656d6c4 2019-05-20 stsp const char *
6416 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6418 8656d6c4 2019-05-20 stsp return ct->path;
6421 8656d6c4 2019-05-20 stsp unsigned int
6422 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6424 8656d6c4 2019-05-20 stsp return ct->status;
6427 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6428 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6429 818c7501 2019-07-11 stsp struct got_repository *repo;
6432 818c7501 2019-07-11 stsp static const struct got_error *
6433 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6435 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6436 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6437 818c7501 2019-07-11 stsp unsigned char status;
6438 818c7501 2019-07-11 stsp struct stat sb;
6439 818c7501 2019-07-11 stsp char *ondisk_path;
6441 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6442 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6443 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6444 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6446 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6448 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6450 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6451 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6452 818c7501 2019-07-11 stsp free(ondisk_path);
6454 818c7501 2019-07-11 stsp return err;
6456 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6457 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6458 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6459 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6461 818c7501 2019-07-11 stsp return NULL;
6464 818c7501 2019-07-11 stsp const struct got_error *
6465 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6466 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6467 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6468 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6470 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6471 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6472 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6473 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6474 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6475 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6476 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6478 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6479 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6480 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6482 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6484 818c7501 2019-07-11 stsp return err;
6486 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6488 818c7501 2019-07-11 stsp goto done;
6490 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6491 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6492 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6495 818c7501 2019-07-11 stsp goto done;
6497 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6499 818c7501 2019-07-11 stsp goto done;
6501 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6503 818c7501 2019-07-11 stsp goto done;
6505 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6507 818c7501 2019-07-11 stsp goto done;
6509 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6512 e51d7b55 2020-01-04 stsp goto done;
6514 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6516 818c7501 2019-07-11 stsp goto done;
6517 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6518 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6519 e51d7b55 2020-01-04 stsp goto done;
6522 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6523 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6525 818c7501 2019-07-11 stsp goto done;
6526 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6528 818c7501 2019-07-11 stsp goto done;
6530 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6532 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6534 818c7501 2019-07-11 stsp goto done;
6536 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6538 818c7501 2019-07-11 stsp goto done;
6540 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6541 818c7501 2019-07-11 stsp worktree->base_commit_id);
6543 818c7501 2019-07-11 stsp goto done;
6544 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6546 818c7501 2019-07-11 stsp goto done;
6548 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6550 818c7501 2019-07-11 stsp goto done;
6552 818c7501 2019-07-11 stsp free(fileindex_path);
6553 818c7501 2019-07-11 stsp free(tmp_branch_name);
6554 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6555 818c7501 2019-07-11 stsp free(branch_ref_name);
6556 818c7501 2019-07-11 stsp if (branch_ref)
6557 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6558 818c7501 2019-07-11 stsp if (wt_branch)
6559 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6560 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6561 818c7501 2019-07-11 stsp if (err) {
6562 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6563 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6564 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6566 818c7501 2019-07-11 stsp if (*tmp_branch) {
6567 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6568 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6570 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6571 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6572 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6574 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6576 818c7501 2019-07-11 stsp return err;
6579 818c7501 2019-07-11 stsp const struct got_error *
6580 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6581 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6582 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6583 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6585 818c7501 2019-07-11 stsp const struct got_error *err;
6586 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6587 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6588 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6589 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6590 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6592 818c7501 2019-07-11 stsp *commit_id = NULL;
6593 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6594 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6595 3e3a69f1 2019-07-25 stsp *branch = NULL;
6596 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6598 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6600 3e3a69f1 2019-07-25 stsp return err;
6602 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6604 f032f1f7 2019-08-04 stsp goto done;
6606 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6607 f032f1f7 2019-08-04 stsp &have_staged_files);
6608 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6609 f032f1f7 2019-08-04 stsp goto done;
6610 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6611 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6612 3e3a69f1 2019-07-25 stsp goto done;
6615 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6617 f032f1f7 2019-08-04 stsp goto done;
6619 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6621 818c7501 2019-07-11 stsp goto done;
6623 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6625 818c7501 2019-07-11 stsp goto done;
6627 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6629 818c7501 2019-07-11 stsp goto done;
6631 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6633 818c7501 2019-07-11 stsp goto done;
6635 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6636 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6638 818c7501 2019-07-11 stsp goto done;
6640 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6642 818c7501 2019-07-11 stsp goto done;
6644 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6646 818c7501 2019-07-11 stsp goto done;
6648 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6649 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6651 818c7501 2019-07-11 stsp goto done;
6653 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6655 818c7501 2019-07-11 stsp goto done;
6657 818c7501 2019-07-11 stsp free(commit_ref_name);
6658 818c7501 2019-07-11 stsp free(branch_ref_name);
6659 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6660 818c7501 2019-07-11 stsp if (commit_ref)
6661 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6662 818c7501 2019-07-11 stsp if (branch_ref)
6663 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6664 818c7501 2019-07-11 stsp if (err) {
6665 818c7501 2019-07-11 stsp free(*commit_id);
6666 818c7501 2019-07-11 stsp *commit_id = NULL;
6667 818c7501 2019-07-11 stsp if (*tmp_branch) {
6668 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6669 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6671 818c7501 2019-07-11 stsp if (*new_base_branch) {
6672 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6673 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6675 818c7501 2019-07-11 stsp if (*branch) {
6676 818c7501 2019-07-11 stsp got_ref_close(*branch);
6677 818c7501 2019-07-11 stsp *branch = NULL;
6679 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6680 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6681 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6683 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6685 818c7501 2019-07-11 stsp return err;
6688 818c7501 2019-07-11 stsp const struct got_error *
6689 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6691 818c7501 2019-07-11 stsp const struct got_error *err;
6692 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6694 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6696 818c7501 2019-07-11 stsp return err;
6698 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6699 818c7501 2019-07-11 stsp free(tmp_branch_name);
6700 818c7501 2019-07-11 stsp return NULL;
6703 818c7501 2019-07-11 stsp static const struct got_error *
6704 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6705 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
6707 0ebf8283 2019-07-24 stsp *logmsg = arg;
6708 818c7501 2019-07-11 stsp return NULL;
6711 818c7501 2019-07-11 stsp static const struct got_error *
6712 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6713 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6714 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6715 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6717 818c7501 2019-07-11 stsp return NULL;
6720 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6721 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6722 01757395 2019-07-12 stsp void *progress_arg;
6723 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6726 01757395 2019-07-12 stsp static const struct got_error *
6727 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6729 01757395 2019-07-12 stsp const struct got_error *err;
6730 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6732 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6734 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6736 01757395 2019-07-12 stsp return err;
6738 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6739 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6740 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6741 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6742 01757395 2019-07-12 stsp return NULL;
6744 01757395 2019-07-12 stsp p = strdup(path);
6745 01757395 2019-07-12 stsp if (p == NULL)
6746 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6748 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6749 01757395 2019-07-12 stsp if (err || new == NULL)
6751 01757395 2019-07-12 stsp return err;
6754 0ebf8283 2019-07-24 stsp static const struct got_error *
6755 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6756 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6758 818c7501 2019-07-11 stsp const struct got_error *err;
6759 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6761 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6762 818c7501 2019-07-11 stsp if (err) {
6763 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6764 818c7501 2019-07-11 stsp goto done;
6765 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6767 818c7501 2019-07-11 stsp goto done;
6768 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6770 818c7501 2019-07-11 stsp goto done;
6771 de05890f 2020-03-05 stsp } else if (is_rebase) {
6772 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6775 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6777 818c7501 2019-07-11 stsp goto done;
6778 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6779 818c7501 2019-07-11 stsp free(stored_id);
6780 818c7501 2019-07-11 stsp if (cmp != 0) {
6781 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6782 818c7501 2019-07-11 stsp goto done;
6786 0ebf8283 2019-07-24 stsp if (commit_ref)
6787 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6788 0ebf8283 2019-07-24 stsp return err;
6791 0ebf8283 2019-07-24 stsp static const struct got_error *
6792 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6793 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6794 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6795 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6796 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6797 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6799 0ebf8283 2019-07-24 stsp const struct got_error *err;
6800 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6801 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6802 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6804 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6806 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6808 0ebf8283 2019-07-24 stsp return err;
6810 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6811 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6812 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6813 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6814 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6815 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6816 818c7501 2019-07-11 stsp if (commit_ref)
6817 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6818 818c7501 2019-07-11 stsp return err;
6821 818c7501 2019-07-11 stsp const struct got_error *
6822 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6823 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6824 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6825 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6826 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6827 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6829 0ebf8283 2019-07-24 stsp const struct got_error *err;
6830 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6832 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6834 0ebf8283 2019-07-24 stsp return err;
6836 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6838 0ebf8283 2019-07-24 stsp goto done;
6840 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6841 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6842 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6844 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6845 0ebf8283 2019-07-24 stsp return err;
6848 0ebf8283 2019-07-24 stsp const struct got_error *
6849 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6850 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6851 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6852 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6853 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6854 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6856 0ebf8283 2019-07-24 stsp const struct got_error *err;
6857 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6859 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6861 0ebf8283 2019-07-24 stsp return err;
6863 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6865 0ebf8283 2019-07-24 stsp goto done;
6867 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6868 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6869 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6871 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6872 0ebf8283 2019-07-24 stsp return err;
6875 0ebf8283 2019-07-24 stsp static const struct got_error *
6876 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6877 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6878 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6879 598eac43 2022-07-22 stsp struct got_reference *tmp_branch, const char *committer,
6880 598eac43 2022-07-22 stsp struct got_commit_object *orig_commit, const char *new_logmsg,
6881 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
6883 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6884 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6885 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6886 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6887 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6888 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6889 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6891 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6892 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6893 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6895 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6897 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6899 3e3a69f1 2019-07-25 stsp return err;
6901 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6902 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6903 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6904 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6905 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
6907 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6908 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6910 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6911 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6912 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6913 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6914 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6915 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6917 01757395 2019-07-12 stsp if (merged_paths) {
6918 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6919 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6920 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6921 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6924 01757395 2019-07-12 stsp goto done;
6927 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6928 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6930 01757395 2019-07-12 stsp goto done;
6933 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6934 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6935 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6937 a0e95631 2019-07-12 stsp goto done;
6938 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6939 a0e95631 2019-07-12 stsp goto done;
6942 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6944 edd02c5e 2019-07-11 stsp goto done;
6946 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6948 818c7501 2019-07-11 stsp goto done;
6950 5943eee2 2019-08-13 stsp if (new_logmsg) {
6951 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6952 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6953 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6954 5943eee2 2019-08-13 stsp goto done;
6957 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6959 5943eee2 2019-08-13 stsp goto done;
6962 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6963 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6964 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
6965 50e7a649 2022-07-22 stsp committer ? committer :
6966 2a47b1e5 2022-11-01 stsp got_object_commit_get_committer(orig_commit), NULL,
6967 50e7a649 2022-07-22 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6969 a0e95631 2019-07-12 stsp goto done;
6971 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6973 a0e95631 2019-07-12 stsp goto done;
6975 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6977 a0e95631 2019-07-12 stsp goto done;
6979 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6980 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6981 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6982 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6983 a0e95631 2019-07-12 stsp err = sync_err;
6985 a0e95631 2019-07-12 stsp free(fileindex_path);
6986 a0e95631 2019-07-12 stsp free(head_commit_id);
6987 a0e95631 2019-07-12 stsp if (head_ref)
6988 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6989 818c7501 2019-07-11 stsp if (err) {
6990 818c7501 2019-07-11 stsp free(*new_commit_id);
6991 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6993 818c7501 2019-07-11 stsp return err;
6996 818c7501 2019-07-11 stsp const struct got_error *
6997 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6998 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6999 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7000 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
7001 12383673 2023-02-18 mark struct got_object_id *orig_commit_id, int allow_conflict,
7002 12383673 2023-02-18 mark struct got_repository *repo)
7004 0ebf8283 2019-07-24 stsp const struct got_error *err;
7005 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7006 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7007 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7009 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7011 0ebf8283 2019-07-24 stsp return err;
7013 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7015 0ebf8283 2019-07-24 stsp goto done;
7016 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7018 0ebf8283 2019-07-24 stsp goto done;
7019 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7020 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7021 0ebf8283 2019-07-24 stsp goto done;
7024 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7025 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
7026 12383673 2023-02-18 mark NULL, allow_conflict, repo);
7028 0ebf8283 2019-07-24 stsp if (commit_ref)
7029 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7030 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7031 0ebf8283 2019-07-24 stsp free(commit_id);
7032 0ebf8283 2019-07-24 stsp return err;
7035 0ebf8283 2019-07-24 stsp const struct got_error *
7036 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7037 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7038 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7039 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
7040 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7041 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
7043 0ebf8283 2019-07-24 stsp const struct got_error *err;
7044 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7045 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7047 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7049 0ebf8283 2019-07-24 stsp return err;
7051 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7053 0ebf8283 2019-07-24 stsp goto done;
7055 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7056 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
7057 12383673 2023-02-18 mark new_logmsg, allow_conflict, repo);
7059 0ebf8283 2019-07-24 stsp if (commit_ref)
7060 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7061 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7062 0ebf8283 2019-07-24 stsp return err;
7065 0ebf8283 2019-07-24 stsp const struct got_error *
7066 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7067 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7069 3e3a69f1 2019-07-25 stsp if (fileindex)
7070 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7071 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7074 69844fba 2019-07-11 stsp static const struct got_error *
7075 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7077 69844fba 2019-07-11 stsp const struct got_error *err;
7078 69844fba 2019-07-11 stsp struct got_reference *ref;
7080 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7081 69844fba 2019-07-11 stsp if (err) {
7082 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7083 69844fba 2019-07-11 stsp return NULL;
7084 69844fba 2019-07-11 stsp return err;
7087 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7088 69844fba 2019-07-11 stsp got_ref_close(ref);
7089 69844fba 2019-07-11 stsp return err;
7092 69844fba 2019-07-11 stsp static const struct got_error *
7093 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7095 69844fba 2019-07-11 stsp const struct got_error *err;
7096 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7097 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7099 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7101 69844fba 2019-07-11 stsp goto done;
7102 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7104 69844fba 2019-07-11 stsp goto done;
7106 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7108 69844fba 2019-07-11 stsp goto done;
7109 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7111 69844fba 2019-07-11 stsp goto done;
7113 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7115 69844fba 2019-07-11 stsp goto done;
7116 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7118 69844fba 2019-07-11 stsp goto done;
7120 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7122 69844fba 2019-07-11 stsp goto done;
7123 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7125 69844fba 2019-07-11 stsp goto done;
7128 69844fba 2019-07-11 stsp free(tmp_branch_name);
7129 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7130 69844fba 2019-07-11 stsp free(branch_ref_name);
7131 69844fba 2019-07-11 stsp free(commit_ref_name);
7132 e600f124 2021-03-21 stsp return err;
7135 336075a4 2022-06-25 op static const struct got_error *
7136 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7137 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7139 e600f124 2021-03-21 stsp const struct got_error *err;
7140 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7141 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7142 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7143 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7144 e600f124 2021-03-21 stsp char *refname = NULL;
7146 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7147 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7148 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7149 e600f124 2021-03-21 stsp branch_name += 11;
7151 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7153 e600f124 2021-03-21 stsp return err;
7155 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7156 e600f124 2021-03-21 stsp new_id_str) == -1) {
7157 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7158 e600f124 2021-03-21 stsp goto done;
7161 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7163 e600f124 2021-03-21 stsp goto done;
7165 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7167 e600f124 2021-03-21 stsp goto done;
7169 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7171 e600f124 2021-03-21 stsp free(new_id_str);
7172 e600f124 2021-03-21 stsp free(refname);
7173 e600f124 2021-03-21 stsp free(old_commit_id);
7175 e600f124 2021-03-21 stsp got_ref_close(ref);
7176 69844fba 2019-07-11 stsp return err;
7179 818c7501 2019-07-11 stsp const struct got_error *
7180 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7181 ef85a376 2023-02-01 mark struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7182 ef85a376 2023-02-01 mark struct got_reference *rebased_branch, struct got_repository *repo,
7183 ef85a376 2023-02-01 mark int create_backup)
7185 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7186 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7187 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7189 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7191 818c7501 2019-07-11 stsp return err;
7193 e600f124 2021-03-21 stsp if (create_backup) {
7194 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7195 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7197 e600f124 2021-03-21 stsp goto done;
7200 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7202 818c7501 2019-07-11 stsp goto done;
7204 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7206 818c7501 2019-07-11 stsp goto done;
7208 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7210 818c7501 2019-07-11 stsp goto done;
7212 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7214 a615e0e7 2020-12-16 stsp goto done;
7216 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7218 a615e0e7 2020-12-16 stsp goto done;
7219 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7220 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7221 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7222 a615e0e7 2020-12-16 stsp err = sync_err;
7224 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7225 a615e0e7 2020-12-16 stsp free(fileindex_path);
7226 818c7501 2019-07-11 stsp free(new_head_commit_id);
7227 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7228 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7229 818c7501 2019-07-11 stsp err = unlockerr;
7230 818c7501 2019-07-11 stsp return err;
7233 818c7501 2019-07-11 stsp const struct got_error *
7234 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7235 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7236 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7237 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7239 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7240 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7241 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7242 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7243 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7244 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7245 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7247 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7249 818c7501 2019-07-11 stsp return err;
7251 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7252 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7254 a44927cc 2022-04-07 stsp goto done;
7256 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7257 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7259 818c7501 2019-07-11 stsp goto done;
7261 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7263 818c7501 2019-07-11 stsp goto done;
7266 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7267 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7268 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7270 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7272 818c7501 2019-07-11 stsp goto done;
7274 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7276 818c7501 2019-07-11 stsp goto done;
7278 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7279 a44927cc 2022-04-07 stsp worktree->path_prefix);
7281 ca355955 2019-07-12 stsp goto done;
7283 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7285 ca355955 2019-07-12 stsp goto done;
7287 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7289 818c7501 2019-07-11 stsp goto done;
7291 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7292 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7293 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7294 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7295 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7296 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7297 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7298 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7299 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7300 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7302 1f1abb7e 2019-08-08 stsp goto sync;
7304 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7305 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7307 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7308 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7309 ca355955 2019-07-12 stsp err = sync_err;
7311 818c7501 2019-07-11 stsp got_ref_close(resolved);
7312 a3a2faf2 2019-07-12 stsp free(tree_id);
7313 818c7501 2019-07-11 stsp free(commit_id);
7314 a44927cc 2022-04-07 stsp if (commit)
7315 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7316 0ebf8283 2019-07-24 stsp if (fileindex)
7317 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7318 0ebf8283 2019-07-24 stsp free(fileindex_path);
7320 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7321 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7322 0ebf8283 2019-07-24 stsp err = unlockerr;
7323 0ebf8283 2019-07-24 stsp return err;
7326 0ebf8283 2019-07-24 stsp const struct got_error *
7327 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7328 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7329 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7330 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7332 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7333 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7334 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7335 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7336 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7337 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7338 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7339 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7341 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7342 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7343 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7344 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7346 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7348 0ebf8283 2019-07-24 stsp return err;
7350 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7352 0ebf8283 2019-07-24 stsp goto done;
7354 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7355 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7356 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7359 0ebf8283 2019-07-24 stsp goto done;
7361 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7363 0ebf8283 2019-07-24 stsp goto done;
7365 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7367 0ebf8283 2019-07-24 stsp goto done;
7369 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7370 0ebf8283 2019-07-24 stsp worktree);
7372 0ebf8283 2019-07-24 stsp goto done;
7374 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7377 0ebf8283 2019-07-24 stsp goto done;
7379 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7381 0ebf8283 2019-07-24 stsp goto done;
7383 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7385 0ebf8283 2019-07-24 stsp goto done;
7387 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7388 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7390 0ebf8283 2019-07-24 stsp goto done;
7391 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7393 0ebf8283 2019-07-24 stsp goto done;
7394 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7395 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7396 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7397 0ebf8283 2019-07-24 stsp goto done;
7400 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7401 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7403 0ebf8283 2019-07-24 stsp goto done;
7404 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7406 0ebf8283 2019-07-24 stsp goto done;
7408 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7410 0ebf8283 2019-07-24 stsp goto done;
7412 0ebf8283 2019-07-24 stsp free(fileindex_path);
7413 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7414 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7415 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7416 0ebf8283 2019-07-24 stsp if (wt_branch)
7417 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7418 0ebf8283 2019-07-24 stsp if (err) {
7419 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7420 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7421 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7423 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7424 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7425 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7427 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7428 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7429 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7430 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7432 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7434 0ebf8283 2019-07-24 stsp return err;
7437 0ebf8283 2019-07-24 stsp const struct got_error *
7438 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7439 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7441 3e3a69f1 2019-07-25 stsp if (fileindex)
7442 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7443 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7446 0ebf8283 2019-07-24 stsp const struct got_error *
7447 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7448 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7450 0ebf8283 2019-07-24 stsp const struct got_error *err;
7451 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7453 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7455 0ebf8283 2019-07-24 stsp return err;
7457 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7458 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7459 0ebf8283 2019-07-24 stsp return NULL;
7462 0ebf8283 2019-07-24 stsp const struct got_error *
7463 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7464 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7465 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7466 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7468 0ebf8283 2019-07-24 stsp const struct got_error *err;
7469 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7470 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7471 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7472 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7473 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7474 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7476 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7477 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7478 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7479 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7481 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7483 3e3a69f1 2019-07-25 stsp return err;
7485 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7487 f032f1f7 2019-08-04 stsp goto done;
7489 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7490 f032f1f7 2019-08-04 stsp &have_staged_files);
7491 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7492 f032f1f7 2019-08-04 stsp goto done;
7493 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7494 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7495 3e3a69f1 2019-07-25 stsp goto done;
7498 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7500 f032f1f7 2019-08-04 stsp goto done;
7502 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7504 0ebf8283 2019-07-24 stsp goto done;
7506 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7508 0ebf8283 2019-07-24 stsp goto done;
7510 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7511 0ebf8283 2019-07-24 stsp worktree);
7513 0ebf8283 2019-07-24 stsp goto done;
7515 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7517 0ebf8283 2019-07-24 stsp goto done;
7519 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7521 0ebf8283 2019-07-24 stsp goto done;
7522 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7524 0ebf8283 2019-07-24 stsp goto done;
7526 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7528 0ebf8283 2019-07-24 stsp goto done;
7529 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7531 0ebf8283 2019-07-24 stsp goto done;
7533 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7535 0ebf8283 2019-07-24 stsp goto done;
7537 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7538 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7539 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7540 0ebf8283 2019-07-24 stsp if (commit_ref)
7541 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7542 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7543 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7544 0ebf8283 2019-07-24 stsp if (err) {
7545 0ebf8283 2019-07-24 stsp free(*commit_id);
7546 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7547 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7548 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7549 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7550 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7551 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7553 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7554 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7555 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7557 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7559 0ebf8283 2019-07-24 stsp return err;
7562 0ebf8283 2019-07-24 stsp static const struct got_error *
7563 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7565 0ebf8283 2019-07-24 stsp const struct got_error *err;
7566 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7567 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7569 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7571 0ebf8283 2019-07-24 stsp goto done;
7572 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7574 0ebf8283 2019-07-24 stsp goto done;
7576 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7577 0ebf8283 2019-07-24 stsp worktree);
7579 0ebf8283 2019-07-24 stsp goto done;
7580 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7582 0ebf8283 2019-07-24 stsp goto done;
7584 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7586 0ebf8283 2019-07-24 stsp goto done;
7587 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7589 0ebf8283 2019-07-24 stsp goto done;
7591 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7593 0ebf8283 2019-07-24 stsp goto done;
7594 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7596 0ebf8283 2019-07-24 stsp goto done;
7598 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7599 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7600 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7601 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7602 0ebf8283 2019-07-24 stsp return err;
7605 0ebf8283 2019-07-24 stsp const struct got_error *
7606 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7607 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7608 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7609 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7611 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7612 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7613 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7614 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7615 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7616 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7618 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7620 0ebf8283 2019-07-24 stsp return err;
7622 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7623 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7625 a44927cc 2022-04-07 stsp goto done;
7627 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7628 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7630 0ebf8283 2019-07-24 stsp goto done;
7632 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7634 0ebf8283 2019-07-24 stsp goto done;
7636 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7638 0ebf8283 2019-07-24 stsp goto done;
7640 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7641 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7643 0ebf8283 2019-07-24 stsp goto done;
7645 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7647 0ebf8283 2019-07-24 stsp goto done;
7649 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7651 0ebf8283 2019-07-24 stsp goto done;
7653 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7654 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7655 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7656 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7657 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7658 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7659 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7660 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7661 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7662 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7664 1f1abb7e 2019-08-08 stsp goto sync;
7666 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7667 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7669 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7670 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7671 0ebf8283 2019-07-24 stsp err = sync_err;
7673 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7674 0ebf8283 2019-07-24 stsp free(tree_id);
7675 818c7501 2019-07-11 stsp free(fileindex_path);
7677 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7678 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7679 818c7501 2019-07-11 stsp err = unlockerr;
7680 818c7501 2019-07-11 stsp return err;
7683 0ebf8283 2019-07-24 stsp const struct got_error *
7684 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7685 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7686 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7688 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7689 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7690 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7691 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7693 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7695 0ebf8283 2019-07-24 stsp return err;
7697 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7698 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7700 e600f124 2021-03-21 stsp goto done;
7702 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7703 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7705 0ebf8283 2019-07-24 stsp goto done;
7707 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7709 0ebf8283 2019-07-24 stsp goto done;
7711 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7713 0ebf8283 2019-07-24 stsp goto done;
7715 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7717 0ebf8283 2019-07-24 stsp goto done;
7719 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7721 a615e0e7 2020-12-16 stsp goto done;
7723 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7725 a615e0e7 2020-12-16 stsp goto done;
7726 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7727 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7728 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7729 a615e0e7 2020-12-16 stsp err = sync_err;
7731 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7732 a615e0e7 2020-12-16 stsp free(fileindex_path);
7733 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7734 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7735 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7736 0ebf8283 2019-07-24 stsp err = unlockerr;
7737 0ebf8283 2019-07-24 stsp return err;
7740 0ebf8283 2019-07-24 stsp const struct got_error *
7741 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7742 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7744 0ebf8283 2019-07-24 stsp const struct got_error *err;
7745 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7747 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7749 0ebf8283 2019-07-24 stsp return err;
7751 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7753 0ebf8283 2019-07-24 stsp goto done;
7755 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7757 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7758 2822a352 2019-10-15 stsp return err;
7761 2822a352 2019-10-15 stsp const struct got_error *
7762 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7763 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7764 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7765 2822a352 2019-10-15 stsp struct got_repository *repo)
7767 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7768 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7769 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7771 2822a352 2019-10-15 stsp *fileindex = NULL;
7772 2822a352 2019-10-15 stsp *branch_ref = NULL;
7773 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7775 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7777 2822a352 2019-10-15 stsp return err;
7779 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7780 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7781 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7782 2822a352 2019-10-15 stsp "update -b or different branch name required");
7783 2822a352 2019-10-15 stsp goto done;
7786 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7788 2822a352 2019-10-15 stsp goto done;
7790 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7791 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7792 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7793 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7796 2822a352 2019-10-15 stsp goto done;
7798 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7800 2822a352 2019-10-15 stsp goto done;
7802 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7803 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7805 2822a352 2019-10-15 stsp if (err) {
7806 2822a352 2019-10-15 stsp if (*branch_ref) {
7807 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7808 2822a352 2019-10-15 stsp *branch_ref = NULL;
7810 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7811 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7812 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7814 2822a352 2019-10-15 stsp if (*fileindex) {
7815 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7816 2822a352 2019-10-15 stsp *fileindex = NULL;
7818 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7820 0cb83759 2019-08-03 stsp return err;
7823 2822a352 2019-10-15 stsp const struct got_error *
7824 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7825 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7826 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7827 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7828 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7830 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7831 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7832 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7833 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7835 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7837 2822a352 2019-10-15 stsp goto done;
7839 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7841 2822a352 2019-10-15 stsp goto done;
7843 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7845 a44927cc 2022-04-07 stsp goto done;
7847 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7848 2822a352 2019-10-15 stsp worktree->path_prefix);
7850 2822a352 2019-10-15 stsp goto done;
7852 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7854 2822a352 2019-10-15 stsp goto done;
7856 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7857 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7859 2822a352 2019-10-15 stsp goto sync;
7861 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7863 2822a352 2019-10-15 stsp goto sync;
7865 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7867 6e210706 2021-01-22 stsp goto sync;
7869 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7871 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7872 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7873 2822a352 2019-10-15 stsp err = sync_err;
7876 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7877 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7878 2822a352 2019-10-15 stsp err = unlockerr;
7879 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7881 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7882 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7883 2822a352 2019-10-15 stsp err = unlockerr;
7884 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7886 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7887 2822a352 2019-10-15 stsp free(fileindex_path);
7888 2822a352 2019-10-15 stsp free(tree_id);
7889 a44927cc 2022-04-07 stsp if (commit)
7890 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7892 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7893 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7894 2822a352 2019-10-15 stsp err = unlockerr;
7895 2822a352 2019-10-15 stsp return err;
7898 2822a352 2019-10-15 stsp const struct got_error *
7899 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7900 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7901 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7903 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7905 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7907 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7909 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7910 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7911 8b692cd0 2019-10-21 stsp err = unlockerr;
7912 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7914 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7915 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7916 8b692cd0 2019-10-21 stsp err = unlockerr;
7917 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7919 f259c4c1 2021-09-24 stsp return err;
7922 f259c4c1 2021-09-24 stsp const struct got_error *
7923 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
7924 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
7926 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
7927 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7929 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7931 f259c4c1 2021-09-24 stsp goto done;
7933 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7935 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
7936 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7937 f259c4c1 2021-09-24 stsp err = sync_err;
7939 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7940 f259c4c1 2021-09-24 stsp free(fileindex_path);
7941 8b692cd0 2019-10-21 stsp return err;
7944 f259c4c1 2021-09-24 stsp static const struct got_error *
7945 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7947 f259c4c1 2021-09-24 stsp const struct got_error *err;
7948 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7950 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7952 f259c4c1 2021-09-24 stsp goto done;
7953 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
7955 f259c4c1 2021-09-24 stsp goto done;
7957 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7959 f259c4c1 2021-09-24 stsp goto done;
7960 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
7962 f259c4c1 2021-09-24 stsp goto done;
7965 f259c4c1 2021-09-24 stsp free(branch_refname);
7966 f259c4c1 2021-09-24 stsp free(commit_refname);
7967 f259c4c1 2021-09-24 stsp return err;
7970 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
7971 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
7972 f259c4c1 2021-09-24 stsp const char *branch_name;
7975 f259c4c1 2021-09-24 stsp static const struct got_error *
7976 2a47b1e5 2022-11-01 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7977 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
7979 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
7981 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7982 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
7983 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
7985 f259c4c1 2021-09-24 stsp return NULL;
7989 f259c4c1 2021-09-24 stsp const struct got_error *
7990 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
7991 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
7992 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
7993 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
7994 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7995 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7997 f259c4c1 2021-09-24 stsp const struct got_error *err;
7998 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8000 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8002 f259c4c1 2021-09-24 stsp goto done;
8004 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8005 f259c4c1 2021-09-24 stsp worktree);
8007 f259c4c1 2021-09-24 stsp goto done;
8009 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8010 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
8011 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
8013 f259c4c1 2021-09-24 stsp free(fileindex_path);
8014 f259c4c1 2021-09-24 stsp return err;
8017 f259c4c1 2021-09-24 stsp const struct got_error *
8018 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
8019 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8020 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
8021 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
8022 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo,
8023 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
8026 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
8027 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
8028 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
8029 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
8030 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
8031 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
8032 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8033 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
8034 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8036 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
8037 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
8039 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
8041 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8043 f259c4c1 2021-09-24 stsp goto done;
8045 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8047 f259c4c1 2021-09-24 stsp goto done;
8049 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8051 f259c4c1 2021-09-24 stsp goto done;
8053 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8054 f259c4c1 2021-09-24 stsp &have_staged_files);
8055 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8056 f259c4c1 2021-09-24 stsp goto done;
8057 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8058 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8059 f259c4c1 2021-09-24 stsp goto done;
8062 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
8063 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
8064 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
8065 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
8066 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
8067 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8068 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
8069 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8070 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8072 f259c4c1 2021-09-24 stsp goto done;
8074 f259c4c1 2021-09-24 stsp if (TAILQ_EMPTY(&commitable_paths)) {
8075 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8076 f259c4c1 2021-09-24 stsp "merge of %s cannot proceed", branch_name);
8077 f259c4c1 2021-09-24 stsp goto done;
8080 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
8081 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
8082 f259c4c1 2021-09-24 stsp const char *ct_path = ct->in_repo_path;
8084 f259c4c1 2021-09-24 stsp while (ct_path[0] == '/')
8085 f259c4c1 2021-09-24 stsp ct_path++;
8086 f259c4c1 2021-09-24 stsp err = check_out_of_date(ct_path, ct->status,
8087 f259c4c1 2021-09-24 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8088 f259c4c1 2021-09-24 stsp head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8090 f259c4c1 2021-09-24 stsp goto done;
8094 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
8095 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
8096 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
8097 2a47b1e5 2022-11-01 stsp head_commit_id, branch_tip, worktree, author, committer, NULL,
8098 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8100 f259c4c1 2021-09-24 stsp goto done;
8102 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
8103 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
8104 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8105 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8106 f259c4c1 2021-09-24 stsp err = sync_err;
8108 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
8109 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
8111 f259c4c1 2021-09-24 stsp free_commitable(ct);
8113 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8114 f259c4c1 2021-09-24 stsp free(fileindex_path);
8115 f259c4c1 2021-09-24 stsp return err;
8118 f259c4c1 2021-09-24 stsp const struct got_error *
8119 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
8120 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
8122 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8123 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8125 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8127 f259c4c1 2021-09-24 stsp goto done;
8129 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8131 f259c4c1 2021-09-24 stsp goto done;
8132 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8133 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8134 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8135 f259c4c1 2021-09-24 stsp err = sync_err;
8137 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8138 f259c4c1 2021-09-24 stsp free(fileindex_path);
8139 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8140 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8141 f259c4c1 2021-09-24 stsp err = unlockerr;
8142 f259c4c1 2021-09-24 stsp return err;
8145 f259c4c1 2021-09-24 stsp const struct got_error *
8146 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8147 f259c4c1 2021-09-24 stsp struct got_repository *repo)
8149 f259c4c1 2021-09-24 stsp const struct got_error *err;
8150 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
8151 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
8153 f259c4c1 2021-09-24 stsp *in_progress = 0;
8155 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8157 f259c4c1 2021-09-24 stsp return err;
8158 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8159 793fcac3 2021-09-24 stsp free(branch_refname);
8160 f259c4c1 2021-09-24 stsp if (err) {
8161 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
8162 f259c4c1 2021-09-24 stsp return err;
8164 f259c4c1 2021-09-24 stsp *in_progress = 1;
8166 f259c4c1 2021-09-24 stsp return NULL;
8169 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
8170 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
8171 f259c4c1 2021-09-24 stsp struct got_reference *branch, struct got_repository *repo)
8173 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
8174 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8175 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
8176 f259c4c1 2021-09-24 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8177 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL;
8178 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8179 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
8181 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8183 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8185 f259c4c1 2021-09-24 stsp return err;
8187 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8189 f259c4c1 2021-09-24 stsp goto done;
8191 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
8192 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
8193 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
8194 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8197 f259c4c1 2021-09-24 stsp goto done;
8199 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8201 f259c4c1 2021-09-24 stsp return err;
8203 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8205 f259c4c1 2021-09-24 stsp return err;
8207 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8210 f259c4c1 2021-09-24 stsp goto done;
8212 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8214 f259c4c1 2021-09-24 stsp goto done;
8216 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8217 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8218 f259c4c1 2021-09-24 stsp goto done;
8221 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
8223 f259c4c1 2021-09-24 stsp goto done;
8225 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8227 f259c4c1 2021-09-24 stsp goto done;
8228 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
8230 f259c4c1 2021-09-24 stsp goto done;
8232 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8234 f259c4c1 2021-09-24 stsp goto done;
8235 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
8237 f259c4c1 2021-09-24 stsp goto done;
8240 f259c4c1 2021-09-24 stsp free(branch_refname);
8241 f259c4c1 2021-09-24 stsp free(commit_refname);
8242 f259c4c1 2021-09-24 stsp free(fileindex_path);
8243 f259c4c1 2021-09-24 stsp if (branch_ref)
8244 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8245 f259c4c1 2021-09-24 stsp if (commit_ref)
8246 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8247 f259c4c1 2021-09-24 stsp if (wt_branch)
8248 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
8249 f259c4c1 2021-09-24 stsp free(wt_branch_tip);
8250 f259c4c1 2021-09-24 stsp if (err) {
8251 f259c4c1 2021-09-24 stsp if (*fileindex) {
8252 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8253 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8255 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8257 f259c4c1 2021-09-24 stsp return err;
8260 f259c4c1 2021-09-24 stsp const struct got_error *
8261 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
8262 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8263 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8265 f259c4c1 2021-09-24 stsp const struct got_error *err;
8266 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
8267 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8268 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8269 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8271 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8272 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8273 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8275 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8277 f259c4c1 2021-09-24 stsp return err;
8279 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8281 f259c4c1 2021-09-24 stsp goto done;
8283 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8284 f259c4c1 2021-09-24 stsp &have_staged_files);
8285 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8286 f259c4c1 2021-09-24 stsp goto done;
8287 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8288 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
8289 f259c4c1 2021-09-24 stsp goto done;
8292 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8294 f259c4c1 2021-09-24 stsp goto done;
8296 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8298 f259c4c1 2021-09-24 stsp goto done;
8300 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8302 f259c4c1 2021-09-24 stsp goto done;
8304 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
8305 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8306 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
8307 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
8308 f259c4c1 2021-09-24 stsp goto done;
8310 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8311 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
8312 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
8313 f259c4c1 2021-09-24 stsp goto done;
8316 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8318 f259c4c1 2021-09-24 stsp goto done;
8320 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
8322 f259c4c1 2021-09-24 stsp goto done;
8324 f259c4c1 2021-09-24 stsp free(commit_refname);
8325 f259c4c1 2021-09-24 stsp free(branch_refname);
8326 f259c4c1 2021-09-24 stsp free(fileindex_path);
8327 f259c4c1 2021-09-24 stsp if (commit_ref)
8328 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8329 f259c4c1 2021-09-24 stsp if (branch_ref)
8330 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8331 f259c4c1 2021-09-24 stsp if (err) {
8332 f259c4c1 2021-09-24 stsp if (*branch_name) {
8333 f259c4c1 2021-09-24 stsp free(*branch_name);
8334 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8336 f259c4c1 2021-09-24 stsp free(*branch_tip);
8337 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8338 f259c4c1 2021-09-24 stsp if (*fileindex) {
8339 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8340 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8342 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8344 f259c4c1 2021-09-24 stsp return err;
8347 f259c4c1 2021-09-24 stsp const struct got_error *
8348 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
8349 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8350 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8352 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8353 f259c4c1 2021-09-24 stsp struct got_object_id *commit_id = NULL;
8354 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8355 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8356 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
8357 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
8359 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
8360 a44927cc 2022-04-07 stsp worktree->base_commit_id);
8362 a44927cc 2022-04-07 stsp goto done;
8364 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8365 a44927cc 2022-04-07 stsp worktree->path_prefix);
8367 f259c4c1 2021-09-24 stsp goto done;
8369 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8371 f259c4c1 2021-09-24 stsp goto done;
8373 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8375 f259c4c1 2021-09-24 stsp goto done;
8377 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
8378 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
8379 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
8380 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
8381 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
8382 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
8383 f259c4c1 2021-09-24 stsp rfa.repo = repo;
8384 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
8385 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8386 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8388 f259c4c1 2021-09-24 stsp goto sync;
8390 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8391 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8393 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8394 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8395 f259c4c1 2021-09-24 stsp err = sync_err;
8397 f259c4c1 2021-09-24 stsp free(tree_id);
8398 f259c4c1 2021-09-24 stsp free(commit_id);
8399 a44927cc 2022-04-07 stsp if (commit)
8400 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8401 f259c4c1 2021-09-24 stsp if (fileindex)
8402 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8403 f259c4c1 2021-09-24 stsp free(fileindex_path);
8405 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8406 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8407 f259c4c1 2021-09-24 stsp err = unlockerr;
8408 f259c4c1 2021-09-24 stsp return err;
8411 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8412 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8413 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8414 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8415 2db2652d 2019-08-07 stsp struct got_repository *repo;
8416 2db2652d 2019-08-07 stsp int have_changes;
8419 336075a4 2022-06-25 op static const struct got_error *
8420 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8421 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8422 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8423 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8425 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8426 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8427 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8428 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8429 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8430 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8432 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8433 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8434 8b13ce36 2019-08-08 stsp return NULL;
8435 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8436 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8438 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8439 735ef5ac 2019-08-03 stsp if (ie == NULL)
8440 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8442 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8443 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8444 735ef5ac 2019-08-03 stsp relpath) == -1)
8445 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8447 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8448 b4b2adf5 2023-02-09 op base_commit_idp = got_fileindex_entry_get_commit_id(
8449 b4b2adf5 2023-02-09 op &base_commit_id, ie);
8452 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8453 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8454 3aa5969e 2019-08-06 stsp goto done;
8455 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8456 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8457 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8458 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8459 735ef5ac 2019-08-03 stsp goto done;
8462 2db2652d 2019-08-07 stsp a->have_changes = 1;
8464 735ef5ac 2019-08-03 stsp p = in_repo_path;
8465 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8467 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8468 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8469 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8471 735ef5ac 2019-08-03 stsp free(in_repo_path);
8472 dc424a06 2019-08-07 stsp return err;
8475 2db2652d 2019-08-07 stsp struct stage_path_arg {
8476 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8477 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8478 2db2652d 2019-08-07 stsp struct got_repository *repo;
8479 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8480 2db2652d 2019-08-07 stsp void *status_arg;
8481 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8482 2db2652d 2019-08-07 stsp void *patch_arg;
8483 7b5dc508 2019-10-28 stsp int staged_something;
8484 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8487 2db2652d 2019-08-07 stsp static const struct got_error *
8488 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8489 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8490 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8491 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8493 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8494 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8495 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8496 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8497 0cb83759 2019-08-03 stsp uint32_t stage;
8498 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8499 0aeb8099 2020-07-23 stsp struct stat sb;
8501 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8502 8b13ce36 2019-08-08 stsp return NULL;
8504 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8505 d3e7c587 2019-08-03 stsp if (ie == NULL)
8506 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8508 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8509 2db2652d 2019-08-07 stsp relpath)== -1)
8510 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8512 0cb83759 2019-08-03 stsp switch (status) {
8513 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8514 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8515 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8516 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8517 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8520 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8521 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8522 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8523 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8524 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8527 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8530 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8531 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8532 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8533 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8534 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8538 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8539 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8542 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8543 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8544 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8545 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8547 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8548 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8549 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8550 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8551 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8552 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8553 35213c7c 2020-07-23 stsp ssize_t target_len;
8554 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8555 35213c7c 2020-07-23 stsp sizeof(target_path));
8556 35213c7c 2020-07-23 stsp if (target_len == -1) {
8557 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8558 35213c7c 2020-07-23 stsp ondisk_path);
8561 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8562 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8563 35213c7c 2020-07-23 stsp a->worktree->root_path);
8566 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8567 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8568 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8572 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8573 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8574 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8576 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8577 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8579 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8580 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8582 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8583 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8585 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8586 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8587 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8591 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
8592 9fdde394 2022-06-04 op * implicitly unstage the file.
8594 9fdde394 2022-06-04 op if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8595 9fdde394 2022-06-04 op sizeof(ie->blob_sha1)) == 0) {
8596 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
8597 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
8600 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8601 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8603 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8604 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8605 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8606 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8609 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8611 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8612 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8616 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8617 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8618 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8619 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8621 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8622 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8625 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8627 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8628 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8630 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8631 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8634 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8638 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8639 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8640 dc424a06 2019-08-07 stsp free(path_content);
8641 2db2652d 2019-08-07 stsp free(ondisk_path);
8642 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8643 0ebf8283 2019-07-24 stsp return err;
8646 0cb83759 2019-08-03 stsp const struct got_error *
8647 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8648 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8649 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8650 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8651 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8653 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8654 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8655 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8656 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8657 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8658 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8659 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8660 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8662 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8664 0cb83759 2019-08-03 stsp return err;
8666 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8667 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8669 735ef5ac 2019-08-03 stsp goto done;
8670 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8672 735ef5ac 2019-08-03 stsp goto done;
8673 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8675 0cb83759 2019-08-03 stsp goto done;
8677 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8678 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8679 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8680 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8681 2db2652d 2019-08-07 stsp oka.repo = repo;
8682 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8683 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8684 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8685 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
8687 735ef5ac 2019-08-03 stsp goto done;
8689 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8690 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8691 2db2652d 2019-08-07 stsp goto done;
8694 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8695 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8696 2db2652d 2019-08-07 stsp spa.repo = repo;
8697 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8698 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8699 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8700 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8701 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8702 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8703 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8704 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8705 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
8707 2db2652d 2019-08-07 stsp goto done;
8709 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8710 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8711 7b5dc508 2019-10-28 stsp goto done;
8714 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8715 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8716 0cb83759 2019-08-03 stsp err = sync_err;
8718 735ef5ac 2019-08-03 stsp if (head_ref)
8719 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8720 735ef5ac 2019-08-03 stsp free(head_commit_id);
8721 0cb83759 2019-08-03 stsp free(fileindex_path);
8722 0cb83759 2019-08-03 stsp if (fileindex)
8723 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8724 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8725 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8726 0cb83759 2019-08-03 stsp err = unlockerr;
8727 0cb83759 2019-08-03 stsp return err;
8730 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8731 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8732 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8733 ad493afc 2019-08-03 stsp struct got_repository *repo;
8734 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8735 ad493afc 2019-08-03 stsp void *progress_arg;
8736 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8737 2e1f37b0 2019-08-08 stsp void *patch_arg;
8740 2e1f37b0 2019-08-08 stsp static const struct got_error *
8741 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8742 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8743 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8744 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8745 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8747 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8748 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8749 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8750 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8751 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8752 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8753 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8754 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8756 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8757 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8759 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8761 2e1f37b0 2019-08-08 stsp return err;
8763 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
8764 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
8765 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8766 eb81bc23 2022-06-28 tracey goto done;
8768 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
8769 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
8770 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8771 eb81bc23 2022-06-28 tracey goto done;
8774 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8776 2e1f37b0 2019-08-08 stsp goto done;
8778 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8780 2e1f37b0 2019-08-08 stsp goto done;
8782 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8784 2e1f37b0 2019-08-08 stsp goto done;
8786 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8789 2e1f37b0 2019-08-08 stsp goto done;
8791 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8793 2e1f37b0 2019-08-08 stsp goto done;
8795 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8797 2e1f37b0 2019-08-08 stsp goto done;
8799 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8800 4b752015 2022-06-30 stsp path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8802 2e1f37b0 2019-08-08 stsp goto done;
8804 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8805 b90054ed 2022-11-01 stsp "got-unstaged-content", "");
8807 2e1f37b0 2019-08-08 stsp goto done;
8808 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8809 b90054ed 2022-11-01 stsp "got-new-staged-content", "");
8811 2e1f37b0 2019-08-08 stsp goto done;
8813 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8814 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8815 2e1f37b0 2019-08-08 stsp goto done;
8817 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8818 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8819 2e1f37b0 2019-08-08 stsp goto done;
8821 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8822 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8823 eb81bc23 2022-06-28 tracey struct diff_chunk_context cc = {};
8824 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8825 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8826 fe621944 2020-11-10 stsp nchanges++;
8828 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8829 2e1f37b0 2019-08-08 stsp int choice;
8830 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8831 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8832 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8833 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8835 2e1f37b0 2019-08-08 stsp goto done;
8836 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8837 2e1f37b0 2019-08-08 stsp have_content = 1;
8839 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8840 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8843 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8844 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8845 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8847 2e1f37b0 2019-08-08 stsp free(label1);
8848 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8849 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8851 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8852 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8853 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8854 2e1f37b0 2019-08-08 stsp if (staged_blob)
8855 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8856 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8857 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8858 fe621944 2020-11-10 stsp err = free_err;
8859 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8860 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8861 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8862 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8863 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8864 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8865 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8866 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8867 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8868 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8869 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8870 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8871 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8872 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8873 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8874 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8875 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8876 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8877 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8879 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8880 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8881 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8882 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8883 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8884 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8885 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8887 2e1f37b0 2019-08-08 stsp free(path1);
8888 2e1f37b0 2019-08-08 stsp free(path2);
8889 fda8017d 2020-07-23 stsp return err;
8892 fda8017d 2020-07-23 stsp static const struct got_error *
8893 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8894 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8895 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8896 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8897 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8898 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8899 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8901 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8902 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8903 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8904 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8905 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8906 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8907 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8908 fda8017d 2020-07-23 stsp struct stat sb;
8910 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8911 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8912 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8914 fda8017d 2020-07-23 stsp return err;
8916 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8917 fda8017d 2020-07-23 stsp return NULL;
8919 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8920 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8921 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8923 fda8017d 2020-07-23 stsp goto done;
8926 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
8927 fda8017d 2020-07-23 stsp if (f == NULL) {
8928 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8929 fda8017d 2020-07-23 stsp path_unstaged_content);
8930 fda8017d 2020-07-23 stsp goto done;
8932 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8933 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8934 fda8017d 2020-07-23 stsp goto done;
8936 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8937 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8938 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8940 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8941 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8942 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8943 fda8017d 2020-07-23 stsp goto done;
8945 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8946 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8947 fda8017d 2020-07-23 stsp goto done;
8949 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8950 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8951 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8952 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8953 fda8017d 2020-07-23 stsp progress_arg);
8955 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8957 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8959 67a66647 2021-05-31 stsp return err;
8961 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8962 67a66647 2021-05-31 stsp parent) == -1) {
8963 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8964 67a66647 2021-05-31 stsp base_path = NULL;
8965 67a66647 2021-05-31 stsp goto done;
8968 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_base_path, &f_base,
8969 b90054ed 2022-11-01 stsp base_path, "");
8971 67a66647 2021-05-31 stsp goto done;
8972 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8973 67a66647 2021-05-31 stsp blob_base);
8975 67a66647 2021-05-31 stsp goto done;
8978 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8979 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8981 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8982 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8983 eec2f5a9 2021-06-03 stsp ondisk_path);
8985 eec2f5a9 2021-06-03 stsp goto done;
8988 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
8989 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8990 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8991 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8992 eec2f5a9 2021-06-03 stsp goto done;
8994 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8995 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8996 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8997 eec2f5a9 2021-06-03 stsp close(fd);
8998 eec2f5a9 2021-06-03 stsp goto done;
9002 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9003 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9004 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9005 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9006 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9009 fda8017d 2020-07-23 stsp goto done;
9011 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9012 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9013 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9015 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9016 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9019 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9020 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9021 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9022 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9023 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9024 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9025 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9026 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9027 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9028 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9029 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9030 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9031 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9032 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9033 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9034 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9035 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9036 67a66647 2021-05-31 stsp free(blob_base_path);
9037 67a66647 2021-05-31 stsp free(parent);
9038 67a66647 2021-05-31 stsp free(base_path);
9039 2e1f37b0 2019-08-08 stsp return err;
9042 ad493afc 2019-08-03 stsp static const struct got_error *
9043 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9044 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9045 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9046 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9048 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9049 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9050 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9051 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9052 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9053 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9054 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9055 9bc94a15 2019-08-03 stsp struct stat sb;
9056 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
9058 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9059 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9060 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9061 2e1f37b0 2019-08-08 stsp return NULL;
9063 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9064 ad493afc 2019-08-03 stsp if (ie == NULL)
9065 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9067 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9069 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9071 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9072 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9074 f69721c3 2019-10-21 stsp goto done;
9075 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9076 f69721c3 2019-10-21 stsp id_str) == -1) {
9077 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9078 eb81bc23 2022-06-28 tracey goto done;
9081 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9082 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9083 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9084 eb81bc23 2022-06-28 tracey goto done;
9086 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9087 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9088 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9089 f69721c3 2019-10-21 stsp goto done;
9092 ad493afc 2019-08-03 stsp switch (staged_status) {
9093 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9094 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9095 eb81bc23 2022-06-28 tracey blob_id, 8192, fd1);
9098 ad493afc 2019-08-03 stsp /* fall through */
9099 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9100 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9101 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9102 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9103 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9104 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9107 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9110 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9111 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9112 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9113 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9114 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9115 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9118 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9119 eb81bc23 2022-06-28 tracey staged_blob_id, 8192, fd2);
9122 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9123 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9124 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9125 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9126 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9127 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9128 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9129 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9130 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9132 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9133 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9134 36bf999c 2020-07-23 stsp char *staged_target;
9135 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9136 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9138 36bf999c 2020-07-23 stsp goto done;
9139 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9140 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9141 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9142 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9143 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9144 36bf999c 2020-07-23 stsp free(staged_target);
9146 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9147 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9148 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9149 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9150 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9151 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9152 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9156 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9159 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9160 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9161 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9162 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9165 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9166 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9167 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9168 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9169 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9172 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9174 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9175 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9179 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9180 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9181 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9182 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9185 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9189 ad493afc 2019-08-03 stsp free(ondisk_path);
9190 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9191 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9192 ad493afc 2019-08-03 stsp if (blob_base)
9193 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9194 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9195 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9196 ad493afc 2019-08-03 stsp if (blob_staged)
9197 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9198 f69721c3 2019-10-21 stsp free(id_str);
9199 f69721c3 2019-10-21 stsp free(label_orig);
9200 ad493afc 2019-08-03 stsp return err;
9203 ad493afc 2019-08-03 stsp const struct got_error *
9204 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9205 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9206 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9207 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9208 ad493afc 2019-08-03 stsp struct got_repository *repo)
9210 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9211 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9212 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9213 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9214 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9216 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9218 ad493afc 2019-08-03 stsp return err;
9220 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9222 ad493afc 2019-08-03 stsp goto done;
9224 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9225 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9226 ad493afc 2019-08-03 stsp upa.repo = repo;
9227 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9228 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9229 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9230 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9231 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9232 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9233 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
9235 ad493afc 2019-08-03 stsp goto done;
9238 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9239 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9240 ad493afc 2019-08-03 stsp err = sync_err;
9242 ad493afc 2019-08-03 stsp free(fileindex_path);
9243 ad493afc 2019-08-03 stsp if (fileindex)
9244 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9245 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9246 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9247 ad493afc 2019-08-03 stsp err = unlockerr;
9248 ad493afc 2019-08-03 stsp return err;
9251 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9252 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9253 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9254 b2118c49 2020-07-28 stsp void *info_arg;
9255 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9256 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9257 b2118c49 2020-07-28 stsp void *cancel_arg;
9260 b2118c49 2020-07-28 stsp static const struct got_error *
9261 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9263 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9264 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9265 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9266 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9267 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9268 b2118c49 2020-07-28 stsp int stage;
9270 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9271 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9273 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9274 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9275 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9278 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9279 b2118c49 2020-07-28 stsp return NULL;
9281 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
9282 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9283 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9284 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9285 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9286 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9287 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
9290 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
9291 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9293 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9294 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9297 b2118c49 2020-07-28 stsp const struct got_error *
9298 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9299 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9300 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9301 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9304 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9305 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9306 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9307 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9309 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9311 b2118c49 2020-07-28 stsp return err;
9313 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9315 b2118c49 2020-07-28 stsp goto done;
9317 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9318 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9319 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9320 b2118c49 2020-07-28 stsp arg.paths = paths;
9321 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9322 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9323 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9326 b2118c49 2020-07-28 stsp free(fileindex_path);
9327 b2118c49 2020-07-28 stsp if (fileindex)
9328 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9329 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9330 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9331 b2118c49 2020-07-28 stsp err = unlockerr;
9332 b2118c49 2020-07-28 stsp return err;
9335 78f5ac24 2022-03-19 op static const struct got_error *
9336 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
9337 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
9338 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
9340 78f5ac24 2022-03-19 op const struct got_error *err;
9341 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
9342 78f5ac24 2022-03-19 op struct stat sb;
9343 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
9345 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
9349 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9350 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
9351 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
9353 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9355 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
9356 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9361 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
9362 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
9363 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
9364 78f5ac24 2022-03-19 op if (errno != ENOENT) {
9365 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
9366 78f5ac24 2022-03-19 op ondisk_path);
9369 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
9374 78f5ac24 2022-03-19 op free(ondisk_path);
9378 78f5ac24 2022-03-19 op static const struct got_error *
9379 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
9380 78f5ac24 2022-03-19 op unsigned char staged_status)
9382 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9383 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9384 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9385 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9386 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
9387 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
9388 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9389 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9390 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9391 78f5ac24 2022-03-19 op return NULL;
9394 78f5ac24 2022-03-19 op static const struct got_error *
9395 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
9397 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
9398 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9399 78f5ac24 2022-03-19 op return NULL;
9402 78f5ac24 2022-03-19 op static const struct got_error *
9403 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
9404 78f5ac24 2022-03-19 op unsigned char staged_status)
9406 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9407 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9408 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9409 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9410 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
9411 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9412 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9413 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9414 78f5ac24 2022-03-19 op return NULL;
9417 78f5ac24 2022-03-19 op const struct got_error *
9418 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
9419 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
9421 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
9424 78f5ac24 2022-03-19 op const struct got_error *
9425 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
9426 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
9427 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
9429 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
9430 78f5ac24 2022-03-19 op int file_renamed = 0;
9431 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
9432 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
9434 78f5ac24 2022-03-19 op *oldpath = NULL;
9435 78f5ac24 2022-03-19 op *newpath = NULL;
9437 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
9438 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
9442 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
9443 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
9447 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
9448 78f5ac24 2022-03-19 op file_renamed = 1;
9450 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
9451 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9452 78f5ac24 2022-03-19 op else if (file_renamed) {
9453 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9454 78f5ac24 2022-03-19 op if (err == NULL)
9455 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9456 78f5ac24 2022-03-19 op } else if (old == NULL)
9457 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9459 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
9463 78f5ac24 2022-03-19 op free(*oldpath);
9464 78f5ac24 2022-03-19 op *oldpath = NULL;
9465 78f5ac24 2022-03-19 op free(*newpath);
9466 78f5ac24 2022-03-19 op *newpath = NULL;
9471 78f5ac24 2022-03-19 op const struct got_error *
9472 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9473 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9474 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
9476 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
9478 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
9479 f2dd7807 2022-05-17 op saa.worktree = worktree;
9480 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
9481 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
9482 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
9483 f2dd7807 2022-05-17 op saa.repo = repo;
9485 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9486 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
9489 f2dd7807 2022-05-17 op const struct got_error *
9490 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9491 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9492 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
9494 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
9496 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
9497 f2dd7807 2022-05-17 op sda.worktree = worktree;
9498 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
9499 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
9500 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
9501 f2dd7807 2022-05-17 op sda.repo = repo;
9502 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
9503 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
9504 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
9505 f2dd7807 2022-05-17 op sda.status_codes = NULL;
9507 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9508 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9511 f2dd7807 2022-05-17 op const struct got_error *
9512 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
9513 4bcdc895 2022-05-17 op const char *fileindex_path)
9515 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
9517 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
9518 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);