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;
1514 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1515 6353ad76 2019-02-08 stsp static const struct got_error *
1516 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1518 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1519 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1520 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1521 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1522 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1524 7154f6ce 2019-03-27 stsp int i = 0;
1525 9bdd68dd 2020-01-02 naddy char *line = NULL;
1526 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1527 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1529 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1530 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1531 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1532 7154f6ce 2019-03-27 stsp if (feof(f))
1534 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1538 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1539 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1541 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1546 9bdd68dd 2020-01-02 naddy free(line);
1548 7154f6ce 2019-03-27 stsp return err;
1551 e2b1e152 2019-07-13 stsp static int
1552 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1554 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1555 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1558 1ebedb77 2019-10-19 stsp static int
1559 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1561 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1562 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1563 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1564 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1565 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1566 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1569 c363b2c1 2019-08-03 stsp static unsigned char
1570 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1572 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1573 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1574 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1575 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1576 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1577 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1578 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1580 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1584 a919d5c4 2020-07-23 stsp static const struct got_error *
1585 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1586 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1587 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1589 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1590 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1591 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1592 a919d5c4 2020-07-23 stsp ssize_t elen;
1593 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1594 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1595 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1597 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1599 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1601 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1603 a919d5c4 2020-07-23 stsp return err;
1604 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1606 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1607 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1609 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1611 a919d5c4 2020-07-23 stsp if (len > 0) {
1612 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1613 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1614 a919d5c4 2020-07-23 stsp len - hdrlen);
1615 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1616 a919d5c4 2020-07-23 stsp hdrlen = 0;
1618 a919d5c4 2020-07-23 stsp } while (len != 0);
1619 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1621 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1622 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1623 a919d5c4 2020-07-23 stsp if (elen == -1)
1624 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1626 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1627 a919d5c4 2020-07-23 stsp if (elen == -1)
1628 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1631 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1632 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1634 a919d5c4 2020-07-23 stsp return NULL;
1637 7154f6ce 2019-03-27 stsp static const struct got_error *
1638 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1639 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1640 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1642 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1643 6353ad76 2019-02-08 stsp struct got_object_id id;
1644 6353ad76 2019-02-08 stsp size_t hdrlen;
1645 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1646 6353ad76 2019-02-08 stsp FILE *f = NULL;
1647 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1648 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1649 6353ad76 2019-02-08 stsp size_t flen, blen;
1650 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1652 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1653 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1656 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1657 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1658 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1660 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1661 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1662 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1663 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1664 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1666 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1667 882ef1b9 2019-12-13 stsp goto done;
1669 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1670 3d35a492 2019-12-13 stsp goto done;
1673 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1674 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1675 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1676 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1677 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1678 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1679 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1680 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1681 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1682 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1683 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1685 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1686 3d35a492 2019-12-13 stsp goto done;
1688 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1689 1338848f 2019-12-13 stsp goto done;
1693 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1694 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1695 1338848f 2019-12-13 stsp goto done;
1698 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1699 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1700 1338848f 2019-12-13 stsp goto done;
1701 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1702 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1703 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1704 1338848f 2019-12-13 stsp goto done;
1707 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1708 f179e66d 2020-07-23 stsp goto done;
1710 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1711 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1712 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1713 1338848f 2019-12-13 stsp goto done;
1716 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1717 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1718 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1720 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1722 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1723 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1724 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1725 f4ae6ddb 2022-07-01 thomas goto done;
1727 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1729 1338848f 2019-12-13 stsp goto done;
1731 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1732 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1733 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1734 a919d5c4 2020-07-23 stsp goto done;
1737 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1738 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1739 ab0d4361 2019-12-13 stsp if (fd == -1) {
1740 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1741 ab0d4361 2019-12-13 stsp goto done;
1745 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1746 6353ad76 2019-02-08 stsp if (f == NULL) {
1747 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1748 6353ad76 2019-02-08 stsp goto done;
1751 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1753 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1754 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1756 7154f6ce 2019-03-27 stsp goto done;
1757 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1758 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1759 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1760 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1761 7154f6ce 2019-03-27 stsp goto done;
1763 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1764 6353ad76 2019-02-08 stsp if (flen != 0)
1765 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1767 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1768 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1769 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1771 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1772 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1773 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1774 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1778 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1781 6353ad76 2019-02-08 stsp hdrlen = 0;
1784 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1785 7154f6ce 2019-03-27 stsp rewind(f);
1786 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1787 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1788 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1790 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1791 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1793 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1794 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1795 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1796 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1797 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1798 9d31a1d8 2018-03-11 stsp return err;
1802 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1803 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1805 e2b1e152 2019-07-13 stsp static const struct got_error *
1806 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1807 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1809 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1810 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1811 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1813 e2b1e152 2019-07-13 stsp return NULL;
1816 9d31a1d8 2018-03-11 stsp static const struct got_error *
1817 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1818 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1819 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1820 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1821 0584f854 2019-04-06 stsp void *progress_arg)
1823 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1824 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1825 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1826 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1827 b8f41171 2019-02-10 stsp struct stat sb;
1828 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1830 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1831 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1834 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1835 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1836 a76c42e6 2019-08-03 stsp goto done;
1838 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1841 abb4604f 2019-07-27 stsp goto done;
1842 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1843 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1845 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1846 e6f4ba31 2021-09-24 thomas if (errno != ENOENT) {
1847 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1848 e6f4ba31 2021-09-24 thomas ondisk_path);
1849 e6f4ba31 2021-09-24 thomas goto done;
1851 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1852 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1853 e6f4ba31 2021-09-24 thomas } else {
1854 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1855 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1857 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1861 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1863 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1864 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1865 b8f41171 2019-02-10 stsp goto done;
1867 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1869 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1870 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1872 5036ab18 2020-04-18 stsp goto done;
1875 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1876 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1877 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1879 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1880 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1881 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1882 c6e8a826 2021-04-05 stsp * updating contents of this file.
1884 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1885 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1886 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1887 c6e8a826 2021-04-05 stsp /* Same commit. */
1888 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1889 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1891 e2b1e152 2019-07-13 stsp goto done;
1892 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1894 6353ad76 2019-02-08 stsp goto done;
1896 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1897 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1898 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1899 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1900 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1901 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1903 0f58026f 2021-04-05 stsp goto done;
1904 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1906 b8f41171 2019-02-10 stsp goto done;
1910 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1911 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1912 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1913 f4ae6ddb 2022-07-01 thomas goto done;
1915 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1917 6353ad76 2019-02-08 stsp goto done;
1919 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1920 234035bc 2019-06-01 stsp int update_timestamps;
1921 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1922 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1923 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1924 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
1925 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
1926 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1927 f4ae6ddb 2022-07-01 thomas goto done;
1929 234035bc 2019-06-01 stsp struct got_object_id id2;
1930 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1931 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1934 f69721c3 2019-10-21 stsp goto done;
1936 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1937 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1938 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1939 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1940 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1941 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1942 f69721c3 2019-10-21 stsp goto done;
1944 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1945 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1946 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1947 234035bc 2019-06-01 stsp goto done;
1950 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1951 36bf999c 2020-07-23 stsp char *link_target;
1952 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1954 36bf999c 2020-07-23 stsp goto done;
1955 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1956 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1957 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1958 36bf999c 2020-07-23 stsp free(link_target);
1960 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1961 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1962 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1963 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1965 f69721c3 2019-10-21 stsp free(label_orig);
1966 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1967 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1968 f4ae6ddb 2022-07-01 thomas goto done;
1970 234035bc 2019-06-01 stsp if (blob2)
1971 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1973 909d120e 2019-09-22 stsp goto done;
1975 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1976 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1977 234035bc 2019-06-01 stsp * unmodified files again.
1979 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1980 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1981 234035bc 2019-06-01 stsp update_timestamps);
1982 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1983 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1984 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1985 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1986 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1988 1ee397ad 2019-07-12 stsp goto done;
1989 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1990 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1992 13d9040b 2019-03-27 stsp goto done;
1994 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1995 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1996 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1997 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1998 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1999 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2000 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2002 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2003 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2004 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2005 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2006 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2009 13d9040b 2019-03-27 stsp goto done;
2012 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2013 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2014 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2016 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2017 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2018 054041d0 2020-06-24 stsp &blob->id);
2021 13d9040b 2019-03-27 stsp goto done;
2023 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2024 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2025 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2029 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2030 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2031 f4ae6ddb 2022-07-01 thomas goto done;
2033 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2035 6353ad76 2019-02-08 stsp free(ondisk_path);
2036 8da9e5f4 2019-01-12 stsp return err;
2039 90285c3b 2019-01-08 stsp static const struct got_error *
2040 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2042 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2043 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2045 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2046 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2048 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2049 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2050 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2052 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2053 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2055 1c4cdd89 2021-06-20 stsp goto done;
2056 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2057 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2058 fddefe3b 2020-10-20 stsp free(ondisk_path);
2059 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2060 1c4cdd89 2021-06-20 stsp parent = NULL;
2061 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2062 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2063 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2064 fddefe3b 2020-10-20 stsp ondisk_path);
2067 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2073 90285c3b 2019-01-08 stsp free(ondisk_path);
2074 1c4cdd89 2021-06-20 stsp free(parent);
2075 90285c3b 2019-01-08 stsp return err;
2078 708d8e67 2019-03-27 stsp static const struct got_error *
2079 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2080 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2081 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2083 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2084 708d8e67 2019-03-27 stsp unsigned char status;
2085 708d8e67 2019-03-27 stsp struct stat sb;
2086 708d8e67 2019-03-27 stsp char *ondisk_path;
2088 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2089 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2091 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2093 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2095 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2097 5a58a424 2020-06-23 stsp goto done;
2099 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2100 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2101 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2102 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2103 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2104 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2105 993e2a1b 2020-07-23 stsp goto done;
2107 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2108 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2109 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2110 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2112 993e2a1b 2020-07-23 stsp goto done;
2113 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2114 993e2a1b 2020-07-23 stsp ie->path);
2115 993e2a1b 2020-07-23 stsp goto done;
2118 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2119 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2120 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2122 5a58a424 2020-06-23 stsp goto done;
2124 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2125 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2127 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2128 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2130 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2132 5a58a424 2020-06-23 stsp goto done;
2133 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2134 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2136 5a58a424 2020-06-23 stsp goto done;
2138 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2141 5a58a424 2020-06-23 stsp free(ondisk_path);
2142 fc6346c4 2019-03-27 stsp return err;
2145 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2146 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2147 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2148 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2149 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2150 8da9e5f4 2019-01-12 stsp void *progress_arg;
2151 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2152 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2155 512f0d0e 2019-01-02 stsp static const struct got_error *
2156 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2157 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2159 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2161 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2162 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2164 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2165 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2168 8da9e5f4 2019-01-12 stsp static const struct got_error *
2169 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2171 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2173 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2174 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2176 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2177 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2180 9d31a1d8 2018-03-11 stsp static const struct got_error *
2181 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2183 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2184 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2185 8da9e5f4 2019-01-12 stsp char *path;
2187 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2188 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2190 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2191 63c5ca5d 2019-08-24 stsp return NULL;
2193 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2194 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2196 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2198 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2199 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2201 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2202 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2204 8da9e5f4 2019-01-12 stsp free(path);
2205 0cd1c46a 2019-03-11 stsp return err;
2208 b2118c49 2020-07-28 stsp const struct got_error *
2209 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2211 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2213 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2214 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2215 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2216 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2219 b2118c49 2020-07-28 stsp return NULL;
2222 818c7501 2019-07-11 stsp static const struct got_error *
2223 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2225 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2226 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2228 517bab73 2019-03-11 stsp *refname = NULL;
2230 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2232 b2118c49 2020-07-28 stsp return err;
2234 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2235 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2236 0647c563 2019-03-11 stsp *refname = NULL;
2238 517bab73 2019-03-11 stsp free(uuidstr);
2239 517bab73 2019-03-11 stsp return err;
2242 818c7501 2019-07-11 stsp const struct got_error *
2243 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2244 f6cd0243 2023-01-28 thomas const char *prefix)
2246 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2249 f6cd0243 2023-01-28 thomas const struct got_error *
2250 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2252 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2255 818c7501 2019-07-11 stsp static const struct got_error *
2256 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2258 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2259 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2262 818c7501 2019-07-11 stsp static const struct got_error *
2263 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2265 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2268 818c7501 2019-07-11 stsp static const struct got_error *
2269 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2271 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2272 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2275 818c7501 2019-07-11 stsp static const struct got_error *
2276 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2278 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2279 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2282 0ebf8283 2019-07-24 stsp static const struct got_error *
2283 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2285 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2286 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2289 0ebf8283 2019-07-24 stsp static const struct got_error *
2290 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2292 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2293 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2296 0ebf8283 2019-07-24 stsp static const struct got_error *
2297 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2299 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2300 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2303 0ebf8283 2019-07-24 stsp static const struct got_error *
2304 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2306 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2307 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2310 0ebf8283 2019-07-24 stsp const struct got_error *
2311 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2312 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2314 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2315 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2316 0ebf8283 2019-07-24 stsp *path = NULL;
2317 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2319 0ebf8283 2019-07-24 stsp return NULL;
2322 10604dce 2021-09-24 thomas static const struct got_error *
2323 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2325 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2326 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2329 10604dce 2021-09-24 thomas static const struct got_error *
2330 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2332 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2333 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2337 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2338 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2340 517bab73 2019-03-11 stsp static const struct got_error *
2341 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2343 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2344 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2345 517bab73 2019-03-11 stsp char *refname;
2347 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2349 517bab73 2019-03-11 stsp return err;
2351 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2353 0cd1c46a 2019-03-11 stsp goto done;
2355 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2357 0cd1c46a 2019-03-11 stsp free(refname);
2359 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2360 3e3a69f1 2019-07-25 stsp return err;
2363 3e3a69f1 2019-07-25 stsp static const struct got_error *
2364 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2366 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2368 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2369 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2370 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2371 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2373 9d31a1d8 2018-03-11 stsp return err;
2377 ebf99748 2019-05-09 stsp static const struct got_error *
2378 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2379 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2381 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2382 ebf99748 2019-05-09 stsp FILE *index = NULL;
2384 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2385 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2386 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2387 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2389 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2391 ebf99748 2019-05-09 stsp goto done;
2393 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2394 ebf99748 2019-05-09 stsp if (index == NULL) {
2395 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2396 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2398 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2399 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2400 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2403 ebf99748 2019-05-09 stsp if (err) {
2404 ebf99748 2019-05-09 stsp free(*fileindex_path);
2405 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2406 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2407 ebf99748 2019-05-09 stsp *fileindex = NULL;
2409 ebf99748 2019-05-09 stsp return err;
2412 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2413 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2414 c932eeeb 2019-05-22 stsp const char *path;
2415 c932eeeb 2019-05-22 stsp size_t path_len;
2416 c932eeeb 2019-05-22 stsp const char *entry_name;
2417 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2418 a484d721 2019-06-10 stsp void *progress_arg;
2421 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2422 c932eeeb 2019-05-22 stsp static const struct got_error *
2423 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2425 1ee397ad 2019-07-12 stsp const struct got_error *err;
2426 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2428 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2429 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2430 c932eeeb 2019-05-22 stsp return NULL;
2431 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2432 102ff934 2019-06-11 stsp return NULL;
2434 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2435 a769b60b 2021-06-27 stsp return NULL;
2437 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2438 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2439 c932eeeb 2019-05-22 stsp return NULL;
2441 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2442 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2443 edd02c5e 2019-07-11 stsp ie->path);
2445 1ee397ad 2019-07-12 stsp return err;
2447 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2448 c932eeeb 2019-05-22 stsp return NULL;
2451 a615e0e7 2020-12-16 stsp static const struct got_error *
2452 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2453 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2454 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2456 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2458 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2459 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2460 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2461 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2462 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2463 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2465 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2466 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2469 9c6338c4 2019-06-02 stsp static const struct got_error *
2470 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2472 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2473 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2474 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2475 867630bb 2020-01-17 stsp struct timespec timeout;
2477 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2478 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2480 9c6338c4 2019-06-02 stsp goto done;
2482 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2484 9c6338c4 2019-06-02 stsp goto done;
2486 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2487 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2488 9c6338c4 2019-06-02 stsp fileindex_path);
2489 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2493 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2494 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2495 867630bb 2020-01-17 stsp * was recorded in the file index.
2497 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2498 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2499 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2501 9c6338c4 2019-06-02 stsp if (new_index)
2502 9c6338c4 2019-06-02 stsp fclose(new_index);
2503 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2504 9c6338c4 2019-06-02 stsp return err;
2507 6ced4176 2019-07-12 stsp static const struct got_error *
2508 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2509 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2510 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2511 945f9229 2022-04-16 thomas struct got_repository *repo)
2513 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2514 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2515 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2516 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2518 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2519 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2520 6ced4176 2019-07-12 stsp *tree_id = NULL;
2522 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2523 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2524 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2525 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2526 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2527 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2528 6ced4176 2019-07-12 stsp goto done;
2530 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2531 945f9229 2022-04-16 thomas worktree->path_prefix);
2533 6ced4176 2019-07-12 stsp goto done;
2534 6ced4176 2019-07-12 stsp return NULL;
2537 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2539 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2540 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2541 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2542 6ced4176 2019-07-12 stsp goto done;
2545 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2547 6ced4176 2019-07-12 stsp goto done;
2549 6ced4176 2019-07-12 stsp free(in_repo_path);
2550 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2552 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2554 6ced4176 2019-07-12 stsp goto done;
2556 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2557 6ced4176 2019-07-12 stsp /* Check out a single file. */
2558 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2559 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2560 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2561 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2562 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2563 6ced4176 2019-07-12 stsp goto done;
2565 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2566 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2567 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2568 6ced4176 2019-07-12 stsp goto done;
2571 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2572 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2574 6ced4176 2019-07-12 stsp return err;
2575 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2576 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2577 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2578 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2579 6ced4176 2019-07-12 stsp goto done;
2582 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2583 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2585 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2586 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2587 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2588 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2589 6ced4176 2019-07-12 stsp goto done;
2591 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2592 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2593 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2594 6ced4176 2019-07-12 stsp goto done;
2599 6ced4176 2019-07-12 stsp free(in_repo_path);
2600 6ced4176 2019-07-12 stsp if (err) {
2601 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2602 6ced4176 2019-07-12 stsp free(*tree_relpath);
2603 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2604 6ced4176 2019-07-12 stsp free(*tree_id);
2605 6ced4176 2019-07-12 stsp *tree_id = NULL;
2607 07ed472d 2019-07-12 stsp return err;
2610 07ed472d 2019-07-12 stsp static const struct got_error *
2611 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2612 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2613 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2614 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2616 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2617 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2618 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2619 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2620 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2622 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2623 7f47418f 2019-12-20 stsp if (err) {
2624 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2625 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2626 7f47418f 2019-12-20 stsp goto done;
2627 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2628 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2630 7f47418f 2019-12-20 stsp return err;
2633 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2634 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2636 07ed472d 2019-07-12 stsp goto done;
2638 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2640 07ed472d 2019-07-12 stsp goto done;
2642 07ed472d 2019-07-12 stsp if (entry_name &&
2643 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2644 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2645 07ed472d 2019-07-12 stsp goto done;
2648 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2649 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2650 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2651 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2652 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2653 07ed472d 2019-07-12 stsp arg.repo = repo;
2654 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2655 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2656 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2657 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2658 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2659 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2662 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2663 07ed472d 2019-07-12 stsp if (commit)
2664 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2665 6ced4176 2019-07-12 stsp return err;
2668 9d31a1d8 2018-03-11 stsp const struct got_error *
2669 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2670 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2671 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2672 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2674 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2675 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2676 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2677 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2678 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2679 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2680 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2681 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2682 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2683 f2ea84fa 2019-07-27 stsp int entry_type;
2684 f2ea84fa 2019-07-27 stsp char *relpath;
2685 f2ea84fa 2019-07-27 stsp char *entry_name;
2686 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2687 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2689 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2691 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2693 9d31a1d8 2018-03-11 stsp return err;
2695 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2696 945f9229 2022-04-16 thomas worktree->base_commit_id);
2697 945f9229 2022-04-16 thomas if (err)
2698 945f9229 2022-04-16 thomas goto done;
2700 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2701 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2702 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2703 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2704 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2705 f2ea84fa 2019-07-27 stsp goto done;
2708 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2709 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2710 945f9229 2022-04-16 thomas worktree, repo);
2711 f2ea84fa 2019-07-27 stsp if (err) {
2712 f2ea84fa 2019-07-27 stsp free(tpd);
2713 6ced4176 2019-07-12 stsp goto done;
2716 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2717 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2718 f2ea84fa 2019-07-27 stsp if (err) {
2719 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2720 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2721 f2ea84fa 2019-07-27 stsp free(tpd);
2722 f2ea84fa 2019-07-27 stsp goto done;
2725 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2727 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2731 51514078 2018-12-25 stsp * Read the file index.
2732 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2733 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2735 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2737 c4cdcb68 2019-04-03 stsp goto done;
2739 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2740 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2741 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2743 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2744 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2745 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2749 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2750 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2751 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2752 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2753 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2754 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2755 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2756 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2760 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2762 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2763 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2764 af12c6b9 2019-06-04 stsp err = sync_err;
2766 9c6338c4 2019-06-02 stsp free(fileindex_path);
2768 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2769 9d31a1d8 2018-03-11 stsp if (commit)
2770 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2771 5ade8233 2019-07-12 stsp if (fileindex)
2772 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2773 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2774 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2775 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2776 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2777 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2778 f2ea84fa 2019-07-27 stsp free(tpd);
2780 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2781 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2782 9d31a1d8 2018-03-11 stsp err = unlockerr;
2783 f8d1f275 2019-02-04 stsp return err;
2786 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2787 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2788 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2789 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2790 234035bc 2019-06-01 stsp void *progress_arg;
2791 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2792 234035bc 2019-06-01 stsp void *cancel_arg;
2793 f69721c3 2019-10-21 stsp const char *label_orig;
2794 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2795 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2798 234035bc 2019-06-01 stsp static const struct got_error *
2799 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2800 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2801 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2802 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2803 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2805 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2806 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2807 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2808 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2809 234035bc 2019-06-01 stsp struct stat sb;
2810 234035bc 2019-06-01 stsp unsigned char status;
2811 234035bc 2019-06-01 stsp int local_changes_subsumed;
2812 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2813 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2815 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2816 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2817 d6c87207 2019-08-02 stsp strlen(path2));
2818 1ee397ad 2019-07-12 stsp if (ie == NULL)
2819 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2820 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2822 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2823 234035bc 2019-06-01 stsp path2) == -1)
2824 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2826 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2829 234035bc 2019-06-01 stsp goto done;
2831 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2832 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2833 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2834 234035bc 2019-06-01 stsp goto done;
2836 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2837 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2838 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2839 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2840 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2841 234035bc 2019-06-01 stsp goto done;
2844 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2845 36bf999c 2020-07-23 stsp char *link_target2;
2846 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2848 36bf999c 2020-07-23 stsp goto done;
2849 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2850 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2851 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2852 36bf999c 2020-07-23 stsp free(link_target2);
2856 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2857 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2858 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2859 1af628f4 2021-06-03 stsp goto done;
2861 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2862 1af628f4 2021-06-03 stsp f_orig, blob1);
2864 1af628f4 2021-06-03 stsp goto done;
2866 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2867 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2868 1af628f4 2021-06-03 stsp goto done;
2869 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2870 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2872 1af628f4 2021-06-03 stsp goto done;
2874 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
2875 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2876 1af628f4 2021-06-03 stsp if (fd == -1) {
2877 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2878 1af628f4 2021-06-03 stsp ondisk_path);
2879 1af628f4 2021-06-03 stsp goto done;
2881 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2882 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2883 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2884 1af628f4 2021-06-03 stsp ondisk_path);
2885 1af628f4 2021-06-03 stsp close(fd);
2886 1af628f4 2021-06-03 stsp goto done;
2888 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2890 1af628f4 2021-06-03 stsp goto done;
2891 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2892 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2893 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2894 1af628f4 2021-06-03 stsp goto done;
2896 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2897 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2898 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
2899 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2900 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2902 234035bc 2019-06-01 stsp } else if (blob1) {
2903 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2904 d6c87207 2019-08-02 stsp strlen(path1));
2905 1ee397ad 2019-07-12 stsp if (ie == NULL)
2906 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2907 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2909 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2910 2b92fad7 2019-06-02 stsp path1) == -1)
2911 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2913 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2916 2b92fad7 2019-06-02 stsp goto done;
2918 2b92fad7 2019-06-02 stsp switch (status) {
2919 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2920 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2921 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2923 1ee397ad 2019-07-12 stsp goto done;
2924 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2926 2b92fad7 2019-06-02 stsp goto done;
2928 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2930 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2931 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2932 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2933 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2935 1ee397ad 2019-07-12 stsp goto done;
2937 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2939 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2940 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2941 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2942 e8f02263 2022-01-23 thomas off_t blob1_size;
2944 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2945 0a22ca1a 2020-09-23 stsp * exists in the repository.
2947 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
2948 e8f02263 2022-01-23 thomas &blob1_size, path1);
2950 0a22ca1a 2020-09-23 stsp goto done;
2951 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2952 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2953 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2955 0a22ca1a 2020-09-23 stsp goto done;
2956 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2959 0a22ca1a 2020-09-23 stsp goto done;
2961 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2964 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2965 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2967 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2968 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2971 0a22ca1a 2020-09-23 stsp goto done;
2974 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2975 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2976 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2977 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2979 1ee397ad 2019-07-12 stsp goto done;
2981 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2982 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2984 1ee397ad 2019-07-12 stsp goto done;
2989 234035bc 2019-06-01 stsp } else if (blob2) {
2990 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2991 234035bc 2019-06-01 stsp path2) == -1)
2992 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2993 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2994 d6c87207 2019-08-02 stsp strlen(path2));
2996 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2997 7f91a133 2019-12-13 stsp -1, NULL, repo);
2999 234035bc 2019-06-01 stsp goto done;
3000 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3001 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3002 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3003 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3004 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3005 1ee397ad 2019-07-12 stsp status, path2);
3006 234035bc 2019-06-01 stsp goto done;
3008 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3009 36bf999c 2020-07-23 stsp char *link_target2;
3010 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3013 36bf999c 2020-07-23 stsp goto done;
3014 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3015 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3016 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3017 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3018 36bf999c 2020-07-23 stsp free(link_target2);
3019 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3020 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3021 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3022 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3023 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3024 526a746f 2020-07-23 stsp a->progress_arg);
3026 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3027 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3030 dfe9fba0 2020-07-23 stsp goto done;
3031 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3032 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3033 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3034 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3036 234035bc 2019-06-01 stsp goto done;
3039 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3040 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3041 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3042 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3043 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3044 ace90326 2021-09-27 thomas 0, 1, a->allow_bad_symlinks, repo,
3045 ace90326 2021-09-27 thomas a->progress_cb, a->progress_arg);
3047 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3048 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3049 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3052 234035bc 2019-06-01 stsp goto done;
3053 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3055 234035bc 2019-06-01 stsp goto done;
3056 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3057 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3058 3969253a 2020-03-07 stsp if (err) {
3059 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3060 3969253a 2020-03-07 stsp goto done;
3062 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3063 2b92fad7 2019-06-02 stsp if (err) {
3064 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3065 2b92fad7 2019-06-02 stsp goto done;
3067 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3068 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3069 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3074 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3075 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3076 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3077 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3078 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3079 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3080 1af628f4 2021-06-03 stsp free(id_str);
3081 54d5be07 2021-06-03 stsp free(label_deriv2);
3082 234035bc 2019-06-01 stsp free(ondisk_path);
3083 234035bc 2019-06-01 stsp return err;
3086 69de9dd4 2021-09-03 stsp static const struct got_error *
3087 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3089 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3091 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3092 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3093 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3094 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3095 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3097 69de9dd4 2021-09-03 stsp return NULL;
3100 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3101 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3102 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3103 234035bc 2019-06-01 stsp struct got_repository *repo;
3106 234035bc 2019-06-01 stsp static const struct got_error *
3107 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3108 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3109 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3110 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3111 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3113 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3114 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3115 234035bc 2019-06-01 stsp unsigned char status;
3116 234035bc 2019-06-01 stsp struct stat sb;
3117 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3118 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3119 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3120 234035bc 2019-06-01 stsp char *ondisk_path;
3122 69de9dd4 2021-09-03 stsp if (id == NULL)
3123 69de9dd4 2021-09-03 stsp return NULL;
3125 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3126 69de9dd4 2021-09-03 stsp if (ie == NULL)
3127 69de9dd4 2021-09-03 stsp return NULL;
3129 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3131 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3133 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3134 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3135 5546d466 2021-09-02 stsp free(ondisk_path);
3137 234035bc 2019-06-01 stsp return err;
3138 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3139 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3141 234035bc 2019-06-01 stsp return NULL;
3144 818c7501 2019-07-11 stsp static const struct got_error *
3145 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3146 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3147 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3148 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3149 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3151 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3152 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3153 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3154 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3155 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3156 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3157 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3158 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3159 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3161 03415a1a 2019-06-02 stsp if (commit_id1) {
3162 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3163 945f9229 2022-04-16 thomas if (err)
3164 945f9229 2022-04-16 thomas goto done;
3165 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3166 03415a1a 2019-06-02 stsp worktree->path_prefix);
3167 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3168 03415a1a 2019-06-02 stsp goto done;
3170 69d57f3d 2020-07-31 stsp if (tree_id1) {
3171 69d57f3d 2020-07-31 stsp char *id_str;
3173 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3175 03415a1a 2019-06-02 stsp goto done;
3177 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3179 f69721c3 2019-10-21 stsp goto done;
3181 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3182 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3183 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3184 f69721c3 2019-10-21 stsp free(id_str);
3185 f69721c3 2019-10-21 stsp goto done;
3187 f69721c3 2019-10-21 stsp free(id_str);
3189 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3190 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3191 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3192 a0f32f33 2022-06-13 thomas goto done;
3196 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3197 945f9229 2022-04-16 thomas if (err)
3198 945f9229 2022-04-16 thomas goto done;
3200 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3201 234035bc 2019-06-01 stsp worktree->path_prefix);
3203 234035bc 2019-06-01 stsp goto done;
3205 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3207 234035bc 2019-06-01 stsp goto done;
3209 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3210 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3211 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3212 19a6a6b5 2022-07-01 thomas goto done;
3215 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3216 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3217 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3218 a0f32f33 2022-06-13 thomas goto done;
3221 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3222 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3223 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3224 19a6a6b5 2022-07-01 thomas goto done;
3227 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3228 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3229 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3230 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3231 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3233 69de9dd4 2021-09-03 stsp goto done;
3235 234035bc 2019-06-01 stsp arg.worktree = worktree;
3236 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3237 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3238 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3239 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3240 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3241 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3242 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3243 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3244 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3245 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3246 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3247 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3248 af12c6b9 2019-06-04 stsp err = sync_err;
3250 945f9229 2022-04-16 thomas if (commit1)
3251 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3252 945f9229 2022-04-16 thomas if (commit2)
3253 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3254 234035bc 2019-06-01 stsp if (tree1)
3255 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3256 234035bc 2019-06-01 stsp if (tree2)
3257 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3258 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3259 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3260 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3261 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3262 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3263 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3264 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3265 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3266 f69721c3 2019-10-21 stsp free(label_orig);
3267 818c7501 2019-07-11 stsp return err;
3270 818c7501 2019-07-11 stsp const struct got_error *
3271 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3272 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3273 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3274 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3276 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3277 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3278 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3280 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3282 818c7501 2019-07-11 stsp return err;
3284 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3286 818c7501 2019-07-11 stsp goto done;
3288 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3289 69de9dd4 2021-09-03 stsp worktree);
3291 818c7501 2019-07-11 stsp goto done;
3293 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3294 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3295 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3297 818c7501 2019-07-11 stsp if (fileindex)
3298 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3299 818c7501 2019-07-11 stsp free(fileindex_path);
3300 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3301 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3302 234035bc 2019-06-01 stsp err = unlockerr;
3303 234035bc 2019-06-01 stsp return err;
3306 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3307 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3308 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3309 927df6b7 2019-02-10 stsp const char *status_path;
3310 927df6b7 2019-02-10 stsp size_t status_path_len;
3311 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3312 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3313 f8d1f275 2019-02-04 stsp void *status_arg;
3314 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3315 f8d1f275 2019-02-04 stsp void *cancel_arg;
3316 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3317 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3318 f2a9dc41 2019-12-13 tracey int report_unchanged;
3319 3143d852 2020-06-25 stsp int no_ignores;
3322 f8d1f275 2019-02-04 stsp static const struct got_error *
3323 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3324 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3325 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3326 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3328 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3329 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3330 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3331 927df6b7 2019-02-10 stsp struct stat sb;
3332 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3333 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3334 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3336 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3338 98eaaa12 2019-08-03 stsp return err;
3340 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3341 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3342 98eaaa12 2019-08-03 stsp return NULL;
3344 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3345 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3346 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3348 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3349 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3350 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3352 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3353 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3354 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3355 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3356 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3359 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3360 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3363 927df6b7 2019-02-10 stsp static const struct got_error *
3364 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3365 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3367 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3368 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3369 f8d1f275 2019-02-04 stsp char *abspath;
3371 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3372 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3374 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3375 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3376 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3377 927df6b7 2019-02-10 stsp return NULL;
3379 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3380 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3381 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3382 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3384 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3385 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3386 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3389 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3390 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3391 f8d1f275 2019-02-04 stsp free(abspath);
3392 9d31a1d8 2018-03-11 stsp return err;
3395 f8d1f275 2019-02-04 stsp static const struct got_error *
3396 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3398 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3399 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3400 2ec1f75b 2019-03-26 stsp unsigned char status;
3402 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3403 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3405 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3406 927df6b7 2019-02-10 stsp return NULL;
3408 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3409 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3410 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3411 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3413 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3414 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3415 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3418 ef20f542 2022-06-26 thomas static void
3419 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3421 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3423 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3424 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3426 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3428 21c2d8be 2023-01-10 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3431 6841da00 2019-08-08 stsp static const struct got_error *
3432 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3434 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3435 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3436 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3437 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3438 6841da00 2019-08-08 stsp size_t linesize = 0;
3439 6841da00 2019-08-08 stsp ssize_t linelen;
3441 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3442 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3443 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3444 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3446 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3447 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3448 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3450 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3451 bd8de430 2019-10-04 stsp if (line[0] == '#')
3454 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3455 bd8de430 2019-10-04 stsp if (line[0] == '!')
3458 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3459 6841da00 2019-08-08 stsp line) == -1) {
3460 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3461 6841da00 2019-08-08 stsp goto done;
3463 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3465 6841da00 2019-08-08 stsp goto done;
3467 6841da00 2019-08-08 stsp if (ferror(f)) {
3468 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3469 6841da00 2019-08-08 stsp goto done;
3472 6841da00 2019-08-08 stsp dirpath = strdup(path);
3473 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3474 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3475 6841da00 2019-08-08 stsp goto done;
3477 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3479 6841da00 2019-08-08 stsp free(line);
3480 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3481 6841da00 2019-08-08 stsp free(dirpath);
3482 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3484 6841da00 2019-08-08 stsp return err;
3487 ef20f542 2022-06-26 thomas static int
3488 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3490 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3492 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3493 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3494 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3495 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3497 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3498 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3500 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3502 bd8de430 2019-10-04 stsp pattern += 3;
3504 bd8de430 2019-10-04 stsp while (*p) {
3505 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3506 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3507 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3508 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3510 bd8de430 2019-10-04 stsp while (*p == '/')
3520 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3521 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3522 6841da00 2019-08-08 stsp * ignores backwards.
3524 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3525 6841da00 2019-08-08 stsp while (pe) {
3526 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3527 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3528 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3529 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3530 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3531 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3532 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3533 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3534 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3539 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3545 6841da00 2019-08-08 stsp static const struct got_error *
3546 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3547 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3549 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3550 6841da00 2019-08-08 stsp char *ignorespath;
3551 886cec17 2019-12-15 stsp int fd = -1;
3552 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3554 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3555 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3556 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3558 886cec17 2019-12-15 stsp if (dirfd != -1) {
3559 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3560 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3561 886cec17 2019-12-15 stsp if (fd == -1) {
3562 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3563 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3564 886cec17 2019-12-15 stsp ignorespath);
3566 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3567 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3568 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3569 886cec17 2019-12-15 stsp ignorespath);
3572 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3576 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3577 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3578 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3579 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3580 886cec17 2019-12-15 stsp ignorespath);
3582 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3585 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3586 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3587 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3588 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3589 6841da00 2019-08-08 stsp free(ignorespath);
3590 6841da00 2019-08-08 stsp return err;
3593 f8d1f275 2019-02-04 stsp static const struct got_error *
3594 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3595 6092c299 2021-10-04 thomas int dirfd)
3597 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3598 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3599 f8d1f275 2019-02-04 stsp char *path = NULL;
3601 6092c299 2021-10-04 thomas if (ignore != NULL)
3602 6092c299 2021-10-04 thomas *ignore = 0;
3604 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3605 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3607 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3608 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3609 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3611 f8d1f275 2019-02-04 stsp path = de->d_name;
3614 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3615 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3616 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3617 6092c299 2021-10-04 thomas *ignore = 1;
3618 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3619 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3620 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3621 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3622 f8d1f275 2019-02-04 stsp if (parent_path[0])
3623 f8d1f275 2019-02-04 stsp free(path);
3624 b72f483a 2019-02-05 stsp return err;
3627 347d1d3e 2019-07-12 stsp static const struct got_error *
3628 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3630 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3631 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3633 3143d852 2020-06-25 stsp if (a->no_ignores)
3634 3143d852 2020-06-25 stsp return NULL;
3636 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3637 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3639 3143d852 2020-06-25 stsp return err;
3641 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3642 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3644 3143d852 2020-06-25 stsp return err;
3647 3143d852 2020-06-25 stsp static const struct got_error *
3648 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3649 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3650 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3651 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3653 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3654 abb4604f 2019-07-27 stsp struct stat sb;
3656 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3658 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3659 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3661 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3662 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3663 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3664 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3665 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3668 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3669 fe1d8685 2022-02-12 thomas return NULL;
3671 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3672 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3673 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3675 abb4604f 2019-07-27 stsp return NULL;
3678 3143d852 2020-06-25 stsp static const struct got_error *
3679 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3680 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3682 3143d852 2020-06-25 stsp const struct got_error *err;
3683 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3685 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3686 3143d852 2020-06-25 stsp ".cvsignore");
3688 3143d852 2020-06-25 stsp return err;
3690 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3691 3143d852 2020-06-25 stsp ".gitignore");
3693 3143d852 2020-06-25 stsp return err;
3695 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3696 3143d852 2020-06-25 stsp if (err) {
3697 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3698 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3699 3143d852 2020-06-25 stsp return err;
3701 3143d852 2020-06-25 stsp for (;;) {
3702 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3703 3143d852 2020-06-25 stsp ".cvsignore");
3706 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3707 3143d852 2020-06-25 stsp ".gitignore");
3710 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3711 3143d852 2020-06-25 stsp if (err) {
3712 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3713 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3716 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3718 b737c85a 2020-06-26 stsp free(parent_path);
3719 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3720 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3723 b737c85a 2020-06-26 stsp free(parent_path);
3724 b737c85a 2020-06-26 stsp free(next_parent_path);
3725 3143d852 2020-06-25 stsp return err;
3728 abb4604f 2019-07-27 stsp static const struct got_error *
3729 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3730 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3731 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3732 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3733 f2a9dc41 2019-12-13 tracey int report_unchanged)
3735 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3736 6fc93f37 2019-12-13 stsp int fd = -1;
3737 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3738 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3739 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3740 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3741 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3743 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3745 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3746 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3747 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3749 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3750 a78810f8 2022-03-13 thomas if (ie) {
3751 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3752 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3753 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3754 a78810f8 2022-03-13 thomas goto done;
3757 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3758 6fc93f37 2019-12-13 stsp if (fd == -1) {
3759 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3760 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3761 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3763 ff56836b 2021-07-08 stsp if (!no_ignores) {
3764 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3765 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3767 ff56836b 2021-07-08 stsp goto done;
3769 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3770 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3771 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3774 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3775 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3776 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3777 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3778 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3779 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3780 abb4604f 2019-07-27 stsp arg.status_path = path;
3781 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3782 abb4604f 2019-07-27 stsp arg.repo = repo;
3783 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3784 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3785 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3786 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3787 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3788 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3789 022fae89 2019-12-06 tracey if (!no_ignores) {
3790 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3791 3143d852 2020-06-25 stsp worktree->root_path, path);
3793 3143d852 2020-06-25 stsp goto done;
3795 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3796 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3797 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3800 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3801 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3802 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3803 927df6b7 2019-02-10 stsp free(ondisk_path);
3804 347d1d3e 2019-07-12 stsp return err;
3807 347d1d3e 2019-07-12 stsp const struct got_error *
3808 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3809 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3810 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3811 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3813 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3814 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3815 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3816 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3818 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3820 347d1d3e 2019-07-12 stsp return err;
3822 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3823 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3824 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3825 f6343036 2021-06-22 stsp no_ignores, 0);
3829 f8d1f275 2019-02-04 stsp free(fileindex_path);
3830 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3831 f8d1f275 2019-02-04 stsp return err;
3834 6c7ab921 2019-03-18 stsp const struct got_error *
3835 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3836 6c7ab921 2019-03-18 stsp const char *arg)
3838 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3839 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3840 6c7ab921 2019-03-18 stsp size_t len;
3841 00bb5ea0 2020-07-23 stsp struct stat sb;
3842 01740607 2020-11-04 stsp char *abspath = NULL;
3843 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3845 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3847 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3848 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3849 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3851 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3852 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3853 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3854 00bb5ea0 2020-07-23 stsp goto done;
3856 727173c3 2020-11-06 stsp sb.st_mode = 0;
3858 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3860 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3861 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3862 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3863 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3865 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3866 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3867 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3868 00bb5ea0 2020-07-23 stsp goto done;
3872 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3873 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3875 d0710d08 2019-07-22 stsp goto done;
3876 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3877 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3878 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3879 00bb5ea0 2020-07-23 stsp goto done;
3882 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3883 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3884 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3885 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3886 00bb5ea0 2020-07-23 stsp goto done;
3888 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3889 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3890 01740607 2020-11-04 stsp goto done;
3892 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3893 01740607 2020-11-04 stsp sizeof(canonpath));
3895 00bb5ea0 2020-07-23 stsp goto done;
3896 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3897 01740607 2020-11-04 stsp if (resolved == NULL) {
3898 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3899 01740607 2020-11-04 stsp goto done;
3904 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3905 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3906 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3907 6c7ab921 2019-03-18 stsp goto done;
3910 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3911 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3912 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3914 f6d88e1a 2019-05-29 stsp goto done;
3916 f6d88e1a 2019-05-29 stsp path = strdup("");
3917 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3918 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3919 f6d88e1a 2019-05-29 stsp goto done;
3923 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3924 6c7ab921 2019-03-18 stsp len = strlen(path);
3925 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3926 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3930 01740607 2020-11-04 stsp free(abspath);
3931 6c7ab921 2019-03-18 stsp free(resolved);
3932 d0710d08 2019-07-22 stsp free(cwd);
3933 6c7ab921 2019-03-18 stsp if (err == NULL)
3934 6c7ab921 2019-03-18 stsp *wt_path = path;
3936 6c7ab921 2019-03-18 stsp free(path);
3937 d00136be 2019-03-26 stsp return err;
3940 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3941 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3942 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3943 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3944 4e68cba3 2019-11-23 stsp void *progress_arg;
3945 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3948 4e68cba3 2019-11-23 stsp static const struct got_error *
3949 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3950 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3951 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3952 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3954 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3955 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3956 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3957 6d022e97 2019-08-04 stsp struct stat sb;
3958 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3960 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3961 dbb83fbd 2019-12-12 stsp relpath) == -1)
3962 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3964 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3966 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3967 12463d8b 2019-12-13 stsp de_name, a->repo);
3969 dbb83fbd 2019-12-12 stsp goto done;
3970 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3971 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3972 dbb83fbd 2019-12-12 stsp goto done;
3973 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3975 dbb83fbd 2019-12-12 stsp goto done;
3978 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3979 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
3980 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
3982 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3983 dbb83fbd 2019-12-12 stsp goto done;
3986 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3988 4e68cba3 2019-11-23 stsp goto done;
3989 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3990 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3991 3969253a 2020-03-07 stsp if (err) {
3992 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3993 3969253a 2020-03-07 stsp goto done;
3995 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3996 07f5b47a 2019-06-02 stsp if (err) {
3997 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3998 4e68cba3 2019-11-23 stsp goto done;
4001 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4003 dbb83fbd 2019-12-12 stsp return err;
4004 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4005 dbb83fbd 2019-12-12 stsp return NULL;
4006 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4009 d00136be 2019-03-26 stsp const struct got_error *
4010 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4011 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4012 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4013 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4015 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4016 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4017 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4018 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4019 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4021 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4023 d00136be 2019-03-26 stsp return err;
4025 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4027 d00136be 2019-03-26 stsp goto done;
4029 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4030 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4031 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4032 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4033 4e68cba3 2019-11-23 stsp saa.repo = repo;
4035 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4036 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4037 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4041 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4042 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4043 af12c6b9 2019-06-04 stsp err = sync_err;
4045 fb399478 2019-07-12 stsp free(fileindex_path);
4046 d00136be 2019-03-26 stsp if (fileindex)
4047 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4048 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4049 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4050 d00136be 2019-03-26 stsp err = unlockerr;
4051 6c7ab921 2019-03-18 stsp return err;
4054 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4055 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4056 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4057 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4058 f2a9dc41 2019-12-13 tracey void *progress_arg;
4059 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4060 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4061 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4062 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4063 766841c2 2020-08-13 stsp const char *status_codes;
4066 f2a9dc41 2019-12-13 tracey static const struct got_error *
4067 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4068 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4069 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4070 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4072 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4073 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4074 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4075 17ed4618 2019-06-02 stsp struct stat sb;
4076 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4078 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4079 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4080 d64cc78a 2022-01-25 thomas return NULL;
4081 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4084 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4085 17ed4618 2019-06-02 stsp if (ie == NULL)
4086 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4088 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4089 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4090 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4091 9acbc4fa 2019-08-03 stsp return NULL;
4092 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4095 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4096 f2a9dc41 2019-12-13 tracey relpath) == -1)
4097 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4099 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4102 f2a9dc41 2019-12-13 tracey goto done;
4104 766841c2 2020-08-13 stsp if (a->status_codes) {
4105 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4107 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4108 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4111 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4112 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4113 766841c2 2020-08-13 stsp free(ondisk_path);
4114 766841c2 2020-08-13 stsp return NULL;
4116 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4117 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4118 766841c2 2020-08-13 stsp static char msg[64];
4119 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4120 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4121 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4122 766841c2 2020-08-13 stsp goto done;
4126 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4127 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4128 f2a9dc41 2019-12-13 tracey goto done;
4129 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4130 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4131 f2a9dc41 2019-12-13 tracey goto done;
4133 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4134 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4135 d64cc78a 2022-01-25 thomas goto done;
4137 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4138 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4139 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4140 f2a9dc41 2019-12-13 tracey goto done;
4144 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4145 20a2ad1c 2020-10-20 stsp size_t root_len;
4147 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4148 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4149 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4150 12463d8b 2019-12-13 stsp ondisk_path);
4151 12463d8b 2019-12-13 stsp goto done;
4153 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4154 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4155 12463d8b 2019-12-13 stsp goto done;
4158 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4160 20a2ad1c 2020-10-20 stsp char *parent;
4161 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4163 2513f20a 2020-10-20 stsp goto done;
4164 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4165 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4166 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4167 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4168 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4169 20a2ad1c 2020-10-20 stsp ondisk_path);
4172 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4173 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4176 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4178 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4179 f2a9dc41 2019-12-13 tracey if (err)
4180 f2a9dc41 2019-12-13 tracey return err;
4181 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4182 f2a9dc41 2019-12-13 tracey return NULL;
4183 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4184 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4187 2ec1f75b 2019-03-26 stsp const struct got_error *
4188 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4189 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4190 766841c2 2020-08-13 stsp const char *status_codes,
4191 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4192 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4194 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4195 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4196 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4197 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4198 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4200 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4202 2ec1f75b 2019-03-26 stsp return err;
4204 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4206 2ec1f75b 2019-03-26 stsp goto done;
4208 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4209 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4210 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4211 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4212 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4213 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4214 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4215 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4216 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4218 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4219 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4220 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4224 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4225 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4226 af12c6b9 2019-06-04 stsp err = sync_err;
4228 fb399478 2019-07-12 stsp free(fileindex_path);
4229 2ec1f75b 2019-03-26 stsp if (fileindex)
4230 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4231 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4232 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4233 2ec1f75b 2019-03-26 stsp err = unlockerr;
4234 33aa809d 2019-08-08 stsp return err;
4237 33aa809d 2019-08-08 stsp static const struct got_error *
4238 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4240 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4241 33aa809d 2019-08-08 stsp char *line = NULL;
4242 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4243 33aa809d 2019-08-08 stsp ssize_t linelen;
4245 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4246 33aa809d 2019-08-08 stsp if (linelen == -1) {
4247 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4248 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4249 33aa809d 2019-08-08 stsp goto done;
4251 33aa809d 2019-08-08 stsp return NULL;
4253 33aa809d 2019-08-08 stsp if (outfile) {
4254 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4255 33aa809d 2019-08-08 stsp if (n != linelen) {
4256 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4257 33aa809d 2019-08-08 stsp goto done;
4260 33aa809d 2019-08-08 stsp if (rejectfile) {
4261 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4262 33aa809d 2019-08-08 stsp if (n != linelen)
4263 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4266 33aa809d 2019-08-08 stsp free(line);
4267 2ec1f75b 2019-03-26 stsp return err;
4270 33aa809d 2019-08-08 stsp static const struct got_error *
4271 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4273 33aa809d 2019-08-08 stsp char *line = NULL;
4274 33aa809d 2019-08-08 stsp size_t linesize = 0;
4275 33aa809d 2019-08-08 stsp ssize_t linelen;
4277 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4278 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4279 500467ff 2019-09-25 hiltjo if (ferror(f))
4280 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4281 500467ff 2019-09-25 hiltjo return NULL;
4283 33aa809d 2019-08-08 stsp free(line);
4284 33aa809d 2019-08-08 stsp return NULL;
4287 33aa809d 2019-08-08 stsp static const struct got_error *
4288 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4289 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4290 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4292 33aa809d 2019-08-08 stsp const struct got_error *err;
4294 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4295 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4296 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4298 33aa809d 2019-08-08 stsp return err;
4299 33aa809d 2019-08-08 stsp (*line_cur1)++;
4301 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4302 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4303 33aa809d 2019-08-08 stsp if (rejectfile)
4304 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4306 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4308 33aa809d 2019-08-08 stsp return err;
4309 33aa809d 2019-08-08 stsp (*line_cur2)++;
4311 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4312 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4313 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4315 33aa809d 2019-08-08 stsp return err;
4316 33aa809d 2019-08-08 stsp (*line_cur2)++;
4318 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4319 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4320 33aa809d 2019-08-08 stsp if (rejectfile)
4321 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4323 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4325 33aa809d 2019-08-08 stsp return err;
4326 33aa809d 2019-08-08 stsp (*line_cur1)++;
4329 f1e81a05 2019-08-10 stsp return NULL;
4332 f1e81a05 2019-08-10 stsp static const struct got_error *
4333 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4334 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4336 f1e81a05 2019-08-10 stsp const struct got_error *err;
4338 f1e81a05 2019-08-10 stsp if (outfile) {
4339 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4340 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4341 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4343 f1e81a05 2019-08-10 stsp return err;
4344 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4347 f1e81a05 2019-08-10 stsp if (rejectfile) {
4348 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4349 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4350 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4352 f1e81a05 2019-08-10 stsp return err;
4353 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4357 33aa809d 2019-08-08 stsp return NULL;
4360 33aa809d 2019-08-08 stsp static const struct got_error *
4361 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4362 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4363 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4364 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4365 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4367 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4368 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4369 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4370 33aa809d 2019-08-08 stsp FILE *hunkfile;
4371 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4372 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4375 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4377 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4378 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4379 fe621944 2020-11-10 stsp start_old = cc.left.start;
4380 fe621944 2020-11-10 stsp end_old = cc.left.end;
4381 fe621944 2020-11-10 stsp start_new = cc.right.start;
4382 fe621944 2020-11-10 stsp end_new = cc.right.end;
4384 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4385 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4386 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4388 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4389 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4390 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4392 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4393 fe621944 2020-11-10 stsp if (diff_state == NULL)
4394 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4395 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4397 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4398 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4399 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4400 33aa809d 2019-08-08 stsp goto done;
4403 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4404 fe621944 2020-11-10 stsp diff_result, &cc);
4405 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4406 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4407 33aa809d 2019-08-08 stsp goto done;
4410 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4411 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4412 33aa809d 2019-08-08 stsp goto done;
4415 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4416 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4418 33aa809d 2019-08-08 stsp goto done;
4420 33aa809d 2019-08-08 stsp switch (*choice) {
4421 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4422 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4423 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4425 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4426 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4427 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4429 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4432 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4436 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4437 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4438 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4439 33aa809d 2019-08-08 stsp return err;
4442 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4443 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4444 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4445 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4446 1f1abb7e 2019-08-08 stsp void *progress_arg;
4447 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4448 33aa809d 2019-08-08 stsp void *patch_arg;
4449 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4450 10604dce 2021-09-24 thomas int unlink_added_files;
4453 e20a8b6f 2019-06-04 stsp static const struct got_error *
4454 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4455 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4456 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4457 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4458 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4460 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4461 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4462 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4463 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4464 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4465 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4466 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4467 fe621944 2020-11-10 stsp struct stat sb2;
4468 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4469 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4470 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4472 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4474 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4476 33aa809d 2019-08-08 stsp return err;
4478 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4479 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4480 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4481 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4482 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4483 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4484 fa3cef63 2020-07-23 stsp goto done;
4486 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4487 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4488 48b4f239 2021-12-31 thomas if (link_len == -1) {
4489 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4492 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4493 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4496 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4497 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4498 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4499 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4500 fa3cef63 2020-07-23 stsp goto done;
4502 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4503 fa3cef63 2020-07-23 stsp sizeof(link_target));
4504 fa3cef63 2020-07-23 stsp if (link_len == -1)
4505 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4506 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4507 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4510 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4511 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4512 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4513 fa3cef63 2020-07-23 stsp goto done;
4516 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4517 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4518 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4519 fa3cef63 2020-07-23 stsp goto done;
4524 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4525 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4526 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4527 fa3cef63 2020-07-23 stsp goto done;
4529 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4530 fa3cef63 2020-07-23 stsp if (n != link_len) {
4531 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4532 fa3cef63 2020-07-23 stsp goto done;
4534 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4535 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4536 fa3cef63 2020-07-23 stsp goto done;
4538 fa3cef63 2020-07-23 stsp rewind(f2);
4541 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4542 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4543 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4544 f4ae6ddb 2022-07-01 thomas goto done;
4547 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4549 33aa809d 2019-08-08 stsp goto done;
4551 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4553 33aa809d 2019-08-08 stsp goto done;
4555 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4557 33aa809d 2019-08-08 stsp goto done;
4559 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4560 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4562 33aa809d 2019-08-08 stsp goto done;
4564 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4567 33aa809d 2019-08-08 stsp goto done;
4569 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4570 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4571 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4572 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4574 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4575 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4576 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4577 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4578 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4579 fe621944 2020-11-10 stsp nchanges++;
4581 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4582 33aa809d 2019-08-08 stsp int choice;
4583 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4584 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4585 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4586 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4587 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4588 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4590 33aa809d 2019-08-08 stsp goto done;
4591 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4592 33aa809d 2019-08-08 stsp have_content = 1;
4593 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4596 1ebedb77 2019-10-19 stsp if (have_content) {
4597 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4598 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4599 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4601 1ebedb77 2019-10-19 stsp goto done;
4603 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4604 a2c162eb 2022-10-30 thomas mode_t mode;
4606 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
4607 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
4608 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4609 fa3cef63 2020-07-23 stsp goto done;
4614 33aa809d 2019-08-08 stsp free(id_str);
4615 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4616 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4618 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4619 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4620 fe621944 2020-11-10 stsp if (err == NULL)
4621 fe621944 2020-11-10 stsp err = free_err;
4622 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4623 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4624 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4625 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4626 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4627 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4628 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4629 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4630 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4631 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4632 33aa809d 2019-08-08 stsp if (err || !have_content) {
4633 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4634 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4635 33aa809d 2019-08-08 stsp free(*path_outfile);
4636 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4638 33aa809d 2019-08-08 stsp free(path1);
4639 33aa809d 2019-08-08 stsp return err;
4642 33aa809d 2019-08-08 stsp static const struct got_error *
4643 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4644 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4645 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4646 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4648 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4649 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4650 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4651 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4652 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4653 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4654 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4655 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4656 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4657 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4658 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4659 f4ae6ddb 2022-07-01 thomas int fd = -1;
4661 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4662 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4663 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4664 d3bcc3d1 2019-08-08 stsp return NULL;
4666 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4667 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4668 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4670 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4671 65084dad 2019-08-08 stsp if (ie == NULL)
4672 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4674 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4675 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4676 a129376b 2019-03-28 stsp if (err) {
4677 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4678 a129376b 2019-03-28 stsp goto done;
4679 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4680 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4681 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4682 e20a8b6f 2019-06-04 stsp goto done;
4685 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4686 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4687 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4688 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4689 a129376b 2019-03-28 stsp goto done;
4692 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4693 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4694 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4695 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4696 a129376b 2019-03-28 stsp goto done;
4699 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4700 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4701 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4702 a129376b 2019-03-28 stsp goto done;
4707 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4708 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
4709 945f9229 2022-04-16 thomas if (err)
4710 945f9229 2022-04-16 thomas goto done;
4712 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4713 a9fa2909 2019-07-27 stsp if (err) {
4714 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4715 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4716 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4717 a9fa2909 2019-07-27 stsp goto done;
4719 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4721 a9fa2909 2019-07-27 stsp goto done;
4723 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4725 a9fa2909 2019-07-27 stsp goto done;
4727 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4728 1233e6b6 2020-10-19 stsp free(te_name);
4729 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4730 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4731 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4732 a9fa2909 2019-07-27 stsp goto done;
4736 a129376b 2019-03-28 stsp switch (status) {
4737 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4738 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4739 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4740 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4741 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4743 33aa809d 2019-08-08 stsp goto done;
4744 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4747 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4748 1f1abb7e 2019-08-08 stsp ie->path);
4750 1ee397ad 2019-07-12 stsp goto done;
4751 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4752 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
4753 10604dce 2021-09-24 thomas if (asprintf(&ondisk_path, "%s/%s",
4754 10604dce 2021-09-24 thomas got_worktree_get_root_path(a->worktree),
4755 10604dce 2021-09-24 thomas relpath) == -1) {
4756 10604dce 2021-09-24 thomas err = got_error_from_errno("asprintf");
4757 10604dce 2021-09-24 thomas goto done;
4759 10604dce 2021-09-24 thomas if (unlink(ondisk_path) == -1) {
4760 10604dce 2021-09-24 thomas err = got_error_from_errno2("unlink",
4761 10604dce 2021-09-24 thomas ondisk_path);
4766 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4767 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4768 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4769 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4770 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4772 33aa809d 2019-08-08 stsp goto done;
4773 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4776 33aa809d 2019-08-08 stsp /* fall through */
4777 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4778 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4779 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4780 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4781 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4782 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4783 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4784 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4785 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4787 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4788 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4789 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4790 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4791 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4792 f4ae6ddb 2022-07-01 thomas goto done;
4795 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4797 65084dad 2019-08-08 stsp goto done;
4799 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4800 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4801 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4802 a129376b 2019-03-28 stsp goto done;
4805 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4806 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4807 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4808 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4809 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4810 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4811 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4813 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4814 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4815 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4816 369fd7e5 2020-07-23 stsp path_content);
4819 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4820 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4821 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4822 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4824 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4825 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4826 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4827 369fd7e5 2020-07-23 stsp goto done;
4831 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4832 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4833 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4834 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4835 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4836 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4838 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4840 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4841 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4842 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4843 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4846 a129376b 2019-03-28 stsp goto done;
4847 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4848 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4849 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4850 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4851 437adc9d 2020-12-10 yzhong blob->id.sha1,
4852 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4854 2e1fa222 2020-07-23 stsp goto done;
4856 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4857 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4858 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4867 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4868 33aa809d 2019-08-08 stsp free(path_content);
4869 e20a8b6f 2019-06-04 stsp free(parent_path);
4870 a129376b 2019-03-28 stsp free(tree_path);
4871 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4872 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4874 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4876 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4877 a129376b 2019-03-28 stsp free(tree_id);
4878 945f9229 2022-04-16 thomas if (base_commit)
4879 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
4880 e20a8b6f 2019-06-04 stsp return err;
4883 e20a8b6f 2019-06-04 stsp const struct got_error *
4884 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4885 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4886 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4887 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4888 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4890 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4891 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4892 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4893 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4894 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4895 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4897 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4899 e20a8b6f 2019-06-04 stsp return err;
4901 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4903 e20a8b6f 2019-06-04 stsp goto done;
4905 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4906 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4907 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4908 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4909 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4910 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4911 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4912 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
4913 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4914 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4915 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
4919 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4920 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4921 e20a8b6f 2019-06-04 stsp err = sync_err;
4923 fb399478 2019-07-12 stsp free(fileindex_path);
4924 a129376b 2019-03-28 stsp if (fileindex)
4925 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4926 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4927 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4928 a129376b 2019-03-28 stsp err = unlockerr;
4929 c4296144 2019-05-09 stsp return err;
4932 cf066bf8 2019-05-09 stsp static void
4933 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4935 24519714 2019-05-09 stsp free(ct->path);
4936 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4937 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4938 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4939 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4940 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4941 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4945 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4946 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4947 24519714 2019-05-09 stsp struct got_repository *repo;
4948 24519714 2019-05-09 stsp struct got_worktree *worktree;
4949 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4950 5f8a88c6 2019-08-03 stsp int have_staged_files;
4951 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4952 ef899790 2022-11-01 thomas int diff_header_shown;
4953 ef899790 2022-11-01 thomas FILE *diff_outfile;
4954 ef899790 2022-11-01 thomas FILE *f1;
4955 ef899790 2022-11-01 thomas FILE *f2;
4959 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
4960 ef899790 2022-11-01 thomas * it as content to the diff engine.
4962 ef899790 2022-11-01 thomas static const struct got_error *
4963 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4964 ef899790 2022-11-01 thomas const char *abspath)
4966 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
4967 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
4968 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
4970 ef899790 2022-11-01 thomas *fd = -1;
4972 ef899790 2022-11-01 thomas if (dirfd != -1) {
4973 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4974 ef899790 2022-11-01 thomas if (target_len == -1)
4975 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
4976 ef899790 2022-11-01 thomas } else {
4977 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
4978 ef899790 2022-11-01 thomas if (target_len == -1)
4979 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
4982 ef899790 2022-11-01 thomas *fd = got_opentempfd();
4983 ef899790 2022-11-01 thomas if (*fd == -1)
4984 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
4986 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
4987 ef899790 2022-11-01 thomas if (outlen == -1) {
4988 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
4989 ef899790 2022-11-01 thomas goto done;
4992 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
4993 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
4994 ef899790 2022-11-01 thomas goto done;
4997 ef899790 2022-11-01 thomas if (err) {
4998 ef899790 2022-11-01 thomas close(*fd);
4999 ef899790 2022-11-01 thomas *fd = -1;
5001 ef899790 2022-11-01 thomas return err;
5004 ef899790 2022-11-01 thomas static const struct got_error *
5005 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5006 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5007 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5009 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5010 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5011 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5012 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5013 ef899790 2022-11-01 thomas char *label1 = NULL;
5014 ef899790 2022-11-01 thomas struct stat sb;
5015 ef899790 2022-11-01 thomas off_t size1 = 0;
5016 ef899790 2022-11-01 thomas int f2_exists = 0;
5017 ef899790 2022-11-01 thomas char *id_str = NULL;
5019 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5021 d259e491 2022-11-01 thomas if (diff_staged) {
5022 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5023 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5024 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5025 d259e491 2022-11-01 thomas return NULL;
5026 d259e491 2022-11-01 thomas } else {
5027 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5028 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5029 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_DELETE)
5030 d259e491 2022-11-01 thomas return NULL;
5033 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5034 ef899790 2022-11-01 thomas if (err)
5035 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5036 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5037 ef899790 2022-11-01 thomas if (err)
5038 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5040 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5041 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5042 ef899790 2022-11-01 thomas if (err)
5043 ef899790 2022-11-01 thomas return err;
5044 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5045 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5046 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5047 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5048 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5049 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5050 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5053 ef899790 2022-11-01 thomas if (diff_staged) {
5054 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5055 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5056 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5057 ef899790 2022-11-01 thomas label1 = ct->path;
5058 ef899790 2022-11-01 thomas label2 = ct->path;
5060 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5061 ef899790 2022-11-01 thomas label2 = ct->path;
5063 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5064 ef899790 2022-11-01 thomas label1 = ct->path;
5066 ef899790 2022-11-01 thomas default:
5067 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5069 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5070 ef899790 2022-11-01 thomas if (fd1 == -1) {
5071 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5072 ef899790 2022-11-01 thomas goto done;
5074 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5075 ef899790 2022-11-01 thomas if (fd2 == -1) {
5076 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5077 ef899790 2022-11-01 thomas goto done;
5079 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5080 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5081 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5082 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5083 ef899790 2022-11-01 thomas goto done;
5086 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5087 ef899790 2022-11-01 thomas if (fd1 == -1) {
5088 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5089 ef899790 2022-11-01 thomas goto done;
5092 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5093 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5094 ef899790 2022-11-01 thomas 8192, fd1);
5095 ef899790 2022-11-01 thomas if (err)
5096 ef899790 2022-11-01 thomas goto done;
5099 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5100 ef899790 2022-11-01 thomas if (dirfd != -1) {
5101 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5102 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5103 ef899790 2022-11-01 thomas if (fd == -1) {
5104 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5105 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5106 ef899790 2022-11-01 thomas ct->ondisk_path);
5107 ef899790 2022-11-01 thomas goto done;
5109 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5110 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5111 ef899790 2022-11-01 thomas if (err)
5112 ef899790 2022-11-01 thomas goto done;
5114 ef899790 2022-11-01 thomas } else {
5115 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5116 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5117 ef899790 2022-11-01 thomas if (fd == -1) {
5118 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5119 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5120 ef899790 2022-11-01 thomas ct->ondisk_path);
5121 ef899790 2022-11-01 thomas goto done;
5123 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5124 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5125 ef899790 2022-11-01 thomas if (err)
5126 ef899790 2022-11-01 thomas goto done;
5129 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5130 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5131 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5132 ef899790 2022-11-01 thomas goto done;
5134 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5135 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5136 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5137 ef899790 2022-11-01 thomas goto done;
5139 ef899790 2022-11-01 thomas fd = -1;
5140 ef899790 2022-11-01 thomas f2_exists = 1;
5143 ef899790 2022-11-01 thomas if (blob1) {
5144 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5145 ef899790 2022-11-01 thomas f1, blob1);
5146 ef899790 2022-11-01 thomas if (err)
5147 ef899790 2022-11-01 thomas goto done;
5150 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5151 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5152 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5154 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5155 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5156 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5157 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5158 ef899790 2022-11-01 thomas if (blob1)
5159 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5160 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5161 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5162 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5163 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5164 ef899790 2022-11-01 thomas return err;
5167 c4296144 2019-05-09 stsp static const struct got_error *
5168 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5169 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5170 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5171 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5173 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5174 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5175 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5176 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5177 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5178 768aea60 2019-05-09 stsp struct stat sb;
5180 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5181 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5182 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5183 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5184 5f8a88c6 2019-08-03 stsp return NULL;
5186 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
5187 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5189 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5190 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5191 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5192 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
5193 5f8a88c6 2019-08-03 stsp return NULL;
5196 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5197 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5198 036813ee 2019-05-09 stsp goto done;
5200 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5201 036813ee 2019-05-09 stsp parent_path = strdup("");
5202 69960a46 2019-05-09 stsp if (parent_path == NULL)
5203 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5205 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5207 69960a46 2019-05-09 stsp return err;
5210 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5211 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5212 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5213 c4296144 2019-05-09 stsp goto done;
5216 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5217 768aea60 2019-05-09 stsp relpath) == -1) {
5218 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5219 768aea60 2019-05-09 stsp goto done;
5222 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5223 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5224 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5225 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5226 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5227 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5228 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5229 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5231 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5232 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5235 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5236 0aeb8099 2020-07-23 stsp goto done;
5238 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5239 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5240 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5241 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5242 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5243 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5244 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5245 12463d8b 2019-12-13 stsp ct->ondisk_path);
5246 12463d8b 2019-12-13 stsp goto done;
5248 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5249 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5250 768aea60 2019-05-09 stsp goto done;
5252 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5255 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5256 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5257 44d03001 2019-05-09 stsp relpath) == -1) {
5258 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5259 44d03001 2019-05-09 stsp goto done;
5262 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5263 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5264 35213c7c 2020-07-23 stsp int is_bad_symlink;
5265 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5266 35213c7c 2020-07-23 stsp ssize_t target_len;
5267 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5268 35213c7c 2020-07-23 stsp sizeof(target_path));
5269 35213c7c 2020-07-23 stsp if (target_len == -1) {
5270 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5271 35213c7c 2020-07-23 stsp ct->ondisk_path);
5272 35213c7c 2020-07-23 stsp goto done;
5274 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5275 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5277 35213c7c 2020-07-23 stsp goto done;
5278 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5279 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5280 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5281 35213c7c 2020-07-23 stsp goto done;
5286 cf066bf8 2019-05-09 stsp ct->status = status;
5287 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5288 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5289 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5290 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5291 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5292 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5293 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5294 b416585c 2019-05-13 stsp goto done;
5296 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5297 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5298 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5299 f0b75401 2019-08-03 stsp goto done;
5302 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5303 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5304 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5305 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5306 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5307 036813ee 2019-05-09 stsp goto done;
5310 24519714 2019-05-09 stsp ct->path = strdup(path);
5311 24519714 2019-05-09 stsp if (ct->path == NULL) {
5312 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5313 24519714 2019-05-09 stsp goto done;
5315 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5316 ef899790 2022-11-01 thomas if (err)
5317 ef899790 2022-11-01 thomas goto done;
5319 ef899790 2022-11-01 thomas if (a->diff_outfile && ct && new != NULL) {
5320 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5321 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5322 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5323 ef899790 2022-11-01 thomas if (err)
5324 ef899790 2022-11-01 thomas goto done;
5327 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5328 ed175427 2019-05-09 stsp free_commitable(ct);
5329 24519714 2019-05-09 stsp free(parent_path);
5330 036813ee 2019-05-09 stsp free(path);
5331 a129376b 2019-03-28 stsp return err;
5334 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5335 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5336 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5337 036813ee 2019-05-09 stsp struct got_repository *);
5339 ed175427 2019-05-09 stsp static const struct got_error *
5340 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5341 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5342 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5343 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5344 afa376bf 2019-05-09 stsp struct got_repository *repo)
5346 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5347 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5348 036813ee 2019-05-09 stsp char *subpath;
5350 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5351 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5352 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5354 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5356 ed175427 2019-05-09 stsp return err;
5358 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5359 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5360 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5361 036813ee 2019-05-09 stsp free(subpath);
5362 036813ee 2019-05-09 stsp return err;
5365 036813ee 2019-05-09 stsp static const struct got_error *
5366 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5368 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5369 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5371 036813ee 2019-05-09 stsp *match = 0;
5373 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5374 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5375 0f63689d 2019-05-10 stsp return NULL;
5378 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5380 0f63689d 2019-05-10 stsp return err;
5381 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5382 036813ee 2019-05-09 stsp free(ct_parent_path);
5383 036813ee 2019-05-09 stsp return err;
5386 768aea60 2019-05-09 stsp static mode_t
5387 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5389 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5390 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5392 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5395 036813ee 2019-05-09 stsp static const struct got_error *
5396 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5397 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5399 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5401 036813ee 2019-05-09 stsp *new_te = NULL;
5403 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5405 036813ee 2019-05-09 stsp goto done;
5407 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5409 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5410 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5411 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5413 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5415 036813ee 2019-05-09 stsp if (err && *new_te) {
5416 56e0773d 2019-11-28 stsp free(*new_te);
5417 036813ee 2019-05-09 stsp *new_te = NULL;
5419 036813ee 2019-05-09 stsp return err;
5422 036813ee 2019-05-09 stsp static const struct got_error *
5423 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5424 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5426 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5427 102b254e 2020-10-19 stsp char *ct_name = NULL;
5429 036813ee 2019-05-09 stsp *new_te = NULL;
5431 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5432 036813ee 2019-05-09 stsp if (*new_te == NULL)
5433 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5435 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5437 036813ee 2019-05-09 stsp goto done;
5438 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5439 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5440 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5441 036813ee 2019-05-09 stsp goto done;
5444 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5446 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5447 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5448 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5450 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5452 102b254e 2020-10-19 stsp free(ct_name);
5453 036813ee 2019-05-09 stsp if (err && *new_te) {
5454 56e0773d 2019-11-28 stsp free(*new_te);
5455 036813ee 2019-05-09 stsp *new_te = NULL;
5457 036813ee 2019-05-09 stsp return err;
5460 036813ee 2019-05-09 stsp static const struct got_error *
5461 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5462 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5464 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5465 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5467 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5469 036813ee 2019-05-09 stsp return err;
5470 036813ee 2019-05-09 stsp if (new_pe == NULL)
5471 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5472 036813ee 2019-05-09 stsp return NULL;
5475 afa376bf 2019-05-09 stsp static const struct got_error *
5476 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5477 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5479 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5480 5f8a88c6 2019-08-03 stsp unsigned char status;
5482 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5483 ae1e948a 2021-09-28 thomas return NULL;
5485 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5486 afa376bf 2019-05-09 stsp ct_path++;
5488 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5489 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5491 5f8a88c6 2019-08-03 stsp status = ct->status;
5493 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5494 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5497 036813ee 2019-05-09 stsp static const struct got_error *
5498 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5499 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5501 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5502 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5503 44d03001 2019-05-09 stsp char *te_path;
5505 44d03001 2019-05-09 stsp *modified = 0;
5507 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5508 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5509 44d03001 2019-05-09 stsp te->name) == -1)
5510 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5512 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5513 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5514 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5515 44d03001 2019-05-09 stsp strlen(te_path));
5516 62d463ca 2020-10-20 naddy if (*modified)
5520 44d03001 2019-05-09 stsp free(te_path);
5521 44d03001 2019-05-09 stsp return err;
5524 44d03001 2019-05-09 stsp static const struct got_error *
5525 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5526 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5527 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5529 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5530 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5532 036813ee 2019-05-09 stsp *ctp = NULL;
5534 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5535 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5536 036813ee 2019-05-09 stsp char *ct_name = NULL;
5537 036813ee 2019-05-09 stsp int path_matches;
5539 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5540 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5541 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5542 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5545 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5546 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5550 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5553 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5555 036813ee 2019-05-09 stsp return err;
5556 036813ee 2019-05-09 stsp if (!path_matches)
5559 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5561 d34b633e 2020-10-19 stsp return err;
5563 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5564 d34b633e 2020-10-19 stsp free(ct_name);
5567 d34b633e 2020-10-19 stsp free(ct_name);
5569 036813ee 2019-05-09 stsp *ctp = ct;
5573 2b496619 2019-07-10 stsp return err;
5576 2b496619 2019-07-10 stsp static const struct got_error *
5577 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5578 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5579 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5580 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5581 2b496619 2019-07-10 stsp struct got_repository *repo)
5583 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5584 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5585 2b496619 2019-07-10 stsp char *subtree_path;
5586 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5587 ba580f68 2020-03-22 stsp int nentries;
5589 2b496619 2019-07-10 stsp *new_tep = NULL;
5591 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5592 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5593 2b496619 2019-07-10 stsp child_path) == -1)
5594 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5596 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5597 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5598 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5599 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5601 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5602 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5603 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5604 2b496619 2019-07-10 stsp goto done;
5606 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5607 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5608 2b496619 2019-07-10 stsp if (err) {
5609 56e0773d 2019-11-28 stsp free(new_te);
5610 2b496619 2019-07-10 stsp goto done;
5612 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5615 2b496619 2019-07-10 stsp free(subtree_path);
5616 2b496619 2019-07-10 stsp if (err == NULL)
5617 2b496619 2019-07-10 stsp *new_tep = new_te;
5618 036813ee 2019-05-09 stsp return err;
5621 036813ee 2019-05-09 stsp static const struct got_error *
5622 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5623 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5624 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5625 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5626 036813ee 2019-05-09 stsp struct got_repository *repo)
5628 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5629 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5630 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5631 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5633 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5634 ba580f68 2020-03-22 stsp *nentries = 0;
5636 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5637 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5638 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5639 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5641 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5642 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5643 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5646 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5647 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5650 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5651 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5653 036813ee 2019-05-09 stsp goto done;
5655 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5656 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5657 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5659 036813ee 2019-05-09 stsp goto done;
5660 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5662 afa376bf 2019-05-09 stsp goto done;
5663 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5664 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5666 2f51b5b3 2019-05-09 stsp goto done;
5667 ba580f68 2020-03-22 stsp (*nentries)++;
5669 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5670 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5671 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5672 2b496619 2019-07-10 stsp == NULL) {
5673 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5674 2b496619 2019-07-10 stsp child_path, path_base_tree,
5675 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5678 2b496619 2019-07-10 stsp goto done;
5679 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5681 2b496619 2019-07-10 stsp goto done;
5682 ba580f68 2020-03-22 stsp (*nentries)++;
5687 2f51b5b3 2019-05-09 stsp if (base_tree) {
5688 56e0773d 2019-11-28 stsp int i, nbase_entries;
5689 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5690 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5691 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5692 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5694 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5695 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5696 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5697 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5699 63c5ca5d 2019-08-24 stsp goto done;
5700 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5702 63c5ca5d 2019-08-24 stsp goto done;
5703 ba580f68 2020-03-22 stsp (*nentries)++;
5707 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5708 44d03001 2019-05-09 stsp int modified;
5709 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5711 036813ee 2019-05-09 stsp goto done;
5712 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5713 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5715 2f51b5b3 2019-05-09 stsp goto done;
5716 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5717 44d03001 2019-05-09 stsp if (modified) {
5718 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5719 ba580f68 2020-03-22 stsp int nsubentries;
5720 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5721 ba580f68 2020-03-22 stsp &nsubentries, te,
5722 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5723 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5725 44d03001 2019-05-09 stsp goto done;
5726 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5727 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5728 ba580f68 2020-03-22 stsp free(new_id);
5731 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5732 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5733 56e0773d 2019-11-28 stsp free(new_id);
5735 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5737 036813ee 2019-05-09 stsp goto done;
5738 ba580f68 2020-03-22 stsp (*nentries)++;
5742 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5743 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5745 f66c734c 2019-09-22 stsp goto done;
5747 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5748 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5749 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5750 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5751 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5752 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5754 2f51b5b3 2019-05-09 stsp goto done;
5755 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5757 2f51b5b3 2019-05-09 stsp goto done;
5758 ba580f68 2020-03-22 stsp (*nentries)++;
5760 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5761 b416585c 2019-05-13 stsp status_arg);
5763 afa376bf 2019-05-09 stsp goto done;
5765 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5766 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5768 2f51b5b3 2019-05-09 stsp goto done;
5769 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5771 2f51b5b3 2019-05-09 stsp goto done;
5772 ba580f68 2020-03-22 stsp (*nentries)++;
5777 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5778 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5780 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5781 2e1fa222 2020-07-23 stsp return err;
5784 2e1fa222 2020-07-23 stsp static const struct got_error *
5785 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5786 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5787 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5788 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5790 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5791 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5792 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5794 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5795 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5796 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5798 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5800 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5801 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5802 437adc9d 2020-12-10 yzhong if (err)
5803 437adc9d 2020-12-10 yzhong goto done;
5806 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5807 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5808 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5809 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5810 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5811 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5812 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5813 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5815 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5816 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5817 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5818 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5819 72fd46fa 2019-09-06 stsp !have_staged_files);
5821 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5822 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5823 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5824 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5825 72fd46fa 2019-09-06 stsp !have_staged_files);
5827 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5829 437adc9d 2020-12-10 yzhong goto done;
5830 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5831 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5832 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5833 3969253a 2020-03-07 stsp if (err) {
5834 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5835 437adc9d 2020-12-10 yzhong goto done;
5837 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5838 3969253a 2020-03-07 stsp if (err) {
5839 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5840 437adc9d 2020-12-10 yzhong goto done;
5843 437adc9d 2020-12-10 yzhong free(relpath);
5844 437adc9d 2020-12-10 yzhong relpath = NULL;
5847 437adc9d 2020-12-10 yzhong free(relpath);
5848 d56d26ce 2019-05-10 stsp return err;
5852 d56d26ce 2019-05-10 stsp static const struct got_error *
5853 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5854 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5855 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5856 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5857 735ef5ac 2019-08-03 stsp int ood_errcode)
5859 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5860 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5861 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5863 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5864 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5865 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5866 1a36436d 2019-06-10 stsp return NULL;
5868 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5869 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5871 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5872 945f9229 2022-04-16 thomas if (err)
5873 945f9229 2022-04-16 thomas goto done;
5874 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5875 9bead371 2019-07-28 stsp if (err) {
5876 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5877 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5878 1a36436d 2019-06-10 stsp goto done;
5879 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5880 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5882 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5883 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5884 945f9229 2022-04-16 thomas if (err)
5885 945f9229 2022-04-16 thomas goto done;
5886 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5887 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5888 1a36436d 2019-06-10 stsp goto done;
5889 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5893 945f9229 2022-04-16 thomas if (commit)
5894 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5895 1a36436d 2019-06-10 stsp return err;
5898 ef20f542 2022-06-26 thomas static const struct got_error *
5899 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5900 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5901 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
5902 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
5903 10604dce 2021-09-24 thomas struct got_worktree *worktree,
5904 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
5905 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5906 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5907 afa376bf 2019-05-09 stsp struct got_repository *repo)
5909 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5910 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5911 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5912 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5913 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5914 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5915 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5916 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5917 10604dce 2021-09-24 thomas int nentries, nparents = 0;
5918 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5919 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5920 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5921 892eab0a 2022-07-22 thomas time_t timestamp;
5923 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5925 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5927 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5929 588edf97 2019-05-10 stsp goto done;
5931 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5933 588edf97 2019-05-10 stsp goto done;
5935 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5936 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
5937 ef899790 2022-11-01 thomas &logmsg, commit_arg);
5942 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5943 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5947 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5948 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5949 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5950 cf066bf8 2019-05-09 stsp char *ondisk_path;
5952 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5953 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5954 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5957 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5958 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5959 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5962 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5963 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5964 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5965 cf066bf8 2019-05-09 stsp goto done;
5967 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5968 2e1fa222 2020-07-23 stsp free(ondisk_path);
5970 cf066bf8 2019-05-09 stsp goto done;
5973 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5974 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5975 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5977 036813ee 2019-05-09 stsp goto done;
5979 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5981 de18fc63 2019-05-09 stsp goto done;
5982 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5983 10604dce 2021-09-24 thomas nparents++;
5984 10604dce 2021-09-24 thomas if (parent_id2) {
5985 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
5986 10604dce 2021-09-24 thomas if (err)
5987 10604dce 2021-09-24 thomas goto done;
5988 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5989 10604dce 2021-09-24 thomas nparents++;
5991 892eab0a 2022-07-22 thomas timestamp = time(NULL);
5992 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5993 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
5994 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5995 33ad4cbe 2019-05-12 jcs free(logmsg);
5997 9d40349a 2019-05-09 stsp goto done;
5999 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6000 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6001 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6002 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6003 09f5bd90 2019-05-09 stsp goto done;
6005 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6006 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6008 09f5bd90 2019-05-09 stsp goto done;
6009 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6011 ebf99748 2019-05-09 stsp goto done;
6012 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6013 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6014 09f5bd90 2019-05-09 stsp goto done;
6016 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6017 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6019 f2c16586 2019-05-09 stsp goto done;
6020 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6022 f2c16586 2019-05-09 stsp goto done;
6024 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6026 09f5bd90 2019-05-09 stsp goto done;
6028 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6030 ebf99748 2019-05-09 stsp goto done;
6032 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6033 588edf97 2019-05-10 stsp if (head_tree)
6034 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6035 588edf97 2019-05-10 stsp if (head_commit)
6036 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6037 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6038 2f17228e 2019-05-12 stsp if (head_ref2) {
6039 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6040 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6041 2f17228e 2019-05-12 stsp err = unlockerr;
6042 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6044 39cd0ff6 2019-07-12 stsp return err;
6047 5c1e53bc 2019-07-28 stsp static const struct got_error *
6048 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6049 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6051 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6052 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6054 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6055 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6056 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6058 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6059 5c1e53bc 2019-07-28 stsp ct_path++;
6061 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6062 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6066 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6067 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6069 5c1e53bc 2019-07-28 stsp return NULL;
6072 f0b75401 2019-08-03 stsp static const struct got_error *
6073 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6075 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6077 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6078 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6079 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6082 f0b75401 2019-08-03 stsp return NULL;
6085 5f8a88c6 2019-08-03 stsp static const struct got_error *
6086 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6087 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6089 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6090 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6092 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6093 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6095 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6096 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6097 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6098 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6099 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6100 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6103 5f8a88c6 2019-08-03 stsp return NULL;
6106 39cd0ff6 2019-07-12 stsp const struct got_error *
6107 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6108 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6109 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6110 ef899790 2022-11-01 thomas int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6111 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6112 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6114 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6115 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6116 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6117 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6118 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6119 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6120 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6121 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6122 ef899790 2022-11-01 thomas char *diff_path = NULL;
6123 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6125 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6127 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6128 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6130 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6132 39cd0ff6 2019-07-12 stsp goto done;
6134 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6136 39cd0ff6 2019-07-12 stsp goto done;
6138 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6140 39cd0ff6 2019-07-12 stsp goto done;
6142 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6144 39cd0ff6 2019-07-12 stsp goto done;
6146 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6147 5f8a88c6 2019-08-03 stsp &have_staged_files);
6148 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6149 5f8a88c6 2019-08-03 stsp goto done;
6150 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6151 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6153 5f8a88c6 2019-08-03 stsp goto done;
6156 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6157 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6158 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6159 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6160 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6161 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6162 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6163 ef899790 2022-11-01 thomas if (show_diff) {
6164 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6165 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6166 ef899790 2022-11-01 thomas if (err)
6167 ef899790 2022-11-01 thomas goto done;
6168 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6169 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6170 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6171 ef899790 2022-11-01 thomas goto done;
6173 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6174 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6175 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6176 ef899790 2022-11-01 thomas goto done;
6180 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6181 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6182 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6184 5c1e53bc 2019-07-28 stsp goto done;
6187 ef899790 2022-11-01 thomas if (show_diff) {
6188 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6189 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6190 ef899790 2022-11-01 thomas goto done;
6194 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6195 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6196 39cd0ff6 2019-07-12 stsp goto done;
6199 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6200 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6202 5c1e53bc 2019-07-28 stsp goto done;
6205 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6206 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6207 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6209 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6210 f0b75401 2019-08-03 stsp ct_path++;
6211 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6212 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6213 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6215 b50cabdf 2019-07-12 stsp goto done;
6219 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6220 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6221 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6222 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6224 39cd0ff6 2019-07-12 stsp goto done;
6226 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6227 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6228 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6229 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6230 39cd0ff6 2019-07-12 stsp err = sync_err;
6232 39cd0ff6 2019-07-12 stsp if (fileindex)
6233 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6234 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6235 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6236 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6237 39cd0ff6 2019-07-12 stsp err = unlockerr;
6238 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6239 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6241 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6243 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6244 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6245 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6246 ef899790 2022-11-01 thomas free(diff_path);
6247 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6248 ef899790 2022-11-01 thomas err == NULL)
6249 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6250 c4296144 2019-05-09 stsp return err;
6253 8656d6c4 2019-05-20 stsp const char *
6254 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6256 8656d6c4 2019-05-20 stsp return ct->path;
6259 8656d6c4 2019-05-20 stsp unsigned int
6260 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6262 8656d6c4 2019-05-20 stsp return ct->status;
6265 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6266 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6267 818c7501 2019-07-11 stsp struct got_repository *repo;
6270 818c7501 2019-07-11 stsp static const struct got_error *
6271 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6273 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6274 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6275 818c7501 2019-07-11 stsp unsigned char status;
6276 818c7501 2019-07-11 stsp struct stat sb;
6277 818c7501 2019-07-11 stsp char *ondisk_path;
6279 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6280 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6281 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6282 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6284 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6286 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6288 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6289 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6290 818c7501 2019-07-11 stsp free(ondisk_path);
6292 818c7501 2019-07-11 stsp return err;
6294 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6295 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6296 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6297 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6299 818c7501 2019-07-11 stsp return NULL;
6302 818c7501 2019-07-11 stsp const struct got_error *
6303 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6304 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6305 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6306 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6308 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6309 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6310 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6311 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6312 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6313 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6314 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6316 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6317 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6318 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6320 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6322 818c7501 2019-07-11 stsp return err;
6324 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6326 818c7501 2019-07-11 stsp goto done;
6328 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6329 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6330 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6333 818c7501 2019-07-11 stsp goto done;
6335 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6337 818c7501 2019-07-11 stsp goto done;
6339 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6341 818c7501 2019-07-11 stsp goto done;
6343 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6345 818c7501 2019-07-11 stsp goto done;
6347 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6350 e51d7b55 2020-01-04 stsp goto done;
6352 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6354 818c7501 2019-07-11 stsp goto done;
6355 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6356 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6357 e51d7b55 2020-01-04 stsp goto done;
6360 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6361 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6363 818c7501 2019-07-11 stsp goto done;
6364 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6366 818c7501 2019-07-11 stsp goto done;
6368 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6370 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6372 818c7501 2019-07-11 stsp goto done;
6374 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6376 818c7501 2019-07-11 stsp goto done;
6378 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6379 818c7501 2019-07-11 stsp worktree->base_commit_id);
6381 818c7501 2019-07-11 stsp goto done;
6382 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6384 818c7501 2019-07-11 stsp goto done;
6386 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6388 818c7501 2019-07-11 stsp goto done;
6390 818c7501 2019-07-11 stsp free(fileindex_path);
6391 818c7501 2019-07-11 stsp free(tmp_branch_name);
6392 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6393 818c7501 2019-07-11 stsp free(branch_ref_name);
6394 818c7501 2019-07-11 stsp if (branch_ref)
6395 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6396 818c7501 2019-07-11 stsp if (wt_branch)
6397 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6398 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6399 818c7501 2019-07-11 stsp if (err) {
6400 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6401 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6402 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6404 818c7501 2019-07-11 stsp if (*tmp_branch) {
6405 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6406 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6408 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6409 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6410 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6412 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6414 818c7501 2019-07-11 stsp return err;
6417 818c7501 2019-07-11 stsp const struct got_error *
6418 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6419 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6420 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6421 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6423 818c7501 2019-07-11 stsp const struct got_error *err;
6424 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6425 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6426 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6427 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6428 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6430 818c7501 2019-07-11 stsp *commit_id = NULL;
6431 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6432 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6433 3e3a69f1 2019-07-25 stsp *branch = NULL;
6434 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6436 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6438 3e3a69f1 2019-07-25 stsp return err;
6440 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6442 f032f1f7 2019-08-04 stsp goto done;
6444 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6445 f032f1f7 2019-08-04 stsp &have_staged_files);
6446 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6447 f032f1f7 2019-08-04 stsp goto done;
6448 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6449 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6450 3e3a69f1 2019-07-25 stsp goto done;
6453 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6455 f032f1f7 2019-08-04 stsp goto done;
6457 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6459 818c7501 2019-07-11 stsp goto done;
6461 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6463 818c7501 2019-07-11 stsp goto done;
6465 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6467 818c7501 2019-07-11 stsp goto done;
6469 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6471 818c7501 2019-07-11 stsp goto done;
6473 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6474 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6476 818c7501 2019-07-11 stsp goto done;
6478 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6480 818c7501 2019-07-11 stsp goto done;
6482 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6484 818c7501 2019-07-11 stsp goto done;
6486 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6487 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6489 818c7501 2019-07-11 stsp goto done;
6491 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6493 818c7501 2019-07-11 stsp goto done;
6495 818c7501 2019-07-11 stsp free(commit_ref_name);
6496 818c7501 2019-07-11 stsp free(branch_ref_name);
6497 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6498 818c7501 2019-07-11 stsp if (commit_ref)
6499 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6500 818c7501 2019-07-11 stsp if (branch_ref)
6501 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6502 818c7501 2019-07-11 stsp if (err) {
6503 818c7501 2019-07-11 stsp free(*commit_id);
6504 818c7501 2019-07-11 stsp *commit_id = NULL;
6505 818c7501 2019-07-11 stsp if (*tmp_branch) {
6506 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6507 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6509 818c7501 2019-07-11 stsp if (*new_base_branch) {
6510 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6511 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6513 818c7501 2019-07-11 stsp if (*branch) {
6514 818c7501 2019-07-11 stsp got_ref_close(*branch);
6515 818c7501 2019-07-11 stsp *branch = NULL;
6517 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6518 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6519 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6521 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6523 818c7501 2019-07-11 stsp return err;
6526 818c7501 2019-07-11 stsp const struct got_error *
6527 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6529 818c7501 2019-07-11 stsp const struct got_error *err;
6530 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6532 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6534 818c7501 2019-07-11 stsp return err;
6536 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6537 818c7501 2019-07-11 stsp free(tmp_branch_name);
6538 818c7501 2019-07-11 stsp return NULL;
6541 818c7501 2019-07-11 stsp static const struct got_error *
6542 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6543 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
6545 0ebf8283 2019-07-24 stsp *logmsg = arg;
6546 818c7501 2019-07-11 stsp return NULL;
6549 818c7501 2019-07-11 stsp static const struct got_error *
6550 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6551 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6552 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6553 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6555 818c7501 2019-07-11 stsp return NULL;
6558 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6559 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6560 01757395 2019-07-12 stsp void *progress_arg;
6561 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6564 01757395 2019-07-12 stsp static const struct got_error *
6565 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6567 01757395 2019-07-12 stsp const struct got_error *err;
6568 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6570 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6572 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6574 01757395 2019-07-12 stsp return err;
6576 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6577 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6578 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6579 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6580 01757395 2019-07-12 stsp return NULL;
6582 01757395 2019-07-12 stsp p = strdup(path);
6583 01757395 2019-07-12 stsp if (p == NULL)
6584 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6586 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6587 01757395 2019-07-12 stsp if (err || new == NULL)
6589 01757395 2019-07-12 stsp return err;
6592 0ebf8283 2019-07-24 stsp static const struct got_error *
6593 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6594 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6596 818c7501 2019-07-11 stsp const struct got_error *err;
6597 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6599 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6600 818c7501 2019-07-11 stsp if (err) {
6601 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6602 818c7501 2019-07-11 stsp goto done;
6603 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6605 818c7501 2019-07-11 stsp goto done;
6606 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6608 818c7501 2019-07-11 stsp goto done;
6609 de05890f 2020-03-05 stsp } else if (is_rebase) {
6610 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6613 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6615 818c7501 2019-07-11 stsp goto done;
6616 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6617 818c7501 2019-07-11 stsp free(stored_id);
6618 818c7501 2019-07-11 stsp if (cmp != 0) {
6619 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6620 818c7501 2019-07-11 stsp goto done;
6624 0ebf8283 2019-07-24 stsp if (commit_ref)
6625 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6626 0ebf8283 2019-07-24 stsp return err;
6629 0ebf8283 2019-07-24 stsp static const struct got_error *
6630 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6631 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6632 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6633 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6634 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6635 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6637 0ebf8283 2019-07-24 stsp const struct got_error *err;
6638 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6639 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6640 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6642 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6644 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6646 0ebf8283 2019-07-24 stsp return err;
6648 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6649 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6650 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6651 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6652 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6653 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6654 818c7501 2019-07-11 stsp if (commit_ref)
6655 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6656 818c7501 2019-07-11 stsp return err;
6659 818c7501 2019-07-11 stsp const struct got_error *
6660 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6661 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6662 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6663 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6664 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6665 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6667 0ebf8283 2019-07-24 stsp const struct got_error *err;
6668 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6670 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6672 0ebf8283 2019-07-24 stsp return err;
6674 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6676 0ebf8283 2019-07-24 stsp goto done;
6678 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6679 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6680 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6682 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6683 0ebf8283 2019-07-24 stsp return err;
6686 0ebf8283 2019-07-24 stsp const struct got_error *
6687 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6688 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6689 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6690 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6691 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6692 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6694 0ebf8283 2019-07-24 stsp const struct got_error *err;
6695 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6697 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6699 0ebf8283 2019-07-24 stsp return err;
6701 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6703 0ebf8283 2019-07-24 stsp goto done;
6705 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6706 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6707 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6709 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6710 0ebf8283 2019-07-24 stsp return err;
6713 0ebf8283 2019-07-24 stsp static const struct got_error *
6714 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6715 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6716 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6717 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
6718 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
6719 150d7275 2022-07-22 thomas struct got_repository *repo)
6721 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6722 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6723 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6724 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6725 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6726 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6727 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6729 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6730 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6731 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6733 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6735 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6737 3e3a69f1 2019-07-25 stsp return err;
6739 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6740 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6741 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6742 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6744 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6745 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6747 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6748 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6749 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6750 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6751 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6752 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6754 01757395 2019-07-12 stsp if (merged_paths) {
6755 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6756 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6757 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6758 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6761 01757395 2019-07-12 stsp goto done;
6764 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6765 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6767 01757395 2019-07-12 stsp goto done;
6770 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6771 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6772 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6774 a0e95631 2019-07-12 stsp goto done;
6775 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6776 a0e95631 2019-07-12 stsp goto done;
6779 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6781 edd02c5e 2019-07-11 stsp goto done;
6783 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6785 818c7501 2019-07-11 stsp goto done;
6787 5943eee2 2019-08-13 stsp if (new_logmsg) {
6788 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6789 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6790 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6791 5943eee2 2019-08-13 stsp goto done;
6794 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6796 5943eee2 2019-08-13 stsp goto done;
6799 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6800 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6801 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
6802 8db00f97 2022-07-22 thomas committer ? committer :
6803 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
6804 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6806 a0e95631 2019-07-12 stsp goto done;
6808 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6810 a0e95631 2019-07-12 stsp goto done;
6812 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6814 a0e95631 2019-07-12 stsp goto done;
6816 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6817 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6818 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6819 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6820 a0e95631 2019-07-12 stsp err = sync_err;
6822 a0e95631 2019-07-12 stsp free(fileindex_path);
6823 a0e95631 2019-07-12 stsp free(head_commit_id);
6824 a0e95631 2019-07-12 stsp if (head_ref)
6825 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6826 818c7501 2019-07-11 stsp if (err) {
6827 818c7501 2019-07-11 stsp free(*new_commit_id);
6828 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6830 818c7501 2019-07-11 stsp return err;
6833 818c7501 2019-07-11 stsp const struct got_error *
6834 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6835 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6836 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6837 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
6838 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6840 0ebf8283 2019-07-24 stsp const struct got_error *err;
6841 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6842 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6843 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6845 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6847 0ebf8283 2019-07-24 stsp return err;
6849 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6851 0ebf8283 2019-07-24 stsp goto done;
6852 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6854 0ebf8283 2019-07-24 stsp goto done;
6855 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6856 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6857 0ebf8283 2019-07-24 stsp goto done;
6860 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6861 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
6862 150d7275 2022-07-22 thomas NULL, repo);
6864 0ebf8283 2019-07-24 stsp if (commit_ref)
6865 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6866 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6867 0ebf8283 2019-07-24 stsp free(commit_id);
6868 0ebf8283 2019-07-24 stsp return err;
6871 0ebf8283 2019-07-24 stsp const struct got_error *
6872 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6873 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6874 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6875 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
6876 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6877 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6879 0ebf8283 2019-07-24 stsp const struct got_error *err;
6880 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6881 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6883 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6885 0ebf8283 2019-07-24 stsp return err;
6887 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6889 0ebf8283 2019-07-24 stsp goto done;
6891 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6892 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
6893 150d7275 2022-07-22 thomas new_logmsg, repo);
6895 0ebf8283 2019-07-24 stsp if (commit_ref)
6896 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6897 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6898 0ebf8283 2019-07-24 stsp return err;
6901 0ebf8283 2019-07-24 stsp const struct got_error *
6902 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6903 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6905 3e3a69f1 2019-07-25 stsp if (fileindex)
6906 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6907 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6910 69844fba 2019-07-11 stsp static const struct got_error *
6911 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6913 69844fba 2019-07-11 stsp const struct got_error *err;
6914 69844fba 2019-07-11 stsp struct got_reference *ref;
6916 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6917 69844fba 2019-07-11 stsp if (err) {
6918 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6919 69844fba 2019-07-11 stsp return NULL;
6920 69844fba 2019-07-11 stsp return err;
6923 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6924 69844fba 2019-07-11 stsp got_ref_close(ref);
6925 69844fba 2019-07-11 stsp return err;
6928 69844fba 2019-07-11 stsp static const struct got_error *
6929 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6931 69844fba 2019-07-11 stsp const struct got_error *err;
6932 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6933 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6935 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6937 69844fba 2019-07-11 stsp goto done;
6938 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6940 69844fba 2019-07-11 stsp goto done;
6942 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6944 69844fba 2019-07-11 stsp goto done;
6945 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6947 69844fba 2019-07-11 stsp goto done;
6949 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6951 69844fba 2019-07-11 stsp goto done;
6952 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6954 69844fba 2019-07-11 stsp goto done;
6956 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6958 69844fba 2019-07-11 stsp goto done;
6959 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6961 69844fba 2019-07-11 stsp goto done;
6964 69844fba 2019-07-11 stsp free(tmp_branch_name);
6965 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6966 69844fba 2019-07-11 stsp free(branch_ref_name);
6967 69844fba 2019-07-11 stsp free(commit_ref_name);
6968 e600f124 2021-03-21 stsp return err;
6971 ef20f542 2022-06-26 thomas static const struct got_error *
6972 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6973 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6975 e600f124 2021-03-21 stsp const struct got_error *err;
6976 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6977 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6978 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6979 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6980 e600f124 2021-03-21 stsp char *refname = NULL;
6982 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6983 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6984 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6985 e600f124 2021-03-21 stsp branch_name += 11;
6987 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6989 e600f124 2021-03-21 stsp return err;
6991 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6992 e600f124 2021-03-21 stsp new_id_str) == -1) {
6993 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6994 e600f124 2021-03-21 stsp goto done;
6997 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6999 e600f124 2021-03-21 stsp goto done;
7001 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7003 e600f124 2021-03-21 stsp goto done;
7005 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7007 e600f124 2021-03-21 stsp free(new_id_str);
7008 e600f124 2021-03-21 stsp free(refname);
7009 e600f124 2021-03-21 stsp free(old_commit_id);
7011 e600f124 2021-03-21 stsp got_ref_close(ref);
7012 69844fba 2019-07-11 stsp return err;
7015 818c7501 2019-07-11 stsp const struct got_error *
7016 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7017 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7018 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7019 f08f28be 2023-02-03 thomas int create_backup)
7021 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7022 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7023 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7025 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7027 818c7501 2019-07-11 stsp return err;
7029 e600f124 2021-03-21 stsp if (create_backup) {
7030 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7031 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7033 e600f124 2021-03-21 stsp goto done;
7036 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7038 818c7501 2019-07-11 stsp goto done;
7040 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7042 818c7501 2019-07-11 stsp goto done;
7044 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7046 818c7501 2019-07-11 stsp goto done;
7048 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7050 a615e0e7 2020-12-16 stsp goto done;
7052 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7054 a615e0e7 2020-12-16 stsp goto done;
7055 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7056 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7057 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7058 a615e0e7 2020-12-16 stsp err = sync_err;
7060 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7061 a615e0e7 2020-12-16 stsp free(fileindex_path);
7062 818c7501 2019-07-11 stsp free(new_head_commit_id);
7063 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7064 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7065 818c7501 2019-07-11 stsp err = unlockerr;
7066 818c7501 2019-07-11 stsp return err;
7069 818c7501 2019-07-11 stsp const struct got_error *
7070 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7071 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7072 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7073 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7075 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7076 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7077 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7078 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7079 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7080 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7081 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7083 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7085 818c7501 2019-07-11 stsp return err;
7087 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7088 945f9229 2022-04-16 thomas worktree->base_commit_id);
7089 945f9229 2022-04-16 thomas if (err)
7090 945f9229 2022-04-16 thomas goto done;
7092 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7093 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7095 818c7501 2019-07-11 stsp goto done;
7097 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7099 818c7501 2019-07-11 stsp goto done;
7102 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7103 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7104 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7106 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7108 818c7501 2019-07-11 stsp goto done;
7110 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7112 818c7501 2019-07-11 stsp goto done;
7114 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7115 945f9229 2022-04-16 thomas worktree->path_prefix);
7117 ca355955 2019-07-12 stsp goto done;
7119 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7121 ca355955 2019-07-12 stsp goto done;
7123 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7125 818c7501 2019-07-11 stsp goto done;
7127 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7128 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7129 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7130 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7131 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7132 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7133 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7134 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7135 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7136 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7138 1f1abb7e 2019-08-08 stsp goto sync;
7140 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7141 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7143 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7144 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7145 ca355955 2019-07-12 stsp err = sync_err;
7147 818c7501 2019-07-11 stsp got_ref_close(resolved);
7148 a3a2faf2 2019-07-12 stsp free(tree_id);
7149 818c7501 2019-07-11 stsp free(commit_id);
7150 945f9229 2022-04-16 thomas if (commit)
7151 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7152 0ebf8283 2019-07-24 stsp if (fileindex)
7153 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7154 0ebf8283 2019-07-24 stsp free(fileindex_path);
7156 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7157 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7158 0ebf8283 2019-07-24 stsp err = unlockerr;
7159 0ebf8283 2019-07-24 stsp return err;
7162 0ebf8283 2019-07-24 stsp const struct got_error *
7163 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7164 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7165 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7166 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7168 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7169 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7170 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7171 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7172 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7173 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7174 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7175 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7177 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7178 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7179 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7180 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7182 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7184 0ebf8283 2019-07-24 stsp return err;
7186 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7188 0ebf8283 2019-07-24 stsp goto done;
7190 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7191 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7192 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7195 0ebf8283 2019-07-24 stsp goto done;
7197 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7199 0ebf8283 2019-07-24 stsp goto done;
7201 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7203 0ebf8283 2019-07-24 stsp goto done;
7205 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7206 0ebf8283 2019-07-24 stsp worktree);
7208 0ebf8283 2019-07-24 stsp goto done;
7210 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7213 0ebf8283 2019-07-24 stsp goto done;
7215 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7217 0ebf8283 2019-07-24 stsp goto done;
7219 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7221 0ebf8283 2019-07-24 stsp goto done;
7223 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7224 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7226 0ebf8283 2019-07-24 stsp goto done;
7227 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7229 0ebf8283 2019-07-24 stsp goto done;
7230 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7231 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7232 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7233 0ebf8283 2019-07-24 stsp goto done;
7236 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7237 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7239 0ebf8283 2019-07-24 stsp goto done;
7240 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7242 0ebf8283 2019-07-24 stsp goto done;
7244 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7246 0ebf8283 2019-07-24 stsp goto done;
7248 0ebf8283 2019-07-24 stsp free(fileindex_path);
7249 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7250 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7251 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7252 0ebf8283 2019-07-24 stsp if (wt_branch)
7253 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7254 0ebf8283 2019-07-24 stsp if (err) {
7255 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7256 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7257 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7259 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7260 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7261 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7263 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7264 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7265 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7266 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7268 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7270 0ebf8283 2019-07-24 stsp return err;
7273 0ebf8283 2019-07-24 stsp const struct got_error *
7274 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7275 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7277 3e3a69f1 2019-07-25 stsp if (fileindex)
7278 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7279 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7282 0ebf8283 2019-07-24 stsp const struct got_error *
7283 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7284 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7286 0ebf8283 2019-07-24 stsp const struct got_error *err;
7287 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7289 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7291 0ebf8283 2019-07-24 stsp return err;
7293 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7294 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7295 0ebf8283 2019-07-24 stsp return NULL;
7298 0ebf8283 2019-07-24 stsp const struct got_error *
7299 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7300 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7301 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7302 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7304 0ebf8283 2019-07-24 stsp const struct got_error *err;
7305 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7306 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7307 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7308 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7309 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7310 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7312 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7313 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7314 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7315 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7317 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7319 3e3a69f1 2019-07-25 stsp return err;
7321 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7323 f032f1f7 2019-08-04 stsp goto done;
7325 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7326 f032f1f7 2019-08-04 stsp &have_staged_files);
7327 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7328 f032f1f7 2019-08-04 stsp goto done;
7329 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7330 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7331 3e3a69f1 2019-07-25 stsp goto done;
7334 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7336 f032f1f7 2019-08-04 stsp goto done;
7338 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7340 0ebf8283 2019-07-24 stsp goto done;
7342 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7344 0ebf8283 2019-07-24 stsp goto done;
7346 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7347 0ebf8283 2019-07-24 stsp worktree);
7349 0ebf8283 2019-07-24 stsp goto done;
7351 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7353 0ebf8283 2019-07-24 stsp goto done;
7355 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7357 0ebf8283 2019-07-24 stsp goto done;
7358 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7360 0ebf8283 2019-07-24 stsp goto done;
7362 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7364 0ebf8283 2019-07-24 stsp goto done;
7365 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7367 0ebf8283 2019-07-24 stsp goto done;
7369 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7371 0ebf8283 2019-07-24 stsp goto done;
7373 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7374 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7375 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7376 0ebf8283 2019-07-24 stsp if (commit_ref)
7377 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7378 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7379 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7380 0ebf8283 2019-07-24 stsp if (err) {
7381 0ebf8283 2019-07-24 stsp free(*commit_id);
7382 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7383 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7384 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7385 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7386 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7387 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7389 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7390 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7391 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7393 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7395 0ebf8283 2019-07-24 stsp return err;
7398 0ebf8283 2019-07-24 stsp static const struct got_error *
7399 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7401 0ebf8283 2019-07-24 stsp const struct got_error *err;
7402 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7403 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7405 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7407 0ebf8283 2019-07-24 stsp goto done;
7408 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7410 0ebf8283 2019-07-24 stsp goto done;
7412 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7413 0ebf8283 2019-07-24 stsp worktree);
7415 0ebf8283 2019-07-24 stsp goto done;
7416 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7418 0ebf8283 2019-07-24 stsp goto done;
7420 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7422 0ebf8283 2019-07-24 stsp goto done;
7423 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7425 0ebf8283 2019-07-24 stsp goto done;
7427 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7429 0ebf8283 2019-07-24 stsp goto done;
7430 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7432 0ebf8283 2019-07-24 stsp goto done;
7434 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7435 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7436 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7437 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7438 0ebf8283 2019-07-24 stsp return err;
7441 0ebf8283 2019-07-24 stsp const struct got_error *
7442 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7443 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7444 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7445 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7447 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7448 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7449 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7450 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7451 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7452 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7454 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7456 0ebf8283 2019-07-24 stsp return err;
7458 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7459 945f9229 2022-04-16 thomas worktree->base_commit_id);
7460 945f9229 2022-04-16 thomas if (err)
7461 945f9229 2022-04-16 thomas goto done;
7463 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7464 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7466 0ebf8283 2019-07-24 stsp goto done;
7468 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7470 0ebf8283 2019-07-24 stsp goto done;
7472 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7474 0ebf8283 2019-07-24 stsp goto done;
7476 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7477 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7479 0ebf8283 2019-07-24 stsp goto done;
7481 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7483 0ebf8283 2019-07-24 stsp goto done;
7485 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7487 0ebf8283 2019-07-24 stsp goto done;
7489 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7490 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7491 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7492 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7493 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7494 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7495 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7496 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7497 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7498 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7500 1f1abb7e 2019-08-08 stsp goto sync;
7502 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7503 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7505 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7506 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7507 0ebf8283 2019-07-24 stsp err = sync_err;
7509 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7510 0ebf8283 2019-07-24 stsp free(tree_id);
7511 818c7501 2019-07-11 stsp free(fileindex_path);
7513 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7514 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7515 818c7501 2019-07-11 stsp err = unlockerr;
7516 818c7501 2019-07-11 stsp return err;
7519 0ebf8283 2019-07-24 stsp const struct got_error *
7520 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7521 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7522 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7524 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7525 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7526 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7527 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7529 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7531 0ebf8283 2019-07-24 stsp return err;
7533 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7534 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7536 e600f124 2021-03-21 stsp goto done;
7538 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7539 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7541 0ebf8283 2019-07-24 stsp goto done;
7543 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7545 0ebf8283 2019-07-24 stsp goto done;
7547 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7549 0ebf8283 2019-07-24 stsp goto done;
7551 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7553 0ebf8283 2019-07-24 stsp goto done;
7555 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7557 a615e0e7 2020-12-16 stsp goto done;
7559 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7561 a615e0e7 2020-12-16 stsp goto done;
7562 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7563 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7564 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7565 a615e0e7 2020-12-16 stsp err = sync_err;
7567 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7568 a615e0e7 2020-12-16 stsp free(fileindex_path);
7569 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7570 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7571 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7572 0ebf8283 2019-07-24 stsp err = unlockerr;
7573 0ebf8283 2019-07-24 stsp return err;
7576 0ebf8283 2019-07-24 stsp const struct got_error *
7577 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7578 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7580 0ebf8283 2019-07-24 stsp const struct got_error *err;
7581 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7583 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7585 0ebf8283 2019-07-24 stsp return err;
7587 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7589 0ebf8283 2019-07-24 stsp goto done;
7591 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7593 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7594 2822a352 2019-10-15 stsp return err;
7597 2822a352 2019-10-15 stsp const struct got_error *
7598 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7599 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7600 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7601 2822a352 2019-10-15 stsp struct got_repository *repo)
7603 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7604 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7605 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7607 2822a352 2019-10-15 stsp *fileindex = NULL;
7608 2822a352 2019-10-15 stsp *branch_ref = NULL;
7609 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7611 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7613 2822a352 2019-10-15 stsp return err;
7615 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7616 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7617 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7618 2822a352 2019-10-15 stsp "update -b or different branch name required");
7619 2822a352 2019-10-15 stsp goto done;
7622 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7624 2822a352 2019-10-15 stsp goto done;
7626 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7627 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7628 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7629 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7632 2822a352 2019-10-15 stsp goto done;
7634 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7636 2822a352 2019-10-15 stsp goto done;
7638 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7639 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7641 2822a352 2019-10-15 stsp if (err) {
7642 2822a352 2019-10-15 stsp if (*branch_ref) {
7643 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7644 2822a352 2019-10-15 stsp *branch_ref = NULL;
7646 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7647 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7648 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7650 2822a352 2019-10-15 stsp if (*fileindex) {
7651 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7652 2822a352 2019-10-15 stsp *fileindex = NULL;
7654 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7656 0cb83759 2019-08-03 stsp return err;
7659 2822a352 2019-10-15 stsp const struct got_error *
7660 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7661 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7662 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7663 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7664 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7666 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7667 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7668 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7669 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7671 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7673 2822a352 2019-10-15 stsp goto done;
7675 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7677 2822a352 2019-10-15 stsp goto done;
7679 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
7680 945f9229 2022-04-16 thomas if (err)
7681 945f9229 2022-04-16 thomas goto done;
7683 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7684 2822a352 2019-10-15 stsp worktree->path_prefix);
7686 2822a352 2019-10-15 stsp goto done;
7688 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7690 2822a352 2019-10-15 stsp goto done;
7692 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7693 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7695 2822a352 2019-10-15 stsp goto sync;
7697 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7699 2822a352 2019-10-15 stsp goto sync;
7701 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7703 6e210706 2021-01-22 stsp goto sync;
7705 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7707 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7708 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7709 2822a352 2019-10-15 stsp err = sync_err;
7712 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7713 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7714 2822a352 2019-10-15 stsp err = unlockerr;
7715 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7717 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7718 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7719 2822a352 2019-10-15 stsp err = unlockerr;
7720 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7722 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7723 2822a352 2019-10-15 stsp free(fileindex_path);
7724 2822a352 2019-10-15 stsp free(tree_id);
7725 945f9229 2022-04-16 thomas if (commit)
7726 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7728 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7729 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7730 2822a352 2019-10-15 stsp err = unlockerr;
7731 2822a352 2019-10-15 stsp return err;
7734 2822a352 2019-10-15 stsp const struct got_error *
7735 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7736 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7737 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7739 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7741 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7743 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7745 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7746 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7747 8b692cd0 2019-10-21 stsp err = unlockerr;
7748 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7750 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7751 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7752 8b692cd0 2019-10-21 stsp err = unlockerr;
7753 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7755 10604dce 2021-09-24 thomas return err;
7758 10604dce 2021-09-24 thomas const struct got_error *
7759 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
7760 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
7762 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
7763 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7765 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7766 10604dce 2021-09-24 thomas if (err)
7767 10604dce 2021-09-24 thomas goto done;
7769 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7771 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
7772 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7773 10604dce 2021-09-24 thomas err = sync_err;
7775 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7776 10604dce 2021-09-24 thomas free(fileindex_path);
7777 8b692cd0 2019-10-21 stsp return err;
7780 10604dce 2021-09-24 thomas static const struct got_error *
7781 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7783 10604dce 2021-09-24 thomas const struct got_error *err;
7784 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7786 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7787 10604dce 2021-09-24 thomas if (err)
7788 10604dce 2021-09-24 thomas goto done;
7789 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
7790 10604dce 2021-09-24 thomas if (err)
7791 10604dce 2021-09-24 thomas goto done;
7793 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7794 10604dce 2021-09-24 thomas if (err)
7795 10604dce 2021-09-24 thomas goto done;
7796 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
7797 10604dce 2021-09-24 thomas if (err)
7798 10604dce 2021-09-24 thomas goto done;
7801 10604dce 2021-09-24 thomas free(branch_refname);
7802 10604dce 2021-09-24 thomas free(commit_refname);
7803 10604dce 2021-09-24 thomas return err;
7806 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
7807 10604dce 2021-09-24 thomas struct got_worktree *worktree;
7808 10604dce 2021-09-24 thomas const char *branch_name;
7811 10604dce 2021-09-24 thomas static const struct got_error *
7812 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7813 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
7815 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
7817 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7818 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
7819 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
7821 10604dce 2021-09-24 thomas return NULL;
7825 10604dce 2021-09-24 thomas const struct got_error *
7826 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
7827 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
7828 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
7829 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
7830 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7831 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7833 10604dce 2021-09-24 thomas const struct got_error *err;
7834 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7836 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7837 10604dce 2021-09-24 thomas if (err)
7838 10604dce 2021-09-24 thomas goto done;
7840 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7841 10604dce 2021-09-24 thomas worktree);
7842 10604dce 2021-09-24 thomas if (err)
7843 10604dce 2021-09-24 thomas goto done;
7845 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7846 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
7847 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
7849 10604dce 2021-09-24 thomas free(fileindex_path);
7850 10604dce 2021-09-24 thomas return err;
7853 10604dce 2021-09-24 thomas const struct got_error *
7854 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
7855 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
7856 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
7857 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
7858 ae1e948a 2021-09-28 thomas struct got_repository *repo,
7859 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
7862 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
7863 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
7864 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
7865 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
7866 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
7867 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
7868 10604dce 2021-09-24 thomas int have_staged_files = 0;
7869 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
7870 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7872 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7873 10604dce 2021-09-24 thomas *new_commit_id = NULL;
7875 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
7877 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7878 10604dce 2021-09-24 thomas if (err)
7879 10604dce 2021-09-24 thomas goto done;
7881 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7882 10604dce 2021-09-24 thomas if (err)
7883 10604dce 2021-09-24 thomas goto done;
7885 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
7886 10604dce 2021-09-24 thomas if (err)
7887 10604dce 2021-09-24 thomas goto done;
7889 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7890 10604dce 2021-09-24 thomas &have_staged_files);
7891 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7892 10604dce 2021-09-24 thomas goto done;
7893 10604dce 2021-09-24 thomas if (have_staged_files) {
7894 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7895 10604dce 2021-09-24 thomas goto done;
7898 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
7899 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
7900 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
7901 10604dce 2021-09-24 thomas cc_arg.repo = repo;
7902 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
7903 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7904 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7905 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7906 10604dce 2021-09-24 thomas if (err)
7907 10604dce 2021-09-24 thomas goto done;
7909 10604dce 2021-09-24 thomas if (TAILQ_EMPTY(&commitable_paths)) {
7910 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7911 10604dce 2021-09-24 thomas "merge of %s cannot proceed", branch_name);
7912 10604dce 2021-09-24 thomas goto done;
7915 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7916 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7917 10604dce 2021-09-24 thomas const char *ct_path = ct->in_repo_path;
7919 10604dce 2021-09-24 thomas while (ct_path[0] == '/')
7920 10604dce 2021-09-24 thomas ct_path++;
7921 10604dce 2021-09-24 thomas err = check_out_of_date(ct_path, ct->status,
7922 10604dce 2021-09-24 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7923 10604dce 2021-09-24 thomas head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7924 10604dce 2021-09-24 thomas if (err)
7925 10604dce 2021-09-24 thomas goto done;
7929 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
7930 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
7931 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
7932 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
7933 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7934 10604dce 2021-09-24 thomas if (err)
7935 10604dce 2021-09-24 thomas goto done;
7937 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
7938 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
7939 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7940 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7941 10604dce 2021-09-24 thomas err = sync_err;
7943 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7944 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7946 10604dce 2021-09-24 thomas free_commitable(ct);
7948 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
7949 10604dce 2021-09-24 thomas free(fileindex_path);
7950 10604dce 2021-09-24 thomas return err;
7953 10604dce 2021-09-24 thomas const struct got_error *
7954 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
7955 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
7957 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
7958 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7960 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
7961 10604dce 2021-09-24 thomas if (err)
7962 10604dce 2021-09-24 thomas goto done;
7964 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7965 10604dce 2021-09-24 thomas if (err)
7966 10604dce 2021-09-24 thomas goto done;
7967 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7968 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7969 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7970 10604dce 2021-09-24 thomas err = sync_err;
7972 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7973 10604dce 2021-09-24 thomas free(fileindex_path);
7974 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
7975 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
7976 10604dce 2021-09-24 thomas err = unlockerr;
7977 10604dce 2021-09-24 thomas return err;
7980 10604dce 2021-09-24 thomas const struct got_error *
7981 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7982 10604dce 2021-09-24 thomas struct got_repository *repo)
7984 10604dce 2021-09-24 thomas const struct got_error *err;
7985 10604dce 2021-09-24 thomas char *branch_refname = NULL;
7986 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
7988 10604dce 2021-09-24 thomas *in_progress = 0;
7990 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7991 10604dce 2021-09-24 thomas if (err)
7992 10604dce 2021-09-24 thomas return err;
7993 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7994 dfe86d1f 2021-09-24 thomas free(branch_refname);
7995 10604dce 2021-09-24 thomas if (err) {
7996 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
7997 10604dce 2021-09-24 thomas return err;
7999 10604dce 2021-09-24 thomas *in_progress = 1;
8001 10604dce 2021-09-24 thomas return NULL;
8004 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8005 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8006 10604dce 2021-09-24 thomas struct got_reference *branch, struct got_repository *repo)
8008 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8009 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8010 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8011 10604dce 2021-09-24 thomas struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8012 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL;
8013 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8014 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8016 10604dce 2021-09-24 thomas *fileindex = NULL;
8018 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8019 10604dce 2021-09-24 thomas if (err)
8020 10604dce 2021-09-24 thomas return err;
8022 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8023 10604dce 2021-09-24 thomas if (err)
8024 10604dce 2021-09-24 thomas goto done;
8026 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8027 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8028 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8029 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8030 10604dce 2021-09-24 thomas &ok_arg);
8031 10604dce 2021-09-24 thomas if (err)
8032 10604dce 2021-09-24 thomas goto done;
8034 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8035 10604dce 2021-09-24 thomas if (err)
8036 10604dce 2021-09-24 thomas return err;
8038 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8039 10604dce 2021-09-24 thomas if (err)
8040 10604dce 2021-09-24 thomas return err;
8042 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8044 10604dce 2021-09-24 thomas if (err)
8045 10604dce 2021-09-24 thomas goto done;
8047 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8048 10604dce 2021-09-24 thomas if (err)
8049 10604dce 2021-09-24 thomas goto done;
8051 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8052 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8053 10604dce 2021-09-24 thomas goto done;
8056 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8057 10604dce 2021-09-24 thomas if (err)
8058 10604dce 2021-09-24 thomas goto done;
8060 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8061 10604dce 2021-09-24 thomas if (err)
8062 10604dce 2021-09-24 thomas goto done;
8063 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8064 10604dce 2021-09-24 thomas if (err)
8065 10604dce 2021-09-24 thomas goto done;
8067 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8068 10604dce 2021-09-24 thomas if (err)
8069 10604dce 2021-09-24 thomas goto done;
8070 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8071 10604dce 2021-09-24 thomas if (err)
8072 10604dce 2021-09-24 thomas goto done;
8075 10604dce 2021-09-24 thomas free(branch_refname);
8076 10604dce 2021-09-24 thomas free(commit_refname);
8077 10604dce 2021-09-24 thomas free(fileindex_path);
8078 10604dce 2021-09-24 thomas if (branch_ref)
8079 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8080 10604dce 2021-09-24 thomas if (commit_ref)
8081 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8082 10604dce 2021-09-24 thomas if (wt_branch)
8083 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
8084 10604dce 2021-09-24 thomas free(wt_branch_tip);
8085 10604dce 2021-09-24 thomas if (err) {
8086 10604dce 2021-09-24 thomas if (*fileindex) {
8087 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8088 10604dce 2021-09-24 thomas *fileindex = NULL;
8090 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8092 10604dce 2021-09-24 thomas return err;
8095 10604dce 2021-09-24 thomas const struct got_error *
8096 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8097 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8098 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8100 10604dce 2021-09-24 thomas const struct got_error *err;
8101 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8102 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8103 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8104 10604dce 2021-09-24 thomas int have_staged_files = 0;
8106 10604dce 2021-09-24 thomas *branch_name = NULL;
8107 10604dce 2021-09-24 thomas *branch_tip = NULL;
8108 10604dce 2021-09-24 thomas *fileindex = NULL;
8110 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8111 10604dce 2021-09-24 thomas if (err)
8112 10604dce 2021-09-24 thomas return err;
8114 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8115 10604dce 2021-09-24 thomas if (err)
8116 10604dce 2021-09-24 thomas goto done;
8118 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8119 10604dce 2021-09-24 thomas &have_staged_files);
8120 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8121 10604dce 2021-09-24 thomas goto done;
8122 10604dce 2021-09-24 thomas if (have_staged_files) {
8123 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8124 10604dce 2021-09-24 thomas goto done;
8127 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8128 10604dce 2021-09-24 thomas if (err)
8129 10604dce 2021-09-24 thomas goto done;
8131 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8132 10604dce 2021-09-24 thomas if (err)
8133 10604dce 2021-09-24 thomas goto done;
8135 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8136 10604dce 2021-09-24 thomas if (err)
8137 10604dce 2021-09-24 thomas goto done;
8139 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8140 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8141 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8142 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8143 10604dce 2021-09-24 thomas goto done;
8145 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8146 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8147 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8148 10604dce 2021-09-24 thomas goto done;
8151 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8152 10604dce 2021-09-24 thomas if (err)
8153 10604dce 2021-09-24 thomas goto done;
8155 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8156 10604dce 2021-09-24 thomas if (err)
8157 10604dce 2021-09-24 thomas goto done;
8159 10604dce 2021-09-24 thomas free(commit_refname);
8160 10604dce 2021-09-24 thomas free(branch_refname);
8161 10604dce 2021-09-24 thomas free(fileindex_path);
8162 10604dce 2021-09-24 thomas if (commit_ref)
8163 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8164 10604dce 2021-09-24 thomas if (branch_ref)
8165 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8166 10604dce 2021-09-24 thomas if (err) {
8167 10604dce 2021-09-24 thomas if (*branch_name) {
8168 10604dce 2021-09-24 thomas free(*branch_name);
8169 10604dce 2021-09-24 thomas *branch_name = NULL;
8171 10604dce 2021-09-24 thomas free(*branch_tip);
8172 10604dce 2021-09-24 thomas *branch_tip = NULL;
8173 10604dce 2021-09-24 thomas if (*fileindex) {
8174 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8175 10604dce 2021-09-24 thomas *fileindex = NULL;
8177 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8179 10604dce 2021-09-24 thomas return err;
8182 10604dce 2021-09-24 thomas const struct got_error *
8183 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8184 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8185 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8187 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8188 10604dce 2021-09-24 thomas struct got_object_id *commit_id = NULL;
8189 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8190 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8191 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8192 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8194 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8195 945f9229 2022-04-16 thomas worktree->base_commit_id);
8196 945f9229 2022-04-16 thomas if (err)
8197 945f9229 2022-04-16 thomas goto done;
8199 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8200 945f9229 2022-04-16 thomas worktree->path_prefix);
8201 10604dce 2021-09-24 thomas if (err)
8202 10604dce 2021-09-24 thomas goto done;
8204 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8205 10604dce 2021-09-24 thomas if (err)
8206 10604dce 2021-09-24 thomas goto done;
8208 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8209 10604dce 2021-09-24 thomas if (err)
8210 10604dce 2021-09-24 thomas goto done;
8212 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8213 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8214 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8215 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8216 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8217 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8218 10604dce 2021-09-24 thomas rfa.repo = repo;
8219 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8220 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8221 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8222 10604dce 2021-09-24 thomas if (err)
8223 10604dce 2021-09-24 thomas goto sync;
8225 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8226 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8228 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8229 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8230 10604dce 2021-09-24 thomas err = sync_err;
8232 10604dce 2021-09-24 thomas free(tree_id);
8233 10604dce 2021-09-24 thomas free(commit_id);
8234 945f9229 2022-04-16 thomas if (commit)
8235 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8236 10604dce 2021-09-24 thomas if (fileindex)
8237 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8238 10604dce 2021-09-24 thomas free(fileindex_path);
8240 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8241 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8242 10604dce 2021-09-24 thomas err = unlockerr;
8243 10604dce 2021-09-24 thomas return err;
8246 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8247 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8248 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8249 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8250 2db2652d 2019-08-07 stsp struct got_repository *repo;
8251 2db2652d 2019-08-07 stsp int have_changes;
8254 ef20f542 2022-06-26 thomas static const struct got_error *
8255 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8256 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8257 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8258 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8260 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8261 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8262 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8263 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8264 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8265 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8267 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8268 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8269 8b13ce36 2019-08-08 stsp return NULL;
8270 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8271 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8273 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8274 735ef5ac 2019-08-03 stsp if (ie == NULL)
8275 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8277 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8278 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8279 735ef5ac 2019-08-03 stsp relpath) == -1)
8280 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8282 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8283 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
8284 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8285 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
8288 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8289 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8290 3aa5969e 2019-08-06 stsp goto done;
8291 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8292 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8293 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8294 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8295 735ef5ac 2019-08-03 stsp goto done;
8298 2db2652d 2019-08-07 stsp a->have_changes = 1;
8300 735ef5ac 2019-08-03 stsp p = in_repo_path;
8301 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8303 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8304 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8305 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8307 735ef5ac 2019-08-03 stsp free(in_repo_path);
8308 dc424a06 2019-08-07 stsp return err;
8311 2db2652d 2019-08-07 stsp struct stage_path_arg {
8312 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8313 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8314 2db2652d 2019-08-07 stsp struct got_repository *repo;
8315 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8316 2db2652d 2019-08-07 stsp void *status_arg;
8317 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8318 2db2652d 2019-08-07 stsp void *patch_arg;
8319 7b5dc508 2019-10-28 stsp int staged_something;
8320 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8323 2db2652d 2019-08-07 stsp static const struct got_error *
8324 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8325 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8326 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8327 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8329 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8330 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8331 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8332 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8333 0cb83759 2019-08-03 stsp uint32_t stage;
8334 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8335 0aeb8099 2020-07-23 stsp struct stat sb;
8337 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8338 8b13ce36 2019-08-08 stsp return NULL;
8340 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8341 d3e7c587 2019-08-03 stsp if (ie == NULL)
8342 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8344 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8345 2db2652d 2019-08-07 stsp relpath)== -1)
8346 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8348 0cb83759 2019-08-03 stsp switch (status) {
8349 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8350 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8351 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8352 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8353 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8356 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8357 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8358 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8359 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8360 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8363 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8366 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8367 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8368 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8369 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8370 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8374 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8375 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8378 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8379 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8380 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8381 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8383 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8384 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8385 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8386 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8387 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8388 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8389 35213c7c 2020-07-23 stsp ssize_t target_len;
8390 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8391 35213c7c 2020-07-23 stsp sizeof(target_path));
8392 35213c7c 2020-07-23 stsp if (target_len == -1) {
8393 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8394 35213c7c 2020-07-23 stsp ondisk_path);
8397 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8398 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8399 35213c7c 2020-07-23 stsp a->worktree->root_path);
8402 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8403 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8404 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8408 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8409 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8410 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8412 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8413 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8415 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8416 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8418 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8419 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8421 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8422 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8423 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8424 f289c82c 2022-06-13 thomas if (err)
8427 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8428 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8430 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8431 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8432 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8433 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8436 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8437 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8439 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8440 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8441 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8442 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8445 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8447 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8448 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8452 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8453 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8454 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8455 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8457 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8458 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8461 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8463 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8464 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8466 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8467 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8470 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8474 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8475 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8476 dc424a06 2019-08-07 stsp free(path_content);
8477 2db2652d 2019-08-07 stsp free(ondisk_path);
8478 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8479 0ebf8283 2019-07-24 stsp return err;
8482 0cb83759 2019-08-03 stsp const struct got_error *
8483 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8484 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8485 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8486 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8487 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8489 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8490 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8491 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8492 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8493 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8494 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8495 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8496 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8498 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8500 0cb83759 2019-08-03 stsp return err;
8502 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8503 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8505 735ef5ac 2019-08-03 stsp goto done;
8506 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8508 735ef5ac 2019-08-03 stsp goto done;
8509 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8511 0cb83759 2019-08-03 stsp goto done;
8513 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8514 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8515 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8516 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8517 2db2652d 2019-08-07 stsp oka.repo = repo;
8518 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8519 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8520 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8521 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
8523 735ef5ac 2019-08-03 stsp goto done;
8525 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8526 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8527 2db2652d 2019-08-07 stsp goto done;
8530 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8531 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8532 2db2652d 2019-08-07 stsp spa.repo = repo;
8533 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8534 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8535 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8536 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8537 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8538 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8539 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8540 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8541 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
8543 2db2652d 2019-08-07 stsp goto done;
8545 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8546 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8547 7b5dc508 2019-10-28 stsp goto done;
8550 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8551 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8552 0cb83759 2019-08-03 stsp err = sync_err;
8554 735ef5ac 2019-08-03 stsp if (head_ref)
8555 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8556 735ef5ac 2019-08-03 stsp free(head_commit_id);
8557 0cb83759 2019-08-03 stsp free(fileindex_path);
8558 0cb83759 2019-08-03 stsp if (fileindex)
8559 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8560 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8561 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8562 0cb83759 2019-08-03 stsp err = unlockerr;
8563 0cb83759 2019-08-03 stsp return err;
8566 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8567 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8568 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8569 ad493afc 2019-08-03 stsp struct got_repository *repo;
8570 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8571 ad493afc 2019-08-03 stsp void *progress_arg;
8572 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8573 2e1f37b0 2019-08-08 stsp void *patch_arg;
8576 2e1f37b0 2019-08-08 stsp static const struct got_error *
8577 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8578 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8579 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8580 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8581 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8583 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8584 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8585 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8586 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8587 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8588 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8589 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8590 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8592 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8593 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8595 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8597 2e1f37b0 2019-08-08 stsp return err;
8599 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8600 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8601 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8602 f4ae6ddb 2022-07-01 thomas goto done;
8604 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8605 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8606 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8607 f4ae6ddb 2022-07-01 thomas goto done;
8610 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8612 2e1f37b0 2019-08-08 stsp goto done;
8614 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8616 2e1f37b0 2019-08-08 stsp goto done;
8618 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8620 2e1f37b0 2019-08-08 stsp goto done;
8622 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8625 2e1f37b0 2019-08-08 stsp goto done;
8627 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8629 2e1f37b0 2019-08-08 stsp goto done;
8631 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8633 2e1f37b0 2019-08-08 stsp goto done;
8635 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8636 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8638 2e1f37b0 2019-08-08 stsp goto done;
8640 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8641 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
8643 2e1f37b0 2019-08-08 stsp goto done;
8644 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8645 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
8647 2e1f37b0 2019-08-08 stsp goto done;
8649 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8650 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8651 2e1f37b0 2019-08-08 stsp goto done;
8653 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8654 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8655 2e1f37b0 2019-08-08 stsp goto done;
8657 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8658 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8659 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
8660 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8661 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8662 fe621944 2020-11-10 stsp nchanges++;
8664 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8665 2e1f37b0 2019-08-08 stsp int choice;
8666 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8667 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8668 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8669 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8671 2e1f37b0 2019-08-08 stsp goto done;
8672 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8673 2e1f37b0 2019-08-08 stsp have_content = 1;
8675 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8676 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8679 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8680 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8681 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8683 2e1f37b0 2019-08-08 stsp free(label1);
8684 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8685 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8687 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8688 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8689 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8690 2e1f37b0 2019-08-08 stsp if (staged_blob)
8691 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8692 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8693 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8694 fe621944 2020-11-10 stsp err = free_err;
8695 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8696 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8697 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8698 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8699 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8700 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8701 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8702 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8703 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8704 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8705 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8706 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8707 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8708 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8709 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8710 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8711 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8712 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8713 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8715 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8716 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8717 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8718 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8719 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8720 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8721 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8723 2e1f37b0 2019-08-08 stsp free(path1);
8724 2e1f37b0 2019-08-08 stsp free(path2);
8725 fda8017d 2020-07-23 stsp return err;
8728 fda8017d 2020-07-23 stsp static const struct got_error *
8729 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8730 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8731 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8732 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8733 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8734 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8735 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8737 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8738 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8739 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8740 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8741 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8742 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8743 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8744 fda8017d 2020-07-23 stsp struct stat sb;
8746 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8747 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8748 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8750 fda8017d 2020-07-23 stsp return err;
8752 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8753 fda8017d 2020-07-23 stsp return NULL;
8755 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8756 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8757 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8759 fda8017d 2020-07-23 stsp goto done;
8762 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
8763 fda8017d 2020-07-23 stsp if (f == NULL) {
8764 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8765 fda8017d 2020-07-23 stsp path_unstaged_content);
8766 fda8017d 2020-07-23 stsp goto done;
8768 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8769 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8770 fda8017d 2020-07-23 stsp goto done;
8772 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8773 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8774 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8776 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8777 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8778 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8779 fda8017d 2020-07-23 stsp goto done;
8781 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8782 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8783 fda8017d 2020-07-23 stsp goto done;
8785 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8786 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8787 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8788 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8789 fda8017d 2020-07-23 stsp progress_arg);
8791 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8793 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8795 67a66647 2021-05-31 stsp return err;
8797 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8798 67a66647 2021-05-31 stsp parent) == -1) {
8799 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8800 67a66647 2021-05-31 stsp base_path = NULL;
8801 67a66647 2021-05-31 stsp goto done;
8804 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
8805 fc2a50f2 2022-11-01 thomas base_path, "");
8807 67a66647 2021-05-31 stsp goto done;
8808 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8809 67a66647 2021-05-31 stsp blob_base);
8811 67a66647 2021-05-31 stsp goto done;
8814 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8815 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8817 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8818 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8819 eec2f5a9 2021-06-03 stsp ondisk_path);
8821 eec2f5a9 2021-06-03 stsp goto done;
8824 06340621 2021-12-31 thomas fd = open(ondisk_path,
8825 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8826 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8827 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8828 eec2f5a9 2021-06-03 stsp goto done;
8830 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8831 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8832 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8833 eec2f5a9 2021-06-03 stsp close(fd);
8834 eec2f5a9 2021-06-03 stsp goto done;
8838 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8839 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8840 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8841 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8842 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8845 fda8017d 2020-07-23 stsp goto done;
8847 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8848 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8849 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8851 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8852 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8855 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8856 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8857 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8858 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8859 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8860 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8861 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8862 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8863 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8864 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8865 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8866 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8867 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8868 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8869 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8870 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8871 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8872 67a66647 2021-05-31 stsp free(blob_base_path);
8873 67a66647 2021-05-31 stsp free(parent);
8874 67a66647 2021-05-31 stsp free(base_path);
8875 2e1f37b0 2019-08-08 stsp return err;
8878 ad493afc 2019-08-03 stsp static const struct got_error *
8879 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8880 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8881 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8882 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8884 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8885 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8886 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8887 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8888 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8889 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8890 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8891 9bc94a15 2019-08-03 stsp struct stat sb;
8892 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8894 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8895 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8896 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8897 2e1f37b0 2019-08-08 stsp return NULL;
8899 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8900 ad493afc 2019-08-03 stsp if (ie == NULL)
8901 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8903 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8905 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8907 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8908 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8910 f69721c3 2019-10-21 stsp goto done;
8911 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8912 f69721c3 2019-10-21 stsp id_str) == -1) {
8913 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8914 f4ae6ddb 2022-07-01 thomas goto done;
8917 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8918 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8919 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8920 f4ae6ddb 2022-07-01 thomas goto done;
8922 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8923 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8924 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8925 f69721c3 2019-10-21 stsp goto done;
8928 ad493afc 2019-08-03 stsp switch (staged_status) {
8929 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8930 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8931 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
8934 ad493afc 2019-08-03 stsp /* fall through */
8935 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8936 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8937 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8938 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8939 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8940 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8943 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8946 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8947 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8948 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8949 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8950 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8951 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8954 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8955 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
8958 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8959 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8960 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8961 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8962 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8963 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8964 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8965 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8966 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8968 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8969 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8970 36bf999c 2020-07-23 stsp char *staged_target;
8971 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8972 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8974 36bf999c 2020-07-23 stsp goto done;
8975 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8976 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8977 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8978 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8979 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8980 36bf999c 2020-07-23 stsp free(staged_target);
8982 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8983 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8984 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8985 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8986 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8987 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8988 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8992 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8995 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8996 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8997 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8998 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9001 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9002 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9003 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9004 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9005 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9008 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9010 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9011 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9015 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9016 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9017 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9018 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9021 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9025 ad493afc 2019-08-03 stsp free(ondisk_path);
9026 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9027 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9028 ad493afc 2019-08-03 stsp if (blob_base)
9029 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9030 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9031 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9032 ad493afc 2019-08-03 stsp if (blob_staged)
9033 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9034 f69721c3 2019-10-21 stsp free(id_str);
9035 f69721c3 2019-10-21 stsp free(label_orig);
9036 ad493afc 2019-08-03 stsp return err;
9039 ad493afc 2019-08-03 stsp const struct got_error *
9040 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9041 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9042 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9043 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9044 ad493afc 2019-08-03 stsp struct got_repository *repo)
9046 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9047 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9048 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9049 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9050 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9052 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9054 ad493afc 2019-08-03 stsp return err;
9056 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9058 ad493afc 2019-08-03 stsp goto done;
9060 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9061 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9062 ad493afc 2019-08-03 stsp upa.repo = repo;
9063 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9064 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9065 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9066 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9067 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9068 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9069 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9071 ad493afc 2019-08-03 stsp goto done;
9074 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9075 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9076 ad493afc 2019-08-03 stsp err = sync_err;
9078 ad493afc 2019-08-03 stsp free(fileindex_path);
9079 ad493afc 2019-08-03 stsp if (fileindex)
9080 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9081 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9082 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9083 ad493afc 2019-08-03 stsp err = unlockerr;
9084 ad493afc 2019-08-03 stsp return err;
9087 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9088 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9089 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9090 b2118c49 2020-07-28 stsp void *info_arg;
9091 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9092 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9093 b2118c49 2020-07-28 stsp void *cancel_arg;
9096 b2118c49 2020-07-28 stsp static const struct got_error *
9097 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9099 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9100 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9101 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9102 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9103 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9104 b2118c49 2020-07-28 stsp int stage;
9106 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9107 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9109 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9110 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9111 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9114 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9115 b2118c49 2020-07-28 stsp return NULL;
9117 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
9118 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
9119 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
9121 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9122 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9123 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9124 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
9125 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
9126 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
9129 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
9130 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
9131 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
9134 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9135 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9138 b2118c49 2020-07-28 stsp const struct got_error *
9139 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9140 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9141 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9142 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9145 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9146 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9147 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9148 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9150 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9152 b2118c49 2020-07-28 stsp return err;
9154 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9156 b2118c49 2020-07-28 stsp goto done;
9158 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9159 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9160 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9161 b2118c49 2020-07-28 stsp arg.paths = paths;
9162 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9163 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9164 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9167 b2118c49 2020-07-28 stsp free(fileindex_path);
9168 b2118c49 2020-07-28 stsp if (fileindex)
9169 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9170 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9171 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9172 b2118c49 2020-07-28 stsp err = unlockerr;
9173 b2118c49 2020-07-28 stsp return err;
9176 814624e7 2022-03-22 thomas static const struct got_error *
9177 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9178 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9179 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9181 814624e7 2022-03-22 thomas const struct got_error *err;
9182 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9183 814624e7 2022-03-22 thomas struct stat sb;
9184 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9186 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9187 814624e7 2022-03-22 thomas if (err)
9188 814624e7 2022-03-22 thomas return err;
9190 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9191 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9192 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9194 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9195 814624e7 2022-03-22 thomas if (ie) {
9196 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9197 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9199 814624e7 2022-03-22 thomas if (err)
9200 814624e7 2022-03-22 thomas goto done;
9201 814624e7 2022-03-22 thomas } else {
9202 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9203 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9204 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9205 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9206 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9207 814624e7 2022-03-22 thomas ondisk_path);
9208 814624e7 2022-03-22 thomas goto done;
9210 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9215 814624e7 2022-03-22 thomas free(ondisk_path);
9216 814624e7 2022-03-22 thomas return err;
9219 814624e7 2022-03-22 thomas static const struct got_error *
9220 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
9221 814624e7 2022-03-22 thomas unsigned char staged_status)
9223 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9224 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9225 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9226 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9227 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9228 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9229 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9230 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9231 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9232 814624e7 2022-03-22 thomas return NULL;
9235 814624e7 2022-03-22 thomas static const struct got_error *
9236 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9238 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9239 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9240 814624e7 2022-03-22 thomas return NULL;
9243 814624e7 2022-03-22 thomas static const struct got_error *
9244 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9245 814624e7 2022-03-22 thomas unsigned char staged_status)
9247 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9248 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9249 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9250 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9251 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9252 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9253 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9254 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9255 814624e7 2022-03-22 thomas return NULL;
9258 814624e7 2022-03-22 thomas const struct got_error *
9259 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9260 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9262 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9265 814624e7 2022-03-22 thomas const struct got_error *
9266 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9267 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9268 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9270 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9271 814624e7 2022-03-22 thomas int file_renamed = 0;
9272 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9273 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9275 814624e7 2022-03-22 thomas *oldpath = NULL;
9276 814624e7 2022-03-22 thomas *newpath = NULL;
9278 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9279 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9280 814624e7 2022-03-22 thomas if (err)
9281 814624e7 2022-03-22 thomas goto done;
9283 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9284 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9285 814624e7 2022-03-22 thomas if (err)
9286 814624e7 2022-03-22 thomas goto done;
9288 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9289 814624e7 2022-03-22 thomas file_renamed = 1;
9291 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9292 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9293 814624e7 2022-03-22 thomas else if (file_renamed) {
9294 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9295 814624e7 2022-03-22 thomas if (err == NULL)
9296 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9297 814624e7 2022-03-22 thomas } else if (old == NULL)
9298 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9300 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9303 814624e7 2022-03-22 thomas if (err) {
9304 814624e7 2022-03-22 thomas free(*oldpath);
9305 814624e7 2022-03-22 thomas *oldpath = NULL;
9306 814624e7 2022-03-22 thomas free(*newpath);
9307 814624e7 2022-03-22 thomas *newpath = NULL;
9309 814624e7 2022-03-22 thomas return err;
9312 814624e7 2022-03-22 thomas const struct got_error *
9313 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9314 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9315 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9317 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9319 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9320 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9321 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9322 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9323 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9324 5e22b737 2022-05-31 thomas saa.repo = repo;
9326 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9327 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9330 5e22b737 2022-05-31 thomas const struct got_error *
9331 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9332 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9333 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9335 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9337 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9338 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9339 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9340 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9341 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9342 5e22b737 2022-05-31 thomas sda.repo = repo;
9343 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9344 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9345 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9346 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9348 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9349 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9352 5e22b737 2022-05-31 thomas const struct got_error *
9353 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9354 36832a8e 2022-05-31 thomas const char *fileindex_path)
9356 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9358 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9359 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9361 5e22b737 2022-05-31 thomas return err;