2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
20 d1f6d47b 2019-02-04 stsp #include <dirent.h>
21 12ce7a6c 2019-08-12 stsp #include <limits.h>
22 f5c49f82 2019-01-06 stsp #include <stddef.h>
23 86c3caaf 2018-03-09 stsp #include <string.h>
24 86c3caaf 2018-03-09 stsp #include <stdio.h>
25 86c3caaf 2018-03-09 stsp #include <stdlib.h>
26 303e14b5 2019-09-23 stsp #include <time.h>
27 86c3caaf 2018-03-09 stsp #include <fcntl.h>
28 86c3caaf 2018-03-09 stsp #include <errno.h>
29 86c3caaf 2018-03-09 stsp #include <unistd.h>
30 9d31a1d8 2018-03-11 stsp #include <zlib.h>
31 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
32 512f0d0e 2019-01-02 stsp #include <libgen.h>
34 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
36 86c3caaf 2018-03-09 stsp #include "got_error.h"
37 86c3caaf 2018-03-09 stsp #include "got_repository.h"
38 5261c201 2018-04-01 stsp #include "got_reference.h"
39 9d31a1d8 2018-03-11 stsp #include "got_object.h"
40 1dd54920 2019-05-11 stsp #include "got_path.h"
41 e6209546 2019-08-22 stsp #include "got_cancel.h"
42 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 234035bc 2019-06-01 stsp #include "got_diff.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
53 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
54 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
55 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
56 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
59 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 a2c162eb 2022-10-30 thomas static mode_t apply_umask(mode_t);
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
95 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &tmpfile, path, "");
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
114 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 09fe317a 2018-03-11 stsp return err;
120 024e9686 2019-05-14 stsp static const struct got_error *
121 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
123 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
124 024e9686 2019-05-14 stsp char *refstr = NULL;
126 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
127 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
128 024e9686 2019-05-14 stsp if (refstr == NULL)
129 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
131 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
132 024e9686 2019-05-14 stsp if (refstr == NULL)
133 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
135 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 024e9686 2019-05-14 stsp free(refstr);
137 024e9686 2019-05-14 stsp return err;
140 86c3caaf 2018-03-09 stsp const struct got_error *
141 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
142 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
144 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
145 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
146 ec22038e 2019-03-10 stsp uuid_t uuid;
147 ec22038e 2019-03-10 stsp uint32_t uuid_status;
148 65596e15 2018-12-24 stsp int obj_type;
149 7ac97322 2018-03-11 stsp char *path_got = NULL;
150 1451e70d 2018-03-10 stsp char *formatstr = NULL;
151 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
152 65596e15 2018-12-24 stsp char *basestr = NULL;
153 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
155 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
160 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
162 65596e15 2018-12-24 stsp return err;
163 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
165 65596e15 2018-12-24 stsp return err;
166 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
169 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
170 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
171 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
174 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
175 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
180 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
181 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
185 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
190 056e7441 2018-03-11 stsp /* Create an empty lock file. */
191 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
195 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
196 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
200 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
201 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
205 65596e15 2018-12-24 stsp /* Record our base commit. */
206 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
209 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
213 1451e70d 2018-03-10 stsp /* Store path to repository. */
214 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
219 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
220 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
225 ec22038e 2019-03-10 stsp /* Generate UUID. */
226 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
227 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
228 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
231 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
233 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
236 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
240 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
241 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
245 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
250 65596e15 2018-12-24 stsp free(commit_id);
251 7ac97322 2018-03-11 stsp free(path_got);
252 1451e70d 2018-03-10 stsp free(formatstr);
253 0bb8a95e 2018-03-12 stsp free(absprefix);
254 65596e15 2018-12-24 stsp free(basestr);
255 ec22038e 2019-03-10 stsp free(uuidstr);
256 86c3caaf 2018-03-09 stsp return err;
259 247140b2 2019-02-05 stsp const struct got_error *
260 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 e5dc7198 2018-12-29 stsp const char *path_prefix)
263 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
265 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
266 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
269 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
270 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
271 e5dc7198 2018-12-29 stsp free(absprefix);
272 e5dc7198 2018-12-29 stsp return NULL;
275 bc70eb79 2019-05-09 stsp const char *
276 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
278 36a38700 2019-05-10 stsp return worktree->head_ref_name;
281 024e9686 2019-05-14 stsp const struct got_error *
282 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
283 024e9686 2019-05-14 stsp struct got_reference *head_ref)
285 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
286 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
288 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
290 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
291 024e9686 2019-05-14 stsp path_got = NULL;
295 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
296 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
297 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
301 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
305 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
306 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
308 024e9686 2019-05-14 stsp free(path_got);
310 024e9686 2019-05-14 stsp free(head_ref_name);
311 024e9686 2019-05-14 stsp return err;
314 b72f483a 2019-02-05 stsp struct got_object_id *
315 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
317 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
320 507dc3bb 2018-12-29 stsp const struct got_error *
321 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
324 507dc3bb 2018-12-29 stsp const struct got_error *err;
325 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
326 507dc3bb 2018-12-29 stsp char *id_str = NULL;
327 507dc3bb 2018-12-29 stsp char *path_got = NULL;
329 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
331 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
332 507dc3bb 2018-12-29 stsp path_got = NULL;
336 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
338 507dc3bb 2018-12-29 stsp return err;
340 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
345 507dc3bb 2018-12-29 stsp /* Record our base commit. */
346 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
349 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
353 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
354 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
355 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
356 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
361 507dc3bb 2018-12-29 stsp got_object_close(obj);
362 507dc3bb 2018-12-29 stsp free(id_str);
363 507dc3bb 2018-12-29 stsp free(path_got);
364 507dc3bb 2018-12-29 stsp return err;
367 50b0790e 2020-09-11 stsp const struct got_gotconfig *
368 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
370 50b0790e 2020-09-11 stsp return worktree->gotconfig;
373 9d31a1d8 2018-03-11 stsp static const struct got_error *
374 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
376 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
379 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
380 86c3caaf 2018-03-09 stsp return NULL;
383 21908da4 2019-01-13 stsp static const struct got_error *
384 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
386 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
387 4a1ddfc2 2019-01-12 stsp char *abspath;
389 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
392 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
393 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 ddcd8544 2019-03-11 stsp struct stat sb;
395 ddcd8544 2019-03-11 stsp err = NULL;
396 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
397 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
398 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
399 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
400 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
403 4a1ddfc2 2019-01-12 stsp free(abspath);
404 68c76935 2019-02-19 stsp return err;
407 68c76935 2019-02-19 stsp static const struct got_error *
408 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
410 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
411 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
412 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
413 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
418 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
420 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
423 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
425 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
428 68c76935 2019-02-19 stsp if (flen1 == 0) {
429 68c76935 2019-02-19 stsp if (flen2 != 0)
432 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
433 68c76935 2019-02-19 stsp if (flen1 != 0)
436 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
437 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
447 68c76935 2019-02-19 stsp return err;
450 68c76935 2019-02-19 stsp static const struct got_error *
451 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
453 68c76935 2019-02-19 stsp struct stat sb;
454 68c76935 2019-02-19 stsp size_t size1, size2;
458 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
459 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
460 68c76935 2019-02-19 stsp size1 = sb.st_size;
462 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
463 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
464 68c76935 2019-02-19 stsp size2 = sb.st_size;
466 68c76935 2019-02-19 stsp if (size1 != size2) {
468 68c76935 2019-02-19 stsp return NULL;
471 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
472 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
473 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
474 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
476 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
479 24f136e0 2021-11-20 thomas static const struct got_error *
480 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
482 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
483 24f136e0 2021-11-20 thomas size_t flen;
484 24f136e0 2021-11-20 thomas ssize_t outlen;
486 24f136e0 2021-11-20 thomas *outsize = 0;
488 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
489 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
491 24f136e0 2021-11-20 thomas for (;;) {
492 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
493 24f136e0 2021-11-20 thomas if (flen == 0) {
494 24f136e0 2021-11-20 thomas if (ferror(f))
495 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
496 24f136e0 2021-11-20 thomas if (feof(f))
499 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
500 24f136e0 2021-11-20 thomas if (outlen == -1)
501 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
502 24f136e0 2021-11-20 thomas if (outlen != flen)
503 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
504 24f136e0 2021-11-20 thomas *outsize += outlen;
507 24f136e0 2021-11-20 thomas return NULL;
510 24f136e0 2021-11-20 thomas static const struct got_error *
511 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
512 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 24f136e0 2021-11-20 thomas const char *ondisk_path)
516 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
517 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
518 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
522 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
524 24f136e0 2021-11-20 thomas *overlapcnt = 0;
526 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
528 24f136e0 2021-11-20 thomas return err;
530 24f136e0 2021-11-20 thomas if (same_content)
531 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
533 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
535 24f136e0 2021-11-20 thomas return err;
536 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
537 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
539 24f136e0 2021-11-20 thomas return err;
540 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
542 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
543 24f136e0 2021-11-20 thomas *overlapcnt = 1;
544 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
546 24f136e0 2021-11-20 thomas goto done;
548 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
550 24f136e0 2021-11-20 thomas goto done;
552 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
554 24f136e0 2021-11-20 thomas goto done;
556 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 fc2a50f2 2022-11-01 thomas base_path_orig, "");
559 24f136e0 2021-11-20 thomas goto done;
560 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 fc2a50f2 2022-11-01 thomas base_path_deriv, "");
563 24f136e0 2021-11-20 thomas goto done;
564 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 fc2a50f2 2022-11-01 thomas base_path_deriv2, "");
567 24f136e0 2021-11-20 thomas goto done;
568 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
570 24f136e0 2021-11-20 thomas goto done;
571 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
573 24f136e0 2021-11-20 thomas goto done;
574 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
576 24f136e0 2021-11-20 thomas goto done;
577 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
578 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
579 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
580 24f136e0 2021-11-20 thomas goto done;
582 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
585 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
586 24f136e0 2021-11-20 thomas path_deriv) < 0) {
587 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
588 24f136e0 2021-11-20 thomas goto done;
590 24f136e0 2021-11-20 thomas if (size_orig > 0) {
591 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
593 24f136e0 2021-11-20 thomas label_orig ? " " : "",
594 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
595 24f136e0 2021-11-20 thomas path_orig) < 0) {
596 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
597 24f136e0 2021-11-20 thomas goto done;
600 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
602 24f136e0 2021-11-20 thomas path_deriv2,
603 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
604 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
605 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
606 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
607 24f136e0 2021-11-20 thomas goto done;
609 24f136e0 2021-11-20 thomas } else if (changed_deriv)
610 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 24f136e0 2021-11-20 thomas else if (changed_deriv2)
612 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
614 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
615 24f136e0 2021-11-20 thomas err == NULL)
616 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
617 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
619 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
621 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
623 24f136e0 2021-11-20 thomas free(path_orig);
624 24f136e0 2021-11-20 thomas free(path_deriv);
625 24f136e0 2021-11-20 thomas free(path_deriv2);
626 24f136e0 2021-11-20 thomas free(base_path_orig);
627 24f136e0 2021-11-20 thomas free(base_path_deriv);
628 24f136e0 2021-11-20 thomas free(base_path_deriv2);
629 24f136e0 2021-11-20 thomas return err;
633 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
634 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
635 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
636 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
637 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
639 6353ad76 2019-02-08 stsp static const struct got_error *
640 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
643 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
647 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
648 6353ad76 2019-02-08 stsp int merged_fd = -1;
649 db590691 2021-06-02 stsp FILE *f_merged = NULL;
650 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
651 234035bc 2019-06-01 stsp int overlapcnt = 0;
652 3524bbf9 2020-10-20 stsp char *parent = NULL;
654 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
656 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
658 3524bbf9 2020-10-20 stsp return err;
660 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
665 fc2a50f2 2022-11-01 thomas err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
669 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
671 24f136e0 2021-11-20 thomas if (err) {
672 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
673 24f136e0 2021-11-20 thomas goto done;
674 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
676 24f136e0 2021-11-20 thomas ondisk_path);
678 24f136e0 2021-11-20 thomas goto done;
681 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
682 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
686 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
687 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
691 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
692 db590691 2021-06-02 stsp if (f_merged == NULL) {
693 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
696 db590691 2021-06-02 stsp merged_fd = -1;
698 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
699 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
700 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
706 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
707 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
711 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
712 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
713 230a42bd 2019-05-11 jcs ondisk_path);
718 fdcb7daf 2019-12-15 stsp if (merged_path)
719 fdcb7daf 2019-12-15 stsp unlink(merged_path);
721 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
723 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
725 6353ad76 2019-02-08 stsp free(merged_path);
726 af54ae4a 2019-02-19 stsp free(base_path);
727 3524bbf9 2020-10-20 stsp free(parent);
728 af57b12a 2020-07-23 stsp return err;
731 af57b12a 2020-07-23 stsp static const struct got_error *
732 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
733 af57b12a 2020-07-23 stsp size_t target_len)
735 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
736 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
737 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
738 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
739 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
740 af57b12a 2020-07-23 stsp ondisk_path);
741 af57b12a 2020-07-23 stsp return NULL;
745 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
747 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
748 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
750 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
751 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
753 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
754 993e2a1b 2020-07-23 stsp * has deleted this symlink.
756 11cc08c1 2020-07-23 stsp static const struct got_error *
757 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
758 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
761 11cc08c1 2020-07-23 stsp const struct got_error *err;
762 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 11cc08c1 2020-07-23 stsp FILE *f = NULL;
765 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
767 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
769 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
770 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
775 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
777 3818e3c4 2020-11-01 naddy goto done;
779 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
780 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
784 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
787 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
788 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
789 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
790 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
791 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
792 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
797 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
798 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
801 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
802 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
806 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
807 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
808 11cc08c1 2020-07-23 stsp free(path);
809 11cc08c1 2020-07-23 stsp free(id_str);
810 11cc08c1 2020-07-23 stsp free(label_deriv);
811 11cc08c1 2020-07-23 stsp return err;
814 d219f183 2020-07-23 stsp /* forward declaration */
815 d219f183 2020-07-23 stsp static const struct got_error *
816 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
818 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
819 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
822 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
823 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
824 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
825 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
827 af57b12a 2020-07-23 stsp static const struct got_error *
828 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
829 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
830 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
831 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
834 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
835 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
836 af57b12a 2020-07-23 stsp struct stat sb;
837 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
838 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
839 11cc08c1 2020-07-23 stsp int have_local_change = 0;
840 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
842 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
843 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
845 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
846 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
847 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
848 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
849 af57b12a 2020-07-23 stsp ondisk_path);
852 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
854 526a746f 2020-07-23 stsp if (blob_orig) {
855 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
860 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
861 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
862 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 11cc08c1 2020-07-23 stsp have_local_change = 1;
865 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
866 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
867 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
868 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
871 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
872 11cc08c1 2020-07-23 stsp if (ancestor_target) {
873 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
874 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
876 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
877 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
879 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
882 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
883 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
884 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
885 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
888 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
891 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
892 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
893 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
894 af57b12a 2020-07-23 stsp strlen(deriv_target));
897 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
899 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
900 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
902 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
905 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
906 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
907 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
910 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
916 af57b12a 2020-07-23 stsp free(ancestor_target);
917 eec2f5a9 2021-06-03 stsp return err;
920 eec2f5a9 2021-06-03 stsp static const struct got_error *
921 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
923 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
924 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
925 eec2f5a9 2021-06-03 stsp ssize_t target_len;
929 eec2f5a9 2021-06-03 stsp *outfile = NULL;
931 eec2f5a9 2021-06-03 stsp f = got_opentemp();
932 eec2f5a9 2021-06-03 stsp if (f == NULL)
933 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
934 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
936 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
939 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
940 eec2f5a9 2021-06-03 stsp if (n != target_len) {
941 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
944 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
945 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
948 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
949 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
956 eec2f5a9 2021-06-03 stsp *outfile = f;
957 14c901f1 2019-08-08 stsp return err;
961 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
963 14c901f1 2019-08-08 stsp * acts as the second derived version.
965 14c901f1 2019-08-08 stsp static const struct got_error *
966 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
968 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
969 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
970 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
973 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
974 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
976 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
979 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
981 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
983 ed6b5030 2020-10-20 stsp return err;
985 67a66647 2021-05-31 stsp if (blob_orig) {
986 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
987 67a66647 2021-05-31 stsp parent) == -1) {
988 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
989 67a66647 2021-05-31 stsp base_path = NULL;
993 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_orig_path, &f_orig,
994 fc2a50f2 2022-11-01 thomas base_path, "");
997 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
998 67a66647 2021-05-31 stsp blob_orig);
1000 67a66647 2021-05-31 stsp goto done;
1001 67a66647 2021-05-31 stsp free(base_path);
1004 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1005 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1006 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1008 db590691 2021-06-02 stsp f_orig = got_opentemp();
1009 db590691 2021-06-02 stsp if (f_orig == NULL) {
1010 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1011 db590691 2021-06-02 stsp goto done;
1015 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1016 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1017 14c901f1 2019-08-08 stsp base_path = NULL;
1018 14c901f1 2019-08-08 stsp goto done;
1021 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1023 14c901f1 2019-08-08 stsp goto done;
1024 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1025 14c901f1 2019-08-08 stsp blob_deriv);
1027 14c901f1 2019-08-08 stsp goto done;
1029 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1031 14c901f1 2019-08-08 stsp goto done;
1032 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1033 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1034 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1035 14c901f1 2019-08-08 stsp goto done;
1039 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1040 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1042 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1043 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1045 eec2f5a9 2021-06-03 stsp goto done;
1048 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1049 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1050 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1051 eec2f5a9 2021-06-03 stsp goto done;
1053 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1054 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1055 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1056 eec2f5a9 2021-06-03 stsp close(fd);
1057 eec2f5a9 2021-06-03 stsp goto done;
1061 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1062 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1063 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1065 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1066 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1067 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1068 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1069 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1070 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1071 14c901f1 2019-08-08 stsp free(base_path);
1072 67a66647 2021-05-31 stsp if (blob_orig_path) {
1073 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1074 67a66647 2021-05-31 stsp free(blob_orig_path);
1076 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1077 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1078 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1080 6353ad76 2019-02-08 stsp free(id_str);
1081 818c7501 2019-07-11 stsp free(label_deriv);
1082 ed6b5030 2020-10-20 stsp free(parent);
1083 4a1ddfc2 2019-01-12 stsp return err;
1086 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1087 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1088 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1089 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1091 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1092 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1094 65b05cec 2020-07-23 stsp *new_iep = NULL;
1096 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1098 054041d0 2020-06-24 stsp return err;
1100 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1101 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1103 054041d0 2020-06-24 stsp goto done;
1105 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1108 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1110 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1111 13d9040b 2019-03-27 stsp return err;
1114 1ebedb77 2019-10-19 stsp static mode_t
1115 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1117 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1119 1ebedb77 2019-10-19 stsp if (executable) {
1120 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1121 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1122 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1123 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1124 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1125 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1128 a2c162eb 2022-10-30 thomas return st_mode;
1131 a2c162eb 2022-10-30 thomas static mode_t
1132 a2c162eb 2022-10-30 thomas apply_umask(mode_t mode)
1134 a2c162eb 2022-10-30 thomas mode_t um;
1136 a2c162eb 2022-10-30 thomas um = umask(000);
1137 a2c162eb 2022-10-30 thomas umask(um);
1138 a2c162eb 2022-10-30 thomas return mode & ~um;
1141 8ba819a3 2020-07-23 stsp /* forward declaration */
1142 13d9040b 2019-03-27 stsp static const struct got_error *
1143 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1144 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1145 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1146 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1147 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1148 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1151 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1152 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1154 8ba819a3 2020-07-23 stsp static const struct got_error *
1155 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1156 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1158 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1159 5a1fbc73 2020-07-23 stsp ssize_t elen;
1160 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1163 c6e8a826 2021-04-05 stsp *did_something = 0;
1166 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1167 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1168 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1169 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1170 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1171 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1173 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1174 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1175 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1176 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1178 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1179 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1180 5a1fbc73 2020-07-23 stsp if (elen == -1)
1181 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1183 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1184 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1185 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1188 c6e8a826 2021-04-05 stsp *did_something = 1;
1189 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1190 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1191 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1192 5a1fbc73 2020-07-23 stsp return err;
1195 3c1ec782 2020-07-23 stsp static const struct got_error *
1196 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1197 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1199 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1200 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1201 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1203 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1205 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1206 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1207 3c1ec782 2020-07-23 stsp return NULL;
1211 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1212 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1213 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1214 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1215 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1217 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1218 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1219 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1221 ce031e9e 2020-10-20 stsp return err;
1222 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1223 ce031e9e 2020-10-20 stsp free(parent);
1224 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1226 ce031e9e 2020-10-20 stsp free(parent);
1227 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1228 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1229 3c1ec782 2020-07-23 stsp free(abspath);
1230 3c1ec782 2020-07-23 stsp return err;
1232 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1233 3c1ec782 2020-07-23 stsp free(abspath);
1235 3c1ec782 2020-07-23 stsp return err;
1237 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1239 3c1ec782 2020-07-23 stsp return err;
1242 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1243 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1244 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1245 3c1ec782 2020-07-23 stsp return NULL;
1248 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1249 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1250 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1251 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1252 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1253 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1255 3c1ec782 2020-07-23 stsp free(path_got);
1256 3c1ec782 2020-07-23 stsp return NULL;
1259 5a1fbc73 2020-07-23 stsp static const struct got_error *
1260 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1261 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1262 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1263 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1264 ace90326 2021-09-27 thomas struct got_repository *repo,
1265 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1267 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1268 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1269 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1270 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1271 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1273 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1276 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1277 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1278 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1279 8ba819a3 2020-07-23 stsp * in the blob object.
1282 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1283 3521b466 2022-07-21 thomas if (err)
1284 3521b466 2022-07-21 thomas return err;
1286 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1287 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1288 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1289 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1290 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1291 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1292 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1293 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1294 3b9f0f87 2020-07-23 stsp progress_arg);
1296 8ba819a3 2020-07-23 stsp if (len > 0) {
1297 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1298 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1299 8ba819a3 2020-07-23 stsp len - hdrlen);
1300 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1301 8ba819a3 2020-07-23 stsp hdrlen = 0;
1303 8ba819a3 2020-07-23 stsp } while (len != 0);
1304 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1306 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1307 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1309 3c1ec782 2020-07-23 stsp return err;
1311 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1312 906c123b 2020-07-23 stsp /* install as a regular file */
1313 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1314 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1315 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1316 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1317 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1318 9e39ca77 2022-07-21 thomas return err;
1321 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1322 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1323 c6e8a826 2021-04-05 stsp int symlink_replaced;
1324 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1325 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1326 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1327 9e39ca77 2022-07-21 thomas return err;
1329 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1330 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1332 9e39ca77 2022-07-21 thomas return err;
1333 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1334 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1335 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1336 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1337 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1338 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1340 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1341 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1344 9e39ca77 2022-07-21 thomas return err; /* Nothing else to do. */
1347 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1348 f4994adc 2020-10-20 stsp char *parent;
1349 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1351 9e39ca77 2022-07-21 thomas return err;
1352 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1353 f4994adc 2020-10-20 stsp free(parent);
1355 9e39ca77 2022-07-21 thomas return err;
1357 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1358 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1360 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1361 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1362 9e39ca77 2022-07-21 thomas return err;
1366 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1367 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1368 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1369 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1370 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1371 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1372 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1373 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1374 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1375 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1376 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1377 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1378 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1380 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1381 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1383 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1384 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1385 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1386 8ba819a3 2020-07-23 stsp return err;
1389 8ba819a3 2020-07-23 stsp static const struct got_error *
1390 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1391 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1392 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1393 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1394 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1395 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1397 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1398 507dc3bb 2018-12-29 stsp int fd = -1;
1399 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1400 507dc3bb 2018-12-29 stsp int update = 0;
1401 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1402 a2c162eb 2022-10-30 thomas mode_t mode;
1404 a2c162eb 2022-10-30 thomas mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1405 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1406 a2c162eb 2022-10-30 thomas O_CLOEXEC, mode);
1407 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1408 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1409 f5375317 2020-10-20 stsp char *parent;
1410 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1412 f5375317 2020-10-20 stsp return err;
1413 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1414 f5375317 2020-10-20 stsp free(parent);
1416 21908da4 2019-01-13 stsp return err;
1417 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1418 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1420 21908da4 2019-01-13 stsp if (fd == -1)
1421 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1422 230a42bd 2019-05-11 jcs ondisk_path);
1423 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1424 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1425 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1426 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1427 3b9f0f87 2020-07-23 stsp goto done;
1429 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1430 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1431 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1432 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1433 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1434 507dc3bb 2018-12-29 stsp goto done;
1436 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1437 fc2a50f2 2022-11-01 thomas ondisk_path, "");
1439 507dc3bb 2018-12-29 stsp goto done;
1440 507dc3bb 2018-12-29 stsp update = 1;
1442 a2c162eb 2022-10-30 thomas if (fchmod(fd, apply_umask(mode)) == -1) {
1443 a2c162eb 2022-10-30 thomas err = got_error_from_errno2("fchmod",
1444 a2c162eb 2022-10-30 thomas tmppath);
1445 a2c162eb 2022-10-30 thomas goto done;
1449 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1452 bd6aa359 2020-07-23 stsp if (progress_cb) {
1453 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1454 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1456 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1457 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1460 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1461 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1463 bd6aa359 2020-07-23 stsp goto done;
1466 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1468 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1469 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1472 9d31a1d8 2018-03-11 stsp if (len > 0) {
1473 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1474 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1475 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1476 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1477 b87c6f83 2018-12-24 stsp goto done;
1478 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1479 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1480 b87c6f83 2018-12-24 stsp goto done;
1482 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1484 9d31a1d8 2018-03-11 stsp } while (len != 0);
1486 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1487 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1488 816dc654 2019-02-16 stsp goto done;
1491 507dc3bb 2018-12-29 stsp if (update) {
1492 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1493 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1494 f6d8c0ac 2020-11-09 stsp goto done;
1496 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1497 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1498 230a42bd 2019-05-11 jcs ondisk_path);
1499 68ed9ba5 2019-02-10 stsp goto done;
1501 63df146d 2020-11-09 stsp free(tmppath);
1502 63df146d 2020-11-09 stsp tmppath = NULL;
1506 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1507 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1508 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1509 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1510 507dc3bb 2018-12-29 stsp free(tmppath);
1511 6353ad76 2019-02-08 stsp return err;
1515 be94c032 2023-02-20 thomas * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1516 be94c032 2023-02-20 thomas * conflict marker is found in newly added lines only.
1518 6353ad76 2019-02-08 stsp static const struct got_error *
1519 be94c032 2023-02-20 thomas get_modified_file_content_status(unsigned char *status,
1520 be94c032 2023-02-20 thomas struct got_blob_object *blob, const char *path, struct stat *sb,
1521 be94c032 2023-02-20 thomas FILE *ondisk_file)
1523 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1524 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1525 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1526 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1527 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1529 be94c032 2023-02-20 thomas FILE *f = NULL, *f1 = NULL;
1530 7154f6ce 2019-03-27 stsp int i = 0;
1531 9bdd68dd 2020-01-02 naddy char *line = NULL;
1532 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1533 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1534 be94c032 2023-02-20 thomas off_t sz1 = 0;
1536 be94c032 2023-02-20 thomas if (*status == GOT_STATUS_MODIFY) {
1537 be94c032 2023-02-20 thomas f = got_opentemp();
1538 be94c032 2023-02-20 thomas if (f == NULL)
1539 be94c032 2023-02-20 thomas return got_error_from_errno("got_opentemp");
1541 be94c032 2023-02-20 thomas f1 = got_opentemp();
1542 be94c032 2023-02-20 thomas if (f1 == NULL) {
1543 be94c032 2023-02-20 thomas err = got_error_from_errno("got_opentemp");
1544 be94c032 2023-02-20 thomas goto done;
1547 be94c032 2023-02-20 thomas if (blob) {
1548 be94c032 2023-02-20 thomas err = got_object_blob_dump_to_file(&sz1, NULL, NULL,
1549 be94c032 2023-02-20 thomas f1, blob);
1550 be94c032 2023-02-20 thomas if (err)
1551 be94c032 2023-02-20 thomas goto done;
1554 be94c032 2023-02-20 thomas err = got_diff_blob_file(blob, f1, sz1, NULL, ondisk_file, 1,
1555 be94c032 2023-02-20 thomas sb, path, GOT_DIFF_ALGORITHM_MYERS, 0, 0, 0, NULL, f);
1556 be94c032 2023-02-20 thomas if (err)
1557 be94c032 2023-02-20 thomas goto done;
1559 be94c032 2023-02-20 thomas if (fflush(f) == EOF) {
1560 be94c032 2023-02-20 thomas err = got_error_from_errno("fflush");
1561 be94c032 2023-02-20 thomas goto done;
1563 be94c032 2023-02-20 thomas if (fseeko(f, 0L, SEEK_SET) == -1) {
1564 be94c032 2023-02-20 thomas err = got_error_from_errno("fseek");
1565 be94c032 2023-02-20 thomas goto done;
1569 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1570 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1571 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1572 7154f6ce 2019-03-27 stsp if (feof(f))
1574 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1578 be94c032 2023-02-20 thomas if (*line == '+' &&
1579 be94c032 2023-02-20 thomas strncmp(line + 1, markers[i], strlen(markers[i])) == 0) {
1580 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1582 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1589 9bdd68dd 2020-01-02 naddy free(line);
1590 be94c032 2023-02-20 thomas if (f != NULL && fclose(f) == EOF && err == NULL)
1591 be94c032 2023-02-20 thomas err = got_error_from_errno("fclose");
1592 be94c032 2023-02-20 thomas if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1593 be94c032 2023-02-20 thomas err = got_error_from_errno("fclose");
1595 7154f6ce 2019-03-27 stsp return err;
1598 e2b1e152 2019-07-13 stsp static int
1599 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1601 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1602 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1605 1ebedb77 2019-10-19 stsp static int
1606 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1608 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1609 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1610 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1611 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1612 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1613 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1616 c363b2c1 2019-08-03 stsp static unsigned char
1617 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1619 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1620 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1621 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1622 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1623 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1624 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1625 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1627 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1631 a919d5c4 2020-07-23 stsp static const struct got_error *
1632 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1633 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1634 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1636 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1637 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1638 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1639 a919d5c4 2020-07-23 stsp ssize_t elen;
1640 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1641 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1642 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1644 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1646 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1648 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1650 a919d5c4 2020-07-23 stsp return err;
1651 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1653 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1654 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1656 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1658 a919d5c4 2020-07-23 stsp if (len > 0) {
1659 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1660 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1661 a919d5c4 2020-07-23 stsp len - hdrlen);
1662 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1663 a919d5c4 2020-07-23 stsp hdrlen = 0;
1665 a919d5c4 2020-07-23 stsp } while (len != 0);
1666 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1668 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1669 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1670 a919d5c4 2020-07-23 stsp if (elen == -1)
1671 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1673 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1674 a919d5c4 2020-07-23 stsp if (elen == -1)
1675 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1678 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1679 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1681 a919d5c4 2020-07-23 stsp return NULL;
1684 7154f6ce 2019-03-27 stsp static const struct got_error *
1685 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1686 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1687 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1689 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1690 6353ad76 2019-02-08 stsp struct got_object_id id;
1691 6353ad76 2019-02-08 stsp size_t hdrlen;
1692 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1693 6353ad76 2019-02-08 stsp FILE *f = NULL;
1694 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1695 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1696 6353ad76 2019-02-08 stsp size_t flen, blen;
1697 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
1699 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
1700 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1701 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1704 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1705 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1706 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1708 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1709 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1710 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1711 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1712 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1714 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1715 882ef1b9 2019-12-13 stsp goto done;
1717 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1718 3d35a492 2019-12-13 stsp goto done;
1721 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1722 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1723 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1724 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1725 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1726 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1727 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1728 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1729 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1730 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1731 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1733 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1734 3d35a492 2019-12-13 stsp goto done;
1736 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1737 1338848f 2019-12-13 stsp goto done;
1741 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1742 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1743 1338848f 2019-12-13 stsp goto done;
1746 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1747 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1748 1338848f 2019-12-13 stsp goto done;
1749 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1750 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1751 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1752 1338848f 2019-12-13 stsp goto done;
1755 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1756 f179e66d 2020-07-23 stsp goto done;
1758 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1759 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1760 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1761 1338848f 2019-12-13 stsp goto done;
1764 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1765 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1766 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
1768 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
1770 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1771 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1772 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1773 f4ae6ddb 2022-07-01 thomas goto done;
1775 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1777 1338848f 2019-12-13 stsp goto done;
1779 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1780 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1781 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1782 a919d5c4 2020-07-23 stsp goto done;
1785 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1786 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1787 ab0d4361 2019-12-13 stsp if (fd == -1) {
1788 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1789 ab0d4361 2019-12-13 stsp goto done;
1793 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1794 6353ad76 2019-02-08 stsp if (f == NULL) {
1795 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1796 6353ad76 2019-02-08 stsp goto done;
1799 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1801 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1802 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1804 7154f6ce 2019-03-27 stsp goto done;
1805 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1806 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1807 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1808 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1809 7154f6ce 2019-03-27 stsp goto done;
1811 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1812 6353ad76 2019-02-08 stsp if (flen != 0)
1813 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1815 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1816 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1817 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1819 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1820 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1821 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1822 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1826 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1829 6353ad76 2019-02-08 stsp hdrlen = 0;
1832 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1833 7154f6ce 2019-03-27 stsp rewind(f);
1834 be94c032 2023-02-20 thomas err = get_modified_file_content_status(status, blob, ie->path,
1836 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1837 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1839 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1840 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1842 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1843 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1844 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1845 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1846 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1847 9d31a1d8 2018-03-11 stsp return err;
1851 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1852 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1854 e2b1e152 2019-07-13 stsp static const struct got_error *
1855 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1856 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1858 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1859 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1860 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1862 e2b1e152 2019-07-13 stsp return NULL;
1865 9d31a1d8 2018-03-11 stsp static const struct got_error *
1866 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1867 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1868 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1869 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1870 0584f854 2019-04-06 stsp void *progress_arg)
1872 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1873 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1874 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1875 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1876 b8f41171 2019-02-10 stsp struct stat sb;
1877 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1879 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1880 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1883 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1884 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1885 a76c42e6 2019-08-03 stsp goto done;
1887 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1890 abb4604f 2019-07-27 stsp goto done;
1891 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1892 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1894 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1895 e6f4ba31 2021-09-24 thomas if (errno != ENOENT) {
1896 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1897 e6f4ba31 2021-09-24 thomas ondisk_path);
1898 e6f4ba31 2021-09-24 thomas goto done;
1900 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1901 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1902 e6f4ba31 2021-09-24 thomas } else {
1903 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1904 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1906 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1910 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1912 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1913 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1914 b8f41171 2019-02-10 stsp goto done;
1916 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1918 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1919 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1921 5036ab18 2020-04-18 stsp goto done;
1924 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1925 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1926 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1928 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1929 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1930 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1931 c6e8a826 2021-04-05 stsp * updating contents of this file.
1933 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1934 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1935 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1936 c6e8a826 2021-04-05 stsp /* Same commit. */
1937 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1938 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1940 e2b1e152 2019-07-13 stsp goto done;
1941 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1943 6353ad76 2019-02-08 stsp goto done;
1945 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1946 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1947 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1948 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1949 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1950 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1952 0f58026f 2021-04-05 stsp goto done;
1953 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1955 b8f41171 2019-02-10 stsp goto done;
1959 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1960 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1961 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1962 f4ae6ddb 2022-07-01 thomas goto done;
1964 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1966 6353ad76 2019-02-08 stsp goto done;
1968 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1969 234035bc 2019-06-01 stsp int update_timestamps;
1970 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1971 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1972 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1973 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
1974 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
1975 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1976 f4ae6ddb 2022-07-01 thomas goto done;
1978 234035bc 2019-06-01 stsp struct got_object_id id2;
1979 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id2, ie);
1980 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1983 f69721c3 2019-10-21 stsp goto done;
1985 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1986 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1987 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1988 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1989 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1990 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1991 f69721c3 2019-10-21 stsp goto done;
1993 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1994 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1995 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1996 234035bc 2019-06-01 stsp goto done;
1999 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2000 36bf999c 2020-07-23 stsp char *link_target;
2001 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2003 36bf999c 2020-07-23 stsp goto done;
2004 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2005 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2006 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2007 36bf999c 2020-07-23 stsp free(link_target);
2009 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2010 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2011 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2012 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2014 f69721c3 2019-10-21 stsp free(label_orig);
2015 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2016 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2017 f4ae6ddb 2022-07-01 thomas goto done;
2019 234035bc 2019-06-01 stsp if (blob2)
2020 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2022 909d120e 2019-09-22 stsp goto done;
2024 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2025 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2026 234035bc 2019-06-01 stsp * unmodified files again.
2028 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2029 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2030 234035bc 2019-06-01 stsp update_timestamps);
2031 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2032 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2033 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2034 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2035 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2037 1ee397ad 2019-07-12 stsp goto done;
2038 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2039 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2041 13d9040b 2019-03-27 stsp goto done;
2043 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2044 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2045 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2046 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2047 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2048 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2049 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2051 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2052 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2053 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2054 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2055 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2058 13d9040b 2019-03-27 stsp goto done;
2061 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2062 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2063 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2065 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2066 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2067 054041d0 2020-06-24 stsp &blob->id);
2070 13d9040b 2019-03-27 stsp goto done;
2072 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2073 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2074 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2078 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2079 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2080 f4ae6ddb 2022-07-01 thomas goto done;
2082 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2084 6353ad76 2019-02-08 stsp free(ondisk_path);
2085 8da9e5f4 2019-01-12 stsp return err;
2088 90285c3b 2019-01-08 stsp static const struct got_error *
2089 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2091 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2092 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2094 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2095 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2097 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2098 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2099 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2101 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2102 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2104 1c4cdd89 2021-06-20 stsp goto done;
2105 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2106 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2107 fddefe3b 2020-10-20 stsp free(ondisk_path);
2108 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2109 1c4cdd89 2021-06-20 stsp parent = NULL;
2110 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2111 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2112 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2113 fddefe3b 2020-10-20 stsp ondisk_path);
2116 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2122 90285c3b 2019-01-08 stsp free(ondisk_path);
2123 1c4cdd89 2021-06-20 stsp free(parent);
2124 90285c3b 2019-01-08 stsp return err;
2127 708d8e67 2019-03-27 stsp static const struct got_error *
2128 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2129 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2130 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2132 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2133 708d8e67 2019-03-27 stsp unsigned char status;
2134 708d8e67 2019-03-27 stsp struct stat sb;
2135 708d8e67 2019-03-27 stsp char *ondisk_path;
2137 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2138 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2140 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2142 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2144 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2146 5a58a424 2020-06-23 stsp goto done;
2148 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2149 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2150 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2151 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2152 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2153 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2154 993e2a1b 2020-07-23 stsp goto done;
2156 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2157 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2158 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2159 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2161 993e2a1b 2020-07-23 stsp goto done;
2162 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2163 993e2a1b 2020-07-23 stsp ie->path);
2164 993e2a1b 2020-07-23 stsp goto done;
2167 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2168 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2169 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2171 5a58a424 2020-06-23 stsp goto done;
2173 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2174 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2176 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2177 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2179 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2181 5a58a424 2020-06-23 stsp goto done;
2182 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2183 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2185 5a58a424 2020-06-23 stsp goto done;
2187 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2190 5a58a424 2020-06-23 stsp free(ondisk_path);
2191 fc6346c4 2019-03-27 stsp return err;
2194 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2195 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2196 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2197 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2198 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2199 8da9e5f4 2019-01-12 stsp void *progress_arg;
2200 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2201 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2204 512f0d0e 2019-01-02 stsp static const struct got_error *
2205 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2206 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2208 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2210 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2211 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2213 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2214 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2217 8da9e5f4 2019-01-12 stsp static const struct got_error *
2218 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2220 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2222 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2223 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2225 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2226 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2229 9d31a1d8 2018-03-11 stsp static const struct got_error *
2230 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2232 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2233 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2234 8da9e5f4 2019-01-12 stsp char *path;
2236 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2237 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2239 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2240 63c5ca5d 2019-08-24 stsp return NULL;
2242 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2243 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2245 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2247 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2248 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2250 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2251 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2253 8da9e5f4 2019-01-12 stsp free(path);
2254 0cd1c46a 2019-03-11 stsp return err;
2257 b2118c49 2020-07-28 stsp const struct got_error *
2258 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2260 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2262 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2263 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2264 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2265 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2268 b2118c49 2020-07-28 stsp return NULL;
2271 818c7501 2019-07-11 stsp static const struct got_error *
2272 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2274 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2275 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2277 517bab73 2019-03-11 stsp *refname = NULL;
2279 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2281 b2118c49 2020-07-28 stsp return err;
2283 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2284 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2285 0647c563 2019-03-11 stsp *refname = NULL;
2287 517bab73 2019-03-11 stsp free(uuidstr);
2288 517bab73 2019-03-11 stsp return err;
2291 818c7501 2019-07-11 stsp const struct got_error *
2292 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2293 f6cd0243 2023-01-28 thomas const char *prefix)
2295 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2298 f6cd0243 2023-01-28 thomas const struct got_error *
2299 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2301 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2304 818c7501 2019-07-11 stsp static const struct got_error *
2305 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2307 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2308 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2311 818c7501 2019-07-11 stsp static const struct got_error *
2312 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2314 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2317 818c7501 2019-07-11 stsp static const struct got_error *
2318 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2320 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2321 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2324 818c7501 2019-07-11 stsp static const struct got_error *
2325 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2327 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2328 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2331 0ebf8283 2019-07-24 stsp static const struct got_error *
2332 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2334 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2335 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2338 0ebf8283 2019-07-24 stsp static const struct got_error *
2339 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2341 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2342 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2345 0ebf8283 2019-07-24 stsp static const struct got_error *
2346 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2348 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2349 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2352 0ebf8283 2019-07-24 stsp static const struct got_error *
2353 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2355 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2356 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2359 0ebf8283 2019-07-24 stsp const struct got_error *
2360 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2361 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2363 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2364 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2365 0ebf8283 2019-07-24 stsp *path = NULL;
2366 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2368 0ebf8283 2019-07-24 stsp return NULL;
2371 10604dce 2021-09-24 thomas static const struct got_error *
2372 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2374 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2375 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2378 10604dce 2021-09-24 thomas static const struct got_error *
2379 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2381 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2382 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2386 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2387 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2389 517bab73 2019-03-11 stsp static const struct got_error *
2390 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2392 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2393 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2394 517bab73 2019-03-11 stsp char *refname;
2396 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2398 517bab73 2019-03-11 stsp return err;
2400 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2402 0cd1c46a 2019-03-11 stsp goto done;
2404 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2406 0cd1c46a 2019-03-11 stsp free(refname);
2408 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2409 3e3a69f1 2019-07-25 stsp return err;
2412 3e3a69f1 2019-07-25 stsp static const struct got_error *
2413 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2415 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2417 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2418 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2419 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2420 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2422 9d31a1d8 2018-03-11 stsp return err;
2426 ebf99748 2019-05-09 stsp static const struct got_error *
2427 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2428 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2430 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2431 ebf99748 2019-05-09 stsp FILE *index = NULL;
2433 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2434 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2435 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2436 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2438 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2440 ebf99748 2019-05-09 stsp goto done;
2442 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2443 ebf99748 2019-05-09 stsp if (index == NULL) {
2444 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2445 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2447 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2448 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2449 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2452 ebf99748 2019-05-09 stsp if (err) {
2453 ebf99748 2019-05-09 stsp free(*fileindex_path);
2454 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2455 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2456 ebf99748 2019-05-09 stsp *fileindex = NULL;
2458 ebf99748 2019-05-09 stsp return err;
2461 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2462 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2463 c932eeeb 2019-05-22 stsp const char *path;
2464 c932eeeb 2019-05-22 stsp size_t path_len;
2465 c932eeeb 2019-05-22 stsp const char *entry_name;
2466 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2467 a484d721 2019-06-10 stsp void *progress_arg;
2470 c932eeeb 2019-05-22 stsp static const struct got_error *
2471 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2473 1ee397ad 2019-07-12 stsp const struct got_error *err;
2474 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2476 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2477 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2478 c932eeeb 2019-05-22 stsp return NULL;
2479 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2480 102ff934 2019-06-11 stsp return NULL;
2482 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2483 a769b60b 2021-06-27 stsp return NULL;
2485 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2486 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2487 c932eeeb 2019-05-22 stsp return NULL;
2489 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2490 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2491 edd02c5e 2019-07-11 stsp ie->path);
2493 1ee397ad 2019-07-12 stsp return err;
2495 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2496 c932eeeb 2019-05-22 stsp return NULL;
2499 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2500 a615e0e7 2020-12-16 stsp static const struct got_error *
2501 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2502 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2503 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2505 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2507 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2508 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2509 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2510 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2511 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2512 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2514 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2515 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2518 9c6338c4 2019-06-02 stsp static const struct got_error *
2519 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2521 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2522 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2523 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2524 867630bb 2020-01-17 stsp struct timespec timeout;
2526 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2527 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2529 9c6338c4 2019-06-02 stsp goto done;
2531 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2533 9c6338c4 2019-06-02 stsp goto done;
2535 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2536 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2537 9c6338c4 2019-06-02 stsp fileindex_path);
2538 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2542 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2543 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2544 867630bb 2020-01-17 stsp * was recorded in the file index.
2546 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2547 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2548 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2550 9c6338c4 2019-06-02 stsp if (new_index)
2551 9c6338c4 2019-06-02 stsp fclose(new_index);
2552 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2553 9c6338c4 2019-06-02 stsp return err;
2556 6ced4176 2019-07-12 stsp static const struct got_error *
2557 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2558 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2559 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2560 945f9229 2022-04-16 thomas struct got_repository *repo)
2562 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2563 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2564 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2565 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2567 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2568 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2569 6ced4176 2019-07-12 stsp *tree_id = NULL;
2571 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2572 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2573 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2574 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2575 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2576 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2577 6ced4176 2019-07-12 stsp goto done;
2579 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2580 945f9229 2022-04-16 thomas worktree->path_prefix);
2582 6ced4176 2019-07-12 stsp goto done;
2583 6ced4176 2019-07-12 stsp return NULL;
2586 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2588 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2589 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2590 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2591 6ced4176 2019-07-12 stsp goto done;
2594 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2596 6ced4176 2019-07-12 stsp goto done;
2598 6ced4176 2019-07-12 stsp free(in_repo_path);
2599 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2601 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2603 6ced4176 2019-07-12 stsp goto done;
2605 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2606 6ced4176 2019-07-12 stsp /* Check out a single file. */
2607 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2608 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2609 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2610 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2611 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2612 6ced4176 2019-07-12 stsp goto done;
2614 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2615 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2616 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2617 6ced4176 2019-07-12 stsp goto done;
2620 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2621 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2623 6ced4176 2019-07-12 stsp return err;
2624 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2625 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2626 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2627 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2628 6ced4176 2019-07-12 stsp goto done;
2631 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2632 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2634 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2635 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2636 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2637 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2638 6ced4176 2019-07-12 stsp goto done;
2640 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2641 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2642 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2643 6ced4176 2019-07-12 stsp goto done;
2648 6ced4176 2019-07-12 stsp free(in_repo_path);
2649 6ced4176 2019-07-12 stsp if (err) {
2650 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2651 6ced4176 2019-07-12 stsp free(*tree_relpath);
2652 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2653 6ced4176 2019-07-12 stsp free(*tree_id);
2654 6ced4176 2019-07-12 stsp *tree_id = NULL;
2656 07ed472d 2019-07-12 stsp return err;
2659 07ed472d 2019-07-12 stsp static const struct got_error *
2660 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2661 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2662 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2663 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2665 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2666 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2667 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2668 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2669 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2671 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2672 7f47418f 2019-12-20 stsp if (err) {
2673 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2674 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2675 7f47418f 2019-12-20 stsp goto done;
2676 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2677 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2679 7f47418f 2019-12-20 stsp return err;
2682 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2683 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2685 07ed472d 2019-07-12 stsp goto done;
2687 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2689 07ed472d 2019-07-12 stsp goto done;
2691 07ed472d 2019-07-12 stsp if (entry_name &&
2692 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2693 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2694 07ed472d 2019-07-12 stsp goto done;
2697 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2698 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2699 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2700 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2701 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2702 07ed472d 2019-07-12 stsp arg.repo = repo;
2703 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2704 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2705 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2706 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2707 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2708 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2711 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2712 07ed472d 2019-07-12 stsp if (commit)
2713 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2714 6ced4176 2019-07-12 stsp return err;
2717 9d31a1d8 2018-03-11 stsp const struct got_error *
2718 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2719 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2720 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2721 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2723 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2724 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2725 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2726 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2727 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2728 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2729 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2730 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2731 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2732 f2ea84fa 2019-07-27 stsp int entry_type;
2733 f2ea84fa 2019-07-27 stsp char *relpath;
2734 f2ea84fa 2019-07-27 stsp char *entry_name;
2735 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2736 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2738 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2740 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2742 9d31a1d8 2018-03-11 stsp return err;
2744 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2745 945f9229 2022-04-16 thomas worktree->base_commit_id);
2746 945f9229 2022-04-16 thomas if (err)
2747 945f9229 2022-04-16 thomas goto done;
2749 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2750 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2751 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2752 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2753 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2754 f2ea84fa 2019-07-27 stsp goto done;
2757 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2758 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2759 945f9229 2022-04-16 thomas worktree, repo);
2760 f2ea84fa 2019-07-27 stsp if (err) {
2761 f2ea84fa 2019-07-27 stsp free(tpd);
2762 6ced4176 2019-07-12 stsp goto done;
2765 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2766 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2767 f2ea84fa 2019-07-27 stsp if (err) {
2768 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2769 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2770 f2ea84fa 2019-07-27 stsp free(tpd);
2771 f2ea84fa 2019-07-27 stsp goto done;
2774 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2776 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2780 51514078 2018-12-25 stsp * Read the file index.
2781 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2782 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2784 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2786 c4cdcb68 2019-04-03 stsp goto done;
2788 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2789 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2790 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2792 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2793 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2794 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2798 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2799 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2800 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2801 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2802 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2803 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2804 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2805 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2809 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2811 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2812 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2813 af12c6b9 2019-06-04 stsp err = sync_err;
2815 9c6338c4 2019-06-02 stsp free(fileindex_path);
2817 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2818 9d31a1d8 2018-03-11 stsp if (commit)
2819 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2820 5ade8233 2019-07-12 stsp if (fileindex)
2821 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2822 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2823 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2824 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2825 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2826 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2827 f2ea84fa 2019-07-27 stsp free(tpd);
2829 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2830 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2831 9d31a1d8 2018-03-11 stsp err = unlockerr;
2832 f8d1f275 2019-02-04 stsp return err;
2835 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2836 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2837 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2838 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2839 234035bc 2019-06-01 stsp void *progress_arg;
2840 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2841 234035bc 2019-06-01 stsp void *cancel_arg;
2842 f69721c3 2019-10-21 stsp const char *label_orig;
2843 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2844 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2847 234035bc 2019-06-01 stsp static const struct got_error *
2848 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2849 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2850 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2851 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2852 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2854 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2855 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2856 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2857 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2858 234035bc 2019-06-01 stsp struct stat sb;
2859 234035bc 2019-06-01 stsp unsigned char status;
2860 234035bc 2019-06-01 stsp int local_changes_subsumed;
2861 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2862 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2864 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2865 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2866 d6c87207 2019-08-02 stsp strlen(path2));
2867 1ee397ad 2019-07-12 stsp if (ie == NULL)
2868 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2869 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2871 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2872 234035bc 2019-06-01 stsp path2) == -1)
2873 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2875 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2878 234035bc 2019-06-01 stsp goto done;
2880 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2881 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2882 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2883 234035bc 2019-06-01 stsp goto done;
2885 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2886 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2887 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2888 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2889 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2890 234035bc 2019-06-01 stsp goto done;
2893 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2894 36bf999c 2020-07-23 stsp char *link_target2;
2895 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2897 36bf999c 2020-07-23 stsp goto done;
2898 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2899 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2900 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2901 36bf999c 2020-07-23 stsp free(link_target2);
2905 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2906 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2907 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2908 1af628f4 2021-06-03 stsp goto done;
2910 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2911 1af628f4 2021-06-03 stsp f_orig, blob1);
2913 1af628f4 2021-06-03 stsp goto done;
2915 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2916 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2917 1af628f4 2021-06-03 stsp goto done;
2918 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2919 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2921 1af628f4 2021-06-03 stsp goto done;
2923 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
2924 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2925 1af628f4 2021-06-03 stsp if (fd == -1) {
2926 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2927 1af628f4 2021-06-03 stsp ondisk_path);
2928 1af628f4 2021-06-03 stsp goto done;
2930 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2931 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2932 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2933 1af628f4 2021-06-03 stsp ondisk_path);
2934 1af628f4 2021-06-03 stsp close(fd);
2935 1af628f4 2021-06-03 stsp goto done;
2937 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2939 1af628f4 2021-06-03 stsp goto done;
2940 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2941 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2942 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2943 1af628f4 2021-06-03 stsp goto done;
2945 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2946 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2947 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
2948 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2949 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2951 234035bc 2019-06-01 stsp } else if (blob1) {
2952 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2953 d6c87207 2019-08-02 stsp strlen(path1));
2954 1ee397ad 2019-07-12 stsp if (ie == NULL)
2955 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2956 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2958 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2959 2b92fad7 2019-06-02 stsp path1) == -1)
2960 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2962 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2965 2b92fad7 2019-06-02 stsp goto done;
2967 2b92fad7 2019-06-02 stsp switch (status) {
2968 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2969 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2970 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2972 1ee397ad 2019-07-12 stsp goto done;
2973 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2975 2b92fad7 2019-06-02 stsp goto done;
2977 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2979 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2980 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2981 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2982 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2984 1ee397ad 2019-07-12 stsp goto done;
2986 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2988 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2989 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2990 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2991 e8f02263 2022-01-23 thomas off_t blob1_size;
2993 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2994 0a22ca1a 2020-09-23 stsp * exists in the repository.
2996 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
2997 e8f02263 2022-01-23 thomas &blob1_size, path1);
2999 0a22ca1a 2020-09-23 stsp goto done;
3000 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3001 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3002 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3004 0a22ca1a 2020-09-23 stsp goto done;
3005 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3008 0a22ca1a 2020-09-23 stsp goto done;
3010 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3013 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3014 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3016 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3017 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3020 0a22ca1a 2020-09-23 stsp goto done;
3023 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3024 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3025 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3026 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3028 1ee397ad 2019-07-12 stsp goto done;
3030 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3031 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3033 1ee397ad 2019-07-12 stsp goto done;
3038 234035bc 2019-06-01 stsp } else if (blob2) {
3039 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3040 234035bc 2019-06-01 stsp path2) == -1)
3041 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3042 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3043 d6c87207 2019-08-02 stsp strlen(path2));
3045 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3046 7f91a133 2019-12-13 stsp -1, NULL, repo);
3048 234035bc 2019-06-01 stsp goto done;
3049 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3050 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3051 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3052 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3053 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3054 1ee397ad 2019-07-12 stsp status, path2);
3055 234035bc 2019-06-01 stsp goto done;
3057 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3058 36bf999c 2020-07-23 stsp char *link_target2;
3059 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3062 36bf999c 2020-07-23 stsp goto done;
3063 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3064 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3065 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3066 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3067 36bf999c 2020-07-23 stsp free(link_target2);
3068 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3069 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3070 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3071 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3072 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3073 526a746f 2020-07-23 stsp a->progress_arg);
3075 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3076 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3079 dfe9fba0 2020-07-23 stsp goto done;
3080 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3081 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3082 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3083 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3085 234035bc 2019-06-01 stsp goto done;
3088 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3089 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3090 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3091 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3092 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3093 ace90326 2021-09-27 thomas 0, 1, a->allow_bad_symlinks, repo,
3094 ace90326 2021-09-27 thomas a->progress_cb, a->progress_arg);
3096 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3097 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3098 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3101 234035bc 2019-06-01 stsp goto done;
3102 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3104 234035bc 2019-06-01 stsp goto done;
3105 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3106 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3107 3969253a 2020-03-07 stsp if (err) {
3108 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3109 3969253a 2020-03-07 stsp goto done;
3111 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3112 2b92fad7 2019-06-02 stsp if (err) {
3113 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3114 2b92fad7 2019-06-02 stsp goto done;
3116 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3117 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3118 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3123 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3124 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3125 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3126 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3127 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3128 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3129 1af628f4 2021-06-03 stsp free(id_str);
3130 54d5be07 2021-06-03 stsp free(label_deriv2);
3131 234035bc 2019-06-01 stsp free(ondisk_path);
3132 234035bc 2019-06-01 stsp return err;
3135 69de9dd4 2021-09-03 stsp static const struct got_error *
3136 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3138 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3140 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3141 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3142 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3143 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3144 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3146 69de9dd4 2021-09-03 stsp return NULL;
3149 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3150 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3151 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3152 234035bc 2019-06-01 stsp struct got_repository *repo;
3155 234035bc 2019-06-01 stsp static const struct got_error *
3156 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3157 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3158 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3159 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3160 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3162 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3163 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3164 234035bc 2019-06-01 stsp unsigned char status;
3165 234035bc 2019-06-01 stsp struct stat sb;
3166 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3167 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3168 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3169 234035bc 2019-06-01 stsp char *ondisk_path;
3171 69de9dd4 2021-09-03 stsp if (id == NULL)
3172 69de9dd4 2021-09-03 stsp return NULL;
3174 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3175 69de9dd4 2021-09-03 stsp if (ie == NULL)
3176 69de9dd4 2021-09-03 stsp return NULL;
3178 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3180 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3182 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3183 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3184 5546d466 2021-09-02 stsp free(ondisk_path);
3186 234035bc 2019-06-01 stsp return err;
3187 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3188 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3190 234035bc 2019-06-01 stsp return NULL;
3193 818c7501 2019-07-11 stsp static const struct got_error *
3194 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3195 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3196 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3197 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3198 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3200 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3201 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3202 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3203 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3204 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3205 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3206 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3207 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3208 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3210 03415a1a 2019-06-02 stsp if (commit_id1) {
3211 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3212 945f9229 2022-04-16 thomas if (err)
3213 945f9229 2022-04-16 thomas goto done;
3214 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3215 03415a1a 2019-06-02 stsp worktree->path_prefix);
3216 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3217 03415a1a 2019-06-02 stsp goto done;
3219 69d57f3d 2020-07-31 stsp if (tree_id1) {
3220 69d57f3d 2020-07-31 stsp char *id_str;
3222 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3224 03415a1a 2019-06-02 stsp goto done;
3226 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3228 f69721c3 2019-10-21 stsp goto done;
3230 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3231 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3232 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3233 f69721c3 2019-10-21 stsp free(id_str);
3234 f69721c3 2019-10-21 stsp goto done;
3236 f69721c3 2019-10-21 stsp free(id_str);
3238 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3239 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3240 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3241 a0f32f33 2022-06-13 thomas goto done;
3245 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3246 945f9229 2022-04-16 thomas if (err)
3247 945f9229 2022-04-16 thomas goto done;
3249 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3250 234035bc 2019-06-01 stsp worktree->path_prefix);
3252 234035bc 2019-06-01 stsp goto done;
3254 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3256 234035bc 2019-06-01 stsp goto done;
3258 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3259 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3260 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3261 19a6a6b5 2022-07-01 thomas goto done;
3264 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3265 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3266 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3267 a0f32f33 2022-06-13 thomas goto done;
3270 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3271 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3272 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3273 19a6a6b5 2022-07-01 thomas goto done;
3276 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3277 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3278 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3279 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3280 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3282 69de9dd4 2021-09-03 stsp goto done;
3284 234035bc 2019-06-01 stsp arg.worktree = worktree;
3285 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3286 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3287 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3288 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3289 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3290 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3291 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3292 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3293 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3294 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3295 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3296 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3297 af12c6b9 2019-06-04 stsp err = sync_err;
3299 945f9229 2022-04-16 thomas if (commit1)
3300 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3301 945f9229 2022-04-16 thomas if (commit2)
3302 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3303 234035bc 2019-06-01 stsp if (tree1)
3304 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3305 234035bc 2019-06-01 stsp if (tree2)
3306 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3307 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3308 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3309 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3310 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3311 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3312 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3313 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3314 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3315 f69721c3 2019-10-21 stsp free(label_orig);
3316 818c7501 2019-07-11 stsp return err;
3319 818c7501 2019-07-11 stsp const struct got_error *
3320 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3321 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3322 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3323 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3325 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3326 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3327 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3329 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3331 818c7501 2019-07-11 stsp return err;
3333 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3335 818c7501 2019-07-11 stsp goto done;
3337 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3338 69de9dd4 2021-09-03 stsp worktree);
3340 818c7501 2019-07-11 stsp goto done;
3342 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3343 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3344 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3346 818c7501 2019-07-11 stsp if (fileindex)
3347 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3348 818c7501 2019-07-11 stsp free(fileindex_path);
3349 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3350 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3351 234035bc 2019-06-01 stsp err = unlockerr;
3352 234035bc 2019-06-01 stsp return err;
3355 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3356 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3357 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3358 927df6b7 2019-02-10 stsp const char *status_path;
3359 927df6b7 2019-02-10 stsp size_t status_path_len;
3360 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3361 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3362 f8d1f275 2019-02-04 stsp void *status_arg;
3363 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3364 f8d1f275 2019-02-04 stsp void *cancel_arg;
3365 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3366 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3367 f2a9dc41 2019-12-13 tracey int report_unchanged;
3368 3143d852 2020-06-25 stsp int no_ignores;
3371 f8d1f275 2019-02-04 stsp static const struct got_error *
3372 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3373 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3374 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3375 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3377 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3378 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3379 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3380 927df6b7 2019-02-10 stsp struct stat sb;
3381 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3382 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3383 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3385 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3386 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3388 98eaaa12 2019-08-03 stsp return err;
3390 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3391 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3392 98eaaa12 2019-08-03 stsp return NULL;
3394 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3395 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3396 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3397 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3398 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3399 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3400 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3401 43010591 2023-02-17 thomas &staged_blob_id, ie);
3404 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3405 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3408 927df6b7 2019-02-10 stsp static const struct got_error *
3409 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3410 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3412 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3413 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3414 f8d1f275 2019-02-04 stsp char *abspath;
3416 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3417 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3419 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3420 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3421 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3422 927df6b7 2019-02-10 stsp return NULL;
3424 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3425 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3426 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3427 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3429 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3430 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3431 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3434 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3435 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3436 f8d1f275 2019-02-04 stsp free(abspath);
3437 9d31a1d8 2018-03-11 stsp return err;
3440 f8d1f275 2019-02-04 stsp static const struct got_error *
3441 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3443 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3444 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3445 2ec1f75b 2019-03-26 stsp unsigned char status;
3447 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3448 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3450 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3451 927df6b7 2019-02-10 stsp return NULL;
3453 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3454 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3455 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3456 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3458 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3459 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3460 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3463 ef20f542 2022-06-26 thomas static void
3464 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3466 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3468 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3469 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3471 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3473 21c2d8be 2023-01-10 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3476 6841da00 2019-08-08 stsp static const struct got_error *
3477 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3479 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3480 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3481 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3482 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3483 6841da00 2019-08-08 stsp size_t linesize = 0;
3484 6841da00 2019-08-08 stsp ssize_t linelen;
3486 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3487 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3488 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3489 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3491 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3492 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3493 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3495 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3496 bd8de430 2019-10-04 stsp if (line[0] == '#')
3499 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3500 bd8de430 2019-10-04 stsp if (line[0] == '!')
3503 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3504 6841da00 2019-08-08 stsp line) == -1) {
3505 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3506 6841da00 2019-08-08 stsp goto done;
3508 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3510 6841da00 2019-08-08 stsp goto done;
3512 6841da00 2019-08-08 stsp if (ferror(f)) {
3513 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3514 6841da00 2019-08-08 stsp goto done;
3517 6841da00 2019-08-08 stsp dirpath = strdup(path);
3518 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3519 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3520 6841da00 2019-08-08 stsp goto done;
3522 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3524 6841da00 2019-08-08 stsp free(line);
3525 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3526 6841da00 2019-08-08 stsp free(dirpath);
3527 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3529 6841da00 2019-08-08 stsp return err;
3532 ef20f542 2022-06-26 thomas static int
3533 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3535 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3537 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3538 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3539 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3540 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3542 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3543 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3545 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3547 bd8de430 2019-10-04 stsp pattern += 3;
3549 bd8de430 2019-10-04 stsp while (*p) {
3550 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3551 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3552 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3553 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3555 bd8de430 2019-10-04 stsp while (*p == '/')
3565 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3566 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3567 6841da00 2019-08-08 stsp * ignores backwards.
3569 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3570 6841da00 2019-08-08 stsp while (pe) {
3571 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3572 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3573 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3574 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3575 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3576 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3577 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3578 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3579 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3584 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3590 6841da00 2019-08-08 stsp static const struct got_error *
3591 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3592 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3594 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3595 6841da00 2019-08-08 stsp char *ignorespath;
3596 886cec17 2019-12-15 stsp int fd = -1;
3597 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3599 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3600 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3601 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3603 886cec17 2019-12-15 stsp if (dirfd != -1) {
3604 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3605 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3606 886cec17 2019-12-15 stsp if (fd == -1) {
3607 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3608 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3609 886cec17 2019-12-15 stsp ignorespath);
3611 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3612 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3613 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3614 886cec17 2019-12-15 stsp ignorespath);
3617 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3621 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3622 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3623 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3624 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3625 886cec17 2019-12-15 stsp ignorespath);
3627 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3630 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3631 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3632 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3633 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3634 6841da00 2019-08-08 stsp free(ignorespath);
3635 6841da00 2019-08-08 stsp return err;
3638 f8d1f275 2019-02-04 stsp static const struct got_error *
3639 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3640 6092c299 2021-10-04 thomas int dirfd)
3642 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3643 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3644 f8d1f275 2019-02-04 stsp char *path = NULL;
3646 6092c299 2021-10-04 thomas if (ignore != NULL)
3647 6092c299 2021-10-04 thomas *ignore = 0;
3649 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3650 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3652 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3653 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3654 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3656 f8d1f275 2019-02-04 stsp path = de->d_name;
3659 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3660 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3661 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3662 6092c299 2021-10-04 thomas *ignore = 1;
3663 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3664 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3665 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3666 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3667 f8d1f275 2019-02-04 stsp if (parent_path[0])
3668 f8d1f275 2019-02-04 stsp free(path);
3669 b72f483a 2019-02-05 stsp return err;
3672 347d1d3e 2019-07-12 stsp static const struct got_error *
3673 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3675 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3676 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3678 3143d852 2020-06-25 stsp if (a->no_ignores)
3679 3143d852 2020-06-25 stsp return NULL;
3681 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3682 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3684 3143d852 2020-06-25 stsp return err;
3686 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3687 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3689 3143d852 2020-06-25 stsp return err;
3692 3143d852 2020-06-25 stsp static const struct got_error *
3693 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3694 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3695 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3696 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3698 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3699 abb4604f 2019-07-27 stsp struct stat sb;
3701 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3703 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3704 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3706 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3707 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3708 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3709 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3710 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3713 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3714 fe1d8685 2022-02-12 thomas return NULL;
3716 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3717 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3718 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3720 abb4604f 2019-07-27 stsp return NULL;
3723 3143d852 2020-06-25 stsp static const struct got_error *
3724 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3725 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3727 3143d852 2020-06-25 stsp const struct got_error *err;
3728 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3730 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3731 3143d852 2020-06-25 stsp ".cvsignore");
3733 3143d852 2020-06-25 stsp return err;
3735 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3736 3143d852 2020-06-25 stsp ".gitignore");
3738 3143d852 2020-06-25 stsp return err;
3740 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3741 3143d852 2020-06-25 stsp if (err) {
3742 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3743 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3744 3143d852 2020-06-25 stsp return err;
3746 3143d852 2020-06-25 stsp for (;;) {
3747 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3748 3143d852 2020-06-25 stsp ".cvsignore");
3751 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3752 3143d852 2020-06-25 stsp ".gitignore");
3755 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3756 3143d852 2020-06-25 stsp if (err) {
3757 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3758 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3761 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3763 b737c85a 2020-06-26 stsp free(parent_path);
3764 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3765 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3768 b737c85a 2020-06-26 stsp free(parent_path);
3769 b737c85a 2020-06-26 stsp free(next_parent_path);
3770 3143d852 2020-06-25 stsp return err;
3773 abb4604f 2019-07-27 stsp static const struct got_error *
3774 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3775 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3776 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3777 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3778 f2a9dc41 2019-12-13 tracey int report_unchanged)
3780 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3781 6fc93f37 2019-12-13 stsp int fd = -1;
3782 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3783 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3784 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3785 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3786 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3788 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3790 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3791 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3792 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3794 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3795 a78810f8 2022-03-13 thomas if (ie) {
3796 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3797 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3798 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3799 a78810f8 2022-03-13 thomas goto done;
3802 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3803 6fc93f37 2019-12-13 stsp if (fd == -1) {
3804 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3805 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3806 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3808 ff56836b 2021-07-08 stsp if (!no_ignores) {
3809 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3810 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3812 ff56836b 2021-07-08 stsp goto done;
3814 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3815 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3816 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3819 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3820 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3821 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3822 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3823 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3824 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3825 abb4604f 2019-07-27 stsp arg.status_path = path;
3826 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3827 abb4604f 2019-07-27 stsp arg.repo = repo;
3828 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3829 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3830 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3831 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3832 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3833 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3834 022fae89 2019-12-06 tracey if (!no_ignores) {
3835 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3836 3143d852 2020-06-25 stsp worktree->root_path, path);
3838 3143d852 2020-06-25 stsp goto done;
3840 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3841 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3842 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3845 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3846 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3847 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3848 927df6b7 2019-02-10 stsp free(ondisk_path);
3849 347d1d3e 2019-07-12 stsp return err;
3852 347d1d3e 2019-07-12 stsp const struct got_error *
3853 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3854 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3855 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3856 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3858 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3859 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3860 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3861 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3863 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3865 347d1d3e 2019-07-12 stsp return err;
3867 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3868 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3869 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3870 f6343036 2021-06-22 stsp no_ignores, 0);
3874 f8d1f275 2019-02-04 stsp free(fileindex_path);
3875 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3876 f8d1f275 2019-02-04 stsp return err;
3879 6c7ab921 2019-03-18 stsp const struct got_error *
3880 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3881 6c7ab921 2019-03-18 stsp const char *arg)
3883 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3884 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3885 6c7ab921 2019-03-18 stsp size_t len;
3886 00bb5ea0 2020-07-23 stsp struct stat sb;
3887 01740607 2020-11-04 stsp char *abspath = NULL;
3888 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3890 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3892 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3893 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3894 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3896 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3897 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3898 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3899 00bb5ea0 2020-07-23 stsp goto done;
3901 727173c3 2020-11-06 stsp sb.st_mode = 0;
3903 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3905 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3906 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3907 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3908 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3910 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3911 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3912 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3913 00bb5ea0 2020-07-23 stsp goto done;
3917 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3918 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3920 d0710d08 2019-07-22 stsp goto done;
3921 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3922 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3923 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3924 00bb5ea0 2020-07-23 stsp goto done;
3927 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3928 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3929 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3930 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3931 00bb5ea0 2020-07-23 stsp goto done;
3933 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3934 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3935 01740607 2020-11-04 stsp goto done;
3937 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3938 01740607 2020-11-04 stsp sizeof(canonpath));
3940 00bb5ea0 2020-07-23 stsp goto done;
3941 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3942 01740607 2020-11-04 stsp if (resolved == NULL) {
3943 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3944 01740607 2020-11-04 stsp goto done;
3949 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3950 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3951 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3952 6c7ab921 2019-03-18 stsp goto done;
3955 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3956 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3957 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3959 f6d88e1a 2019-05-29 stsp goto done;
3961 f6d88e1a 2019-05-29 stsp path = strdup("");
3962 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3963 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3964 f6d88e1a 2019-05-29 stsp goto done;
3968 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3969 6c7ab921 2019-03-18 stsp len = strlen(path);
3970 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3971 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3975 01740607 2020-11-04 stsp free(abspath);
3976 6c7ab921 2019-03-18 stsp free(resolved);
3977 d0710d08 2019-07-22 stsp free(cwd);
3978 6c7ab921 2019-03-18 stsp if (err == NULL)
3979 6c7ab921 2019-03-18 stsp *wt_path = path;
3981 6c7ab921 2019-03-18 stsp free(path);
3982 d00136be 2019-03-26 stsp return err;
3985 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3986 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3987 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3988 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3989 4e68cba3 2019-11-23 stsp void *progress_arg;
3990 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3993 4e68cba3 2019-11-23 stsp static const struct got_error *
3994 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3995 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3996 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3997 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3999 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4000 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4001 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4002 6d022e97 2019-08-04 stsp struct stat sb;
4003 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4005 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4006 dbb83fbd 2019-12-12 stsp relpath) == -1)
4007 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4009 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4011 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4012 12463d8b 2019-12-13 stsp de_name, a->repo);
4014 dbb83fbd 2019-12-12 stsp goto done;
4015 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4016 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
4017 dbb83fbd 2019-12-12 stsp goto done;
4018 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4020 dbb83fbd 2019-12-12 stsp goto done;
4023 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4024 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4025 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4027 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4028 dbb83fbd 2019-12-12 stsp goto done;
4031 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4033 4e68cba3 2019-11-23 stsp goto done;
4034 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4035 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4036 3969253a 2020-03-07 stsp if (err) {
4037 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4038 3969253a 2020-03-07 stsp goto done;
4040 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4041 07f5b47a 2019-06-02 stsp if (err) {
4042 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4043 4e68cba3 2019-11-23 stsp goto done;
4046 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4048 dbb83fbd 2019-12-12 stsp return err;
4049 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4050 dbb83fbd 2019-12-12 stsp return NULL;
4051 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4054 d00136be 2019-03-26 stsp const struct got_error *
4055 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4056 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4057 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4058 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4060 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4061 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4062 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4063 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4064 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4066 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4068 d00136be 2019-03-26 stsp return err;
4070 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4072 d00136be 2019-03-26 stsp goto done;
4074 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4075 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4076 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4077 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4078 4e68cba3 2019-11-23 stsp saa.repo = repo;
4080 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4081 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4082 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4086 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4087 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4088 af12c6b9 2019-06-04 stsp err = sync_err;
4090 fb399478 2019-07-12 stsp free(fileindex_path);
4091 d00136be 2019-03-26 stsp if (fileindex)
4092 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4093 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4094 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4095 d00136be 2019-03-26 stsp err = unlockerr;
4096 6c7ab921 2019-03-18 stsp return err;
4099 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4100 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4101 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4102 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4103 f2a9dc41 2019-12-13 tracey void *progress_arg;
4104 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4105 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4106 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4107 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4108 766841c2 2020-08-13 stsp const char *status_codes;
4111 f2a9dc41 2019-12-13 tracey static const struct got_error *
4112 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4113 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4114 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4115 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4117 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4118 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4119 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4120 17ed4618 2019-06-02 stsp struct stat sb;
4121 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4123 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4124 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4125 d64cc78a 2022-01-25 thomas return NULL;
4126 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4129 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4130 17ed4618 2019-06-02 stsp if (ie == NULL)
4131 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4133 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4134 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4135 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4136 9acbc4fa 2019-08-03 stsp return NULL;
4137 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4140 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4141 f2a9dc41 2019-12-13 tracey relpath) == -1)
4142 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4144 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4147 f2a9dc41 2019-12-13 tracey goto done;
4149 766841c2 2020-08-13 stsp if (a->status_codes) {
4150 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4152 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4153 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4156 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4157 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4158 766841c2 2020-08-13 stsp free(ondisk_path);
4159 766841c2 2020-08-13 stsp return NULL;
4161 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4162 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4163 766841c2 2020-08-13 stsp static char msg[64];
4164 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4165 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4166 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4167 766841c2 2020-08-13 stsp goto done;
4171 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4172 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4173 f2a9dc41 2019-12-13 tracey goto done;
4174 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4175 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4176 f2a9dc41 2019-12-13 tracey goto done;
4178 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4179 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4180 d64cc78a 2022-01-25 thomas goto done;
4182 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4183 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4184 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4185 f2a9dc41 2019-12-13 tracey goto done;
4189 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4190 20a2ad1c 2020-10-20 stsp size_t root_len;
4192 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4193 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4194 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4195 12463d8b 2019-12-13 stsp ondisk_path);
4196 12463d8b 2019-12-13 stsp goto done;
4198 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4199 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4200 12463d8b 2019-12-13 stsp goto done;
4203 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4205 20a2ad1c 2020-10-20 stsp char *parent;
4206 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4208 2513f20a 2020-10-20 stsp goto done;
4209 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4210 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4211 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4212 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4213 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4214 20a2ad1c 2020-10-20 stsp ondisk_path);
4217 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4218 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4221 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4223 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4224 f2a9dc41 2019-12-13 tracey if (err)
4225 f2a9dc41 2019-12-13 tracey return err;
4226 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4227 f2a9dc41 2019-12-13 tracey return NULL;
4228 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4229 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4232 2ec1f75b 2019-03-26 stsp const struct got_error *
4233 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4234 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4235 766841c2 2020-08-13 stsp const char *status_codes,
4236 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4237 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4239 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4240 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4241 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4242 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4243 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4245 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4247 2ec1f75b 2019-03-26 stsp return err;
4249 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4251 2ec1f75b 2019-03-26 stsp goto done;
4253 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4254 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4255 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4256 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4257 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4258 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4259 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4260 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4261 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4263 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4264 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4265 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4269 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4270 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4271 af12c6b9 2019-06-04 stsp err = sync_err;
4273 fb399478 2019-07-12 stsp free(fileindex_path);
4274 2ec1f75b 2019-03-26 stsp if (fileindex)
4275 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4276 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4277 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4278 2ec1f75b 2019-03-26 stsp err = unlockerr;
4279 33aa809d 2019-08-08 stsp return err;
4282 33aa809d 2019-08-08 stsp static const struct got_error *
4283 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4285 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4286 33aa809d 2019-08-08 stsp char *line = NULL;
4287 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4288 33aa809d 2019-08-08 stsp ssize_t linelen;
4290 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4291 33aa809d 2019-08-08 stsp if (linelen == -1) {
4292 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4293 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4294 33aa809d 2019-08-08 stsp goto done;
4296 33aa809d 2019-08-08 stsp return NULL;
4298 33aa809d 2019-08-08 stsp if (outfile) {
4299 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4300 33aa809d 2019-08-08 stsp if (n != linelen) {
4301 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4302 33aa809d 2019-08-08 stsp goto done;
4305 33aa809d 2019-08-08 stsp if (rejectfile) {
4306 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4307 33aa809d 2019-08-08 stsp if (n != linelen)
4308 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4311 33aa809d 2019-08-08 stsp free(line);
4312 2ec1f75b 2019-03-26 stsp return err;
4315 33aa809d 2019-08-08 stsp static const struct got_error *
4316 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4318 33aa809d 2019-08-08 stsp char *line = NULL;
4319 33aa809d 2019-08-08 stsp size_t linesize = 0;
4320 33aa809d 2019-08-08 stsp ssize_t linelen;
4322 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4323 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4324 500467ff 2019-09-25 hiltjo if (ferror(f))
4325 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4326 500467ff 2019-09-25 hiltjo return NULL;
4328 33aa809d 2019-08-08 stsp free(line);
4329 33aa809d 2019-08-08 stsp return NULL;
4332 33aa809d 2019-08-08 stsp static const struct got_error *
4333 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4334 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4335 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4337 33aa809d 2019-08-08 stsp const struct got_error *err;
4339 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4340 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4341 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4343 33aa809d 2019-08-08 stsp return err;
4344 33aa809d 2019-08-08 stsp (*line_cur1)++;
4346 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4347 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4348 33aa809d 2019-08-08 stsp if (rejectfile)
4349 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4351 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4353 33aa809d 2019-08-08 stsp return err;
4354 33aa809d 2019-08-08 stsp (*line_cur2)++;
4356 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4357 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4358 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4360 33aa809d 2019-08-08 stsp return err;
4361 33aa809d 2019-08-08 stsp (*line_cur2)++;
4363 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4364 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4365 33aa809d 2019-08-08 stsp if (rejectfile)
4366 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4368 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4370 33aa809d 2019-08-08 stsp return err;
4371 33aa809d 2019-08-08 stsp (*line_cur1)++;
4374 f1e81a05 2019-08-10 stsp return NULL;
4377 f1e81a05 2019-08-10 stsp static const struct got_error *
4378 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4379 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4381 f1e81a05 2019-08-10 stsp const struct got_error *err;
4383 f1e81a05 2019-08-10 stsp if (outfile) {
4384 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4385 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4386 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4388 f1e81a05 2019-08-10 stsp return err;
4389 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4392 f1e81a05 2019-08-10 stsp if (rejectfile) {
4393 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4394 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4395 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4397 f1e81a05 2019-08-10 stsp return err;
4398 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4402 33aa809d 2019-08-08 stsp return NULL;
4405 33aa809d 2019-08-08 stsp static const struct got_error *
4406 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4407 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4408 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4409 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4410 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4412 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4413 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4414 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4415 33aa809d 2019-08-08 stsp FILE *hunkfile;
4416 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4417 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4420 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4422 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4423 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4424 fe621944 2020-11-10 stsp start_old = cc.left.start;
4425 fe621944 2020-11-10 stsp end_old = cc.left.end;
4426 fe621944 2020-11-10 stsp start_new = cc.right.start;
4427 fe621944 2020-11-10 stsp end_new = cc.right.end;
4429 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4430 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4431 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4433 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4434 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4435 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4437 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4438 fe621944 2020-11-10 stsp if (diff_state == NULL)
4439 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4440 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4442 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4443 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4444 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4445 33aa809d 2019-08-08 stsp goto done;
4448 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4449 fe621944 2020-11-10 stsp diff_result, &cc);
4450 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4451 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4452 33aa809d 2019-08-08 stsp goto done;
4455 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4456 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4457 33aa809d 2019-08-08 stsp goto done;
4460 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4461 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4463 33aa809d 2019-08-08 stsp goto done;
4465 33aa809d 2019-08-08 stsp switch (*choice) {
4466 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4467 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4468 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4470 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4471 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4472 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4474 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4477 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4481 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4482 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4483 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4484 33aa809d 2019-08-08 stsp return err;
4487 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4488 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4489 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4490 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4491 1f1abb7e 2019-08-08 stsp void *progress_arg;
4492 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4493 33aa809d 2019-08-08 stsp void *patch_arg;
4494 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4495 10604dce 2021-09-24 thomas int unlink_added_files;
4498 e20a8b6f 2019-06-04 stsp static const struct got_error *
4499 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4500 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4501 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4502 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4503 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4505 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4506 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4507 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4508 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4509 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4510 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4511 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4512 fe621944 2020-11-10 stsp struct stat sb2;
4513 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4514 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4515 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4517 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4519 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4521 33aa809d 2019-08-08 stsp return err;
4523 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4524 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4525 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4526 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4527 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4528 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4529 fa3cef63 2020-07-23 stsp goto done;
4531 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4532 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4533 48b4f239 2021-12-31 thomas if (link_len == -1) {
4534 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4537 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4538 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4541 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4542 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4543 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4544 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4545 fa3cef63 2020-07-23 stsp goto done;
4547 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4548 fa3cef63 2020-07-23 stsp sizeof(link_target));
4549 fa3cef63 2020-07-23 stsp if (link_len == -1)
4550 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4551 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4552 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4555 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4556 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4557 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4558 fa3cef63 2020-07-23 stsp goto done;
4561 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4562 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4563 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4564 fa3cef63 2020-07-23 stsp goto done;
4569 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4570 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4571 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4572 fa3cef63 2020-07-23 stsp goto done;
4574 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4575 fa3cef63 2020-07-23 stsp if (n != link_len) {
4576 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4577 fa3cef63 2020-07-23 stsp goto done;
4579 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4580 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4581 fa3cef63 2020-07-23 stsp goto done;
4583 fa3cef63 2020-07-23 stsp rewind(f2);
4586 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4587 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4588 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4589 f4ae6ddb 2022-07-01 thomas goto done;
4592 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4594 33aa809d 2019-08-08 stsp goto done;
4596 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4598 33aa809d 2019-08-08 stsp goto done;
4600 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4602 33aa809d 2019-08-08 stsp goto done;
4604 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4605 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4607 33aa809d 2019-08-08 stsp goto done;
4609 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4612 33aa809d 2019-08-08 stsp goto done;
4614 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4615 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4616 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4617 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4619 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4620 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4621 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4622 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4623 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4624 fe621944 2020-11-10 stsp nchanges++;
4626 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4627 33aa809d 2019-08-08 stsp int choice;
4628 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4629 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4630 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4631 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4632 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4633 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4635 33aa809d 2019-08-08 stsp goto done;
4636 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4637 33aa809d 2019-08-08 stsp have_content = 1;
4638 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4641 1ebedb77 2019-10-19 stsp if (have_content) {
4642 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4643 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4644 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4646 1ebedb77 2019-10-19 stsp goto done;
4648 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4649 a2c162eb 2022-10-30 thomas mode_t mode;
4651 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
4652 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
4653 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4654 fa3cef63 2020-07-23 stsp goto done;
4659 33aa809d 2019-08-08 stsp free(id_str);
4660 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4661 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4663 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4664 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4665 fe621944 2020-11-10 stsp if (err == NULL)
4666 fe621944 2020-11-10 stsp err = free_err;
4667 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4668 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4669 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4670 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4671 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4672 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4673 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4674 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4675 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4676 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4677 33aa809d 2019-08-08 stsp if (err || !have_content) {
4678 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4679 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4680 33aa809d 2019-08-08 stsp free(*path_outfile);
4681 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4683 33aa809d 2019-08-08 stsp free(path1);
4684 33aa809d 2019-08-08 stsp return err;
4687 33aa809d 2019-08-08 stsp static const struct got_error *
4688 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4689 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4690 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4691 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4693 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4694 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4695 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4696 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4697 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4698 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4699 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4700 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4701 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4702 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4703 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4704 f4ae6ddb 2022-07-01 thomas int fd = -1;
4706 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4707 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4708 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4709 d3bcc3d1 2019-08-08 stsp return NULL;
4711 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4712 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4713 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4715 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4716 65084dad 2019-08-08 stsp if (ie == NULL)
4717 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4719 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4720 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4721 a129376b 2019-03-28 stsp if (err) {
4722 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4723 a129376b 2019-03-28 stsp goto done;
4724 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4725 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4726 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4727 e20a8b6f 2019-06-04 stsp goto done;
4730 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4731 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4732 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4733 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4734 a129376b 2019-03-28 stsp goto done;
4737 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4738 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4739 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4740 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4741 a129376b 2019-03-28 stsp goto done;
4744 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4745 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4746 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4747 a129376b 2019-03-28 stsp goto done;
4752 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4753 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
4754 945f9229 2022-04-16 thomas if (err)
4755 945f9229 2022-04-16 thomas goto done;
4757 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4758 a9fa2909 2019-07-27 stsp if (err) {
4759 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4760 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4761 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4762 a9fa2909 2019-07-27 stsp goto done;
4764 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4766 a9fa2909 2019-07-27 stsp goto done;
4768 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4770 a9fa2909 2019-07-27 stsp goto done;
4772 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4773 1233e6b6 2020-10-19 stsp free(te_name);
4774 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4775 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4776 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4777 a9fa2909 2019-07-27 stsp goto done;
4781 a129376b 2019-03-28 stsp switch (status) {
4782 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4783 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4784 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4785 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4786 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4788 33aa809d 2019-08-08 stsp goto done;
4789 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4792 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4793 1f1abb7e 2019-08-08 stsp ie->path);
4795 1ee397ad 2019-07-12 stsp goto done;
4796 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4797 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
4798 10604dce 2021-09-24 thomas if (asprintf(&ondisk_path, "%s/%s",
4799 10604dce 2021-09-24 thomas got_worktree_get_root_path(a->worktree),
4800 10604dce 2021-09-24 thomas relpath) == -1) {
4801 10604dce 2021-09-24 thomas err = got_error_from_errno("asprintf");
4802 10604dce 2021-09-24 thomas goto done;
4804 10604dce 2021-09-24 thomas if (unlink(ondisk_path) == -1) {
4805 10604dce 2021-09-24 thomas err = got_error_from_errno2("unlink",
4806 10604dce 2021-09-24 thomas ondisk_path);
4811 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4812 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4813 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4814 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4815 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4817 33aa809d 2019-08-08 stsp goto done;
4818 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4821 33aa809d 2019-08-08 stsp /* fall through */
4822 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4823 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4824 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4825 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4826 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4827 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4828 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
4829 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
4831 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
4832 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4833 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4834 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4835 f4ae6ddb 2022-07-01 thomas goto done;
4838 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4840 65084dad 2019-08-08 stsp goto done;
4842 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4843 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4844 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4845 a129376b 2019-03-28 stsp goto done;
4848 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4849 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4850 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4851 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4852 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4853 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4854 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4856 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4857 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4858 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4859 369fd7e5 2020-07-23 stsp path_content);
4862 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4863 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4864 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4865 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4867 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4868 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4869 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4870 369fd7e5 2020-07-23 stsp goto done;
4874 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4875 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4876 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4877 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4878 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4879 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4881 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4883 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4884 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4885 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4886 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4889 a129376b 2019-03-28 stsp goto done;
4890 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4891 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4892 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4893 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4894 437adc9d 2020-12-10 yzhong blob->id.sha1,
4895 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4897 2e1fa222 2020-07-23 stsp goto done;
4899 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4900 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4901 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4910 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4911 33aa809d 2019-08-08 stsp free(path_content);
4912 e20a8b6f 2019-06-04 stsp free(parent_path);
4913 a129376b 2019-03-28 stsp free(tree_path);
4914 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4915 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4917 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4919 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4920 a129376b 2019-03-28 stsp free(tree_id);
4921 945f9229 2022-04-16 thomas if (base_commit)
4922 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
4923 e20a8b6f 2019-06-04 stsp return err;
4926 e20a8b6f 2019-06-04 stsp const struct got_error *
4927 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4928 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4929 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4930 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4931 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4933 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4934 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4935 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4936 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4937 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4938 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4940 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4942 e20a8b6f 2019-06-04 stsp return err;
4944 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4946 e20a8b6f 2019-06-04 stsp goto done;
4948 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4949 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4950 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4951 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4952 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4953 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4954 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4955 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
4956 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4957 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4958 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
4962 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4963 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4964 e20a8b6f 2019-06-04 stsp err = sync_err;
4966 fb399478 2019-07-12 stsp free(fileindex_path);
4967 a129376b 2019-03-28 stsp if (fileindex)
4968 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4969 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4970 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4971 a129376b 2019-03-28 stsp err = unlockerr;
4972 c4296144 2019-05-09 stsp return err;
4975 cf066bf8 2019-05-09 stsp static void
4976 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4978 24519714 2019-05-09 stsp free(ct->path);
4979 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4980 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4981 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4982 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4983 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4984 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4988 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4989 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4990 24519714 2019-05-09 stsp struct got_repository *repo;
4991 24519714 2019-05-09 stsp struct got_worktree *worktree;
4992 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4993 5f8a88c6 2019-08-03 stsp int have_staged_files;
4994 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4995 ef899790 2022-11-01 thomas int diff_header_shown;
4996 be94c032 2023-02-20 thomas int commit_conflicts;
4997 ef899790 2022-11-01 thomas FILE *diff_outfile;
4998 ef899790 2022-11-01 thomas FILE *f1;
4999 ef899790 2022-11-01 thomas FILE *f2;
5003 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5004 ef899790 2022-11-01 thomas * it as content to the diff engine.
5006 ef899790 2022-11-01 thomas static const struct got_error *
5007 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5008 ef899790 2022-11-01 thomas const char *abspath)
5010 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5011 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5012 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5014 ef899790 2022-11-01 thomas *fd = -1;
5016 ef899790 2022-11-01 thomas if (dirfd != -1) {
5017 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5018 ef899790 2022-11-01 thomas if (target_len == -1)
5019 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5020 ef899790 2022-11-01 thomas } else {
5021 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5022 ef899790 2022-11-01 thomas if (target_len == -1)
5023 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5026 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5027 ef899790 2022-11-01 thomas if (*fd == -1)
5028 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5030 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5031 ef899790 2022-11-01 thomas if (outlen == -1) {
5032 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5033 ef899790 2022-11-01 thomas goto done;
5036 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5037 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5038 ef899790 2022-11-01 thomas goto done;
5041 ef899790 2022-11-01 thomas if (err) {
5042 ef899790 2022-11-01 thomas close(*fd);
5043 ef899790 2022-11-01 thomas *fd = -1;
5045 ef899790 2022-11-01 thomas return err;
5048 ef899790 2022-11-01 thomas static const struct got_error *
5049 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5050 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5051 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5053 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5054 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5055 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5056 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5057 ef899790 2022-11-01 thomas char *label1 = NULL;
5058 ef899790 2022-11-01 thomas struct stat sb;
5059 ef899790 2022-11-01 thomas off_t size1 = 0;
5060 ef899790 2022-11-01 thomas int f2_exists = 0;
5061 ef899790 2022-11-01 thomas char *id_str = NULL;
5063 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5065 d259e491 2022-11-01 thomas if (diff_staged) {
5066 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5067 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5068 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5069 d259e491 2022-11-01 thomas return NULL;
5070 d259e491 2022-11-01 thomas } else {
5071 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5072 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5073 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5074 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5075 d259e491 2022-11-01 thomas return NULL;
5078 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5079 ef899790 2022-11-01 thomas if (err)
5080 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5081 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5082 ef899790 2022-11-01 thomas if (err)
5083 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5085 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5086 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5087 ef899790 2022-11-01 thomas if (err)
5088 ef899790 2022-11-01 thomas return err;
5089 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5090 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5091 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5092 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5093 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5094 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5095 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5098 ef899790 2022-11-01 thomas if (diff_staged) {
5099 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5100 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5101 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5102 ef899790 2022-11-01 thomas label1 = ct->path;
5103 ef899790 2022-11-01 thomas label2 = ct->path;
5105 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5106 ef899790 2022-11-01 thomas label2 = ct->path;
5108 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5109 ef899790 2022-11-01 thomas label1 = ct->path;
5111 ef899790 2022-11-01 thomas default:
5112 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5114 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5115 ef899790 2022-11-01 thomas if (fd1 == -1) {
5116 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5117 ef899790 2022-11-01 thomas goto done;
5119 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5120 ef899790 2022-11-01 thomas if (fd2 == -1) {
5121 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5122 ef899790 2022-11-01 thomas goto done;
5124 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5125 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5126 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5127 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5128 ef899790 2022-11-01 thomas goto done;
5131 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5132 ef899790 2022-11-01 thomas if (fd1 == -1) {
5133 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5134 ef899790 2022-11-01 thomas goto done;
5137 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5138 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5139 ef899790 2022-11-01 thomas 8192, fd1);
5140 ef899790 2022-11-01 thomas if (err)
5141 ef899790 2022-11-01 thomas goto done;
5144 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5145 ef899790 2022-11-01 thomas if (dirfd != -1) {
5146 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5147 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5148 ef899790 2022-11-01 thomas if (fd == -1) {
5149 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5150 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5151 ef899790 2022-11-01 thomas ct->ondisk_path);
5152 ef899790 2022-11-01 thomas goto done;
5154 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5155 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5156 ef899790 2022-11-01 thomas if (err)
5157 ef899790 2022-11-01 thomas goto done;
5159 ef899790 2022-11-01 thomas } else {
5160 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5161 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5162 ef899790 2022-11-01 thomas if (fd == -1) {
5163 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5164 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5165 ef899790 2022-11-01 thomas ct->ondisk_path);
5166 ef899790 2022-11-01 thomas goto done;
5168 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5169 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5170 ef899790 2022-11-01 thomas if (err)
5171 ef899790 2022-11-01 thomas goto done;
5174 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5175 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5176 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5177 ef899790 2022-11-01 thomas goto done;
5179 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5180 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5181 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5182 ef899790 2022-11-01 thomas goto done;
5184 ef899790 2022-11-01 thomas fd = -1;
5185 ef899790 2022-11-01 thomas f2_exists = 1;
5188 ef899790 2022-11-01 thomas if (blob1) {
5189 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5190 ef899790 2022-11-01 thomas f1, blob1);
5191 ef899790 2022-11-01 thomas if (err)
5192 ef899790 2022-11-01 thomas goto done;
5195 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5196 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5197 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5199 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5200 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5201 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5202 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5203 ef899790 2022-11-01 thomas if (blob1)
5204 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5205 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5206 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5207 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5208 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5209 ef899790 2022-11-01 thomas return err;
5212 c4296144 2019-05-09 stsp static const struct got_error *
5213 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5214 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5215 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5216 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5218 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5219 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5220 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5221 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5222 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5223 768aea60 2019-05-09 stsp struct stat sb;
5225 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5226 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5227 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5228 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5229 5f8a88c6 2019-08-03 stsp return NULL;
5231 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5232 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5233 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5236 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5237 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5238 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5239 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5240 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5241 5f8a88c6 2019-08-03 stsp return NULL;
5244 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5245 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5246 036813ee 2019-05-09 stsp goto done;
5248 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5249 036813ee 2019-05-09 stsp parent_path = strdup("");
5250 69960a46 2019-05-09 stsp if (parent_path == NULL)
5251 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5253 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5255 69960a46 2019-05-09 stsp return err;
5258 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5259 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5260 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5261 c4296144 2019-05-09 stsp goto done;
5264 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5265 768aea60 2019-05-09 stsp relpath) == -1) {
5266 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5267 768aea60 2019-05-09 stsp goto done;
5270 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5271 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5272 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5273 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5274 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5275 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5276 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5277 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5279 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5280 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5283 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5284 0aeb8099 2020-07-23 stsp goto done;
5286 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5287 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5288 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5289 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5290 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5291 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5292 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5293 12463d8b 2019-12-13 stsp ct->ondisk_path);
5294 12463d8b 2019-12-13 stsp goto done;
5296 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5297 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5298 768aea60 2019-05-09 stsp goto done;
5300 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5303 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5304 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5305 44d03001 2019-05-09 stsp relpath) == -1) {
5306 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5307 44d03001 2019-05-09 stsp goto done;
5310 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5311 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5312 35213c7c 2020-07-23 stsp int is_bad_symlink;
5313 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5314 35213c7c 2020-07-23 stsp ssize_t target_len;
5315 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5316 35213c7c 2020-07-23 stsp sizeof(target_path));
5317 35213c7c 2020-07-23 stsp if (target_len == -1) {
5318 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5319 35213c7c 2020-07-23 stsp ct->ondisk_path);
5320 35213c7c 2020-07-23 stsp goto done;
5322 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5323 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5325 35213c7c 2020-07-23 stsp goto done;
5326 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5327 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5328 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5329 35213c7c 2020-07-23 stsp goto done;
5334 cf066bf8 2019-05-09 stsp ct->status = status;
5335 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5336 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5337 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5338 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5339 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5340 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5341 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5342 b416585c 2019-05-13 stsp goto done;
5344 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5345 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5346 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5347 f0b75401 2019-08-03 stsp goto done;
5350 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5351 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5352 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5353 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5354 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5355 036813ee 2019-05-09 stsp goto done;
5358 24519714 2019-05-09 stsp ct->path = strdup(path);
5359 24519714 2019-05-09 stsp if (ct->path == NULL) {
5360 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5361 24519714 2019-05-09 stsp goto done;
5363 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5364 ef899790 2022-11-01 thomas if (err)
5365 ef899790 2022-11-01 thomas goto done;
5367 ef899790 2022-11-01 thomas if (a->diff_outfile && ct && new != NULL) {
5368 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5369 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5370 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5371 ef899790 2022-11-01 thomas if (err)
5372 ef899790 2022-11-01 thomas goto done;
5375 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5376 ed175427 2019-05-09 stsp free_commitable(ct);
5377 24519714 2019-05-09 stsp free(parent_path);
5378 036813ee 2019-05-09 stsp free(path);
5379 a129376b 2019-03-28 stsp return err;
5382 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5383 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5384 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5385 036813ee 2019-05-09 stsp struct got_repository *);
5387 ed175427 2019-05-09 stsp static const struct got_error *
5388 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5389 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5390 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5391 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5392 afa376bf 2019-05-09 stsp struct got_repository *repo)
5394 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5395 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5396 036813ee 2019-05-09 stsp char *subpath;
5398 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5399 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5400 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5402 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5404 ed175427 2019-05-09 stsp return err;
5406 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5407 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5408 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5409 036813ee 2019-05-09 stsp free(subpath);
5410 036813ee 2019-05-09 stsp return err;
5413 036813ee 2019-05-09 stsp static const struct got_error *
5414 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5416 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5417 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5419 036813ee 2019-05-09 stsp *match = 0;
5421 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5422 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5423 0f63689d 2019-05-10 stsp return NULL;
5426 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5428 0f63689d 2019-05-10 stsp return err;
5429 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5430 036813ee 2019-05-09 stsp free(ct_parent_path);
5431 036813ee 2019-05-09 stsp return err;
5434 768aea60 2019-05-09 stsp static mode_t
5435 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5437 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5438 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5440 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5443 036813ee 2019-05-09 stsp static const struct got_error *
5444 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5445 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5447 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5449 036813ee 2019-05-09 stsp *new_te = NULL;
5451 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5453 036813ee 2019-05-09 stsp goto done;
5455 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5457 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5458 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5459 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5461 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5463 036813ee 2019-05-09 stsp if (err && *new_te) {
5464 56e0773d 2019-11-28 stsp free(*new_te);
5465 036813ee 2019-05-09 stsp *new_te = NULL;
5467 036813ee 2019-05-09 stsp return err;
5470 036813ee 2019-05-09 stsp static const struct got_error *
5471 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5472 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5474 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5475 102b254e 2020-10-19 stsp char *ct_name = NULL;
5477 036813ee 2019-05-09 stsp *new_te = NULL;
5479 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5480 036813ee 2019-05-09 stsp if (*new_te == NULL)
5481 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5483 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5485 036813ee 2019-05-09 stsp goto done;
5486 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5487 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5488 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5489 036813ee 2019-05-09 stsp goto done;
5492 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5494 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5495 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5496 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5498 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5500 102b254e 2020-10-19 stsp free(ct_name);
5501 036813ee 2019-05-09 stsp if (err && *new_te) {
5502 56e0773d 2019-11-28 stsp free(*new_te);
5503 036813ee 2019-05-09 stsp *new_te = NULL;
5505 036813ee 2019-05-09 stsp return err;
5508 036813ee 2019-05-09 stsp static const struct got_error *
5509 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5510 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5512 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5513 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5515 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5517 036813ee 2019-05-09 stsp return err;
5518 036813ee 2019-05-09 stsp if (new_pe == NULL)
5519 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5520 036813ee 2019-05-09 stsp return NULL;
5523 afa376bf 2019-05-09 stsp static const struct got_error *
5524 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5525 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5527 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5528 5f8a88c6 2019-08-03 stsp unsigned char status;
5530 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5531 ae1e948a 2021-09-28 thomas return NULL;
5533 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5534 afa376bf 2019-05-09 stsp ct_path++;
5536 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5537 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5539 5f8a88c6 2019-08-03 stsp status = ct->status;
5541 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5542 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5545 036813ee 2019-05-09 stsp static const struct got_error *
5546 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5547 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5549 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5550 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5551 44d03001 2019-05-09 stsp char *te_path;
5553 44d03001 2019-05-09 stsp *modified = 0;
5555 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5556 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5557 44d03001 2019-05-09 stsp te->name) == -1)
5558 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5560 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5561 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5562 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5563 44d03001 2019-05-09 stsp strlen(te_path));
5564 62d463ca 2020-10-20 naddy if (*modified)
5568 44d03001 2019-05-09 stsp free(te_path);
5569 44d03001 2019-05-09 stsp return err;
5572 44d03001 2019-05-09 stsp static const struct got_error *
5573 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5574 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5575 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5577 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5578 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5580 036813ee 2019-05-09 stsp *ctp = NULL;
5582 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5583 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5584 036813ee 2019-05-09 stsp char *ct_name = NULL;
5585 036813ee 2019-05-09 stsp int path_matches;
5587 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5588 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5589 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5590 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5591 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5594 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5595 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5599 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5602 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5604 036813ee 2019-05-09 stsp return err;
5605 036813ee 2019-05-09 stsp if (!path_matches)
5608 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5610 d34b633e 2020-10-19 stsp return err;
5612 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5613 d34b633e 2020-10-19 stsp free(ct_name);
5616 d34b633e 2020-10-19 stsp free(ct_name);
5618 036813ee 2019-05-09 stsp *ctp = ct;
5622 2b496619 2019-07-10 stsp return err;
5625 2b496619 2019-07-10 stsp static const struct got_error *
5626 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5627 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5628 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5629 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5630 2b496619 2019-07-10 stsp struct got_repository *repo)
5632 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5633 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5634 2b496619 2019-07-10 stsp char *subtree_path;
5635 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5636 ba580f68 2020-03-22 stsp int nentries;
5638 2b496619 2019-07-10 stsp *new_tep = NULL;
5640 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5641 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5642 2b496619 2019-07-10 stsp child_path) == -1)
5643 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5645 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5646 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5647 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5648 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5650 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5651 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5652 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5653 2b496619 2019-07-10 stsp goto done;
5655 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5656 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5657 2b496619 2019-07-10 stsp if (err) {
5658 56e0773d 2019-11-28 stsp free(new_te);
5659 2b496619 2019-07-10 stsp goto done;
5661 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5664 2b496619 2019-07-10 stsp free(subtree_path);
5665 2b496619 2019-07-10 stsp if (err == NULL)
5666 2b496619 2019-07-10 stsp *new_tep = new_te;
5667 036813ee 2019-05-09 stsp return err;
5670 036813ee 2019-05-09 stsp static const struct got_error *
5671 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5672 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5673 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5674 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5675 036813ee 2019-05-09 stsp struct got_repository *repo)
5677 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5678 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5679 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5680 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5682 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5683 ba580f68 2020-03-22 stsp *nentries = 0;
5685 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5686 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5687 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5688 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5690 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5691 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5692 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5695 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5696 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5699 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5700 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5702 036813ee 2019-05-09 stsp goto done;
5704 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5705 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5706 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5708 036813ee 2019-05-09 stsp goto done;
5709 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5711 afa376bf 2019-05-09 stsp goto done;
5712 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5713 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5715 2f51b5b3 2019-05-09 stsp goto done;
5716 ba580f68 2020-03-22 stsp (*nentries)++;
5718 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5719 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5720 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5721 2b496619 2019-07-10 stsp == NULL) {
5722 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5723 2b496619 2019-07-10 stsp child_path, path_base_tree,
5724 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5727 2b496619 2019-07-10 stsp goto done;
5728 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5730 2b496619 2019-07-10 stsp goto done;
5731 ba580f68 2020-03-22 stsp (*nentries)++;
5736 2f51b5b3 2019-05-09 stsp if (base_tree) {
5737 56e0773d 2019-11-28 stsp int i, nbase_entries;
5738 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5739 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5740 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5741 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5743 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5744 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5745 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5746 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5748 63c5ca5d 2019-08-24 stsp goto done;
5749 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5751 63c5ca5d 2019-08-24 stsp goto done;
5752 ba580f68 2020-03-22 stsp (*nentries)++;
5756 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5757 44d03001 2019-05-09 stsp int modified;
5758 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5760 036813ee 2019-05-09 stsp goto done;
5761 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5762 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5764 2f51b5b3 2019-05-09 stsp goto done;
5765 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5766 44d03001 2019-05-09 stsp if (modified) {
5767 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5768 ba580f68 2020-03-22 stsp int nsubentries;
5769 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5770 ba580f68 2020-03-22 stsp &nsubentries, te,
5771 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5772 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5774 44d03001 2019-05-09 stsp goto done;
5775 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5776 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5777 ba580f68 2020-03-22 stsp free(new_id);
5780 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5781 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5782 56e0773d 2019-11-28 stsp free(new_id);
5784 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5786 036813ee 2019-05-09 stsp goto done;
5787 ba580f68 2020-03-22 stsp (*nentries)++;
5791 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5792 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5794 f66c734c 2019-09-22 stsp goto done;
5796 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5797 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5798 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5799 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
5800 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5801 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5802 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5804 2f51b5b3 2019-05-09 stsp goto done;
5805 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5807 2f51b5b3 2019-05-09 stsp goto done;
5808 ba580f68 2020-03-22 stsp (*nentries)++;
5810 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5811 b416585c 2019-05-13 stsp status_arg);
5813 afa376bf 2019-05-09 stsp goto done;
5815 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5816 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5818 2f51b5b3 2019-05-09 stsp goto done;
5819 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5821 2f51b5b3 2019-05-09 stsp goto done;
5822 ba580f68 2020-03-22 stsp (*nentries)++;
5827 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5828 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5830 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5831 2e1fa222 2020-07-23 stsp return err;
5834 2e1fa222 2020-07-23 stsp static const struct got_error *
5835 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5836 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5837 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5838 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5840 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5841 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5842 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5844 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5845 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5846 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5848 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5850 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5851 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5852 437adc9d 2020-12-10 yzhong if (err)
5853 437adc9d 2020-12-10 yzhong goto done;
5856 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5857 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5858 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5859 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5860 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5861 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5862 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5863 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5865 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5866 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5867 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5868 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5869 72fd46fa 2019-09-06 stsp !have_staged_files);
5871 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5872 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5873 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5874 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5875 72fd46fa 2019-09-06 stsp !have_staged_files);
5877 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5879 437adc9d 2020-12-10 yzhong goto done;
5880 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5881 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5882 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5883 3969253a 2020-03-07 stsp if (err) {
5884 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5885 437adc9d 2020-12-10 yzhong goto done;
5887 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5888 3969253a 2020-03-07 stsp if (err) {
5889 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5890 437adc9d 2020-12-10 yzhong goto done;
5893 437adc9d 2020-12-10 yzhong free(relpath);
5894 437adc9d 2020-12-10 yzhong relpath = NULL;
5897 437adc9d 2020-12-10 yzhong free(relpath);
5898 d56d26ce 2019-05-10 stsp return err;
5902 d56d26ce 2019-05-10 stsp static const struct got_error *
5903 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5904 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5905 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5906 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5907 735ef5ac 2019-08-03 stsp int ood_errcode)
5909 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5910 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5911 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5913 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5914 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5915 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5916 1a36436d 2019-06-10 stsp return NULL;
5918 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5919 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5921 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5922 945f9229 2022-04-16 thomas if (err)
5923 945f9229 2022-04-16 thomas goto done;
5924 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5925 9bead371 2019-07-28 stsp if (err) {
5926 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5927 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5928 1a36436d 2019-06-10 stsp goto done;
5929 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5930 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5932 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5933 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5934 945f9229 2022-04-16 thomas if (err)
5935 945f9229 2022-04-16 thomas goto done;
5936 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5937 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5938 1a36436d 2019-06-10 stsp goto done;
5939 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5943 945f9229 2022-04-16 thomas if (commit)
5944 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5945 1a36436d 2019-06-10 stsp return err;
5948 ef20f542 2022-06-26 thomas static const struct got_error *
5949 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5950 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5951 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
5952 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
5953 10604dce 2021-09-24 thomas struct got_worktree *worktree,
5954 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
5955 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5956 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5957 afa376bf 2019-05-09 stsp struct got_repository *repo)
5959 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5960 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5961 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5962 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5963 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5964 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5965 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5966 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5967 10604dce 2021-09-24 thomas int nentries, nparents = 0;
5968 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5969 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5970 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5971 892eab0a 2022-07-22 thomas time_t timestamp;
5973 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5975 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5977 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5979 588edf97 2019-05-10 stsp goto done;
5981 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5983 588edf97 2019-05-10 stsp goto done;
5985 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5986 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
5987 ef899790 2022-11-01 thomas &logmsg, commit_arg);
5992 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5993 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5997 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5998 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5999 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6000 cf066bf8 2019-05-09 stsp char *ondisk_path;
6002 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6003 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6004 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6007 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6008 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6009 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6010 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6013 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6014 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6015 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6016 cf066bf8 2019-05-09 stsp goto done;
6018 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6019 2e1fa222 2020-07-23 stsp free(ondisk_path);
6021 cf066bf8 2019-05-09 stsp goto done;
6024 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6025 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6026 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6028 036813ee 2019-05-09 stsp goto done;
6030 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6032 de18fc63 2019-05-09 stsp goto done;
6033 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6034 10604dce 2021-09-24 thomas nparents++;
6035 10604dce 2021-09-24 thomas if (parent_id2) {
6036 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6037 10604dce 2021-09-24 thomas if (err)
6038 10604dce 2021-09-24 thomas goto done;
6039 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6040 10604dce 2021-09-24 thomas nparents++;
6042 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6043 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6044 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6045 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6046 33ad4cbe 2019-05-12 jcs free(logmsg);
6048 9d40349a 2019-05-09 stsp goto done;
6050 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6051 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6052 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6053 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6054 09f5bd90 2019-05-09 stsp goto done;
6056 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6057 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6059 09f5bd90 2019-05-09 stsp goto done;
6060 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6062 ebf99748 2019-05-09 stsp goto done;
6063 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6064 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6065 09f5bd90 2019-05-09 stsp goto done;
6067 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6068 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6070 f2c16586 2019-05-09 stsp goto done;
6071 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6073 f2c16586 2019-05-09 stsp goto done;
6075 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6077 09f5bd90 2019-05-09 stsp goto done;
6079 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6081 ebf99748 2019-05-09 stsp goto done;
6083 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6084 588edf97 2019-05-10 stsp if (head_tree)
6085 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6086 588edf97 2019-05-10 stsp if (head_commit)
6087 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6088 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6089 2f17228e 2019-05-12 stsp if (head_ref2) {
6090 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6091 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6092 2f17228e 2019-05-12 stsp err = unlockerr;
6093 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6095 39cd0ff6 2019-07-12 stsp return err;
6098 5c1e53bc 2019-07-28 stsp static const struct got_error *
6099 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6100 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6102 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6103 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6105 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6106 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6107 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6109 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6110 5c1e53bc 2019-07-28 stsp ct_path++;
6112 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6113 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6117 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6118 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6120 5c1e53bc 2019-07-28 stsp return NULL;
6123 f0b75401 2019-08-03 stsp static const struct got_error *
6124 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6126 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6128 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6129 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6130 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6133 f0b75401 2019-08-03 stsp return NULL;
6136 5f8a88c6 2019-08-03 stsp static const struct got_error *
6137 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6138 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6140 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6141 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6143 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6144 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6146 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6147 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6148 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6149 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6150 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6151 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6154 5f8a88c6 2019-08-03 stsp return NULL;
6157 39cd0ff6 2019-07-12 stsp const struct got_error *
6158 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6159 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6160 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6161 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6162 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6163 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6164 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6166 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6167 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6168 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6169 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6170 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6171 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6172 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6173 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6174 ef899790 2022-11-01 thomas char *diff_path = NULL;
6175 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6177 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6179 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6180 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6182 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6184 39cd0ff6 2019-07-12 stsp goto done;
6186 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6188 39cd0ff6 2019-07-12 stsp goto done;
6190 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6192 39cd0ff6 2019-07-12 stsp goto done;
6194 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6196 39cd0ff6 2019-07-12 stsp goto done;
6198 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6199 5f8a88c6 2019-08-03 stsp &have_staged_files);
6200 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6201 5f8a88c6 2019-08-03 stsp goto done;
6202 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6203 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6205 5f8a88c6 2019-08-03 stsp goto done;
6208 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6209 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6210 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6211 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6212 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6213 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6214 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6215 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6216 ef899790 2022-11-01 thomas if (show_diff) {
6217 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6218 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6219 ef899790 2022-11-01 thomas if (err)
6220 ef899790 2022-11-01 thomas goto done;
6221 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6222 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6223 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6224 ef899790 2022-11-01 thomas goto done;
6226 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6227 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6228 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6229 ef899790 2022-11-01 thomas goto done;
6233 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6234 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6235 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6237 5c1e53bc 2019-07-28 stsp goto done;
6240 ef899790 2022-11-01 thomas if (show_diff) {
6241 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6242 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6243 ef899790 2022-11-01 thomas goto done;
6247 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6248 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6249 39cd0ff6 2019-07-12 stsp goto done;
6252 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6253 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6255 5c1e53bc 2019-07-28 stsp goto done;
6258 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6259 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6260 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6262 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6263 f0b75401 2019-08-03 stsp ct_path++;
6264 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6265 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6266 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6268 b50cabdf 2019-07-12 stsp goto done;
6272 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6273 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6274 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6275 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6277 39cd0ff6 2019-07-12 stsp goto done;
6279 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6280 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6281 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6282 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6283 39cd0ff6 2019-07-12 stsp err = sync_err;
6285 39cd0ff6 2019-07-12 stsp if (fileindex)
6286 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6287 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6288 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6289 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6290 39cd0ff6 2019-07-12 stsp err = unlockerr;
6291 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6292 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6294 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6296 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6297 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6298 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6299 ef899790 2022-11-01 thomas free(diff_path);
6300 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6301 ef899790 2022-11-01 thomas err == NULL)
6302 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6303 c4296144 2019-05-09 stsp return err;
6306 8656d6c4 2019-05-20 stsp const char *
6307 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6309 8656d6c4 2019-05-20 stsp return ct->path;
6312 8656d6c4 2019-05-20 stsp unsigned int
6313 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6315 8656d6c4 2019-05-20 stsp return ct->status;
6318 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6319 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6320 818c7501 2019-07-11 stsp struct got_repository *repo;
6323 818c7501 2019-07-11 stsp static const struct got_error *
6324 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6326 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6327 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6328 818c7501 2019-07-11 stsp unsigned char status;
6329 818c7501 2019-07-11 stsp struct stat sb;
6330 818c7501 2019-07-11 stsp char *ondisk_path;
6332 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6333 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6334 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6335 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6337 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6339 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6341 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6342 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6343 818c7501 2019-07-11 stsp free(ondisk_path);
6345 818c7501 2019-07-11 stsp return err;
6347 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6348 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6349 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6350 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6352 818c7501 2019-07-11 stsp return NULL;
6355 818c7501 2019-07-11 stsp const struct got_error *
6356 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6357 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6358 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6359 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6361 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6362 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6363 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6364 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6365 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6366 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6367 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6369 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6370 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6371 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6373 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6375 818c7501 2019-07-11 stsp return err;
6377 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6379 818c7501 2019-07-11 stsp goto done;
6381 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6382 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6383 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6386 818c7501 2019-07-11 stsp goto done;
6388 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6390 818c7501 2019-07-11 stsp goto done;
6392 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6394 818c7501 2019-07-11 stsp goto done;
6396 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6398 818c7501 2019-07-11 stsp goto done;
6400 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6403 e51d7b55 2020-01-04 stsp goto done;
6405 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6407 818c7501 2019-07-11 stsp goto done;
6408 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6409 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6410 e51d7b55 2020-01-04 stsp goto done;
6413 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6414 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6416 818c7501 2019-07-11 stsp goto done;
6417 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6419 818c7501 2019-07-11 stsp goto done;
6421 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6423 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6425 818c7501 2019-07-11 stsp goto done;
6427 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6429 818c7501 2019-07-11 stsp goto done;
6431 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6432 818c7501 2019-07-11 stsp worktree->base_commit_id);
6434 818c7501 2019-07-11 stsp goto done;
6435 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6437 818c7501 2019-07-11 stsp goto done;
6439 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6441 818c7501 2019-07-11 stsp goto done;
6443 818c7501 2019-07-11 stsp free(fileindex_path);
6444 818c7501 2019-07-11 stsp free(tmp_branch_name);
6445 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6446 818c7501 2019-07-11 stsp free(branch_ref_name);
6447 818c7501 2019-07-11 stsp if (branch_ref)
6448 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6449 818c7501 2019-07-11 stsp if (wt_branch)
6450 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6451 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6452 818c7501 2019-07-11 stsp if (err) {
6453 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6454 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6455 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6457 818c7501 2019-07-11 stsp if (*tmp_branch) {
6458 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6459 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6461 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6462 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6463 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6465 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6467 818c7501 2019-07-11 stsp return err;
6470 818c7501 2019-07-11 stsp const struct got_error *
6471 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6472 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6473 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6474 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6476 818c7501 2019-07-11 stsp const struct got_error *err;
6477 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6478 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6479 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6480 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6481 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6483 818c7501 2019-07-11 stsp *commit_id = NULL;
6484 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6485 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6486 3e3a69f1 2019-07-25 stsp *branch = NULL;
6487 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6489 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6491 3e3a69f1 2019-07-25 stsp return err;
6493 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6495 f032f1f7 2019-08-04 stsp goto done;
6497 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6498 f032f1f7 2019-08-04 stsp &have_staged_files);
6499 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6500 f032f1f7 2019-08-04 stsp goto done;
6501 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6502 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6503 3e3a69f1 2019-07-25 stsp goto done;
6506 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6508 f032f1f7 2019-08-04 stsp goto done;
6510 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6512 818c7501 2019-07-11 stsp goto done;
6514 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6516 818c7501 2019-07-11 stsp goto done;
6518 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6520 818c7501 2019-07-11 stsp goto done;
6522 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6524 818c7501 2019-07-11 stsp goto done;
6526 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6527 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6529 818c7501 2019-07-11 stsp goto done;
6531 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6533 818c7501 2019-07-11 stsp goto done;
6535 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6537 818c7501 2019-07-11 stsp goto done;
6539 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6540 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6542 818c7501 2019-07-11 stsp goto done;
6544 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6546 818c7501 2019-07-11 stsp goto done;
6548 818c7501 2019-07-11 stsp free(commit_ref_name);
6549 818c7501 2019-07-11 stsp free(branch_ref_name);
6550 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6551 818c7501 2019-07-11 stsp if (commit_ref)
6552 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6553 818c7501 2019-07-11 stsp if (branch_ref)
6554 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6555 818c7501 2019-07-11 stsp if (err) {
6556 818c7501 2019-07-11 stsp free(*commit_id);
6557 818c7501 2019-07-11 stsp *commit_id = NULL;
6558 818c7501 2019-07-11 stsp if (*tmp_branch) {
6559 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6560 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6562 818c7501 2019-07-11 stsp if (*new_base_branch) {
6563 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6564 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6566 818c7501 2019-07-11 stsp if (*branch) {
6567 818c7501 2019-07-11 stsp got_ref_close(*branch);
6568 818c7501 2019-07-11 stsp *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 3e3a69f1 2019-07-25 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_in_progress(int *in_progress, struct got_worktree *worktree)
6582 818c7501 2019-07-11 stsp const struct got_error *err;
6583 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6585 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6587 818c7501 2019-07-11 stsp return err;
6589 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6590 818c7501 2019-07-11 stsp free(tmp_branch_name);
6591 818c7501 2019-07-11 stsp return NULL;
6594 818c7501 2019-07-11 stsp static const struct got_error *
6595 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6596 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
6598 0ebf8283 2019-07-24 stsp *logmsg = arg;
6599 818c7501 2019-07-11 stsp return NULL;
6602 818c7501 2019-07-11 stsp static const struct got_error *
6603 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6604 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6605 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6606 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6608 818c7501 2019-07-11 stsp return NULL;
6611 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6612 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6613 01757395 2019-07-12 stsp void *progress_arg;
6614 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6617 01757395 2019-07-12 stsp static const struct got_error *
6618 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6620 01757395 2019-07-12 stsp const struct got_error *err;
6621 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6623 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6625 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6627 01757395 2019-07-12 stsp return err;
6629 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6630 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6631 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6632 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6633 01757395 2019-07-12 stsp return NULL;
6635 01757395 2019-07-12 stsp p = strdup(path);
6636 01757395 2019-07-12 stsp if (p == NULL)
6637 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6639 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6640 01757395 2019-07-12 stsp if (err || new == NULL)
6642 01757395 2019-07-12 stsp return err;
6645 0ebf8283 2019-07-24 stsp static const struct got_error *
6646 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6647 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6649 818c7501 2019-07-11 stsp const struct got_error *err;
6650 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6652 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6653 818c7501 2019-07-11 stsp if (err) {
6654 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6655 818c7501 2019-07-11 stsp goto done;
6656 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6658 818c7501 2019-07-11 stsp goto done;
6659 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6661 818c7501 2019-07-11 stsp goto done;
6662 de05890f 2020-03-05 stsp } else if (is_rebase) {
6663 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6666 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6668 818c7501 2019-07-11 stsp goto done;
6669 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6670 818c7501 2019-07-11 stsp free(stored_id);
6671 818c7501 2019-07-11 stsp if (cmp != 0) {
6672 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6673 818c7501 2019-07-11 stsp goto done;
6677 0ebf8283 2019-07-24 stsp if (commit_ref)
6678 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6679 0ebf8283 2019-07-24 stsp return err;
6682 0ebf8283 2019-07-24 stsp static const struct got_error *
6683 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6684 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6685 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6686 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6687 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6688 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6690 0ebf8283 2019-07-24 stsp const struct got_error *err;
6691 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6692 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6693 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6695 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6697 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6699 0ebf8283 2019-07-24 stsp return err;
6701 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6702 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6703 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6704 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6705 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6706 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6707 818c7501 2019-07-11 stsp if (commit_ref)
6708 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6709 818c7501 2019-07-11 stsp return err;
6712 818c7501 2019-07-11 stsp const struct got_error *
6713 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6714 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6715 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6716 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6717 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6718 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6720 0ebf8283 2019-07-24 stsp const struct got_error *err;
6721 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6723 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6725 0ebf8283 2019-07-24 stsp return err;
6727 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6729 0ebf8283 2019-07-24 stsp goto done;
6731 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6732 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6733 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6735 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6736 0ebf8283 2019-07-24 stsp return err;
6739 0ebf8283 2019-07-24 stsp const struct got_error *
6740 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6741 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6742 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6743 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6744 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6745 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6747 0ebf8283 2019-07-24 stsp const struct got_error *err;
6748 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6750 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6752 0ebf8283 2019-07-24 stsp return err;
6754 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6756 0ebf8283 2019-07-24 stsp goto done;
6758 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6759 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6760 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6762 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6763 0ebf8283 2019-07-24 stsp return err;
6766 0ebf8283 2019-07-24 stsp static const struct got_error *
6767 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6768 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6769 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6770 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
6771 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
6772 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
6774 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6775 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6776 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6777 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6778 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6779 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6780 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6782 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6783 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6784 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6786 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6788 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6790 3e3a69f1 2019-07-25 stsp return err;
6792 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6793 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6794 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6795 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6796 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
6798 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6799 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6801 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6802 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6803 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6804 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6805 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6806 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6808 01757395 2019-07-12 stsp if (merged_paths) {
6809 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6810 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6811 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6812 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6815 01757395 2019-07-12 stsp goto done;
6818 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6819 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6821 01757395 2019-07-12 stsp goto done;
6824 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6825 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6826 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6828 a0e95631 2019-07-12 stsp goto done;
6829 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6830 a0e95631 2019-07-12 stsp goto done;
6833 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6835 edd02c5e 2019-07-11 stsp goto done;
6837 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6839 818c7501 2019-07-11 stsp goto done;
6841 5943eee2 2019-08-13 stsp if (new_logmsg) {
6842 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6843 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6844 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6845 5943eee2 2019-08-13 stsp goto done;
6848 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6850 5943eee2 2019-08-13 stsp goto done;
6853 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6854 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6855 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
6856 8db00f97 2022-07-22 thomas committer ? committer :
6857 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
6858 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6860 a0e95631 2019-07-12 stsp goto done;
6862 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6864 a0e95631 2019-07-12 stsp goto done;
6866 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6868 a0e95631 2019-07-12 stsp goto done;
6870 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6871 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6872 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6873 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6874 a0e95631 2019-07-12 stsp err = sync_err;
6876 a0e95631 2019-07-12 stsp free(fileindex_path);
6877 a0e95631 2019-07-12 stsp free(head_commit_id);
6878 a0e95631 2019-07-12 stsp if (head_ref)
6879 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6880 818c7501 2019-07-11 stsp if (err) {
6881 818c7501 2019-07-11 stsp free(*new_commit_id);
6882 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6884 818c7501 2019-07-11 stsp return err;
6887 818c7501 2019-07-11 stsp const struct got_error *
6888 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6889 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6890 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6891 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
6892 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
6893 be94c032 2023-02-20 thomas struct got_repository *repo)
6895 0ebf8283 2019-07-24 stsp const struct got_error *err;
6896 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6897 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6898 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6900 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6902 0ebf8283 2019-07-24 stsp return err;
6904 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6906 0ebf8283 2019-07-24 stsp goto done;
6907 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6909 0ebf8283 2019-07-24 stsp goto done;
6910 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6911 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6912 0ebf8283 2019-07-24 stsp goto done;
6915 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6916 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
6917 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
6919 0ebf8283 2019-07-24 stsp if (commit_ref)
6920 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6921 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6922 0ebf8283 2019-07-24 stsp free(commit_id);
6923 0ebf8283 2019-07-24 stsp return err;
6926 0ebf8283 2019-07-24 stsp const struct got_error *
6927 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6928 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6929 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6930 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
6931 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6932 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
6934 0ebf8283 2019-07-24 stsp const struct got_error *err;
6935 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6936 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6938 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6940 0ebf8283 2019-07-24 stsp return err;
6942 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6944 0ebf8283 2019-07-24 stsp goto done;
6946 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6947 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
6948 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
6950 0ebf8283 2019-07-24 stsp if (commit_ref)
6951 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6952 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6953 0ebf8283 2019-07-24 stsp return err;
6956 0ebf8283 2019-07-24 stsp const struct got_error *
6957 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6958 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6960 3e3a69f1 2019-07-25 stsp if (fileindex)
6961 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6962 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6965 69844fba 2019-07-11 stsp static const struct got_error *
6966 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6968 69844fba 2019-07-11 stsp const struct got_error *err;
6969 69844fba 2019-07-11 stsp struct got_reference *ref;
6971 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6972 69844fba 2019-07-11 stsp if (err) {
6973 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6974 69844fba 2019-07-11 stsp return NULL;
6975 69844fba 2019-07-11 stsp return err;
6978 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6979 69844fba 2019-07-11 stsp got_ref_close(ref);
6980 69844fba 2019-07-11 stsp return err;
6983 69844fba 2019-07-11 stsp static const struct got_error *
6984 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6986 69844fba 2019-07-11 stsp const struct got_error *err;
6987 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6988 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6990 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6992 69844fba 2019-07-11 stsp goto done;
6993 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6995 69844fba 2019-07-11 stsp goto done;
6997 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6999 69844fba 2019-07-11 stsp goto done;
7000 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7002 69844fba 2019-07-11 stsp goto done;
7004 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7006 69844fba 2019-07-11 stsp goto done;
7007 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7009 69844fba 2019-07-11 stsp goto done;
7011 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7013 69844fba 2019-07-11 stsp goto done;
7014 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7016 69844fba 2019-07-11 stsp goto done;
7019 69844fba 2019-07-11 stsp free(tmp_branch_name);
7020 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7021 69844fba 2019-07-11 stsp free(branch_ref_name);
7022 69844fba 2019-07-11 stsp free(commit_ref_name);
7023 e600f124 2021-03-21 stsp return err;
7026 ef20f542 2022-06-26 thomas static const struct got_error *
7027 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7028 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7030 e600f124 2021-03-21 stsp const struct got_error *err;
7031 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7032 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7033 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7034 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7035 e600f124 2021-03-21 stsp char *refname = NULL;
7037 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7038 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7039 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7040 e600f124 2021-03-21 stsp branch_name += 11;
7042 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7044 e600f124 2021-03-21 stsp return err;
7046 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7047 e600f124 2021-03-21 stsp new_id_str) == -1) {
7048 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7049 e600f124 2021-03-21 stsp goto done;
7052 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7054 e600f124 2021-03-21 stsp goto done;
7056 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7058 e600f124 2021-03-21 stsp goto done;
7060 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7062 e600f124 2021-03-21 stsp free(new_id_str);
7063 e600f124 2021-03-21 stsp free(refname);
7064 e600f124 2021-03-21 stsp free(old_commit_id);
7066 e600f124 2021-03-21 stsp got_ref_close(ref);
7067 69844fba 2019-07-11 stsp return err;
7070 818c7501 2019-07-11 stsp const struct got_error *
7071 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7072 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7073 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7074 f08f28be 2023-02-03 thomas int create_backup)
7076 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7077 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7078 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7080 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7082 818c7501 2019-07-11 stsp return err;
7084 e600f124 2021-03-21 stsp if (create_backup) {
7085 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7086 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7088 e600f124 2021-03-21 stsp goto done;
7091 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7093 818c7501 2019-07-11 stsp goto done;
7095 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7097 818c7501 2019-07-11 stsp goto done;
7099 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7101 818c7501 2019-07-11 stsp goto done;
7103 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7105 a615e0e7 2020-12-16 stsp goto done;
7107 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7109 a615e0e7 2020-12-16 stsp goto done;
7110 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7111 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7112 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7113 a615e0e7 2020-12-16 stsp err = sync_err;
7115 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7116 a615e0e7 2020-12-16 stsp free(fileindex_path);
7117 818c7501 2019-07-11 stsp free(new_head_commit_id);
7118 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7119 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7120 818c7501 2019-07-11 stsp err = unlockerr;
7121 818c7501 2019-07-11 stsp return err;
7124 818c7501 2019-07-11 stsp const struct got_error *
7125 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7126 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7127 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7128 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7130 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7131 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7132 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7133 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7134 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7135 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7136 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7138 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7140 818c7501 2019-07-11 stsp return err;
7142 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7143 945f9229 2022-04-16 thomas worktree->base_commit_id);
7144 945f9229 2022-04-16 thomas if (err)
7145 945f9229 2022-04-16 thomas goto done;
7147 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7148 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7150 818c7501 2019-07-11 stsp goto done;
7152 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7154 818c7501 2019-07-11 stsp goto done;
7157 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7158 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7159 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7161 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7163 818c7501 2019-07-11 stsp goto done;
7165 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7167 818c7501 2019-07-11 stsp goto done;
7169 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7170 945f9229 2022-04-16 thomas worktree->path_prefix);
7172 ca355955 2019-07-12 stsp goto done;
7174 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7176 ca355955 2019-07-12 stsp goto done;
7178 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7180 818c7501 2019-07-11 stsp goto done;
7182 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7183 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7184 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7185 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7186 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7187 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7188 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7189 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7190 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7191 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7193 1f1abb7e 2019-08-08 stsp goto sync;
7195 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7196 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7198 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7199 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7200 ca355955 2019-07-12 stsp err = sync_err;
7202 818c7501 2019-07-11 stsp got_ref_close(resolved);
7203 a3a2faf2 2019-07-12 stsp free(tree_id);
7204 818c7501 2019-07-11 stsp free(commit_id);
7205 945f9229 2022-04-16 thomas if (commit)
7206 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7207 0ebf8283 2019-07-24 stsp if (fileindex)
7208 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7209 0ebf8283 2019-07-24 stsp free(fileindex_path);
7211 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7212 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7213 0ebf8283 2019-07-24 stsp err = unlockerr;
7214 0ebf8283 2019-07-24 stsp return err;
7217 0ebf8283 2019-07-24 stsp const struct got_error *
7218 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7219 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7220 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7221 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7223 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7224 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7225 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7226 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7227 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7228 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7229 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7230 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7232 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7233 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7234 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7235 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7237 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7239 0ebf8283 2019-07-24 stsp return err;
7241 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7243 0ebf8283 2019-07-24 stsp goto done;
7245 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7246 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7247 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7250 0ebf8283 2019-07-24 stsp goto done;
7252 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7254 0ebf8283 2019-07-24 stsp goto done;
7256 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7258 0ebf8283 2019-07-24 stsp goto done;
7260 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7261 0ebf8283 2019-07-24 stsp worktree);
7263 0ebf8283 2019-07-24 stsp goto done;
7265 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7268 0ebf8283 2019-07-24 stsp goto done;
7270 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7272 0ebf8283 2019-07-24 stsp goto done;
7274 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7276 0ebf8283 2019-07-24 stsp goto done;
7278 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7279 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7281 0ebf8283 2019-07-24 stsp goto done;
7282 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7284 0ebf8283 2019-07-24 stsp goto done;
7285 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7286 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7287 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7288 0ebf8283 2019-07-24 stsp goto done;
7291 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7292 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7294 0ebf8283 2019-07-24 stsp goto done;
7295 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7297 0ebf8283 2019-07-24 stsp goto done;
7299 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7301 0ebf8283 2019-07-24 stsp goto done;
7303 0ebf8283 2019-07-24 stsp free(fileindex_path);
7304 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7305 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7306 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7307 0ebf8283 2019-07-24 stsp if (wt_branch)
7308 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7309 0ebf8283 2019-07-24 stsp if (err) {
7310 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7311 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7312 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7314 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7315 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7316 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7318 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7319 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7320 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7321 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7323 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7325 0ebf8283 2019-07-24 stsp return err;
7328 0ebf8283 2019-07-24 stsp const struct got_error *
7329 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7330 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7332 3e3a69f1 2019-07-25 stsp if (fileindex)
7333 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7334 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7337 0ebf8283 2019-07-24 stsp const struct got_error *
7338 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7339 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7341 0ebf8283 2019-07-24 stsp const struct got_error *err;
7342 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7344 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7346 0ebf8283 2019-07-24 stsp return err;
7348 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7349 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7350 0ebf8283 2019-07-24 stsp return NULL;
7353 0ebf8283 2019-07-24 stsp const struct got_error *
7354 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7355 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7356 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7357 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7359 0ebf8283 2019-07-24 stsp const struct got_error *err;
7360 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7361 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7362 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7363 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7364 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7365 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7367 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7368 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7369 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7370 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7372 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7374 3e3a69f1 2019-07-25 stsp return err;
7376 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7378 f032f1f7 2019-08-04 stsp goto done;
7380 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7381 f032f1f7 2019-08-04 stsp &have_staged_files);
7382 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7383 f032f1f7 2019-08-04 stsp goto done;
7384 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7385 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7386 3e3a69f1 2019-07-25 stsp goto done;
7389 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7391 f032f1f7 2019-08-04 stsp goto done;
7393 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7395 0ebf8283 2019-07-24 stsp goto done;
7397 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7399 0ebf8283 2019-07-24 stsp goto done;
7401 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7402 0ebf8283 2019-07-24 stsp worktree);
7404 0ebf8283 2019-07-24 stsp goto done;
7406 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7408 0ebf8283 2019-07-24 stsp goto done;
7410 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7412 0ebf8283 2019-07-24 stsp goto done;
7413 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7415 0ebf8283 2019-07-24 stsp goto done;
7417 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7419 0ebf8283 2019-07-24 stsp goto done;
7420 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7422 0ebf8283 2019-07-24 stsp goto done;
7424 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7426 0ebf8283 2019-07-24 stsp goto done;
7428 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7429 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7430 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7431 0ebf8283 2019-07-24 stsp if (commit_ref)
7432 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7433 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7434 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7435 0ebf8283 2019-07-24 stsp if (err) {
7436 0ebf8283 2019-07-24 stsp free(*commit_id);
7437 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7438 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7439 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7440 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7441 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7442 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7444 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7445 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7446 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7448 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7450 0ebf8283 2019-07-24 stsp return err;
7453 0ebf8283 2019-07-24 stsp static const struct got_error *
7454 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7456 0ebf8283 2019-07-24 stsp const struct got_error *err;
7457 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7458 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7460 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7462 0ebf8283 2019-07-24 stsp goto done;
7463 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7465 0ebf8283 2019-07-24 stsp goto done;
7467 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7468 0ebf8283 2019-07-24 stsp worktree);
7470 0ebf8283 2019-07-24 stsp goto done;
7471 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7473 0ebf8283 2019-07-24 stsp goto done;
7475 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7477 0ebf8283 2019-07-24 stsp goto done;
7478 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7480 0ebf8283 2019-07-24 stsp goto done;
7482 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7484 0ebf8283 2019-07-24 stsp goto done;
7485 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7487 0ebf8283 2019-07-24 stsp goto done;
7489 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7490 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7491 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7492 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7493 0ebf8283 2019-07-24 stsp return err;
7496 0ebf8283 2019-07-24 stsp const struct got_error *
7497 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7498 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7499 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7500 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7502 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7503 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7504 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7505 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7506 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7507 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7509 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7511 0ebf8283 2019-07-24 stsp return err;
7513 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7514 945f9229 2022-04-16 thomas worktree->base_commit_id);
7515 945f9229 2022-04-16 thomas if (err)
7516 945f9229 2022-04-16 thomas goto done;
7518 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7519 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7521 0ebf8283 2019-07-24 stsp goto done;
7523 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7525 0ebf8283 2019-07-24 stsp goto done;
7527 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7529 0ebf8283 2019-07-24 stsp goto done;
7531 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7532 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7534 0ebf8283 2019-07-24 stsp goto done;
7536 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7538 0ebf8283 2019-07-24 stsp goto done;
7540 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7542 0ebf8283 2019-07-24 stsp goto done;
7544 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7545 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7546 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7547 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7548 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7549 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7550 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7551 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7552 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7553 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7555 1f1abb7e 2019-08-08 stsp goto sync;
7557 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7558 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7560 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7561 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7562 0ebf8283 2019-07-24 stsp err = sync_err;
7564 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7565 0ebf8283 2019-07-24 stsp free(tree_id);
7566 818c7501 2019-07-11 stsp free(fileindex_path);
7568 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7569 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7570 818c7501 2019-07-11 stsp err = unlockerr;
7571 818c7501 2019-07-11 stsp return err;
7574 0ebf8283 2019-07-24 stsp const struct got_error *
7575 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7576 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7577 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7579 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7580 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7581 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7582 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7584 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7586 0ebf8283 2019-07-24 stsp return err;
7588 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7589 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7591 e600f124 2021-03-21 stsp goto done;
7593 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7594 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7596 0ebf8283 2019-07-24 stsp goto done;
7598 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7600 0ebf8283 2019-07-24 stsp goto done;
7602 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7604 0ebf8283 2019-07-24 stsp goto done;
7606 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7608 0ebf8283 2019-07-24 stsp goto done;
7610 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7612 a615e0e7 2020-12-16 stsp goto done;
7614 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7616 a615e0e7 2020-12-16 stsp goto done;
7617 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7618 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7619 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7620 a615e0e7 2020-12-16 stsp err = sync_err;
7622 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7623 a615e0e7 2020-12-16 stsp free(fileindex_path);
7624 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7625 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7626 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7627 0ebf8283 2019-07-24 stsp err = unlockerr;
7628 0ebf8283 2019-07-24 stsp return err;
7631 0ebf8283 2019-07-24 stsp const struct got_error *
7632 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7633 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7635 0ebf8283 2019-07-24 stsp const struct got_error *err;
7636 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7638 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7640 0ebf8283 2019-07-24 stsp return err;
7642 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7644 0ebf8283 2019-07-24 stsp goto done;
7646 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7648 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7649 2822a352 2019-10-15 stsp return err;
7652 2822a352 2019-10-15 stsp const struct got_error *
7653 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7654 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7655 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7656 2822a352 2019-10-15 stsp struct got_repository *repo)
7658 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7659 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7660 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7662 2822a352 2019-10-15 stsp *fileindex = NULL;
7663 2822a352 2019-10-15 stsp *branch_ref = NULL;
7664 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7666 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7668 2822a352 2019-10-15 stsp return err;
7670 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7671 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7672 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7673 2822a352 2019-10-15 stsp "update -b or different branch name required");
7674 2822a352 2019-10-15 stsp goto done;
7677 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7679 2822a352 2019-10-15 stsp goto done;
7681 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7682 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7683 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7684 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7687 2822a352 2019-10-15 stsp goto done;
7689 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7691 2822a352 2019-10-15 stsp goto done;
7693 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7694 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7696 2822a352 2019-10-15 stsp if (err) {
7697 2822a352 2019-10-15 stsp if (*branch_ref) {
7698 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7699 2822a352 2019-10-15 stsp *branch_ref = NULL;
7701 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7702 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7703 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7705 2822a352 2019-10-15 stsp if (*fileindex) {
7706 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7707 2822a352 2019-10-15 stsp *fileindex = NULL;
7709 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7711 0cb83759 2019-08-03 stsp return err;
7714 2822a352 2019-10-15 stsp const struct got_error *
7715 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7716 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7717 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7718 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7719 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7721 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7722 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7723 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7724 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7726 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7728 2822a352 2019-10-15 stsp goto done;
7730 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7732 2822a352 2019-10-15 stsp goto done;
7734 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
7735 945f9229 2022-04-16 thomas if (err)
7736 945f9229 2022-04-16 thomas goto done;
7738 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7739 2822a352 2019-10-15 stsp worktree->path_prefix);
7741 2822a352 2019-10-15 stsp goto done;
7743 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7745 2822a352 2019-10-15 stsp goto done;
7747 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7748 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7750 2822a352 2019-10-15 stsp goto sync;
7752 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7754 2822a352 2019-10-15 stsp goto sync;
7756 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7758 6e210706 2021-01-22 stsp goto sync;
7760 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7762 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7763 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7764 2822a352 2019-10-15 stsp err = sync_err;
7767 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7768 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7769 2822a352 2019-10-15 stsp err = unlockerr;
7770 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7772 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7773 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7774 2822a352 2019-10-15 stsp err = unlockerr;
7775 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7777 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7778 2822a352 2019-10-15 stsp free(fileindex_path);
7779 2822a352 2019-10-15 stsp free(tree_id);
7780 945f9229 2022-04-16 thomas if (commit)
7781 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7783 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7784 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7785 2822a352 2019-10-15 stsp err = unlockerr;
7786 2822a352 2019-10-15 stsp return err;
7789 2822a352 2019-10-15 stsp const struct got_error *
7790 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7791 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7792 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7794 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7796 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7798 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7800 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7801 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7802 8b692cd0 2019-10-21 stsp err = unlockerr;
7803 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7805 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7806 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7807 8b692cd0 2019-10-21 stsp err = unlockerr;
7808 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7810 10604dce 2021-09-24 thomas return err;
7813 10604dce 2021-09-24 thomas const struct got_error *
7814 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
7815 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
7817 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
7818 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7820 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7821 10604dce 2021-09-24 thomas if (err)
7822 10604dce 2021-09-24 thomas goto done;
7824 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7826 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
7827 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7828 10604dce 2021-09-24 thomas err = sync_err;
7830 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7831 10604dce 2021-09-24 thomas free(fileindex_path);
7832 8b692cd0 2019-10-21 stsp return err;
7835 10604dce 2021-09-24 thomas static const struct got_error *
7836 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7838 10604dce 2021-09-24 thomas const struct got_error *err;
7839 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7841 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7842 10604dce 2021-09-24 thomas if (err)
7843 10604dce 2021-09-24 thomas goto done;
7844 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
7845 10604dce 2021-09-24 thomas if (err)
7846 10604dce 2021-09-24 thomas goto done;
7848 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7849 10604dce 2021-09-24 thomas if (err)
7850 10604dce 2021-09-24 thomas goto done;
7851 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
7852 10604dce 2021-09-24 thomas if (err)
7853 10604dce 2021-09-24 thomas goto done;
7856 10604dce 2021-09-24 thomas free(branch_refname);
7857 10604dce 2021-09-24 thomas free(commit_refname);
7858 10604dce 2021-09-24 thomas return err;
7861 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
7862 10604dce 2021-09-24 thomas struct got_worktree *worktree;
7863 10604dce 2021-09-24 thomas const char *branch_name;
7866 10604dce 2021-09-24 thomas static const struct got_error *
7867 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7868 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
7870 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
7872 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7873 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
7874 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
7876 10604dce 2021-09-24 thomas return NULL;
7880 10604dce 2021-09-24 thomas const struct got_error *
7881 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
7882 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
7883 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
7884 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
7885 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7886 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7888 10604dce 2021-09-24 thomas const struct got_error *err;
7889 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7891 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7892 10604dce 2021-09-24 thomas if (err)
7893 10604dce 2021-09-24 thomas goto done;
7895 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7896 10604dce 2021-09-24 thomas worktree);
7897 10604dce 2021-09-24 thomas if (err)
7898 10604dce 2021-09-24 thomas goto done;
7900 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7901 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
7902 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
7904 10604dce 2021-09-24 thomas free(fileindex_path);
7905 10604dce 2021-09-24 thomas return err;
7908 10604dce 2021-09-24 thomas const struct got_error *
7909 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
7910 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
7911 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
7912 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
7913 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
7914 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
7917 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
7918 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
7919 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
7920 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
7921 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
7922 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
7923 10604dce 2021-09-24 thomas int have_staged_files = 0;
7924 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
7925 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7927 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7928 10604dce 2021-09-24 thomas *new_commit_id = NULL;
7930 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
7932 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7933 10604dce 2021-09-24 thomas if (err)
7934 10604dce 2021-09-24 thomas goto done;
7936 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7937 10604dce 2021-09-24 thomas if (err)
7938 10604dce 2021-09-24 thomas goto done;
7940 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
7941 10604dce 2021-09-24 thomas if (err)
7942 10604dce 2021-09-24 thomas goto done;
7944 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7945 10604dce 2021-09-24 thomas &have_staged_files);
7946 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7947 10604dce 2021-09-24 thomas goto done;
7948 10604dce 2021-09-24 thomas if (have_staged_files) {
7949 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7950 10604dce 2021-09-24 thomas goto done;
7953 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
7954 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
7955 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
7956 10604dce 2021-09-24 thomas cc_arg.repo = repo;
7957 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
7958 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7959 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
7960 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7961 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7962 10604dce 2021-09-24 thomas if (err)
7963 10604dce 2021-09-24 thomas goto done;
7965 10604dce 2021-09-24 thomas if (TAILQ_EMPTY(&commitable_paths)) {
7966 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7967 10604dce 2021-09-24 thomas "merge of %s cannot proceed", branch_name);
7968 10604dce 2021-09-24 thomas goto done;
7971 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7972 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7973 10604dce 2021-09-24 thomas const char *ct_path = ct->in_repo_path;
7975 10604dce 2021-09-24 thomas while (ct_path[0] == '/')
7976 10604dce 2021-09-24 thomas ct_path++;
7977 10604dce 2021-09-24 thomas err = check_out_of_date(ct_path, ct->status,
7978 10604dce 2021-09-24 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7979 10604dce 2021-09-24 thomas head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7980 10604dce 2021-09-24 thomas if (err)
7981 10604dce 2021-09-24 thomas goto done;
7985 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
7986 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
7987 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
7988 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
7989 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7990 10604dce 2021-09-24 thomas if (err)
7991 10604dce 2021-09-24 thomas goto done;
7993 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
7994 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
7995 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7996 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7997 10604dce 2021-09-24 thomas err = sync_err;
7999 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8000 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8002 10604dce 2021-09-24 thomas free_commitable(ct);
8004 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8005 10604dce 2021-09-24 thomas free(fileindex_path);
8006 10604dce 2021-09-24 thomas return err;
8009 10604dce 2021-09-24 thomas const struct got_error *
8010 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8011 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8013 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8014 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8016 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8017 10604dce 2021-09-24 thomas if (err)
8018 10604dce 2021-09-24 thomas goto done;
8020 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8021 10604dce 2021-09-24 thomas if (err)
8022 10604dce 2021-09-24 thomas goto done;
8023 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8024 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8025 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8026 10604dce 2021-09-24 thomas err = sync_err;
8028 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8029 10604dce 2021-09-24 thomas free(fileindex_path);
8030 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8031 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8032 10604dce 2021-09-24 thomas err = unlockerr;
8033 10604dce 2021-09-24 thomas return err;
8036 10604dce 2021-09-24 thomas const struct got_error *
8037 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8038 10604dce 2021-09-24 thomas struct got_repository *repo)
8040 10604dce 2021-09-24 thomas const struct got_error *err;
8041 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8042 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8044 10604dce 2021-09-24 thomas *in_progress = 0;
8046 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8047 10604dce 2021-09-24 thomas if (err)
8048 10604dce 2021-09-24 thomas return err;
8049 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8050 dfe86d1f 2021-09-24 thomas free(branch_refname);
8051 10604dce 2021-09-24 thomas if (err) {
8052 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8053 10604dce 2021-09-24 thomas return err;
8055 10604dce 2021-09-24 thomas *in_progress = 1;
8057 10604dce 2021-09-24 thomas return NULL;
8060 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8061 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8062 10604dce 2021-09-24 thomas struct got_reference *branch, struct got_repository *repo)
8064 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8065 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8066 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8067 10604dce 2021-09-24 thomas struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8068 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL;
8069 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8070 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8072 10604dce 2021-09-24 thomas *fileindex = NULL;
8074 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8075 10604dce 2021-09-24 thomas if (err)
8076 10604dce 2021-09-24 thomas return err;
8078 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8079 10604dce 2021-09-24 thomas if (err)
8080 10604dce 2021-09-24 thomas goto done;
8082 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8083 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8084 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8085 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8086 10604dce 2021-09-24 thomas &ok_arg);
8087 10604dce 2021-09-24 thomas if (err)
8088 10604dce 2021-09-24 thomas goto done;
8090 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8091 10604dce 2021-09-24 thomas if (err)
8092 10604dce 2021-09-24 thomas return err;
8094 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8095 10604dce 2021-09-24 thomas if (err)
8096 10604dce 2021-09-24 thomas return err;
8098 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8100 10604dce 2021-09-24 thomas if (err)
8101 10604dce 2021-09-24 thomas goto done;
8103 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8104 10604dce 2021-09-24 thomas if (err)
8105 10604dce 2021-09-24 thomas goto done;
8107 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8108 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8109 10604dce 2021-09-24 thomas goto done;
8112 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8113 10604dce 2021-09-24 thomas if (err)
8114 10604dce 2021-09-24 thomas goto done;
8116 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8117 10604dce 2021-09-24 thomas if (err)
8118 10604dce 2021-09-24 thomas goto done;
8119 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8120 10604dce 2021-09-24 thomas if (err)
8121 10604dce 2021-09-24 thomas goto done;
8123 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8124 10604dce 2021-09-24 thomas if (err)
8125 10604dce 2021-09-24 thomas goto done;
8126 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8127 10604dce 2021-09-24 thomas if (err)
8128 10604dce 2021-09-24 thomas goto done;
8131 10604dce 2021-09-24 thomas free(branch_refname);
8132 10604dce 2021-09-24 thomas free(commit_refname);
8133 10604dce 2021-09-24 thomas free(fileindex_path);
8134 10604dce 2021-09-24 thomas if (branch_ref)
8135 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8136 10604dce 2021-09-24 thomas if (commit_ref)
8137 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8138 10604dce 2021-09-24 thomas if (wt_branch)
8139 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
8140 10604dce 2021-09-24 thomas free(wt_branch_tip);
8141 10604dce 2021-09-24 thomas if (err) {
8142 10604dce 2021-09-24 thomas if (*fileindex) {
8143 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8144 10604dce 2021-09-24 thomas *fileindex = NULL;
8146 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8148 10604dce 2021-09-24 thomas return err;
8151 10604dce 2021-09-24 thomas const struct got_error *
8152 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8153 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8154 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8156 10604dce 2021-09-24 thomas const struct got_error *err;
8157 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8158 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8159 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8160 10604dce 2021-09-24 thomas int have_staged_files = 0;
8162 10604dce 2021-09-24 thomas *branch_name = NULL;
8163 10604dce 2021-09-24 thomas *branch_tip = NULL;
8164 10604dce 2021-09-24 thomas *fileindex = NULL;
8166 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8167 10604dce 2021-09-24 thomas if (err)
8168 10604dce 2021-09-24 thomas return err;
8170 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8171 10604dce 2021-09-24 thomas if (err)
8172 10604dce 2021-09-24 thomas goto done;
8174 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8175 10604dce 2021-09-24 thomas &have_staged_files);
8176 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8177 10604dce 2021-09-24 thomas goto done;
8178 10604dce 2021-09-24 thomas if (have_staged_files) {
8179 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8180 10604dce 2021-09-24 thomas goto done;
8183 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8184 10604dce 2021-09-24 thomas if (err)
8185 10604dce 2021-09-24 thomas goto done;
8187 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8188 10604dce 2021-09-24 thomas if (err)
8189 10604dce 2021-09-24 thomas goto done;
8191 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8192 10604dce 2021-09-24 thomas if (err)
8193 10604dce 2021-09-24 thomas goto done;
8195 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8196 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8197 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8198 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8199 10604dce 2021-09-24 thomas goto done;
8201 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8202 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8203 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8204 10604dce 2021-09-24 thomas goto done;
8207 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8208 10604dce 2021-09-24 thomas if (err)
8209 10604dce 2021-09-24 thomas goto done;
8211 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8212 10604dce 2021-09-24 thomas if (err)
8213 10604dce 2021-09-24 thomas goto done;
8215 10604dce 2021-09-24 thomas free(commit_refname);
8216 10604dce 2021-09-24 thomas free(branch_refname);
8217 10604dce 2021-09-24 thomas free(fileindex_path);
8218 10604dce 2021-09-24 thomas if (commit_ref)
8219 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8220 10604dce 2021-09-24 thomas if (branch_ref)
8221 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8222 10604dce 2021-09-24 thomas if (err) {
8223 10604dce 2021-09-24 thomas if (*branch_name) {
8224 10604dce 2021-09-24 thomas free(*branch_name);
8225 10604dce 2021-09-24 thomas *branch_name = NULL;
8227 10604dce 2021-09-24 thomas free(*branch_tip);
8228 10604dce 2021-09-24 thomas *branch_tip = NULL;
8229 10604dce 2021-09-24 thomas if (*fileindex) {
8230 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8231 10604dce 2021-09-24 thomas *fileindex = NULL;
8233 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8235 10604dce 2021-09-24 thomas return err;
8238 10604dce 2021-09-24 thomas const struct got_error *
8239 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8240 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8241 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8243 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8244 10604dce 2021-09-24 thomas struct got_object_id *commit_id = NULL;
8245 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8246 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8247 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8248 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8250 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8251 945f9229 2022-04-16 thomas worktree->base_commit_id);
8252 945f9229 2022-04-16 thomas if (err)
8253 945f9229 2022-04-16 thomas goto done;
8255 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8256 945f9229 2022-04-16 thomas worktree->path_prefix);
8257 10604dce 2021-09-24 thomas if (err)
8258 10604dce 2021-09-24 thomas goto done;
8260 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8261 10604dce 2021-09-24 thomas if (err)
8262 10604dce 2021-09-24 thomas goto done;
8264 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8265 10604dce 2021-09-24 thomas if (err)
8266 10604dce 2021-09-24 thomas goto done;
8268 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8269 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8270 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8271 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8272 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8273 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8274 10604dce 2021-09-24 thomas rfa.repo = repo;
8275 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8276 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8277 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8278 10604dce 2021-09-24 thomas if (err)
8279 10604dce 2021-09-24 thomas goto sync;
8281 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8282 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8284 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8285 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8286 10604dce 2021-09-24 thomas err = sync_err;
8288 10604dce 2021-09-24 thomas free(tree_id);
8289 10604dce 2021-09-24 thomas free(commit_id);
8290 945f9229 2022-04-16 thomas if (commit)
8291 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8292 10604dce 2021-09-24 thomas if (fileindex)
8293 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8294 10604dce 2021-09-24 thomas free(fileindex_path);
8296 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8297 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8298 10604dce 2021-09-24 thomas err = unlockerr;
8299 10604dce 2021-09-24 thomas return err;
8302 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8303 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8304 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8305 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8306 2db2652d 2019-08-07 stsp struct got_repository *repo;
8307 2db2652d 2019-08-07 stsp int have_changes;
8310 ef20f542 2022-06-26 thomas static const struct got_error *
8311 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8312 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8313 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8314 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8316 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8317 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8318 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8319 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8320 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8321 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8323 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8324 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8325 8b13ce36 2019-08-08 stsp return NULL;
8326 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8327 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8329 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8330 735ef5ac 2019-08-03 stsp if (ie == NULL)
8331 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8333 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8334 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8335 735ef5ac 2019-08-03 stsp relpath) == -1)
8336 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8338 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8339 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
8340 43010591 2023-02-17 thomas &base_commit_id, ie);
8343 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8344 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8345 3aa5969e 2019-08-06 stsp goto done;
8346 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8347 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8348 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8349 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8350 735ef5ac 2019-08-03 stsp goto done;
8353 2db2652d 2019-08-07 stsp a->have_changes = 1;
8355 735ef5ac 2019-08-03 stsp p = in_repo_path;
8356 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8358 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8359 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8360 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8362 735ef5ac 2019-08-03 stsp free(in_repo_path);
8363 dc424a06 2019-08-07 stsp return err;
8366 2db2652d 2019-08-07 stsp struct stage_path_arg {
8367 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8368 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8369 2db2652d 2019-08-07 stsp struct got_repository *repo;
8370 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8371 2db2652d 2019-08-07 stsp void *status_arg;
8372 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8373 2db2652d 2019-08-07 stsp void *patch_arg;
8374 7b5dc508 2019-10-28 stsp int staged_something;
8375 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8378 2db2652d 2019-08-07 stsp static const struct got_error *
8379 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8380 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8381 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8382 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8384 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8385 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8386 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8387 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8388 0cb83759 2019-08-03 stsp uint32_t stage;
8389 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8390 0aeb8099 2020-07-23 stsp struct stat sb;
8392 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8393 8b13ce36 2019-08-08 stsp return NULL;
8395 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8396 d3e7c587 2019-08-03 stsp if (ie == NULL)
8397 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8399 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8400 2db2652d 2019-08-07 stsp relpath)== -1)
8401 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8403 0cb83759 2019-08-03 stsp switch (status) {
8404 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8405 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8406 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8407 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8408 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8411 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8412 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8413 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8414 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8415 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8418 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8421 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8422 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8423 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8424 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8425 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8429 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8430 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8433 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8434 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8435 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8436 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8438 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8439 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8440 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8441 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8442 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8443 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8444 35213c7c 2020-07-23 stsp ssize_t target_len;
8445 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8446 35213c7c 2020-07-23 stsp sizeof(target_path));
8447 35213c7c 2020-07-23 stsp if (target_len == -1) {
8448 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8449 35213c7c 2020-07-23 stsp ondisk_path);
8452 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8453 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8454 35213c7c 2020-07-23 stsp a->worktree->root_path);
8457 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8458 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8459 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8463 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8464 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8465 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8467 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8468 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8470 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8471 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8473 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8474 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8476 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8477 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8478 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8479 f289c82c 2022-06-13 thomas if (err)
8482 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8483 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8485 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8486 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8487 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8488 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8491 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8492 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8494 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8495 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8496 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8497 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8500 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8502 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8503 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8507 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8508 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8509 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8510 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8512 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8513 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8516 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8518 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8519 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8521 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8522 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8525 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8529 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8530 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8531 dc424a06 2019-08-07 stsp free(path_content);
8532 2db2652d 2019-08-07 stsp free(ondisk_path);
8533 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8534 0ebf8283 2019-07-24 stsp return err;
8537 0cb83759 2019-08-03 stsp const struct got_error *
8538 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8539 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8540 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8541 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8542 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8544 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8545 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8546 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8547 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8548 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8549 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8550 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8551 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8553 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8555 0cb83759 2019-08-03 stsp return err;
8557 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8558 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8560 735ef5ac 2019-08-03 stsp goto done;
8561 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8563 735ef5ac 2019-08-03 stsp goto done;
8564 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8566 0cb83759 2019-08-03 stsp goto done;
8568 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8569 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8570 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8571 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8572 2db2652d 2019-08-07 stsp oka.repo = repo;
8573 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8574 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8575 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8576 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
8578 735ef5ac 2019-08-03 stsp goto done;
8580 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8581 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8582 2db2652d 2019-08-07 stsp goto done;
8585 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8586 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8587 2db2652d 2019-08-07 stsp spa.repo = repo;
8588 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8589 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8590 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8591 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8592 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8593 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8594 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8595 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8596 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
8598 2db2652d 2019-08-07 stsp goto done;
8600 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8601 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8602 7b5dc508 2019-10-28 stsp goto done;
8605 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8606 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8607 0cb83759 2019-08-03 stsp err = sync_err;
8609 735ef5ac 2019-08-03 stsp if (head_ref)
8610 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8611 735ef5ac 2019-08-03 stsp free(head_commit_id);
8612 0cb83759 2019-08-03 stsp free(fileindex_path);
8613 0cb83759 2019-08-03 stsp if (fileindex)
8614 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8615 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8616 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8617 0cb83759 2019-08-03 stsp err = unlockerr;
8618 0cb83759 2019-08-03 stsp return err;
8621 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8622 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8623 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8624 ad493afc 2019-08-03 stsp struct got_repository *repo;
8625 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8626 ad493afc 2019-08-03 stsp void *progress_arg;
8627 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8628 2e1f37b0 2019-08-08 stsp void *patch_arg;
8631 2e1f37b0 2019-08-08 stsp static const struct got_error *
8632 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8633 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8634 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8635 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8636 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8638 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8639 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8640 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8641 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8642 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8643 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8644 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8645 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8647 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8648 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8650 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8652 2e1f37b0 2019-08-08 stsp return err;
8654 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8655 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8656 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8657 f4ae6ddb 2022-07-01 thomas goto done;
8659 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8660 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8661 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8662 f4ae6ddb 2022-07-01 thomas goto done;
8665 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8667 2e1f37b0 2019-08-08 stsp goto done;
8669 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8671 2e1f37b0 2019-08-08 stsp goto done;
8673 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8675 2e1f37b0 2019-08-08 stsp goto done;
8677 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8680 2e1f37b0 2019-08-08 stsp goto done;
8682 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8684 2e1f37b0 2019-08-08 stsp goto done;
8686 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8688 2e1f37b0 2019-08-08 stsp goto done;
8690 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8691 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8693 2e1f37b0 2019-08-08 stsp goto done;
8695 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8696 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
8698 2e1f37b0 2019-08-08 stsp goto done;
8699 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8700 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
8702 2e1f37b0 2019-08-08 stsp goto done;
8704 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8705 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8706 2e1f37b0 2019-08-08 stsp goto done;
8708 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8709 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8710 2e1f37b0 2019-08-08 stsp goto done;
8712 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8713 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8714 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
8715 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8716 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8717 fe621944 2020-11-10 stsp nchanges++;
8719 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8720 2e1f37b0 2019-08-08 stsp int choice;
8721 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8722 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8723 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8724 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8726 2e1f37b0 2019-08-08 stsp goto done;
8727 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8728 2e1f37b0 2019-08-08 stsp have_content = 1;
8730 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8731 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8734 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8735 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8736 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8738 2e1f37b0 2019-08-08 stsp free(label1);
8739 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8740 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8742 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8743 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8744 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8745 2e1f37b0 2019-08-08 stsp if (staged_blob)
8746 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8747 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8748 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8749 fe621944 2020-11-10 stsp err = free_err;
8750 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8751 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8752 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8753 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8754 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8755 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8756 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8757 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8758 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8759 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8760 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8761 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8762 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8763 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8764 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8765 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8766 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8767 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8768 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8770 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8771 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8772 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8773 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8774 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8775 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8776 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8778 2e1f37b0 2019-08-08 stsp free(path1);
8779 2e1f37b0 2019-08-08 stsp free(path2);
8780 fda8017d 2020-07-23 stsp return err;
8783 fda8017d 2020-07-23 stsp static const struct got_error *
8784 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8785 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8786 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8787 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8788 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8789 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8790 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8792 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8793 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8794 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8795 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8796 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8797 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8798 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8799 fda8017d 2020-07-23 stsp struct stat sb;
8801 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8802 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8803 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8805 fda8017d 2020-07-23 stsp return err;
8807 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8808 fda8017d 2020-07-23 stsp return NULL;
8810 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8811 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8812 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8814 fda8017d 2020-07-23 stsp goto done;
8817 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
8818 fda8017d 2020-07-23 stsp if (f == NULL) {
8819 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8820 fda8017d 2020-07-23 stsp path_unstaged_content);
8821 fda8017d 2020-07-23 stsp goto done;
8823 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8824 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8825 fda8017d 2020-07-23 stsp goto done;
8827 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8828 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8829 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8831 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8832 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8833 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8834 fda8017d 2020-07-23 stsp goto done;
8836 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8837 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8838 fda8017d 2020-07-23 stsp goto done;
8840 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8841 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8842 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8843 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8844 fda8017d 2020-07-23 stsp progress_arg);
8846 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8848 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8850 67a66647 2021-05-31 stsp return err;
8852 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8853 67a66647 2021-05-31 stsp parent) == -1) {
8854 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8855 67a66647 2021-05-31 stsp base_path = NULL;
8856 67a66647 2021-05-31 stsp goto done;
8859 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
8860 fc2a50f2 2022-11-01 thomas base_path, "");
8862 67a66647 2021-05-31 stsp goto done;
8863 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8864 67a66647 2021-05-31 stsp blob_base);
8866 67a66647 2021-05-31 stsp goto done;
8869 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8870 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8872 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8873 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8874 eec2f5a9 2021-06-03 stsp ondisk_path);
8876 eec2f5a9 2021-06-03 stsp goto done;
8879 06340621 2021-12-31 thomas fd = open(ondisk_path,
8880 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8881 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8882 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8883 eec2f5a9 2021-06-03 stsp goto done;
8885 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8886 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8887 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8888 eec2f5a9 2021-06-03 stsp close(fd);
8889 eec2f5a9 2021-06-03 stsp goto done;
8893 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8894 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8895 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8896 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8897 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8900 fda8017d 2020-07-23 stsp goto done;
8902 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8903 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8904 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8906 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8907 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8910 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8911 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8912 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8913 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8914 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8915 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8916 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8917 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8918 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8919 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8920 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8921 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8922 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8923 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8924 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8925 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8926 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8927 67a66647 2021-05-31 stsp free(blob_base_path);
8928 67a66647 2021-05-31 stsp free(parent);
8929 67a66647 2021-05-31 stsp free(base_path);
8930 2e1f37b0 2019-08-08 stsp return err;
8933 ad493afc 2019-08-03 stsp static const struct got_error *
8934 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8935 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8936 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8937 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8939 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8940 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8941 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8942 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8943 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8944 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8945 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8946 9bc94a15 2019-08-03 stsp struct stat sb;
8947 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8949 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8950 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8951 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8952 2e1f37b0 2019-08-08 stsp return NULL;
8954 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8955 ad493afc 2019-08-03 stsp if (ie == NULL)
8956 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8958 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8960 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8962 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8963 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8965 f69721c3 2019-10-21 stsp goto done;
8966 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8967 f69721c3 2019-10-21 stsp id_str) == -1) {
8968 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8969 f4ae6ddb 2022-07-01 thomas goto done;
8972 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8973 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8974 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8975 f4ae6ddb 2022-07-01 thomas goto done;
8977 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8978 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8979 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8980 f69721c3 2019-10-21 stsp goto done;
8983 ad493afc 2019-08-03 stsp switch (staged_status) {
8984 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8985 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8986 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
8989 ad493afc 2019-08-03 stsp /* fall through */
8990 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8991 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8992 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8993 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8994 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8995 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8998 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9001 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9002 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9003 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9004 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9005 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9006 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9009 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9010 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9013 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9014 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9015 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9016 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9017 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9018 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9019 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9020 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9021 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9023 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9024 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9025 36bf999c 2020-07-23 stsp char *staged_target;
9026 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9027 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9029 36bf999c 2020-07-23 stsp goto done;
9030 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9031 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9032 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9033 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9034 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9035 36bf999c 2020-07-23 stsp free(staged_target);
9037 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9038 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9039 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9040 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9041 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9042 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9043 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9047 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9050 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9051 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9052 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9053 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9056 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9057 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9058 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9059 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9060 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9063 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9065 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9066 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9070 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9071 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9072 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9073 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9076 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9080 ad493afc 2019-08-03 stsp free(ondisk_path);
9081 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9082 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9083 ad493afc 2019-08-03 stsp if (blob_base)
9084 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9085 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9086 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9087 ad493afc 2019-08-03 stsp if (blob_staged)
9088 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9089 f69721c3 2019-10-21 stsp free(id_str);
9090 f69721c3 2019-10-21 stsp free(label_orig);
9091 ad493afc 2019-08-03 stsp return err;
9094 ad493afc 2019-08-03 stsp const struct got_error *
9095 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9096 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9097 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9098 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9099 ad493afc 2019-08-03 stsp struct got_repository *repo)
9101 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9102 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9103 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9104 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9105 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9107 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9109 ad493afc 2019-08-03 stsp return err;
9111 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9113 ad493afc 2019-08-03 stsp goto done;
9115 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9116 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9117 ad493afc 2019-08-03 stsp upa.repo = repo;
9118 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9119 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9120 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9121 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9122 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9123 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9124 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9126 ad493afc 2019-08-03 stsp goto done;
9129 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9130 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9131 ad493afc 2019-08-03 stsp err = sync_err;
9133 ad493afc 2019-08-03 stsp free(fileindex_path);
9134 ad493afc 2019-08-03 stsp if (fileindex)
9135 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9136 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9137 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9138 ad493afc 2019-08-03 stsp err = unlockerr;
9139 ad493afc 2019-08-03 stsp return err;
9142 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9143 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9144 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9145 b2118c49 2020-07-28 stsp void *info_arg;
9146 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9147 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9148 b2118c49 2020-07-28 stsp void *cancel_arg;
9151 b2118c49 2020-07-28 stsp static const struct got_error *
9152 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9154 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9155 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9156 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9157 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9158 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9159 b2118c49 2020-07-28 stsp int stage;
9161 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9162 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9164 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9165 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9166 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9169 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9170 b2118c49 2020-07-28 stsp return NULL;
9172 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9173 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9174 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9175 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9176 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9177 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9178 43010591 2023-02-17 thomas &staged_blob_id, ie);
9181 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9182 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9184 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9185 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9188 b2118c49 2020-07-28 stsp const struct got_error *
9189 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9190 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9191 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9192 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9195 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9196 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9197 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9198 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9200 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9202 b2118c49 2020-07-28 stsp return err;
9204 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9206 b2118c49 2020-07-28 stsp goto done;
9208 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9209 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9210 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9211 b2118c49 2020-07-28 stsp arg.paths = paths;
9212 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9213 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9214 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9217 b2118c49 2020-07-28 stsp free(fileindex_path);
9218 b2118c49 2020-07-28 stsp if (fileindex)
9219 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9220 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9221 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9222 b2118c49 2020-07-28 stsp err = unlockerr;
9223 b2118c49 2020-07-28 stsp return err;
9226 814624e7 2022-03-22 thomas static const struct got_error *
9227 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9228 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9229 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9231 814624e7 2022-03-22 thomas const struct got_error *err;
9232 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9233 814624e7 2022-03-22 thomas struct stat sb;
9234 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9236 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9237 814624e7 2022-03-22 thomas if (err)
9238 814624e7 2022-03-22 thomas return err;
9240 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9241 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9242 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9244 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9245 814624e7 2022-03-22 thomas if (ie) {
9246 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9247 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9249 814624e7 2022-03-22 thomas if (err)
9250 814624e7 2022-03-22 thomas goto done;
9251 814624e7 2022-03-22 thomas } else {
9252 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9253 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9254 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9255 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9256 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9257 814624e7 2022-03-22 thomas ondisk_path);
9258 814624e7 2022-03-22 thomas goto done;
9260 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9265 814624e7 2022-03-22 thomas free(ondisk_path);
9266 814624e7 2022-03-22 thomas return err;
9269 814624e7 2022-03-22 thomas static const struct got_error *
9270 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
9271 814624e7 2022-03-22 thomas unsigned char staged_status)
9273 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9274 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9275 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9276 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9277 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9278 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9279 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9280 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9281 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9282 814624e7 2022-03-22 thomas return NULL;
9285 814624e7 2022-03-22 thomas static const struct got_error *
9286 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9288 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9289 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9290 814624e7 2022-03-22 thomas return NULL;
9293 814624e7 2022-03-22 thomas static const struct got_error *
9294 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9295 814624e7 2022-03-22 thomas unsigned char staged_status)
9297 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9298 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9299 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9300 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9301 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9302 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9303 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9304 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9305 814624e7 2022-03-22 thomas return NULL;
9308 814624e7 2022-03-22 thomas const struct got_error *
9309 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9310 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9312 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9315 814624e7 2022-03-22 thomas const struct got_error *
9316 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9317 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9318 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9320 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9321 814624e7 2022-03-22 thomas int file_renamed = 0;
9322 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9323 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9325 814624e7 2022-03-22 thomas *oldpath = NULL;
9326 814624e7 2022-03-22 thomas *newpath = NULL;
9328 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9329 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9330 814624e7 2022-03-22 thomas if (err)
9331 814624e7 2022-03-22 thomas goto done;
9333 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9334 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9335 814624e7 2022-03-22 thomas if (err)
9336 814624e7 2022-03-22 thomas goto done;
9338 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9339 814624e7 2022-03-22 thomas file_renamed = 1;
9341 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9342 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9343 814624e7 2022-03-22 thomas else if (file_renamed) {
9344 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9345 814624e7 2022-03-22 thomas if (err == NULL)
9346 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9347 814624e7 2022-03-22 thomas } else if (old == NULL)
9348 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9350 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9353 814624e7 2022-03-22 thomas if (err) {
9354 814624e7 2022-03-22 thomas free(*oldpath);
9355 814624e7 2022-03-22 thomas *oldpath = NULL;
9356 814624e7 2022-03-22 thomas free(*newpath);
9357 814624e7 2022-03-22 thomas *newpath = NULL;
9359 814624e7 2022-03-22 thomas return err;
9362 814624e7 2022-03-22 thomas const struct got_error *
9363 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9364 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9365 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9367 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9369 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9370 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9371 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9372 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9373 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9374 5e22b737 2022-05-31 thomas saa.repo = repo;
9376 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9377 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9380 5e22b737 2022-05-31 thomas const struct got_error *
9381 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9382 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9383 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9385 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9387 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9388 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9389 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9390 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9391 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9392 5e22b737 2022-05-31 thomas sda.repo = repo;
9393 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9394 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9395 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9396 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9398 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9399 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9402 5e22b737 2022-05-31 thomas const struct got_error *
9403 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9404 36832a8e 2022-05-31 thomas const char *fileindex_path)
9406 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9408 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9409 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9411 5e22b737 2022-05-31 thomas return err;