2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 ec22038e 2019-03-10 stsp #include <uuid.h>
36 7154f6ce 2019-03-27 stsp #include <util.h>
38 86c3caaf 2018-03-09 stsp #include "got_error.h"
39 86c3caaf 2018-03-09 stsp #include "got_repository.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 9d31a1d8 2018-03-11 stsp #include "got_object.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 e6209546 2019-08-22 stsp #include "got_cancel.h"
44 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 234035bc 2019-06-01 stsp #include "got_diff.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
55 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
56 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
57 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
61 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 b2b3fce1 2022-10-29 op static mode_t apply_umask(mode_t);
69 99724ed4 2018-03-10 stsp static const struct got_error *
70 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
72 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
75 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
76 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
78 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
83 507dc3bb 2018-12-29 stsp static const struct got_error *
84 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
86 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
87 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
88 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
89 507dc3bb 2018-12-29 stsp char *path = NULL;
91 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
92 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
93 507dc3bb 2018-12-29 stsp path = NULL;
97 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &tmpfile, path, "");
101 507dc3bb 2018-12-29 stsp if (content) {
102 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
103 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
104 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
109 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
110 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
111 2a57020b 2019-02-20 stsp unlink(tmppath);
116 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
117 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
118 230a42bd 2019-05-11 jcs free(tmppath);
119 09fe317a 2018-03-11 stsp return err;
122 024e9686 2019-05-14 stsp static const struct got_error *
123 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
125 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
126 024e9686 2019-05-14 stsp char *refstr = NULL;
128 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
129 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
130 024e9686 2019-05-14 stsp if (refstr == NULL)
131 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
133 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
134 024e9686 2019-05-14 stsp if (refstr == NULL)
135 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
137 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
138 024e9686 2019-05-14 stsp free(refstr);
139 024e9686 2019-05-14 stsp return err;
142 86c3caaf 2018-03-09 stsp const struct got_error *
143 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
144 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
146 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
147 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
148 ec22038e 2019-03-10 stsp uuid_t uuid;
149 ec22038e 2019-03-10 stsp uint32_t uuid_status;
150 65596e15 2018-12-24 stsp int obj_type;
151 7ac97322 2018-03-11 stsp char *path_got = NULL;
152 1451e70d 2018-03-10 stsp char *formatstr = NULL;
153 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
154 65596e15 2018-12-24 stsp char *basestr = NULL;
155 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
157 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
158 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
162 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
164 65596e15 2018-12-24 stsp return err;
165 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
167 65596e15 2018-12-24 stsp return err;
168 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
169 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
171 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
172 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
173 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
176 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
177 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
178 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
182 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
183 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
184 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
187 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
188 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
192 056e7441 2018-03-11 stsp /* Create an empty lock file. */
193 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
197 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
198 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
202 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
203 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
207 65596e15 2018-12-24 stsp /* Record our base commit. */
208 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
211 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
215 1451e70d 2018-03-10 stsp /* Store path to repository. */
216 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
217 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
221 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
222 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
223 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
227 ec22038e 2019-03-10 stsp /* Generate UUID. */
228 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
229 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
230 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
233 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
234 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
235 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
238 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
242 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
243 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
244 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
247 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
252 65596e15 2018-12-24 stsp free(commit_id);
253 7ac97322 2018-03-11 stsp free(path_got);
254 1451e70d 2018-03-10 stsp free(formatstr);
255 0bb8a95e 2018-03-12 stsp free(absprefix);
256 65596e15 2018-12-24 stsp free(basestr);
257 ec22038e 2019-03-10 stsp free(uuidstr);
258 86c3caaf 2018-03-09 stsp return err;
261 247140b2 2019-02-05 stsp const struct got_error *
262 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
263 e5dc7198 2018-12-29 stsp const char *path_prefix)
265 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
267 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
268 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
269 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
271 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
272 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
273 e5dc7198 2018-12-29 stsp free(absprefix);
274 e5dc7198 2018-12-29 stsp return NULL;
277 bc70eb79 2019-05-09 stsp const char *
278 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
280 36a38700 2019-05-10 stsp return worktree->head_ref_name;
283 024e9686 2019-05-14 stsp const struct got_error *
284 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
285 024e9686 2019-05-14 stsp struct got_reference *head_ref)
287 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
288 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
290 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
291 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
292 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
293 024e9686 2019-05-14 stsp path_got = NULL;
297 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
298 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
299 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
303 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
307 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
308 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
310 024e9686 2019-05-14 stsp free(path_got);
312 024e9686 2019-05-14 stsp free(head_ref_name);
313 024e9686 2019-05-14 stsp return err;
316 b72f483a 2019-02-05 stsp struct got_object_id *
317 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
319 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
322 507dc3bb 2018-12-29 stsp const struct got_error *
323 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
324 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
326 507dc3bb 2018-12-29 stsp const struct got_error *err;
327 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
328 507dc3bb 2018-12-29 stsp char *id_str = NULL;
329 507dc3bb 2018-12-29 stsp char *path_got = NULL;
331 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
332 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
333 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
334 507dc3bb 2018-12-29 stsp path_got = NULL;
338 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
340 507dc3bb 2018-12-29 stsp return err;
342 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
343 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
347 507dc3bb 2018-12-29 stsp /* Record our base commit. */
348 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
351 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
355 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
356 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
357 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
358 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
363 507dc3bb 2018-12-29 stsp got_object_close(obj);
364 507dc3bb 2018-12-29 stsp free(id_str);
365 507dc3bb 2018-12-29 stsp free(path_got);
366 507dc3bb 2018-12-29 stsp return err;
369 50b0790e 2020-09-11 stsp const struct got_gotconfig *
370 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
372 50b0790e 2020-09-11 stsp return worktree->gotconfig;
375 9d31a1d8 2018-03-11 stsp static const struct got_error *
376 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
378 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
379 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
380 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
381 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
382 86c3caaf 2018-03-09 stsp return NULL;
385 21908da4 2019-01-13 stsp static const struct got_error *
386 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
388 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
389 4a1ddfc2 2019-01-12 stsp char *abspath;
391 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
392 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
394 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
395 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
396 ddcd8544 2019-03-11 stsp struct stat sb;
397 ddcd8544 2019-03-11 stsp err = NULL;
398 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
399 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
400 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
401 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
402 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
405 4a1ddfc2 2019-01-12 stsp free(abspath);
406 68c76935 2019-02-19 stsp return err;
409 68c76935 2019-02-19 stsp static const struct got_error *
410 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
412 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
413 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
414 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
415 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
420 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
421 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
422 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
425 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
426 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
427 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
430 68c76935 2019-02-19 stsp if (flen1 == 0) {
431 68c76935 2019-02-19 stsp if (flen2 != 0)
434 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
435 68c76935 2019-02-19 stsp if (flen1 != 0)
438 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
439 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
449 68c76935 2019-02-19 stsp return err;
452 68c76935 2019-02-19 stsp static const struct got_error *
453 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
455 68c76935 2019-02-19 stsp struct stat sb;
456 68c76935 2019-02-19 stsp size_t size1, size2;
460 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
461 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
462 68c76935 2019-02-19 stsp size1 = sb.st_size;
464 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
465 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
466 68c76935 2019-02-19 stsp size2 = sb.st_size;
468 68c76935 2019-02-19 stsp if (size1 != size2) {
470 68c76935 2019-02-19 stsp return NULL;
473 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
474 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
475 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
476 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
478 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
481 0e039681 2021-11-15 stsp static const struct got_error *
482 0e039681 2021-11-15 stsp copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
484 0e039681 2021-11-15 stsp uint8_t fbuf[65536];
485 0e039681 2021-11-15 stsp size_t flen;
486 0e039681 2021-11-15 stsp ssize_t outlen;
488 0e039681 2021-11-15 stsp *outsize = 0;
490 0e039681 2021-11-15 stsp if (fseek(f, 0L, SEEK_SET) == -1)
491 0e039681 2021-11-15 stsp return got_ferror(f, GOT_ERR_IO);
494 0e039681 2021-11-15 stsp flen = fread(fbuf, 1, sizeof(fbuf), f);
495 0e039681 2021-11-15 stsp if (flen == 0) {
496 0e039681 2021-11-15 stsp if (ferror(f))
497 0e039681 2021-11-15 stsp return got_error_from_errno("fread");
498 0e039681 2021-11-15 stsp if (feof(f))
501 0e039681 2021-11-15 stsp outlen = write(outfd, fbuf, flen);
502 0e039681 2021-11-15 stsp if (outlen == -1)
503 0e039681 2021-11-15 stsp return got_error_from_errno("write");
504 0e039681 2021-11-15 stsp if (outlen != flen)
505 0e039681 2021-11-15 stsp return got_error(GOT_ERR_IO);
506 0e039681 2021-11-15 stsp *outsize += outlen;
509 0e039681 2021-11-15 stsp return NULL;
512 0e039681 2021-11-15 stsp static const struct got_error *
513 0e039681 2021-11-15 stsp merge_binary_file(int *overlapcnt, int merged_fd,
514 0e039681 2021-11-15 stsp FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
515 0e039681 2021-11-15 stsp const char *label_deriv, const char *label_orig, const char *label_deriv2,
516 0e039681 2021-11-15 stsp const char *ondisk_path)
518 0e039681 2021-11-15 stsp const struct got_error *err = NULL;
519 0e039681 2021-11-15 stsp int same_content, changed_deriv, changed_deriv2;
520 0e039681 2021-11-15 stsp int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
521 0e039681 2021-11-15 stsp off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
522 0e039681 2021-11-15 stsp char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
523 0e039681 2021-11-15 stsp char *base_path_orig = NULL, *base_path_deriv = NULL;
524 0e039681 2021-11-15 stsp char *base_path_deriv2 = NULL;
526 0e039681 2021-11-15 stsp *overlapcnt = 0;
528 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_deriv2);
530 0e039681 2021-11-15 stsp return err;
532 0e039681 2021-11-15 stsp if (same_content)
533 0e039681 2021-11-15 stsp return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
535 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_orig);
537 0e039681 2021-11-15 stsp return err;
538 0e039681 2021-11-15 stsp changed_deriv = !same_content;
539 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv2, f_orig);
541 0e039681 2021-11-15 stsp return err;
542 0e039681 2021-11-15 stsp changed_deriv2 = !same_content;
544 0e039681 2021-11-15 stsp if (changed_deriv && changed_deriv2) {
545 0e039681 2021-11-15 stsp *overlapcnt = 1;
546 0e039681 2021-11-15 stsp if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
547 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
550 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
551 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
554 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
555 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
558 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_orig, &fd_orig,
559 b90054ed 2022-11-01 stsp base_path_orig, "");
562 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
563 b90054ed 2022-11-01 stsp base_path_deriv, "");
566 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
567 b90054ed 2022-11-01 stsp base_path_deriv2, "");
570 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
573 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
576 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
579 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "Binary files differ and cannot be "
580 0e039681 2021-11-15 stsp "merged automatically:\n") < 0) {
581 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
584 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
585 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
586 0e039681 2021-11-15 stsp label_deriv ? " " : "",
587 0e039681 2021-11-15 stsp label_deriv ? label_deriv : "",
588 0e039681 2021-11-15 stsp path_deriv) < 0) {
589 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
592 0e039681 2021-11-15 stsp if (size_orig > 0) {
593 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
594 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_ORIG,
595 0e039681 2021-11-15 stsp label_orig ? " " : "",
596 0e039681 2021-11-15 stsp label_orig ? label_orig : "",
597 0e039681 2021-11-15 stsp path_orig) < 0) {
598 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
602 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
603 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
604 0e039681 2021-11-15 stsp path_deriv2,
605 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_END,
606 0e039681 2021-11-15 stsp label_deriv2 ? " " : "",
607 0e039681 2021-11-15 stsp label_deriv2 ? label_deriv2 : "") < 0) {
608 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
611 0e039681 2021-11-15 stsp } else if (changed_deriv)
612 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
613 0e039681 2021-11-15 stsp else if (changed_deriv2)
614 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
616 0e039681 2021-11-15 stsp if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
617 0e039681 2021-11-15 stsp err == NULL)
618 0e039681 2021-11-15 stsp err = got_error_from_errno2("unlink", path_orig);
619 0e039681 2021-11-15 stsp if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
620 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_orig);
621 0e039681 2021-11-15 stsp if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
622 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv);
623 0e039681 2021-11-15 stsp if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
624 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv2);
625 0e039681 2021-11-15 stsp free(path_orig);
626 0e039681 2021-11-15 stsp free(path_deriv);
627 0e039681 2021-11-15 stsp free(path_deriv2);
628 0e039681 2021-11-15 stsp free(base_path_orig);
629 0e039681 2021-11-15 stsp free(base_path_deriv);
630 0e039681 2021-11-15 stsp free(base_path_deriv2);
631 0e039681 2021-11-15 stsp return err;
635 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
636 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
637 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
638 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
639 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
641 6353ad76 2019-02-08 stsp static const struct got_error *
642 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
643 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
644 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
645 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
646 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
647 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
649 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
650 6353ad76 2019-02-08 stsp int merged_fd = -1;
651 db590691 2021-06-02 stsp FILE *f_merged = NULL;
652 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
653 234035bc 2019-06-01 stsp int overlapcnt = 0;
654 3524bbf9 2020-10-20 stsp char *parent = NULL;
656 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
658 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
660 3524bbf9 2020-10-20 stsp return err;
662 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
663 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
667 b90054ed 2022-11-01 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
671 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
672 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
674 0e039681 2021-11-15 stsp if (err->code != GOT_ERR_FILE_BINARY)
676 0e039681 2021-11-15 stsp err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
677 0e039681 2021-11-15 stsp f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
678 0e039681 2021-11-15 stsp ondisk_path);
683 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
684 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
688 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
693 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
694 db590691 2021-06-02 stsp if (f_merged == NULL) {
695 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
698 db590691 2021-06-02 stsp merged_fd = -1;
700 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
701 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
702 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
708 b2b3fce1 2022-10-29 op if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
709 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
713 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
714 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
715 230a42bd 2019-05-11 jcs ondisk_path);
720 fdcb7daf 2019-12-15 stsp if (merged_path)
721 fdcb7daf 2019-12-15 stsp unlink(merged_path);
723 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
724 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
725 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
726 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
727 6353ad76 2019-02-08 stsp free(merged_path);
728 af54ae4a 2019-02-19 stsp free(base_path);
729 3524bbf9 2020-10-20 stsp free(parent);
730 af57b12a 2020-07-23 stsp return err;
733 af57b12a 2020-07-23 stsp static const struct got_error *
734 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
735 af57b12a 2020-07-23 stsp size_t target_len)
737 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
738 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
739 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
740 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
741 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
742 af57b12a 2020-07-23 stsp ondisk_path);
743 af57b12a 2020-07-23 stsp return NULL;
747 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
748 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
749 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
750 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
752 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
753 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
755 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
756 993e2a1b 2020-07-23 stsp * has deleted this symlink.
758 11cc08c1 2020-07-23 stsp static const struct got_error *
759 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
760 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
761 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
763 11cc08c1 2020-07-23 stsp const struct got_error *err;
764 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
765 11cc08c1 2020-07-23 stsp FILE *f = NULL;
767 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
769 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
771 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
772 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
773 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
777 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
779 3818e3c4 2020-11-01 naddy goto done;
781 b2b3fce1 2022-10-29 op if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
782 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
786 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
787 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
788 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
789 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
790 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
791 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
792 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
793 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
794 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
795 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
799 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
800 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
803 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
804 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
808 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
809 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
810 11cc08c1 2020-07-23 stsp free(path);
811 11cc08c1 2020-07-23 stsp free(id_str);
812 11cc08c1 2020-07-23 stsp free(label_deriv);
813 11cc08c1 2020-07-23 stsp return err;
816 d219f183 2020-07-23 stsp /* forward declaration */
817 d219f183 2020-07-23 stsp static const struct got_error *
818 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
819 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
820 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
821 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
824 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
825 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
826 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
827 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
829 af57b12a 2020-07-23 stsp static const struct got_error *
830 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
831 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
832 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
833 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
834 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
836 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
837 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
838 af57b12a 2020-07-23 stsp struct stat sb;
839 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
840 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
841 11cc08c1 2020-07-23 stsp int have_local_change = 0;
842 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
844 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
845 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
847 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
848 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
849 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
850 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
851 af57b12a 2020-07-23 stsp ondisk_path);
854 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
856 526a746f 2020-07-23 stsp if (blob_orig) {
857 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
862 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
863 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
864 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
865 11cc08c1 2020-07-23 stsp have_local_change = 1;
867 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
868 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
869 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
870 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
871 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
873 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
874 11cc08c1 2020-07-23 stsp if (ancestor_target) {
875 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
876 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
878 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
879 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
880 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
881 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
884 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
885 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
886 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
887 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
890 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
893 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
894 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
895 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
896 af57b12a 2020-07-23 stsp strlen(deriv_target));
899 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
900 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
901 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
902 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
903 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
904 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
907 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
908 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
909 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
912 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
918 af57b12a 2020-07-23 stsp free(ancestor_target);
919 eec2f5a9 2021-06-03 stsp return err;
922 eec2f5a9 2021-06-03 stsp static const struct got_error *
923 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
925 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
926 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
927 eec2f5a9 2021-06-03 stsp ssize_t target_len;
931 eec2f5a9 2021-06-03 stsp *outfile = NULL;
933 eec2f5a9 2021-06-03 stsp f = got_opentemp();
934 eec2f5a9 2021-06-03 stsp if (f == NULL)
935 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
936 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
937 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
938 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
941 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
942 eec2f5a9 2021-06-03 stsp if (n != target_len) {
943 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
946 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
947 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
950 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
951 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
958 eec2f5a9 2021-06-03 stsp *outfile = f;
959 14c901f1 2019-08-08 stsp return err;
963 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
964 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
965 14c901f1 2019-08-08 stsp * acts as the second derived version.
967 14c901f1 2019-08-08 stsp static const struct got_error *
968 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
969 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
970 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
971 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
972 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
973 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
975 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
976 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
977 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
978 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
979 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
981 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
983 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
985 ed6b5030 2020-10-20 stsp return err;
987 67a66647 2021-05-31 stsp if (blob_orig) {
988 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
989 67a66647 2021-05-31 stsp parent) == -1) {
990 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
991 67a66647 2021-05-31 stsp base_path = NULL;
995 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_orig_path, &f_orig,
996 b90054ed 2022-11-01 stsp base_path, "");
999 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1000 67a66647 2021-05-31 stsp blob_orig);
1002 67a66647 2021-05-31 stsp goto done;
1003 67a66647 2021-05-31 stsp free(base_path);
1006 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1007 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1008 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1010 db590691 2021-06-02 stsp f_orig = got_opentemp();
1011 db590691 2021-06-02 stsp if (f_orig == NULL) {
1012 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1013 db590691 2021-06-02 stsp goto done;
1017 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1018 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1019 14c901f1 2019-08-08 stsp base_path = NULL;
1020 14c901f1 2019-08-08 stsp goto done;
1023 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1025 14c901f1 2019-08-08 stsp goto done;
1026 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1027 14c901f1 2019-08-08 stsp blob_deriv);
1029 14c901f1 2019-08-08 stsp goto done;
1031 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1033 14c901f1 2019-08-08 stsp goto done;
1034 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1035 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1036 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1037 14c901f1 2019-08-08 stsp goto done;
1041 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1042 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1044 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1045 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1047 eec2f5a9 2021-06-03 stsp goto done;
1050 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1051 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1052 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1053 eec2f5a9 2021-06-03 stsp goto done;
1055 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1056 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1057 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1058 eec2f5a9 2021-06-03 stsp close(fd);
1059 eec2f5a9 2021-06-03 stsp goto done;
1063 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1064 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1065 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1067 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1068 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1069 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1070 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1071 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1072 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1073 14c901f1 2019-08-08 stsp free(base_path);
1074 67a66647 2021-05-31 stsp if (blob_orig_path) {
1075 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1076 67a66647 2021-05-31 stsp free(blob_orig_path);
1078 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1079 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1080 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1082 6353ad76 2019-02-08 stsp free(id_str);
1083 818c7501 2019-07-11 stsp free(label_deriv);
1084 ed6b5030 2020-10-20 stsp free(parent);
1085 4a1ddfc2 2019-01-12 stsp return err;
1088 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1089 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1090 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1091 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1093 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1094 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1096 65b05cec 2020-07-23 stsp *new_iep = NULL;
1098 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1100 054041d0 2020-06-24 stsp return err;
1102 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1103 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1105 054041d0 2020-06-24 stsp goto done;
1107 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1110 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1112 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1113 13d9040b 2019-03-27 stsp return err;
1116 1ebedb77 2019-10-19 stsp static mode_t
1117 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1119 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1121 1ebedb77 2019-10-19 stsp if (executable) {
1122 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1123 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1124 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1125 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1126 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1127 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1130 b2b3fce1 2022-10-29 op return st_mode;
1133 b2b3fce1 2022-10-29 op static mode_t
1134 b2b3fce1 2022-10-29 op apply_umask(mode_t mode)
1138 b2b3fce1 2022-10-29 op um = umask(000);
1140 b2b3fce1 2022-10-29 op return mode & ~um;
1143 8ba819a3 2020-07-23 stsp /* forward declaration */
1144 13d9040b 2019-03-27 stsp static const struct got_error *
1145 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1146 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1147 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1148 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1149 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1150 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1153 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1154 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1156 8ba819a3 2020-07-23 stsp static const struct got_error *
1157 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1158 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1160 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1161 5a1fbc73 2020-07-23 stsp ssize_t elen;
1162 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1165 c6e8a826 2021-04-05 stsp *did_something = 0;
1168 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1169 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1170 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1171 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1172 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1173 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1175 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1176 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1177 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
1178 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1180 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1181 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1182 5a1fbc73 2020-07-23 stsp if (elen == -1)
1183 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1185 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1186 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1187 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1190 c6e8a826 2021-04-05 stsp *did_something = 1;
1191 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1192 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1193 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1194 5a1fbc73 2020-07-23 stsp return err;
1197 3c1ec782 2020-07-23 stsp static const struct got_error *
1198 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1199 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1201 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1202 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1203 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1205 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1207 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1208 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1209 3c1ec782 2020-07-23 stsp return NULL;
1213 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1214 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1215 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1216 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1217 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1219 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1220 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1221 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1223 ce031e9e 2020-10-20 stsp return err;
1224 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1225 ce031e9e 2020-10-20 stsp free(parent);
1226 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1228 ce031e9e 2020-10-20 stsp free(parent);
1229 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1230 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1231 3c1ec782 2020-07-23 stsp free(abspath);
1232 3c1ec782 2020-07-23 stsp return err;
1234 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1235 3c1ec782 2020-07-23 stsp free(abspath);
1237 3c1ec782 2020-07-23 stsp return err;
1239 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1241 3c1ec782 2020-07-23 stsp return err;
1244 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1245 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1246 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1247 3c1ec782 2020-07-23 stsp return NULL;
1250 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1251 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1252 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1253 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1254 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1255 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1257 3c1ec782 2020-07-23 stsp free(path_got);
1258 3c1ec782 2020-07-23 stsp return NULL;
1261 5a1fbc73 2020-07-23 stsp static const struct got_error *
1262 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1263 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1264 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1265 5267b9e4 2021-09-26 stsp int path_is_unversioned, int allow_bad_symlinks,
1266 5267b9e4 2021-09-26 stsp struct got_repository *repo,
1267 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1269 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1270 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1271 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1272 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1273 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1275 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1278 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1279 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1280 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1281 8ba819a3 2020-07-23 stsp * in the blob object.
1284 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1285 538b6881 2022-07-21 florian if (err)
1286 538b6881 2022-07-21 florian return err;
1288 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1289 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1290 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1291 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1292 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1293 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1294 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1295 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1296 3b9f0f87 2020-07-23 stsp progress_arg);
1298 8ba819a3 2020-07-23 stsp if (len > 0) {
1299 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1300 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1301 8ba819a3 2020-07-23 stsp len - hdrlen);
1302 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1303 8ba819a3 2020-07-23 stsp hdrlen = 0;
1305 8ba819a3 2020-07-23 stsp } while (len != 0);
1306 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1308 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1309 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1311 3c1ec782 2020-07-23 stsp return err;
1313 5267b9e4 2021-09-26 stsp if (*is_bad_symlink && !allow_bad_symlinks) {
1314 906c123b 2020-07-23 stsp /* install as a regular file */
1315 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1316 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1317 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1318 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1319 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1320 3c29341b 2022-07-21 florian return err;
1323 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1324 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1325 c6e8a826 2021-04-05 stsp int symlink_replaced;
1326 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1327 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1328 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1329 3c29341b 2022-07-21 florian return err;
1331 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1332 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1334 3c29341b 2022-07-21 florian return err;
1335 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1336 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1337 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1338 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1339 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1340 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1342 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1343 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1346 3c29341b 2022-07-21 florian return err; /* Nothing else to do. */
1349 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1350 f4994adc 2020-10-20 stsp char *parent;
1351 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1353 3c29341b 2022-07-21 florian return err;
1354 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1355 f4994adc 2020-10-20 stsp free(parent);
1357 3c29341b 2022-07-21 florian return err;
1359 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1360 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1362 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1363 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1364 3c29341b 2022-07-21 florian return err;
1368 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1369 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1370 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1371 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1372 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1373 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1374 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1375 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1376 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1377 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1378 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1379 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1380 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1382 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1383 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1385 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1386 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1387 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1388 8ba819a3 2020-07-23 stsp return err;
1391 8ba819a3 2020-07-23 stsp static const struct got_error *
1392 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1393 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1394 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1395 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1396 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1397 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1399 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1400 507dc3bb 2018-12-29 stsp int fd = -1;
1401 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1402 507dc3bb 2018-12-29 stsp int update = 0;
1403 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1404 b2b3fce1 2022-10-29 op mode_t mode;
1406 b2b3fce1 2022-10-29 op mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1407 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1408 b2b3fce1 2022-10-29 op O_CLOEXEC, mode);
1409 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1410 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1411 f5375317 2020-10-20 stsp char *parent;
1412 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1414 f5375317 2020-10-20 stsp return err;
1415 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1416 f5375317 2020-10-20 stsp free(parent);
1418 21908da4 2019-01-13 stsp return err;
1419 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1420 8bd0cdad 2021-12-31 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1422 21908da4 2019-01-13 stsp if (fd == -1)
1423 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1424 230a42bd 2019-05-11 jcs ondisk_path);
1425 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1426 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1427 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1428 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1429 3b9f0f87 2020-07-23 stsp goto done;
1431 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1432 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1433 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1434 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1435 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1436 507dc3bb 2018-12-29 stsp goto done;
1438 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1439 b90054ed 2022-11-01 stsp ondisk_path, "");
1441 507dc3bb 2018-12-29 stsp goto done;
1442 507dc3bb 2018-12-29 stsp update = 1;
1444 b2b3fce1 2022-10-29 op if (fchmod(fd, apply_umask(mode)) == -1) {
1445 b2b3fce1 2022-10-29 op err = got_error_from_errno2("fchmod",
1451 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1454 bd6aa359 2020-07-23 stsp if (progress_cb) {
1455 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1456 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1458 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1459 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1462 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1463 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1465 bd6aa359 2020-07-23 stsp goto done;
1468 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1470 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1471 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1474 9d31a1d8 2018-03-11 stsp if (len > 0) {
1475 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1476 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1477 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1478 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1479 b87c6f83 2018-12-24 stsp goto done;
1480 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1481 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1482 b87c6f83 2018-12-24 stsp goto done;
1484 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1486 9d31a1d8 2018-03-11 stsp } while (len != 0);
1488 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1489 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1490 816dc654 2019-02-16 stsp goto done;
1493 507dc3bb 2018-12-29 stsp if (update) {
1494 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1495 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1496 f6d8c0ac 2020-11-09 stsp goto done;
1498 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1499 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1500 230a42bd 2019-05-11 jcs ondisk_path);
1501 68ed9ba5 2019-02-10 stsp goto done;
1503 63df146d 2020-11-09 stsp free(tmppath);
1504 63df146d 2020-11-09 stsp tmppath = NULL;
1508 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1509 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1510 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1511 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1512 507dc3bb 2018-12-29 stsp free(tmppath);
1513 6353ad76 2019-02-08 stsp return err;
1517 12383673 2023-02-18 mark * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1518 12383673 2023-02-18 mark * conflict marker is found in newly added lines only.
1520 6353ad76 2019-02-08 stsp static const struct got_error *
1521 12383673 2023-02-18 mark get_modified_file_content_status(unsigned char *status,
1522 12383673 2023-02-18 mark struct got_blob_object *blob, const char *path, struct stat *sb,
1523 12383673 2023-02-18 mark FILE *ondisk_file)
1525 d952957d 2023-02-19 mark const struct got_error *err, *free_err;
1526 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1527 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1528 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1529 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1531 d952957d 2023-02-19 mark FILE *f1 = NULL;
1532 d952957d 2023-02-19 mark struct got_diffreg_result *diffreg_result = NULL;
1533 d952957d 2023-02-19 mark struct diff_result *r;
1534 d952957d 2023-02-19 mark int nchunks_parsed, n, i = 0, ln = 0;
1535 9bdd68dd 2020-01-02 naddy char *line = NULL;
1536 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1537 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1539 d952957d 2023-02-19 mark if (*status != GOT_STATUS_MODIFY)
1540 d952957d 2023-02-19 mark return NULL;
1542 d952957d 2023-02-19 mark f1 = got_opentemp();
1543 d952957d 2023-02-19 mark if (f1 == NULL)
1544 d952957d 2023-02-19 mark return got_error_from_errno("got_opentemp");
1546 d952957d 2023-02-19 mark if (blob) {
1547 d952957d 2023-02-19 mark got_object_blob_rewind(blob);
1548 d952957d 2023-02-19 mark err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1550 12383673 2023-02-18 mark goto done;
1553 d952957d 2023-02-19 mark err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1554 d952957d 2023-02-19 mark 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1556 d952957d 2023-02-19 mark goto done;
1558 d952957d 2023-02-19 mark r = diffreg_result->result;
1560 d952957d 2023-02-19 mark for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1561 d952957d 2023-02-19 mark struct diff_chunk *c;
1562 d952957d 2023-02-19 mark struct diff_chunk_context cc = {};
1563 7783f73c 2023-02-20 mark off_t pos;
1564 d952957d 2023-02-19 mark int clc, crc;
1567 d952957d 2023-02-19 mark * We can optimise a little by advancing straight
1568 d952957d 2023-02-19 mark * to the next chunk if this one has no added lines.
1570 d952957d 2023-02-19 mark c = diff_chunk_get(r, n);
1571 d952957d 2023-02-19 mark clc = diff_chunk_get_left_count(c);
1572 d952957d 2023-02-19 mark crc = diff_chunk_get_right_count(c);
1574 7783f73c 2023-02-20 mark if (!crc || crc == clc) {
1575 d952957d 2023-02-19 mark nchunks_parsed = 1;
1576 7783f73c 2023-02-20 mark continue; /* removed or unchanged lines */
1579 7783f73c 2023-02-20 mark pos = diff_chunk_get_right_start_pos(c);
1580 7783f73c 2023-02-20 mark if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1581 7783f73c 2023-02-20 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1582 7783f73c 2023-02-20 mark goto done;
1585 7783f73c 2023-02-20 mark diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1586 7783f73c 2023-02-20 mark ln = cc.right.start;
1588 d952957d 2023-02-19 mark while (ln < cc.right.end) {
1589 d952957d 2023-02-19 mark linelen = getline(&line, &linesize, ondisk_file);
1590 d952957d 2023-02-19 mark if (linelen == -1) {
1591 d952957d 2023-02-19 mark if (feof(ondisk_file))
1593 d952957d 2023-02-19 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1597 d952957d 2023-02-19 mark if (line && strncmp(line, markers[i],
1598 d952957d 2023-02-19 mark strlen(markers[i])) == 0) {
1599 d952957d 2023-02-19 mark if (strcmp(markers[i],
1600 d952957d 2023-02-19 mark GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1601 d952957d 2023-02-19 mark *status = GOT_STATUS_CONFLICT;
1602 d952957d 2023-02-19 mark goto done;
1611 9bdd68dd 2020-01-02 naddy free(line);
1612 12383673 2023-02-18 mark if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1613 12383673 2023-02-18 mark err = got_error_from_errno("fclose");
1614 d952957d 2023-02-19 mark free_err = got_diffreg_result_free(diffreg_result);
1615 d952957d 2023-02-19 mark if (err == NULL)
1616 d952957d 2023-02-19 mark err = free_err;
1618 7154f6ce 2019-03-27 stsp return err;
1621 e2b1e152 2019-07-13 stsp static int
1622 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1624 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1625 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1628 1ebedb77 2019-10-19 stsp static int
1629 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1631 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1632 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1633 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1634 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1635 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1636 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1639 c363b2c1 2019-08-03 stsp static unsigned char
1640 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1642 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1643 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1644 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1645 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1646 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1647 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1648 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1650 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1654 a919d5c4 2020-07-23 stsp static const struct got_error *
1655 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1656 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1657 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1659 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1660 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1661 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1662 a919d5c4 2020-07-23 stsp ssize_t elen;
1663 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1664 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1665 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1667 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1669 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1671 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1673 a919d5c4 2020-07-23 stsp return err;
1674 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1676 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1677 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1679 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1681 a919d5c4 2020-07-23 stsp if (len > 0) {
1682 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1683 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1684 a919d5c4 2020-07-23 stsp len - hdrlen);
1685 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1686 a919d5c4 2020-07-23 stsp hdrlen = 0;
1688 a919d5c4 2020-07-23 stsp } while (len != 0);
1689 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1691 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1692 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1693 a919d5c4 2020-07-23 stsp if (elen == -1)
1694 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1696 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1697 a919d5c4 2020-07-23 stsp if (elen == -1)
1698 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1701 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1702 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1704 a919d5c4 2020-07-23 stsp return NULL;
1707 7154f6ce 2019-03-27 stsp static const struct got_error *
1708 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1709 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1710 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1712 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1713 6353ad76 2019-02-08 stsp struct got_object_id id;
1714 6353ad76 2019-02-08 stsp size_t hdrlen;
1715 eb81bc23 2022-06-28 tracey int fd = -1, fd1 = -1;
1716 6353ad76 2019-02-08 stsp FILE *f = NULL;
1717 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1718 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1719 6353ad76 2019-02-08 stsp size_t flen, blen;
1720 2c4740ad 2023-02-12 mark unsigned char staged_status;
1722 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
1723 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1724 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1727 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1728 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1729 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1731 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1732 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1733 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1734 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1735 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1737 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1738 882ef1b9 2019-12-13 stsp goto done;
1740 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1741 3d35a492 2019-12-13 stsp goto done;
1744 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1745 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1746 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1747 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1748 5c02d2a5 2021-09-26 stsp else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1749 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1750 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1751 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1752 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1753 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1754 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1756 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1757 3d35a492 2019-12-13 stsp goto done;
1759 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1760 1338848f 2019-12-13 stsp goto done;
1764 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1765 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1766 1338848f 2019-12-13 stsp goto done;
1769 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1770 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1771 1338848f 2019-12-13 stsp goto done;
1772 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1773 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1774 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1775 1338848f 2019-12-13 stsp goto done;
1778 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1779 f179e66d 2020-07-23 stsp goto done;
1781 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1782 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1783 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1784 1338848f 2019-12-13 stsp goto done;
1787 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1788 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1789 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
1791 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
1793 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1794 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1795 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1796 eb81bc23 2022-06-28 tracey goto done;
1798 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1800 1338848f 2019-12-13 stsp goto done;
1802 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1803 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1804 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1805 a919d5c4 2020-07-23 stsp goto done;
1808 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1809 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1810 ab0d4361 2019-12-13 stsp if (fd == -1) {
1811 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1812 ab0d4361 2019-12-13 stsp goto done;
1816 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1817 6353ad76 2019-02-08 stsp if (f == NULL) {
1818 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1819 6353ad76 2019-02-08 stsp goto done;
1822 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1824 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1825 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1827 7154f6ce 2019-03-27 stsp goto done;
1828 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1829 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1830 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1831 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1832 7154f6ce 2019-03-27 stsp goto done;
1834 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1835 6353ad76 2019-02-08 stsp if (flen != 0)
1836 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1838 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1839 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1840 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1842 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1843 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1844 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1845 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1849 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1852 6353ad76 2019-02-08 stsp hdrlen = 0;
1855 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1856 7154f6ce 2019-03-27 stsp rewind(f);
1857 12383673 2023-02-18 mark err = get_modified_file_content_status(status, blob, ie->path,
1859 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1860 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1862 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1863 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1865 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1866 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1867 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1868 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1869 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1870 9d31a1d8 2018-03-11 stsp return err;
1874 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1875 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1877 e2b1e152 2019-07-13 stsp static const struct got_error *
1878 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1879 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1881 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1882 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1883 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1885 e2b1e152 2019-07-13 stsp return NULL;
1888 9d31a1d8 2018-03-11 stsp static const struct got_error *
1889 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1890 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1891 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1892 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1893 0584f854 2019-04-06 stsp void *progress_arg)
1895 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1896 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1897 eb81bc23 2022-06-28 tracey char *ondisk_path = NULL;
1898 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1899 b8f41171 2019-02-10 stsp struct stat sb;
1900 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
1902 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1903 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1906 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1907 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1908 a76c42e6 2019-08-03 stsp goto done;
1910 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1913 abb4604f 2019-07-27 stsp goto done;
1914 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1915 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1917 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1918 f6764181 2021-09-24 stsp if (errno != ENOENT) {
1919 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1920 f6764181 2021-09-24 stsp ondisk_path);
1921 f6764181 2021-09-24 stsp goto done;
1923 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1924 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1926 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1927 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1929 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1933 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1935 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1936 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1937 b8f41171 2019-02-10 stsp goto done;
1939 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1941 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1942 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1944 5036ab18 2020-04-18 stsp goto done;
1947 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1948 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1949 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1951 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1952 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1953 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1954 c6e8a826 2021-04-05 stsp * updating contents of this file.
1956 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1957 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1958 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1959 c6e8a826 2021-04-05 stsp /* Same commit. */
1960 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1961 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1963 e2b1e152 2019-07-13 stsp goto done;
1964 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1966 6353ad76 2019-02-08 stsp goto done;
1968 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1969 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1970 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1971 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1972 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1973 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1975 0f58026f 2021-04-05 stsp goto done;
1976 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1978 b8f41171 2019-02-10 stsp goto done;
1982 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1983 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1984 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1985 eb81bc23 2022-06-28 tracey goto done;
1987 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1989 6353ad76 2019-02-08 stsp goto done;
1991 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1992 234035bc 2019-06-01 stsp int update_timestamps;
1993 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1994 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1995 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1996 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
1997 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
1998 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1999 eb81bc23 2022-06-28 tracey goto done;
2001 234035bc 2019-06-01 stsp struct got_object_id id2;
2002 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id2, ie);
2003 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2006 f69721c3 2019-10-21 stsp goto done;
2008 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2009 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2010 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2011 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2012 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2013 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2014 f69721c3 2019-10-21 stsp goto done;
2016 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2017 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2018 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2019 234035bc 2019-06-01 stsp goto done;
2022 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2023 36bf999c 2020-07-23 stsp char *link_target;
2024 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2026 36bf999c 2020-07-23 stsp goto done;
2027 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2028 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2029 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2030 36bf999c 2020-07-23 stsp free(link_target);
2032 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2033 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2034 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2035 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2037 f69721c3 2019-10-21 stsp free(label_orig);
2038 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2039 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2040 eb81bc23 2022-06-28 tracey goto done;
2042 234035bc 2019-06-01 stsp if (blob2)
2043 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2045 909d120e 2019-09-22 stsp goto done;
2047 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2048 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2049 234035bc 2019-06-01 stsp * unmodified files again.
2051 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2052 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2053 234035bc 2019-06-01 stsp update_timestamps);
2054 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2055 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2056 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2057 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2058 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2060 1ee397ad 2019-07-12 stsp goto done;
2061 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2062 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2064 13d9040b 2019-03-27 stsp goto done;
2066 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2067 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2068 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2069 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2070 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2071 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
2072 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
2074 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2075 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2076 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2077 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2078 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2081 13d9040b 2019-03-27 stsp goto done;
2084 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2085 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2086 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2088 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2089 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2090 054041d0 2020-06-24 stsp &blob->id);
2093 13d9040b 2019-03-27 stsp goto done;
2095 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2096 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2097 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2101 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2102 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2103 eb81bc23 2022-06-28 tracey goto done;
2105 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2107 6353ad76 2019-02-08 stsp free(ondisk_path);
2108 8da9e5f4 2019-01-12 stsp return err;
2111 90285c3b 2019-01-08 stsp static const struct got_error *
2112 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2114 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2115 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2117 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2118 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2120 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2121 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2122 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2124 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2125 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2127 1c4cdd89 2021-06-20 stsp goto done;
2128 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2129 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2130 fddefe3b 2020-10-20 stsp free(ondisk_path);
2131 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2132 1c4cdd89 2021-06-20 stsp parent = NULL;
2133 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2134 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2135 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2136 fddefe3b 2020-10-20 stsp ondisk_path);
2139 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2145 90285c3b 2019-01-08 stsp free(ondisk_path);
2146 1c4cdd89 2021-06-20 stsp free(parent);
2147 90285c3b 2019-01-08 stsp return err;
2150 708d8e67 2019-03-27 stsp static const struct got_error *
2151 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2152 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2153 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2155 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2156 708d8e67 2019-03-27 stsp unsigned char status;
2157 708d8e67 2019-03-27 stsp struct stat sb;
2158 708d8e67 2019-03-27 stsp char *ondisk_path;
2160 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2161 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2163 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2165 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2167 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2169 5a58a424 2020-06-23 stsp goto done;
2171 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2172 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2173 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2174 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2175 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2176 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2177 993e2a1b 2020-07-23 stsp goto done;
2179 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2180 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2181 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2182 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2184 993e2a1b 2020-07-23 stsp goto done;
2185 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2186 993e2a1b 2020-07-23 stsp ie->path);
2187 993e2a1b 2020-07-23 stsp goto done;
2190 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2191 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2192 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2194 5a58a424 2020-06-23 stsp goto done;
2196 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2197 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2199 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2200 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2202 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2204 5a58a424 2020-06-23 stsp goto done;
2205 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2206 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2208 5a58a424 2020-06-23 stsp goto done;
2210 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2213 5a58a424 2020-06-23 stsp free(ondisk_path);
2214 fc6346c4 2019-03-27 stsp return err;
2217 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2218 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2219 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2220 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2221 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2222 8da9e5f4 2019-01-12 stsp void *progress_arg;
2223 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2224 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2227 512f0d0e 2019-01-02 stsp static const struct got_error *
2228 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2229 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2231 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2233 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2234 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2236 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2237 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2240 8da9e5f4 2019-01-12 stsp static const struct got_error *
2241 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2243 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2245 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2246 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2248 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2249 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2252 9d31a1d8 2018-03-11 stsp static const struct got_error *
2253 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2255 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2256 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2257 8da9e5f4 2019-01-12 stsp char *path;
2259 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2260 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2262 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2263 63c5ca5d 2019-08-24 stsp return NULL;
2265 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2266 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2268 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2270 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2271 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2273 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2274 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2276 8da9e5f4 2019-01-12 stsp free(path);
2277 0cd1c46a 2019-03-11 stsp return err;
2280 b2118c49 2020-07-28 stsp const struct got_error *
2281 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2283 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2285 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2286 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2287 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2288 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2291 b2118c49 2020-07-28 stsp return NULL;
2294 818c7501 2019-07-11 stsp static const struct got_error *
2295 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2297 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2298 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2300 517bab73 2019-03-11 stsp *refname = NULL;
2302 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2304 b2118c49 2020-07-28 stsp return err;
2306 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2307 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2308 0647c563 2019-03-11 stsp *refname = NULL;
2310 517bab73 2019-03-11 stsp free(uuidstr);
2311 517bab73 2019-03-11 stsp return err;
2314 818c7501 2019-07-11 stsp const struct got_error *
2315 9587e6cc 2023-01-28 mark got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2316 9587e6cc 2023-01-28 mark const char *prefix)
2318 9587e6cc 2023-01-28 mark return get_ref_name(refname, worktree, prefix);
2321 9587e6cc 2023-01-28 mark const struct got_error *
2322 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2324 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2327 818c7501 2019-07-11 stsp static const struct got_error *
2328 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2330 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2331 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2334 818c7501 2019-07-11 stsp static const struct got_error *
2335 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2337 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2340 818c7501 2019-07-11 stsp static const struct got_error *
2341 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2343 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2344 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2347 818c7501 2019-07-11 stsp static const struct got_error *
2348 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2350 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2351 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2354 0ebf8283 2019-07-24 stsp static const struct got_error *
2355 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2357 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2358 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2361 0ebf8283 2019-07-24 stsp static const struct got_error *
2362 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2364 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2365 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2368 0ebf8283 2019-07-24 stsp static const struct got_error *
2369 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2371 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2372 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2375 0ebf8283 2019-07-24 stsp static const struct got_error *
2376 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2378 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2379 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2382 0ebf8283 2019-07-24 stsp const struct got_error *
2383 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2384 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2386 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2387 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2388 0ebf8283 2019-07-24 stsp *path = NULL;
2389 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2391 0ebf8283 2019-07-24 stsp return NULL;
2394 f259c4c1 2021-09-24 stsp static const struct got_error *
2395 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2397 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2398 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2401 f259c4c1 2021-09-24 stsp static const struct got_error *
2402 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2404 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2405 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2409 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2410 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2412 517bab73 2019-03-11 stsp static const struct got_error *
2413 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2415 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2416 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2417 517bab73 2019-03-11 stsp char *refname;
2419 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2421 517bab73 2019-03-11 stsp return err;
2423 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2425 0cd1c46a 2019-03-11 stsp goto done;
2427 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2429 0cd1c46a 2019-03-11 stsp free(refname);
2431 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2432 3e3a69f1 2019-07-25 stsp return err;
2435 3e3a69f1 2019-07-25 stsp static const struct got_error *
2436 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2438 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2440 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2441 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2442 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2443 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2445 9d31a1d8 2018-03-11 stsp return err;
2449 ebf99748 2019-05-09 stsp static const struct got_error *
2450 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2451 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2453 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2454 ebf99748 2019-05-09 stsp FILE *index = NULL;
2456 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2457 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2458 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2459 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2461 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2463 ebf99748 2019-05-09 stsp goto done;
2465 00fe21f2 2021-12-31 stsp index = fopen(*fileindex_path, "rbe");
2466 ebf99748 2019-05-09 stsp if (index == NULL) {
2467 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2468 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2470 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2471 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2472 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2475 ebf99748 2019-05-09 stsp if (err) {
2476 ebf99748 2019-05-09 stsp free(*fileindex_path);
2477 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2478 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2479 ebf99748 2019-05-09 stsp *fileindex = NULL;
2481 ebf99748 2019-05-09 stsp return err;
2484 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2485 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2486 c932eeeb 2019-05-22 stsp const char *path;
2487 c932eeeb 2019-05-22 stsp size_t path_len;
2488 c932eeeb 2019-05-22 stsp const char *entry_name;
2489 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2490 a484d721 2019-06-10 stsp void *progress_arg;
2493 c932eeeb 2019-05-22 stsp static const struct got_error *
2494 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2496 1ee397ad 2019-07-12 stsp const struct got_error *err;
2497 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2499 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2500 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2501 c932eeeb 2019-05-22 stsp return NULL;
2502 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2503 102ff934 2019-06-11 stsp return NULL;
2505 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2506 a769b60b 2021-06-27 stsp return NULL;
2508 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2509 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2510 c932eeeb 2019-05-22 stsp return NULL;
2512 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2513 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2514 edd02c5e 2019-07-11 stsp ie->path);
2516 1ee397ad 2019-07-12 stsp return err;
2518 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2519 c932eeeb 2019-05-22 stsp return NULL;
2522 b0a0c9ed 2023-02-06 op /* Bump base commit ID of all files within an updated part of the work tree. */
2523 a615e0e7 2020-12-16 stsp static const struct got_error *
2524 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2525 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2526 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2528 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2530 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2531 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2532 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2533 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2534 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2535 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2537 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2538 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2541 9c6338c4 2019-06-02 stsp static const struct got_error *
2542 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2544 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2545 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2546 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2547 867630bb 2020-01-17 stsp struct timespec timeout;
2549 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2550 b90054ed 2022-11-01 stsp fileindex_path, "");
2552 9c6338c4 2019-06-02 stsp goto done;
2554 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2556 9c6338c4 2019-06-02 stsp goto done;
2558 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2559 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2560 9c6338c4 2019-06-02 stsp fileindex_path);
2561 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2565 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2566 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2567 867630bb 2020-01-17 stsp * was recorded in the file index.
2569 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2570 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2571 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2573 9c6338c4 2019-06-02 stsp if (new_index)
2574 9c6338c4 2019-06-02 stsp fclose(new_index);
2575 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2576 9c6338c4 2019-06-02 stsp return err;
2579 6ced4176 2019-07-12 stsp static const struct got_error *
2580 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2581 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2582 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit, struct got_worktree *worktree,
2583 a44927cc 2022-04-07 stsp struct got_repository *repo)
2585 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2586 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2587 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2588 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2590 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2591 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2592 6ced4176 2019-07-12 stsp *tree_id = NULL;
2594 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2595 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2596 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2597 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2598 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2599 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2600 6ced4176 2019-07-12 stsp goto done;
2602 a44927cc 2022-04-07 stsp err = got_object_id_by_path(tree_id, repo, base_commit,
2603 a44927cc 2022-04-07 stsp worktree->path_prefix);
2605 6ced4176 2019-07-12 stsp goto done;
2606 6ced4176 2019-07-12 stsp return NULL;
2609 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2611 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2612 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2613 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2614 6ced4176 2019-07-12 stsp goto done;
2617 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2619 6ced4176 2019-07-12 stsp goto done;
2621 6ced4176 2019-07-12 stsp free(in_repo_path);
2622 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2624 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2626 6ced4176 2019-07-12 stsp goto done;
2628 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2629 6ced4176 2019-07-12 stsp /* Check out a single file. */
2630 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2631 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2632 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2633 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2634 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2635 6ced4176 2019-07-12 stsp goto done;
2637 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2638 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2639 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2640 6ced4176 2019-07-12 stsp goto done;
2643 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2644 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2646 6ced4176 2019-07-12 stsp return err;
2647 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2648 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2649 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2650 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2651 6ced4176 2019-07-12 stsp goto done;
2654 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2655 a44927cc 2022-04-07 stsp base_commit, in_repo_path);
2657 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2658 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2659 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2660 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2661 6ced4176 2019-07-12 stsp goto done;
2663 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2664 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2665 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2666 6ced4176 2019-07-12 stsp goto done;
2671 6ced4176 2019-07-12 stsp free(in_repo_path);
2672 6ced4176 2019-07-12 stsp if (err) {
2673 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2674 6ced4176 2019-07-12 stsp free(*tree_relpath);
2675 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2676 6ced4176 2019-07-12 stsp free(*tree_id);
2677 6ced4176 2019-07-12 stsp *tree_id = NULL;
2679 07ed472d 2019-07-12 stsp return err;
2682 07ed472d 2019-07-12 stsp static const struct got_error *
2683 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2684 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2685 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2686 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2688 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2689 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2690 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2691 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2692 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2694 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2695 7f47418f 2019-12-20 stsp if (err) {
2696 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2697 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2698 7f47418f 2019-12-20 stsp goto done;
2699 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2700 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2702 7f47418f 2019-12-20 stsp return err;
2705 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2706 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2708 07ed472d 2019-07-12 stsp goto done;
2710 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2712 07ed472d 2019-07-12 stsp goto done;
2714 07ed472d 2019-07-12 stsp if (entry_name &&
2715 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2716 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2717 07ed472d 2019-07-12 stsp goto done;
2720 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2721 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2722 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2723 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2724 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2725 07ed472d 2019-07-12 stsp arg.repo = repo;
2726 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2727 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2728 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2729 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2730 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2731 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2734 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2735 07ed472d 2019-07-12 stsp if (commit)
2736 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2737 6ced4176 2019-07-12 stsp return err;
2740 9d31a1d8 2018-03-11 stsp const struct got_error *
2741 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2742 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2743 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2744 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2746 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2747 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2748 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2749 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2750 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2751 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2752 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2753 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2754 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2755 f2ea84fa 2019-07-27 stsp int entry_type;
2756 f2ea84fa 2019-07-27 stsp char *relpath;
2757 f2ea84fa 2019-07-27 stsp char *entry_name;
2758 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2759 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2761 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2763 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2765 9d31a1d8 2018-03-11 stsp return err;
2767 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
2768 a44927cc 2022-04-07 stsp worktree->base_commit_id);
2770 a44927cc 2022-04-07 stsp goto done;
2772 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2773 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2774 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2775 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2776 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2777 f2ea84fa 2019-07-27 stsp goto done;
2780 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2781 a44927cc 2022-04-07 stsp &tpd->relpath, &tpd->tree_id, pe->path, commit,
2782 a44927cc 2022-04-07 stsp worktree, repo);
2783 f2ea84fa 2019-07-27 stsp if (err) {
2784 f2ea84fa 2019-07-27 stsp free(tpd);
2785 6ced4176 2019-07-12 stsp goto done;
2788 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2789 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2790 f2ea84fa 2019-07-27 stsp if (err) {
2791 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2792 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2793 f2ea84fa 2019-07-27 stsp free(tpd);
2794 f2ea84fa 2019-07-27 stsp goto done;
2797 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2799 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2803 51514078 2018-12-25 stsp * Read the file index.
2804 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2805 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2807 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2809 c4cdcb68 2019-04-03 stsp goto done;
2811 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2812 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2813 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2815 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2816 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2817 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2821 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2822 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2823 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2824 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2825 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2826 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2827 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2828 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2832 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2834 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2835 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2836 af12c6b9 2019-06-04 stsp err = sync_err;
2838 9c6338c4 2019-06-02 stsp free(fileindex_path);
2840 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2841 9d31a1d8 2018-03-11 stsp if (commit)
2842 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2843 5ade8233 2019-07-12 stsp if (fileindex)
2844 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2845 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2846 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2847 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2848 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2849 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2850 f2ea84fa 2019-07-27 stsp free(tpd);
2852 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2853 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2854 9d31a1d8 2018-03-11 stsp err = unlockerr;
2855 f8d1f275 2019-02-04 stsp return err;
2858 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2859 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2860 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2861 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2862 234035bc 2019-06-01 stsp void *progress_arg;
2863 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2864 234035bc 2019-06-01 stsp void *cancel_arg;
2865 f69721c3 2019-10-21 stsp const char *label_orig;
2866 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2867 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2870 234035bc 2019-06-01 stsp static const struct got_error *
2871 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2872 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
2873 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
2874 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
2875 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2877 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2878 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2879 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2880 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2881 234035bc 2019-06-01 stsp struct stat sb;
2882 234035bc 2019-06-01 stsp unsigned char status;
2883 234035bc 2019-06-01 stsp int local_changes_subsumed;
2884 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2885 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2887 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2888 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2889 d6c87207 2019-08-02 stsp strlen(path2));
2890 1ee397ad 2019-07-12 stsp if (ie == NULL)
2891 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2892 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2894 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2895 234035bc 2019-06-01 stsp path2) == -1)
2896 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2898 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2901 234035bc 2019-06-01 stsp goto done;
2903 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2904 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2905 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2906 234035bc 2019-06-01 stsp goto done;
2908 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2909 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2910 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2911 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2912 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2913 234035bc 2019-06-01 stsp goto done;
2916 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2917 36bf999c 2020-07-23 stsp char *link_target2;
2918 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2920 36bf999c 2020-07-23 stsp goto done;
2921 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2922 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2923 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2924 36bf999c 2020-07-23 stsp free(link_target2);
2928 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2929 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2930 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2931 1af628f4 2021-06-03 stsp goto done;
2933 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2934 1af628f4 2021-06-03 stsp f_orig, blob1);
2936 1af628f4 2021-06-03 stsp goto done;
2938 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2939 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2940 1af628f4 2021-06-03 stsp goto done;
2941 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2942 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2944 1af628f4 2021-06-03 stsp goto done;
2946 c0df5966 2021-12-31 stsp fd = open(ondisk_path,
2947 c0df5966 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2948 1af628f4 2021-06-03 stsp if (fd == -1) {
2949 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2950 1af628f4 2021-06-03 stsp ondisk_path);
2951 1af628f4 2021-06-03 stsp goto done;
2953 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2954 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2955 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2956 1af628f4 2021-06-03 stsp ondisk_path);
2957 1af628f4 2021-06-03 stsp close(fd);
2958 1af628f4 2021-06-03 stsp goto done;
2960 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2962 1af628f4 2021-06-03 stsp goto done;
2963 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2964 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2965 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2966 1af628f4 2021-06-03 stsp goto done;
2968 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2969 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2970 c48f94a4 2023-01-29 stsp mode2, a->label_orig, NULL, label_deriv2,
2971 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2972 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2974 234035bc 2019-06-01 stsp } else if (blob1) {
2975 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2976 d6c87207 2019-08-02 stsp strlen(path1));
2977 1ee397ad 2019-07-12 stsp if (ie == NULL)
2978 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2979 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2981 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2982 2b92fad7 2019-06-02 stsp path1) == -1)
2983 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2985 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2988 2b92fad7 2019-06-02 stsp goto done;
2990 2b92fad7 2019-06-02 stsp switch (status) {
2991 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2992 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2993 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2995 1ee397ad 2019-07-12 stsp goto done;
2996 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2998 2b92fad7 2019-06-02 stsp goto done;
3000 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3002 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3003 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3004 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3005 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3007 1ee397ad 2019-07-12 stsp goto done;
3009 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3011 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3012 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3013 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3014 72840534 2022-01-19 stsp off_t blob1_size;
3016 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3017 0a22ca1a 2020-09-23 stsp * exists in the repository.
3019 72840534 2022-01-19 stsp err = got_object_blob_file_create(&id, &blob1_f,
3020 72840534 2022-01-19 stsp &blob1_size, path1);
3022 0a22ca1a 2020-09-23 stsp goto done;
3023 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3024 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3025 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3027 0a22ca1a 2020-09-23 stsp goto done;
3028 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3031 0a22ca1a 2020-09-23 stsp goto done;
3033 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3036 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3037 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3039 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3040 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3043 0a22ca1a 2020-09-23 stsp goto done;
3046 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3047 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3048 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3049 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3051 1ee397ad 2019-07-12 stsp goto done;
3053 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3054 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3056 1ee397ad 2019-07-12 stsp goto done;
3061 234035bc 2019-06-01 stsp } else if (blob2) {
3062 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3063 234035bc 2019-06-01 stsp path2) == -1)
3064 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3065 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3066 d6c87207 2019-08-02 stsp strlen(path2));
3068 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3069 7f91a133 2019-12-13 stsp -1, NULL, repo);
3071 234035bc 2019-06-01 stsp goto done;
3072 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3073 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3074 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3075 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3076 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3077 1ee397ad 2019-07-12 stsp status, path2);
3078 234035bc 2019-06-01 stsp goto done;
3080 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3081 36bf999c 2020-07-23 stsp char *link_target2;
3082 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3085 36bf999c 2020-07-23 stsp goto done;
3086 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3087 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3088 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3089 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3090 36bf999c 2020-07-23 stsp free(link_target2);
3091 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3092 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3093 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3094 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3095 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3096 526a746f 2020-07-23 stsp a->progress_arg);
3098 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3099 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3102 dfe9fba0 2020-07-23 stsp goto done;
3103 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3104 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3105 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3106 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3108 234035bc 2019-06-01 stsp goto done;
3111 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3112 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3113 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3114 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3115 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3116 5267b9e4 2021-09-26 stsp 0, 1, a->allow_bad_symlinks, repo,
3117 5267b9e4 2021-09-26 stsp a->progress_cb, a->progress_arg);
3119 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3120 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3121 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3124 234035bc 2019-06-01 stsp goto done;
3125 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3127 234035bc 2019-06-01 stsp goto done;
3128 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3129 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3130 3969253a 2020-03-07 stsp if (err) {
3131 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3132 3969253a 2020-03-07 stsp goto done;
3134 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3135 2b92fad7 2019-06-02 stsp if (err) {
3136 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3137 2b92fad7 2019-06-02 stsp goto done;
3139 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3140 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3141 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3146 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3147 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3148 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3149 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3150 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3151 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3152 1af628f4 2021-06-03 stsp free(id_str);
3153 54d5be07 2021-06-03 stsp free(label_deriv2);
3154 234035bc 2019-06-01 stsp free(ondisk_path);
3155 234035bc 2019-06-01 stsp return err;
3158 69de9dd4 2021-09-03 stsp static const struct got_error *
3159 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3161 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3163 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3164 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3165 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3166 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3167 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3169 69de9dd4 2021-09-03 stsp return NULL;
3172 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3173 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3174 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3175 234035bc 2019-06-01 stsp struct got_repository *repo;
3178 234035bc 2019-06-01 stsp static const struct got_error *
3179 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3180 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3181 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3182 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3183 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3185 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3186 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3187 234035bc 2019-06-01 stsp unsigned char status;
3188 234035bc 2019-06-01 stsp struct stat sb;
3189 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3190 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3191 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3192 234035bc 2019-06-01 stsp char *ondisk_path;
3194 69de9dd4 2021-09-03 stsp if (id == NULL)
3195 69de9dd4 2021-09-03 stsp return NULL;
3197 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3198 69de9dd4 2021-09-03 stsp if (ie == NULL)
3199 69de9dd4 2021-09-03 stsp return NULL;
3201 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3203 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3205 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3206 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3207 5546d466 2021-09-02 stsp free(ondisk_path);
3209 234035bc 2019-06-01 stsp return err;
3210 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3211 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3213 234035bc 2019-06-01 stsp return NULL;
3216 818c7501 2019-07-11 stsp static const struct got_error *
3217 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3218 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3219 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3220 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3221 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3223 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3224 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3225 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3226 a44927cc 2022-04-07 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3227 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3228 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3229 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3230 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
3231 f9d37699 2022-06-28 stsp int fd1 = -1, fd2 = -1;
3233 03415a1a 2019-06-02 stsp if (commit_id1) {
3234 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit1, repo, commit_id1);
3236 a44927cc 2022-04-07 stsp goto done;
3237 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id1, repo, commit1,
3238 03415a1a 2019-06-02 stsp worktree->path_prefix);
3239 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3240 03415a1a 2019-06-02 stsp goto done;
3242 69d57f3d 2020-07-31 stsp if (tree_id1) {
3243 69d57f3d 2020-07-31 stsp char *id_str;
3245 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3247 03415a1a 2019-06-02 stsp goto done;
3249 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3251 f69721c3 2019-10-21 stsp goto done;
3253 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3254 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3255 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3256 f69721c3 2019-10-21 stsp free(id_str);
3257 f69721c3 2019-10-21 stsp goto done;
3259 f69721c3 2019-10-21 stsp free(id_str);
3261 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3262 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3263 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3264 b72706c3 2022-06-01 stsp goto done;
3268 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit2, repo, commit_id2);
3270 a44927cc 2022-04-07 stsp goto done;
3272 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id2, repo, commit2,
3273 234035bc 2019-06-01 stsp worktree->path_prefix);
3275 234035bc 2019-06-01 stsp goto done;
3277 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3279 234035bc 2019-06-01 stsp goto done;
3281 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3282 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3283 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3284 f9d37699 2022-06-28 stsp goto done;
3287 f9d37699 2022-06-28 stsp fd1 = got_opentempfd();
3288 f9d37699 2022-06-28 stsp if (fd1 == -1) {
3289 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3290 b72706c3 2022-06-01 stsp goto done;
3293 f9d37699 2022-06-28 stsp fd2 = got_opentempfd();
3294 f9d37699 2022-06-28 stsp if (fd2 == -1) {
3295 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3296 f9d37699 2022-06-28 stsp goto done;
3299 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3300 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3301 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3302 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3303 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3305 69de9dd4 2021-09-03 stsp goto done;
3307 234035bc 2019-06-01 stsp arg.worktree = worktree;
3308 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3309 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3310 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3311 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3312 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3313 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3314 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3315 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3316 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3317 b72706c3 2022-06-01 stsp merge_file_cb, &arg, 1);
3318 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3319 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3320 af12c6b9 2019-06-04 stsp err = sync_err;
3322 a44927cc 2022-04-07 stsp if (commit1)
3323 a44927cc 2022-04-07 stsp got_object_commit_close(commit1);
3324 a44927cc 2022-04-07 stsp if (commit2)
3325 a44927cc 2022-04-07 stsp got_object_commit_close(commit2);
3326 234035bc 2019-06-01 stsp if (tree1)
3327 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3328 234035bc 2019-06-01 stsp if (tree2)
3329 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3330 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3331 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3332 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3333 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3334 f9d37699 2022-06-28 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3335 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3336 f9d37699 2022-06-28 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3337 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3338 f69721c3 2019-10-21 stsp free(label_orig);
3339 818c7501 2019-07-11 stsp return err;
3342 818c7501 2019-07-11 stsp const struct got_error *
3343 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3344 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3345 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3346 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3348 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3349 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3350 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3352 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3354 818c7501 2019-07-11 stsp return err;
3356 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3358 818c7501 2019-07-11 stsp goto done;
3360 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3361 69de9dd4 2021-09-03 stsp worktree);
3363 818c7501 2019-07-11 stsp goto done;
3365 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3366 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3367 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3369 818c7501 2019-07-11 stsp if (fileindex)
3370 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3371 818c7501 2019-07-11 stsp free(fileindex_path);
3372 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3373 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3374 234035bc 2019-06-01 stsp err = unlockerr;
3375 234035bc 2019-06-01 stsp return err;
3378 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3379 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3380 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3381 927df6b7 2019-02-10 stsp const char *status_path;
3382 927df6b7 2019-02-10 stsp size_t status_path_len;
3383 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3384 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3385 f8d1f275 2019-02-04 stsp void *status_arg;
3386 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3387 f8d1f275 2019-02-04 stsp void *cancel_arg;
3388 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3389 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3390 f2a9dc41 2019-12-13 tracey int report_unchanged;
3391 3143d852 2020-06-25 stsp int no_ignores;
3394 f8d1f275 2019-02-04 stsp static const struct got_error *
3395 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3396 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3397 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3398 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3400 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3401 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3402 2c4740ad 2023-02-12 mark unsigned char staged_status;
3403 927df6b7 2019-02-10 stsp struct stat sb;
3404 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3405 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3406 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3408 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
3409 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3411 98eaaa12 2019-08-03 stsp return err;
3413 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3414 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3415 98eaaa12 2019-08-03 stsp return NULL;
3417 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
3418 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3419 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
3420 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3421 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3422 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3423 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3424 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
3427 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3428 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3431 927df6b7 2019-02-10 stsp static const struct got_error *
3432 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3433 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3435 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3436 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3437 f8d1f275 2019-02-04 stsp char *abspath;
3439 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3440 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3442 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3443 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3444 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3445 927df6b7 2019-02-10 stsp return NULL;
3447 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3448 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3449 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3450 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3452 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3453 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3454 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3457 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3458 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3459 f8d1f275 2019-02-04 stsp free(abspath);
3460 9d31a1d8 2018-03-11 stsp return err;
3463 f8d1f275 2019-02-04 stsp static const struct got_error *
3464 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3466 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3467 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3468 2ec1f75b 2019-03-26 stsp unsigned char status;
3470 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3471 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3473 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3474 927df6b7 2019-02-10 stsp return NULL;
3476 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&blob_id, ie);
3477 b4b2adf5 2023-02-09 op got_fileindex_entry_get_commit_id(&commit_id, ie);
3478 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3479 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3481 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3482 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3483 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3487 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3489 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3491 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3492 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3494 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3496 d8bacb93 2023-01-10 mark got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3499 6841da00 2019-08-08 stsp static const struct got_error *
3500 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3502 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3503 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3504 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3505 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3506 6841da00 2019-08-08 stsp size_t linesize = 0;
3507 6841da00 2019-08-08 stsp ssize_t linelen;
3509 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3510 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3511 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3512 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3514 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3515 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3516 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3518 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3519 bd8de430 2019-10-04 stsp if (line[0] == '#')
3522 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3523 bd8de430 2019-10-04 stsp if (line[0] == '!')
3526 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3527 6841da00 2019-08-08 stsp line) == -1) {
3528 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3529 6841da00 2019-08-08 stsp goto done;
3531 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3533 6841da00 2019-08-08 stsp goto done;
3535 6841da00 2019-08-08 stsp if (ferror(f)) {
3536 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3537 6841da00 2019-08-08 stsp goto done;
3540 6841da00 2019-08-08 stsp dirpath = strdup(path);
3541 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3542 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3543 6841da00 2019-08-08 stsp goto done;
3545 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3547 6841da00 2019-08-08 stsp free(line);
3548 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3549 6841da00 2019-08-08 stsp free(dirpath);
3550 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3552 6841da00 2019-08-08 stsp return err;
3555 249b637c 2023-02-20 stsp static int
3556 249b637c 2023-02-20 stsp match_path(const char *pattern, size_t pattern_len, const char *path,
3557 249b637c 2023-02-20 stsp int flags)
3559 249b637c 2023-02-20 stsp char buf[PATH_MAX];
3562 249b637c 2023-02-20 stsp * Trailing slashes signify directories.
3563 249b637c 2023-02-20 stsp * Append a * to make such patterns conform to fnmatch rules.
3565 249b637c 2023-02-20 stsp if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3566 249b637c 2023-02-20 stsp if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3567 249b637c 2023-02-20 stsp return FNM_NOMATCH; /* XXX */
3569 249b637c 2023-02-20 stsp return fnmatch(buf, path, flags);
3572 249b637c 2023-02-20 stsp return fnmatch(pattern, path, flags);
3576 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3578 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3580 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3581 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3582 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3583 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3585 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3586 249b637c 2023-02-20 stsp const char *p;
3588 249b637c 2023-02-20 stsp if (pi->path_len < 3 ||
3589 249b637c 2023-02-20 stsp strncmp(pi->path, "**/", 3) != 0)
3592 bd8de430 2019-10-04 stsp while (*p) {
3593 249b637c 2023-02-20 stsp if (match_path(pi->path + 3,
3594 249b637c 2023-02-20 stsp pi->path_len - 3, p,
3595 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3596 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3597 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3599 bd8de430 2019-10-04 stsp while (*p == '/')
3609 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3610 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3611 6841da00 2019-08-08 stsp * ignores backwards.
3613 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3614 6841da00 2019-08-08 stsp while (pe) {
3615 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3616 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3617 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3618 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3619 249b637c 2023-02-20 stsp int flags = FNM_LEADING_DIR;
3620 249b637c 2023-02-20 stsp if (strstr(pi->path, "/**/") == NULL)
3621 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3622 249b637c 2023-02-20 stsp if (match_path(pi->path, pi->path_len,
3623 249b637c 2023-02-20 stsp path, flags))
3628 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3634 6841da00 2019-08-08 stsp static const struct got_error *
3635 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3636 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3638 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3639 6841da00 2019-08-08 stsp char *ignorespath;
3640 886cec17 2019-12-15 stsp int fd = -1;
3641 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3643 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3644 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3645 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3647 886cec17 2019-12-15 stsp if (dirfd != -1) {
3648 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3649 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3650 886cec17 2019-12-15 stsp if (fd == -1) {
3651 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3652 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3653 886cec17 2019-12-15 stsp ignorespath);
3655 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3656 5e91dae4 2022-08-30 stsp if (ignoresfile == NULL)
3657 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3658 886cec17 2019-12-15 stsp ignorespath);
3661 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3665 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3666 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3667 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3668 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3669 886cec17 2019-12-15 stsp ignorespath);
3671 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3674 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3675 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3676 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3677 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3678 6841da00 2019-08-08 stsp free(ignorespath);
3679 6841da00 2019-08-08 stsp return err;
3682 f8d1f275 2019-02-04 stsp static const struct got_error *
3683 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3684 62da3196 2021-10-01 stsp int dirfd)
3686 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3687 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3688 f8d1f275 2019-02-04 stsp char *path = NULL;
3690 62da3196 2021-10-01 stsp if (ignore != NULL)
3691 62da3196 2021-10-01 stsp *ignore = 0;
3693 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3694 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3696 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3697 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3698 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3700 f8d1f275 2019-02-04 stsp path = de->d_name;
3703 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3704 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3705 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3706 62da3196 2021-10-01 stsp *ignore = 1;
3707 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3708 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3709 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3710 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3711 f8d1f275 2019-02-04 stsp if (parent_path[0])
3712 f8d1f275 2019-02-04 stsp free(path);
3713 b72f483a 2019-02-05 stsp return err;
3716 347d1d3e 2019-07-12 stsp static const struct got_error *
3717 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3719 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3720 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3722 3143d852 2020-06-25 stsp if (a->no_ignores)
3723 3143d852 2020-06-25 stsp return NULL;
3725 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3726 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3728 3143d852 2020-06-25 stsp return err;
3730 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3731 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3733 3143d852 2020-06-25 stsp return err;
3736 3143d852 2020-06-25 stsp static const struct got_error *
3737 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3738 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3739 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3740 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3742 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3743 abb4604f 2019-07-27 stsp struct stat sb;
3745 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3747 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3748 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3750 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3751 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3752 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3753 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3754 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3757 916237f3 2022-02-11 stsp if (!no_ignores && match_ignores(ignores, path))
3758 916237f3 2022-02-11 stsp return NULL;
3760 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3761 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3762 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3764 abb4604f 2019-07-27 stsp return NULL;
3767 3143d852 2020-06-25 stsp static const struct got_error *
3768 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3769 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3771 3143d852 2020-06-25 stsp const struct got_error *err;
3772 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3774 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3775 3143d852 2020-06-25 stsp ".cvsignore");
3777 3143d852 2020-06-25 stsp return err;
3779 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3780 3143d852 2020-06-25 stsp ".gitignore");
3782 3143d852 2020-06-25 stsp return err;
3784 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3785 3143d852 2020-06-25 stsp if (err) {
3786 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3787 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3788 3143d852 2020-06-25 stsp return err;
3790 3143d852 2020-06-25 stsp for (;;) {
3791 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3792 3143d852 2020-06-25 stsp ".cvsignore");
3795 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3796 3143d852 2020-06-25 stsp ".gitignore");
3799 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3800 3143d852 2020-06-25 stsp if (err) {
3801 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3802 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3805 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3807 b737c85a 2020-06-26 stsp free(parent_path);
3808 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3809 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3812 b737c85a 2020-06-26 stsp free(parent_path);
3813 b737c85a 2020-06-26 stsp free(next_parent_path);
3814 3143d852 2020-06-25 stsp return err;
3817 abb4604f 2019-07-27 stsp static const struct got_error *
3818 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3819 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3820 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3821 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3822 f2a9dc41 2019-12-13 tracey int report_unchanged)
3824 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3825 6fc93f37 2019-12-13 stsp int fd = -1;
3826 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3827 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3828 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3829 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3830 a84c0d30 2022-03-12 stsp struct got_fileindex_entry *ie;
3832 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3834 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3835 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3836 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3838 a84c0d30 2022-03-12 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3840 a84c0d30 2022-03-12 stsp err = report_single_file_status(path, ondisk_path,
3841 a84c0d30 2022-03-12 stsp fileindex, status_cb, status_arg, repo,
3842 a84c0d30 2022-03-12 stsp report_unchanged, &ignores, no_ignores);
3843 a84c0d30 2022-03-12 stsp goto done;
3846 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3847 6fc93f37 2019-12-13 stsp if (fd == -1) {
3848 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3849 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
3850 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3852 ff56836b 2021-07-08 stsp if (!no_ignores) {
3853 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3854 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3856 ff56836b 2021-07-08 stsp goto done;
3858 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3859 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3860 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3863 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3864 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3865 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3866 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3867 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3868 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3869 abb4604f 2019-07-27 stsp arg.status_path = path;
3870 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3871 abb4604f 2019-07-27 stsp arg.repo = repo;
3872 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3873 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3874 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3875 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3876 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3877 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3878 022fae89 2019-12-06 tracey if (!no_ignores) {
3879 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3880 3143d852 2020-06-25 stsp worktree->root_path, path);
3882 3143d852 2020-06-25 stsp goto done;
3884 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3885 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3886 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3889 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3890 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3891 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3892 927df6b7 2019-02-10 stsp free(ondisk_path);
3893 347d1d3e 2019-07-12 stsp return err;
3896 347d1d3e 2019-07-12 stsp const struct got_error *
3897 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3898 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3899 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3900 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3902 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3903 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3904 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3905 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3907 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3909 347d1d3e 2019-07-12 stsp return err;
3911 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3912 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3913 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3914 f6343036 2021-06-22 stsp no_ignores, 0);
3918 f8d1f275 2019-02-04 stsp free(fileindex_path);
3919 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3920 f8d1f275 2019-02-04 stsp return err;
3923 6c7ab921 2019-03-18 stsp const struct got_error *
3924 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3925 6c7ab921 2019-03-18 stsp const char *arg)
3927 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3928 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3929 6c7ab921 2019-03-18 stsp size_t len;
3930 00bb5ea0 2020-07-23 stsp struct stat sb;
3931 01740607 2020-11-04 stsp char *abspath = NULL;
3932 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3934 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3936 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3937 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3938 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3940 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3941 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3942 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3943 00bb5ea0 2020-07-23 stsp goto done;
3945 727173c3 2020-11-06 stsp sb.st_mode = 0;
3947 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3949 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3950 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3951 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3952 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3954 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3955 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3956 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3957 00bb5ea0 2020-07-23 stsp goto done;
3961 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3962 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3964 d0710d08 2019-07-22 stsp goto done;
3965 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3966 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3967 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3968 00bb5ea0 2020-07-23 stsp goto done;
3971 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3972 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3973 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3974 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3975 00bb5ea0 2020-07-23 stsp goto done;
3977 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3978 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3979 01740607 2020-11-04 stsp goto done;
3981 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3982 01740607 2020-11-04 stsp sizeof(canonpath));
3984 00bb5ea0 2020-07-23 stsp goto done;
3985 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3986 01740607 2020-11-04 stsp if (resolved == NULL) {
3987 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3988 01740607 2020-11-04 stsp goto done;
3993 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3994 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3995 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3996 6c7ab921 2019-03-18 stsp goto done;
3999 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4000 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4001 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4003 f6d88e1a 2019-05-29 stsp goto done;
4005 f6d88e1a 2019-05-29 stsp path = strdup("");
4006 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4007 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4008 f6d88e1a 2019-05-29 stsp goto done;
4012 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4013 6c7ab921 2019-03-18 stsp len = strlen(path);
4014 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4015 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4019 01740607 2020-11-04 stsp free(abspath);
4020 6c7ab921 2019-03-18 stsp free(resolved);
4021 d0710d08 2019-07-22 stsp free(cwd);
4022 6c7ab921 2019-03-18 stsp if (err == NULL)
4023 6c7ab921 2019-03-18 stsp *wt_path = path;
4025 6c7ab921 2019-03-18 stsp free(path);
4026 d00136be 2019-03-26 stsp return err;
4029 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4030 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4031 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4032 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4033 4e68cba3 2019-11-23 stsp void *progress_arg;
4034 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4037 4e68cba3 2019-11-23 stsp static const struct got_error *
4038 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4039 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4040 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4041 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4043 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4044 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4045 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4046 6d022e97 2019-08-04 stsp struct stat sb;
4047 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4049 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4050 dbb83fbd 2019-12-12 stsp relpath) == -1)
4051 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4053 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4055 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4056 12463d8b 2019-12-13 stsp de_name, a->repo);
4058 dbb83fbd 2019-12-12 stsp goto done;
4059 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4060 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
4061 dbb83fbd 2019-12-12 stsp goto done;
4062 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4064 dbb83fbd 2019-12-12 stsp goto done;
4067 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4068 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
4069 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
4071 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4072 dbb83fbd 2019-12-12 stsp goto done;
4075 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4077 4e68cba3 2019-11-23 stsp goto done;
4078 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4079 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4080 3969253a 2020-03-07 stsp if (err) {
4081 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4082 3969253a 2020-03-07 stsp goto done;
4084 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4085 07f5b47a 2019-06-02 stsp if (err) {
4086 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4087 4e68cba3 2019-11-23 stsp goto done;
4090 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4092 dbb83fbd 2019-12-12 stsp return err;
4093 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4094 dbb83fbd 2019-12-12 stsp return NULL;
4095 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4098 d00136be 2019-03-26 stsp const struct got_error *
4099 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4100 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4101 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4102 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4104 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4105 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4106 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4107 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4108 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4110 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4112 d00136be 2019-03-26 stsp return err;
4114 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4116 d00136be 2019-03-26 stsp goto done;
4118 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4119 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4120 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4121 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4122 4e68cba3 2019-11-23 stsp saa.repo = repo;
4124 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4125 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4126 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4130 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4131 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4132 af12c6b9 2019-06-04 stsp err = sync_err;
4134 fb399478 2019-07-12 stsp free(fileindex_path);
4135 d00136be 2019-03-26 stsp if (fileindex)
4136 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4137 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4138 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4139 d00136be 2019-03-26 stsp err = unlockerr;
4140 6c7ab921 2019-03-18 stsp return err;
4143 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4144 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4145 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4146 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4147 f2a9dc41 2019-12-13 tracey void *progress_arg;
4148 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4149 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4150 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4151 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4152 766841c2 2020-08-13 stsp const char *status_codes;
4155 f2a9dc41 2019-12-13 tracey static const struct got_error *
4156 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4157 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4158 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4159 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4161 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4162 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4163 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4164 17ed4618 2019-06-02 stsp struct stat sb;
4165 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4167 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4168 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4169 4e12cd97 2022-01-25 stsp return NULL;
4170 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4173 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4174 17ed4618 2019-06-02 stsp if (ie == NULL)
4175 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4177 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4178 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4179 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4180 9acbc4fa 2019-08-03 stsp return NULL;
4181 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4184 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4185 f2a9dc41 2019-12-13 tracey relpath) == -1)
4186 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4188 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4191 f2a9dc41 2019-12-13 tracey goto done;
4193 766841c2 2020-08-13 stsp if (a->status_codes) {
4194 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4196 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4197 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4200 5e91dae4 2022-08-30 stsp if (i == ncodes) {
4201 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4202 766841c2 2020-08-13 stsp free(ondisk_path);
4203 766841c2 2020-08-13 stsp return NULL;
4205 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4206 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4207 766841c2 2020-08-13 stsp static char msg[64];
4208 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4209 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4210 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4211 766841c2 2020-08-13 stsp goto done;
4215 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4216 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4217 f2a9dc41 2019-12-13 tracey goto done;
4218 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4219 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4220 f2a9dc41 2019-12-13 tracey goto done;
4222 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4223 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4224 4e12cd97 2022-01-25 stsp goto done;
4226 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4227 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4228 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4229 f2a9dc41 2019-12-13 tracey goto done;
4233 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4234 20a2ad1c 2020-10-20 stsp size_t root_len;
4236 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4237 a06ca3f7 2022-10-15 stsp if (unlinkat(dirfd, de_name, 0) == -1) {
4238 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4239 12463d8b 2019-12-13 stsp ondisk_path);
4240 12463d8b 2019-12-13 stsp goto done;
4242 a06ca3f7 2022-10-15 stsp } else if (unlink(ondisk_path) == -1) {
4243 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4244 12463d8b 2019-12-13 stsp goto done;
4247 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4249 20a2ad1c 2020-10-20 stsp char *parent;
4250 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4252 2513f20a 2020-10-20 stsp goto done;
4253 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4254 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4255 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4256 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4257 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4258 20a2ad1c 2020-10-20 stsp ondisk_path);
4261 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4262 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4265 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4267 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4268 f2a9dc41 2019-12-13 tracey if (err)
4269 f2a9dc41 2019-12-13 tracey return err;
4270 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4271 f2a9dc41 2019-12-13 tracey return NULL;
4272 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4273 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4276 2ec1f75b 2019-03-26 stsp const struct got_error *
4277 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4278 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4279 766841c2 2020-08-13 stsp const char *status_codes,
4280 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4281 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4283 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4284 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4285 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4286 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4287 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4289 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4291 2ec1f75b 2019-03-26 stsp return err;
4293 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4295 2ec1f75b 2019-03-26 stsp goto done;
4297 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4298 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4299 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4300 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4301 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4302 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4303 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4304 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4305 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4307 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4308 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4309 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4313 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4314 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4315 af12c6b9 2019-06-04 stsp err = sync_err;
4317 fb399478 2019-07-12 stsp free(fileindex_path);
4318 2ec1f75b 2019-03-26 stsp if (fileindex)
4319 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4320 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4321 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4322 2ec1f75b 2019-03-26 stsp err = unlockerr;
4323 33aa809d 2019-08-08 stsp return err;
4326 33aa809d 2019-08-08 stsp static const struct got_error *
4327 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4329 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4330 33aa809d 2019-08-08 stsp char *line = NULL;
4331 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4332 33aa809d 2019-08-08 stsp ssize_t linelen;
4334 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4335 33aa809d 2019-08-08 stsp if (linelen == -1) {
4336 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4337 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4338 33aa809d 2019-08-08 stsp goto done;
4340 33aa809d 2019-08-08 stsp return NULL;
4342 33aa809d 2019-08-08 stsp if (outfile) {
4343 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4344 33aa809d 2019-08-08 stsp if (n != linelen) {
4345 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4346 33aa809d 2019-08-08 stsp goto done;
4349 33aa809d 2019-08-08 stsp if (rejectfile) {
4350 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4351 33aa809d 2019-08-08 stsp if (n != linelen)
4352 910d235d 2023-01-10 mark err = got_ferror(rejectfile, GOT_ERR_IO);
4355 33aa809d 2019-08-08 stsp free(line);
4356 2ec1f75b 2019-03-26 stsp return err;
4359 33aa809d 2019-08-08 stsp static const struct got_error *
4360 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4362 33aa809d 2019-08-08 stsp char *line = NULL;
4363 33aa809d 2019-08-08 stsp size_t linesize = 0;
4364 33aa809d 2019-08-08 stsp ssize_t linelen;
4366 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4367 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4368 500467ff 2019-09-25 hiltjo if (ferror(f))
4369 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4370 500467ff 2019-09-25 hiltjo return NULL;
4372 33aa809d 2019-08-08 stsp free(line);
4373 33aa809d 2019-08-08 stsp return NULL;
4376 33aa809d 2019-08-08 stsp static const struct got_error *
4377 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4378 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4379 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4381 33aa809d 2019-08-08 stsp const struct got_error *err;
4383 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4384 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4385 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4387 33aa809d 2019-08-08 stsp return err;
4388 33aa809d 2019-08-08 stsp (*line_cur1)++;
4390 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4391 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4392 33aa809d 2019-08-08 stsp if (rejectfile)
4393 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4395 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4397 33aa809d 2019-08-08 stsp return err;
4398 33aa809d 2019-08-08 stsp (*line_cur2)++;
4400 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4401 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4402 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4404 33aa809d 2019-08-08 stsp return err;
4405 33aa809d 2019-08-08 stsp (*line_cur2)++;
4407 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4408 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4409 33aa809d 2019-08-08 stsp if (rejectfile)
4410 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4412 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4414 33aa809d 2019-08-08 stsp return err;
4415 33aa809d 2019-08-08 stsp (*line_cur1)++;
4418 f1e81a05 2019-08-10 stsp return NULL;
4421 f1e81a05 2019-08-10 stsp static const struct got_error *
4422 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4423 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4425 f1e81a05 2019-08-10 stsp const struct got_error *err;
4427 f1e81a05 2019-08-10 stsp if (outfile) {
4428 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4429 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4430 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4432 f1e81a05 2019-08-10 stsp return err;
4433 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4436 f1e81a05 2019-08-10 stsp if (rejectfile) {
4437 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4438 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4439 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4441 f1e81a05 2019-08-10 stsp return err;
4442 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4446 33aa809d 2019-08-08 stsp return NULL;
4449 33aa809d 2019-08-08 stsp static const struct got_error *
4450 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4451 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4452 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4453 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4454 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4456 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4457 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4458 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4459 33aa809d 2019-08-08 stsp FILE *hunkfile;
4460 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4461 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4464 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4466 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4467 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4468 fe621944 2020-11-10 stsp start_old = cc.left.start;
4469 fe621944 2020-11-10 stsp end_old = cc.left.end;
4470 fe621944 2020-11-10 stsp start_new = cc.right.start;
4471 fe621944 2020-11-10 stsp end_new = cc.right.end;
4473 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4474 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4475 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4477 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4478 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4479 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4481 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4482 fe621944 2020-11-10 stsp if (diff_state == NULL)
4483 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4484 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4486 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4487 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4488 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4489 33aa809d 2019-08-08 stsp goto done;
4492 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4493 fe621944 2020-11-10 stsp diff_result, &cc);
4494 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4495 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4496 33aa809d 2019-08-08 stsp goto done;
4499 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4500 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4501 33aa809d 2019-08-08 stsp goto done;
4504 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4505 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4507 33aa809d 2019-08-08 stsp goto done;
4509 33aa809d 2019-08-08 stsp switch (*choice) {
4510 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4511 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4512 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4514 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4515 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4516 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4518 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4521 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4525 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4526 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4527 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4528 33aa809d 2019-08-08 stsp return err;
4531 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4532 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4533 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4534 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4535 1f1abb7e 2019-08-08 stsp void *progress_arg;
4536 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4537 33aa809d 2019-08-08 stsp void *patch_arg;
4538 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4539 f259c4c1 2021-09-24 stsp int unlink_added_files;
4542 e20a8b6f 2019-06-04 stsp static const struct got_error *
4543 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4544 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4545 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4546 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4547 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4549 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4550 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4551 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4552 eb81bc23 2022-06-28 tracey int fd = -1, fd2 = -1;
4553 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4554 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4555 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4556 fe621944 2020-11-10 stsp struct stat sb2;
4557 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4558 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4559 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4561 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4563 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4565 33aa809d 2019-08-08 stsp return err;
4567 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4568 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4569 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4570 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4571 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4572 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4573 fa3cef63 2020-07-23 stsp goto done;
4575 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4576 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4577 c0df5966 2021-12-31 stsp if (link_len == -1) {
4578 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4581 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4582 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4585 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4586 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4587 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4588 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4589 fa3cef63 2020-07-23 stsp goto done;
4591 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4592 fa3cef63 2020-07-23 stsp sizeof(link_target));
4593 fa3cef63 2020-07-23 stsp if (link_len == -1)
4594 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4595 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4596 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4599 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4600 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4601 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4602 fa3cef63 2020-07-23 stsp goto done;
4605 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4606 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4607 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4608 fa3cef63 2020-07-23 stsp goto done;
4613 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4614 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4615 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4616 fa3cef63 2020-07-23 stsp goto done;
4618 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4619 fa3cef63 2020-07-23 stsp if (n != link_len) {
4620 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4621 fa3cef63 2020-07-23 stsp goto done;
4623 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4624 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4625 fa3cef63 2020-07-23 stsp goto done;
4627 fa3cef63 2020-07-23 stsp rewind(f2);
4630 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4631 eb81bc23 2022-06-28 tracey if (fd == -1) {
4632 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4633 eb81bc23 2022-06-28 tracey goto done;
4636 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4638 33aa809d 2019-08-08 stsp goto done;
4640 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4642 33aa809d 2019-08-08 stsp goto done;
4644 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4646 33aa809d 2019-08-08 stsp goto done;
4648 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4649 4b752015 2022-06-30 stsp 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4651 33aa809d 2019-08-08 stsp goto done;
4653 b90054ed 2022-11-01 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4656 33aa809d 2019-08-08 stsp goto done;
4658 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4659 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4660 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4661 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4663 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4664 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4665 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4666 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4667 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4668 fe621944 2020-11-10 stsp nchanges++;
4670 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4671 33aa809d 2019-08-08 stsp int choice;
4672 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4673 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4674 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4675 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4676 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4677 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4679 33aa809d 2019-08-08 stsp goto done;
4680 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4681 33aa809d 2019-08-08 stsp have_content = 1;
4682 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4685 1ebedb77 2019-10-19 stsp if (have_content) {
4686 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4687 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4688 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4690 1ebedb77 2019-10-19 stsp goto done;
4692 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4693 b2b3fce1 2022-10-29 op mode_t mode;
4695 b2b3fce1 2022-10-29 op mode = apply_umask(sb2.st_mode);
4696 b2b3fce1 2022-10-29 op if (fchmod(fileno(outfile), mode) == -1) {
4697 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4698 fa3cef63 2020-07-23 stsp goto done;
4703 33aa809d 2019-08-08 stsp free(id_str);
4704 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4705 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4707 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4708 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4709 fe621944 2020-11-10 stsp if (err == NULL)
4710 fe621944 2020-11-10 stsp err = free_err;
4711 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4712 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4713 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4714 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4715 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4716 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4717 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4718 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4719 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4720 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4721 33aa809d 2019-08-08 stsp if (err || !have_content) {
4722 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4723 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4724 33aa809d 2019-08-08 stsp free(*path_outfile);
4725 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4727 33aa809d 2019-08-08 stsp free(path1);
4728 33aa809d 2019-08-08 stsp return err;
4731 33aa809d 2019-08-08 stsp static const struct got_error *
4732 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4733 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4734 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4735 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4737 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4738 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4739 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4740 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4741 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
4742 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4743 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4744 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4745 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4746 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4747 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4748 eb81bc23 2022-06-28 tracey int fd = -1;
4750 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4751 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4752 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4753 d3bcc3d1 2019-08-08 stsp return NULL;
4755 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4756 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4757 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4759 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4760 65084dad 2019-08-08 stsp if (ie == NULL)
4761 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4763 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4764 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4765 a129376b 2019-03-28 stsp if (err) {
4766 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4767 a129376b 2019-03-28 stsp goto done;
4768 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4769 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4770 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4771 e20a8b6f 2019-06-04 stsp goto done;
4774 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4775 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4776 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4777 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4778 a129376b 2019-03-28 stsp goto done;
4781 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4782 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4783 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4784 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4785 a129376b 2019-03-28 stsp goto done;
4788 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4789 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4790 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4791 a129376b 2019-03-28 stsp goto done;
4796 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
4797 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
4799 a44927cc 2022-04-07 stsp goto done;
4801 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4802 a9fa2909 2019-07-27 stsp if (err) {
4803 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4804 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4805 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4806 a9fa2909 2019-07-27 stsp goto done;
4808 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4810 a9fa2909 2019-07-27 stsp goto done;
4812 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4814 a9fa2909 2019-07-27 stsp goto done;
4816 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4817 1233e6b6 2020-10-19 stsp free(te_name);
4818 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4819 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4820 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4821 a9fa2909 2019-07-27 stsp goto done;
4825 a129376b 2019-03-28 stsp switch (status) {
4826 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4827 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4828 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4829 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4830 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4832 33aa809d 2019-08-08 stsp goto done;
4833 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4836 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4837 1f1abb7e 2019-08-08 stsp ie->path);
4839 1ee397ad 2019-07-12 stsp goto done;
4840 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4841 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
4842 f259c4c1 2021-09-24 stsp if (asprintf(&ondisk_path, "%s/%s",
4843 f259c4c1 2021-09-24 stsp got_worktree_get_root_path(a->worktree),
4844 f259c4c1 2021-09-24 stsp relpath) == -1) {
4845 f259c4c1 2021-09-24 stsp err = got_error_from_errno("asprintf");
4846 f259c4c1 2021-09-24 stsp goto done;
4848 f259c4c1 2021-09-24 stsp if (unlink(ondisk_path) == -1) {
4849 f259c4c1 2021-09-24 stsp err = got_error_from_errno2("unlink",
4850 f259c4c1 2021-09-24 stsp ondisk_path);
4855 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4856 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4857 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4858 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4859 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4861 33aa809d 2019-08-08 stsp goto done;
4862 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4865 33aa809d 2019-08-08 stsp /* fall through */
4866 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4867 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4868 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4869 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4870 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4871 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4872 b4b2adf5 2023-02-09 op staged_status == GOT_STATUS_MODIFY)
4873 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
4875 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
4876 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4877 eb81bc23 2022-06-28 tracey if (fd == -1) {
4878 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4879 eb81bc23 2022-06-28 tracey goto done;
4882 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4884 65084dad 2019-08-08 stsp goto done;
4886 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4887 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4888 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4889 a129376b 2019-03-28 stsp goto done;
4892 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4893 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4894 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4895 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4896 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4897 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4898 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4900 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4901 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4902 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4903 369fd7e5 2020-07-23 stsp path_content);
4906 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4907 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4908 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4909 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4911 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4912 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4913 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4914 369fd7e5 2020-07-23 stsp goto done;
4918 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4919 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4920 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4921 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4922 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4923 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4925 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4927 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4928 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4929 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4930 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4933 a129376b 2019-03-28 stsp goto done;
4934 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4935 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4936 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4937 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4938 437adc9d 2020-12-10 yzhong blob->id.sha1,
4939 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4941 2e1fa222 2020-07-23 stsp goto done;
4943 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4944 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4945 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4954 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4955 33aa809d 2019-08-08 stsp free(path_content);
4956 e20a8b6f 2019-06-04 stsp free(parent_path);
4957 a129376b 2019-03-28 stsp free(tree_path);
4958 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4959 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4961 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4963 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4964 a129376b 2019-03-28 stsp free(tree_id);
4965 a44927cc 2022-04-07 stsp if (base_commit)
4966 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
4967 e20a8b6f 2019-06-04 stsp return err;
4970 e20a8b6f 2019-06-04 stsp const struct got_error *
4971 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4972 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4973 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4974 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4975 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4977 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4978 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4979 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4980 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4981 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4982 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4984 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4986 e20a8b6f 2019-06-04 stsp return err;
4988 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4990 e20a8b6f 2019-06-04 stsp goto done;
4992 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4993 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4994 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4995 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4996 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4997 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4998 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4999 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
5000 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5001 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5002 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
5006 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5007 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5008 e20a8b6f 2019-06-04 stsp err = sync_err;
5010 fb399478 2019-07-12 stsp free(fileindex_path);
5011 a129376b 2019-03-28 stsp if (fileindex)
5012 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5013 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5014 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5015 a129376b 2019-03-28 stsp err = unlockerr;
5016 c4296144 2019-05-09 stsp return err;
5019 cf066bf8 2019-05-09 stsp static void
5020 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5022 24519714 2019-05-09 stsp free(ct->path);
5023 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5024 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5025 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5026 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5027 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5028 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5032 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5033 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5034 24519714 2019-05-09 stsp struct got_repository *repo;
5035 24519714 2019-05-09 stsp struct got_worktree *worktree;
5036 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5037 5f8a88c6 2019-08-03 stsp int have_staged_files;
5038 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5039 2a47b1e5 2022-11-01 stsp int diff_header_shown;
5040 12383673 2023-02-18 mark int commit_conflicts;
5041 2a47b1e5 2022-11-01 stsp FILE *diff_outfile;
5047 2a47b1e5 2022-11-01 stsp * Create a file which contains the target path of a symlink so we can feed
5048 2a47b1e5 2022-11-01 stsp * it as content to the diff engine.
5050 2a47b1e5 2022-11-01 stsp static const struct got_error *
5051 2a47b1e5 2022-11-01 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5052 2a47b1e5 2022-11-01 stsp const char *abspath)
5054 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5055 2a47b1e5 2022-11-01 stsp char target_path[PATH_MAX];
5056 2a47b1e5 2022-11-01 stsp ssize_t target_len, outlen;
5060 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5061 2a47b1e5 2022-11-01 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5062 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5063 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlinkat", abspath);
5065 2a47b1e5 2022-11-01 stsp target_len = readlink(abspath, target_path, PATH_MAX);
5066 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5067 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlink", abspath);
5070 2a47b1e5 2022-11-01 stsp *fd = got_opentempfd();
5071 2a47b1e5 2022-11-01 stsp if (*fd == -1)
5072 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentempfd");
5074 2a47b1e5 2022-11-01 stsp outlen = write(*fd, target_path, target_len);
5075 2a47b1e5 2022-11-01 stsp if (outlen == -1) {
5076 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5077 2a47b1e5 2022-11-01 stsp goto done;
5080 2a47b1e5 2022-11-01 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
5081 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("lseek", abspath);
5082 2a47b1e5 2022-11-01 stsp goto done;
5085 2a47b1e5 2022-11-01 stsp if (err) {
5086 2a47b1e5 2022-11-01 stsp close(*fd);
5089 2a47b1e5 2022-11-01 stsp return err;
5092 2a47b1e5 2022-11-01 stsp static const struct got_error *
5093 2a47b1e5 2022-11-01 stsp append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5094 2a47b1e5 2022-11-01 stsp FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5095 6d15dc69 2022-11-01 stsp int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5097 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5098 2a47b1e5 2022-11-01 stsp struct got_blob_object *blob1 = NULL;
5099 2a47b1e5 2022-11-01 stsp int fd = -1, fd1 = -1, fd2 = -1;
5100 2a47b1e5 2022-11-01 stsp FILE *ondisk_file = NULL;
5101 2a47b1e5 2022-11-01 stsp char *label1 = NULL;
5102 2a47b1e5 2022-11-01 stsp struct stat sb;
5103 2a47b1e5 2022-11-01 stsp off_t size1 = 0;
5104 2a47b1e5 2022-11-01 stsp int f2_exists = 0;
5105 2a47b1e5 2022-11-01 stsp char *id_str = NULL;
5107 2a47b1e5 2022-11-01 stsp memset(&sb, 0, sizeof(sb));
5109 4ba5cca9 2022-11-01 stsp if (diff_staged) {
5110 4ba5cca9 2022-11-01 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5111 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_ADD &&
5112 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_DELETE)
5113 4ba5cca9 2022-11-01 stsp return NULL;
5115 4ba5cca9 2022-11-01 stsp if (ct->status != GOT_STATUS_MODIFY &&
5116 4ba5cca9 2022-11-01 stsp ct->status != GOT_STATUS_ADD &&
5117 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5118 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5119 4ba5cca9 2022-11-01 stsp return NULL;
5122 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f1);
5124 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5125 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f2);
5127 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5129 2a47b1e5 2022-11-01 stsp if (!*diff_header_shown) {
5130 2a47b1e5 2022-11-01 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
5132 2a47b1e5 2022-11-01 stsp return err;
5133 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5134 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree));
5135 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "commit - %s\n", id_str);
5136 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "path + %s%s\n",
5137 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree),
5138 2a47b1e5 2022-11-01 stsp diff_staged ? " (staged changes)" : "");
5139 2a47b1e5 2022-11-01 stsp *diff_header_shown = 1;
5142 2a47b1e5 2022-11-01 stsp if (diff_staged) {
5143 2a47b1e5 2022-11-01 stsp const char *label1 = NULL, *label2 = NULL;
5144 2a47b1e5 2022-11-01 stsp switch (ct->staged_status) {
5145 2a47b1e5 2022-11-01 stsp case GOT_STATUS_MODIFY:
5146 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5147 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5149 2a47b1e5 2022-11-01 stsp case GOT_STATUS_ADD:
5150 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5152 2a47b1e5 2022-11-01 stsp case GOT_STATUS_DELETE:
5153 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5156 2a47b1e5 2022-11-01 stsp return got_error(GOT_ERR_FILE_STATUS);
5158 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5159 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5160 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5161 2a47b1e5 2022-11-01 stsp goto done;
5163 2a47b1e5 2022-11-01 stsp fd2 = got_opentempfd();
5164 2a47b1e5 2022-11-01 stsp if (fd2 == -1) {
5165 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5166 2a47b1e5 2022-11-01 stsp goto done;
5168 2a47b1e5 2022-11-01 stsp err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5169 2a47b1e5 2022-11-01 stsp fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5170 1f3405c9 2023-01-17 mark label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5171 a76e88e5 2023-01-10 mark NULL, repo, diff_outfile);
5172 2a47b1e5 2022-11-01 stsp goto done;
5175 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5176 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5177 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5178 2a47b1e5 2022-11-01 stsp goto done;
5181 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_ADD) {
5182 2a47b1e5 2022-11-01 stsp err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5183 2a47b1e5 2022-11-01 stsp 8192, fd1);
5185 2a47b1e5 2022-11-01 stsp goto done;
5188 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_DELETE) {
5189 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5190 2a47b1e5 2022-11-01 stsp fd = openat(dirfd, de_name,
5191 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5192 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5193 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5194 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("openat",
5195 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5196 2a47b1e5 2022-11-01 stsp goto done;
5198 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5199 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5201 2a47b1e5 2022-11-01 stsp goto done;
5204 2a47b1e5 2022-11-01 stsp fd = open(ct->ondisk_path,
5205 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5206 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5207 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5208 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("open",
5209 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5210 2a47b1e5 2022-11-01 stsp goto done;
5212 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5213 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5215 2a47b1e5 2022-11-01 stsp goto done;
5218 2a47b1e5 2022-11-01 stsp if (fstatat(fd, ct->ondisk_path, &sb,
5219 2a47b1e5 2022-11-01 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5220 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fstatat", ct->ondisk_path);
5221 2a47b1e5 2022-11-01 stsp goto done;
5223 2a47b1e5 2022-11-01 stsp ondisk_file = fdopen(fd, "r");
5224 2a47b1e5 2022-11-01 stsp if (ondisk_file == NULL) {
5225 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fdopen", ct->ondisk_path);
5226 2a47b1e5 2022-11-01 stsp goto done;
5229 2a47b1e5 2022-11-01 stsp f2_exists = 1;
5232 2a47b1e5 2022-11-01 stsp if (blob1) {
5233 2a47b1e5 2022-11-01 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5234 2a47b1e5 2022-11-01 stsp f1, blob1);
5236 2a47b1e5 2022-11-01 stsp goto done;
5239 2a47b1e5 2022-11-01 stsp err = got_diff_blob_file(blob1, f1, size1, label1,
5240 2a47b1e5 2022-11-01 stsp ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5241 1f3405c9 2023-01-17 mark GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5243 2a47b1e5 2022-11-01 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5244 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5245 2a47b1e5 2022-11-01 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5246 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5247 2a47b1e5 2022-11-01 stsp if (blob1)
5248 2a47b1e5 2022-11-01 stsp got_object_blob_close(blob1);
5249 2a47b1e5 2022-11-01 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5250 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5251 2a47b1e5 2022-11-01 stsp if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5252 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
5253 2a47b1e5 2022-11-01 stsp return err;
5256 c4296144 2019-05-09 stsp static const struct got_error *
5257 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5258 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5259 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5260 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5262 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5263 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5264 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5265 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5266 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5267 768aea60 2019-05-09 stsp struct stat sb;
5269 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5270 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5271 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5272 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5273 5f8a88c6 2019-08-03 stsp return NULL;
5275 12383673 2023-02-18 mark if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5276 12383673 2023-02-18 mark printf("C %s\n", relpath);
5277 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5280 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5281 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5282 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5283 12383673 2023-02-18 mark status != GOT_STATUS_DELETE &&
5284 12383673 2023-02-18 mark status != GOT_STATUS_CONFLICT)
5285 5f8a88c6 2019-08-03 stsp return NULL;
5288 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5289 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5290 036813ee 2019-05-09 stsp goto done;
5292 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5293 036813ee 2019-05-09 stsp parent_path = strdup("");
5294 69960a46 2019-05-09 stsp if (parent_path == NULL)
5295 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5297 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5299 69960a46 2019-05-09 stsp return err;
5302 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5303 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5304 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5305 c4296144 2019-05-09 stsp goto done;
5308 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5309 768aea60 2019-05-09 stsp relpath) == -1) {
5310 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5311 768aea60 2019-05-09 stsp goto done;
5314 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5315 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5316 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5317 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5318 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5319 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5320 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5321 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5323 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5324 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5327 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5328 0aeb8099 2020-07-23 stsp goto done;
5330 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5331 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5332 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5333 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5334 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5335 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5336 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5337 12463d8b 2019-12-13 stsp ct->ondisk_path);
5338 12463d8b 2019-12-13 stsp goto done;
5340 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5341 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5342 768aea60 2019-05-09 stsp goto done;
5344 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5347 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5348 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5349 44d03001 2019-05-09 stsp relpath) == -1) {
5350 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5351 44d03001 2019-05-09 stsp goto done;
5354 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5355 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5356 35213c7c 2020-07-23 stsp int is_bad_symlink;
5357 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5358 35213c7c 2020-07-23 stsp ssize_t target_len;
5359 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5360 35213c7c 2020-07-23 stsp sizeof(target_path));
5361 35213c7c 2020-07-23 stsp if (target_len == -1) {
5362 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5363 35213c7c 2020-07-23 stsp ct->ondisk_path);
5364 35213c7c 2020-07-23 stsp goto done;
5366 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5367 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5369 35213c7c 2020-07-23 stsp goto done;
5370 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5371 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5372 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5373 35213c7c 2020-07-23 stsp goto done;
5378 cf066bf8 2019-05-09 stsp ct->status = status;
5379 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5380 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5381 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5382 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5383 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5384 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5385 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5386 b416585c 2019-05-13 stsp goto done;
5388 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5389 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5390 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5391 f0b75401 2019-08-03 stsp goto done;
5394 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5395 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5396 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5397 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5398 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5399 036813ee 2019-05-09 stsp goto done;
5402 24519714 2019-05-09 stsp ct->path = strdup(path);
5403 24519714 2019-05-09 stsp if (ct->path == NULL) {
5404 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5405 24519714 2019-05-09 stsp goto done;
5407 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5409 2a47b1e5 2022-11-01 stsp goto done;
5411 2a47b1e5 2022-11-01 stsp if (a->diff_outfile && ct && new != NULL) {
5412 2a47b1e5 2022-11-01 stsp err = append_ct_diff(ct, &a->diff_header_shown,
5413 2a47b1e5 2022-11-01 stsp a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5414 6d15dc69 2022-11-01 stsp a->have_staged_files, a->repo, a->worktree);
5416 2a47b1e5 2022-11-01 stsp goto done;
5419 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5420 ed175427 2019-05-09 stsp free_commitable(ct);
5421 24519714 2019-05-09 stsp free(parent_path);
5422 036813ee 2019-05-09 stsp free(path);
5423 a129376b 2019-03-28 stsp return err;
5426 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5427 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5428 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5429 036813ee 2019-05-09 stsp struct got_repository *);
5431 ed175427 2019-05-09 stsp static const struct got_error *
5432 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5433 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5434 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5435 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5436 afa376bf 2019-05-09 stsp struct got_repository *repo)
5438 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5439 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5440 036813ee 2019-05-09 stsp char *subpath;
5442 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5443 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5444 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5446 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5448 ed175427 2019-05-09 stsp return err;
5450 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5451 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5452 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5453 036813ee 2019-05-09 stsp free(subpath);
5454 036813ee 2019-05-09 stsp return err;
5457 036813ee 2019-05-09 stsp static const struct got_error *
5458 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5460 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5461 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5463 036813ee 2019-05-09 stsp *match = 0;
5465 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5466 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5467 0f63689d 2019-05-10 stsp return NULL;
5470 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5472 0f63689d 2019-05-10 stsp return err;
5473 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5474 036813ee 2019-05-09 stsp free(ct_parent_path);
5475 036813ee 2019-05-09 stsp return err;
5478 768aea60 2019-05-09 stsp static mode_t
5479 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5481 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5482 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5484 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5487 036813ee 2019-05-09 stsp static const struct got_error *
5488 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5489 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5491 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5493 036813ee 2019-05-09 stsp *new_te = NULL;
5495 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5497 036813ee 2019-05-09 stsp goto done;
5499 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5501 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5502 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5503 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5505 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5507 036813ee 2019-05-09 stsp if (err && *new_te) {
5508 56e0773d 2019-11-28 stsp free(*new_te);
5509 036813ee 2019-05-09 stsp *new_te = NULL;
5511 036813ee 2019-05-09 stsp return err;
5514 036813ee 2019-05-09 stsp static const struct got_error *
5515 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5516 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5518 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5519 102b254e 2020-10-19 stsp char *ct_name = NULL;
5521 036813ee 2019-05-09 stsp *new_te = NULL;
5523 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5524 036813ee 2019-05-09 stsp if (*new_te == NULL)
5525 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5527 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5529 036813ee 2019-05-09 stsp goto done;
5530 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5531 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5532 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5533 036813ee 2019-05-09 stsp goto done;
5536 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5538 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5539 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5540 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5542 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5544 102b254e 2020-10-19 stsp free(ct_name);
5545 036813ee 2019-05-09 stsp if (err && *new_te) {
5546 56e0773d 2019-11-28 stsp free(*new_te);
5547 036813ee 2019-05-09 stsp *new_te = NULL;
5549 036813ee 2019-05-09 stsp return err;
5552 036813ee 2019-05-09 stsp static const struct got_error *
5553 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5554 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5556 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5557 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5559 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5561 036813ee 2019-05-09 stsp return err;
5562 036813ee 2019-05-09 stsp if (new_pe == NULL)
5563 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5564 036813ee 2019-05-09 stsp return NULL;
5567 afa376bf 2019-05-09 stsp static const struct got_error *
5568 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5569 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5571 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5572 5f8a88c6 2019-08-03 stsp unsigned char status;
5574 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5575 0ff8d236 2021-09-28 stsp return NULL;
5577 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5578 afa376bf 2019-05-09 stsp ct_path++;
5580 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5581 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5583 5f8a88c6 2019-08-03 stsp status = ct->status;
5585 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5586 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5589 036813ee 2019-05-09 stsp static const struct got_error *
5590 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5591 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5593 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5594 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5595 44d03001 2019-05-09 stsp char *te_path;
5597 44d03001 2019-05-09 stsp *modified = 0;
5599 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5600 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5601 44d03001 2019-05-09 stsp te->name) == -1)
5602 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5604 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5605 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5606 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5607 44d03001 2019-05-09 stsp strlen(te_path));
5608 62d463ca 2020-10-20 naddy if (*modified)
5612 44d03001 2019-05-09 stsp free(te_path);
5613 44d03001 2019-05-09 stsp return err;
5616 44d03001 2019-05-09 stsp static const struct got_error *
5617 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5618 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5619 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5621 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5622 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5624 036813ee 2019-05-09 stsp *ctp = NULL;
5626 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5627 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5628 036813ee 2019-05-09 stsp char *ct_name = NULL;
5629 036813ee 2019-05-09 stsp int path_matches;
5631 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5632 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5633 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5634 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5635 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5638 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5639 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5643 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5646 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5648 036813ee 2019-05-09 stsp return err;
5649 036813ee 2019-05-09 stsp if (!path_matches)
5652 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5654 d34b633e 2020-10-19 stsp return err;
5656 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5657 d34b633e 2020-10-19 stsp free(ct_name);
5660 d34b633e 2020-10-19 stsp free(ct_name);
5662 036813ee 2019-05-09 stsp *ctp = ct;
5666 2b496619 2019-07-10 stsp return err;
5669 2b496619 2019-07-10 stsp static const struct got_error *
5670 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5671 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5672 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5673 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5674 2b496619 2019-07-10 stsp struct got_repository *repo)
5676 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5677 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5678 2b496619 2019-07-10 stsp char *subtree_path;
5679 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5680 ba580f68 2020-03-22 stsp int nentries;
5682 2b496619 2019-07-10 stsp *new_tep = NULL;
5684 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5685 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5686 2b496619 2019-07-10 stsp child_path) == -1)
5687 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5689 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5690 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5691 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5692 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5694 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5695 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5696 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5697 2b496619 2019-07-10 stsp goto done;
5699 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5700 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5701 2b496619 2019-07-10 stsp if (err) {
5702 56e0773d 2019-11-28 stsp free(new_te);
5703 2b496619 2019-07-10 stsp goto done;
5705 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5708 2b496619 2019-07-10 stsp free(subtree_path);
5709 2b496619 2019-07-10 stsp if (err == NULL)
5710 2b496619 2019-07-10 stsp *new_tep = new_te;
5711 036813ee 2019-05-09 stsp return err;
5714 036813ee 2019-05-09 stsp static const struct got_error *
5715 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5716 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5717 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5718 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5719 036813ee 2019-05-09 stsp struct got_repository *repo)
5721 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5722 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5723 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5724 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5726 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5727 ba580f68 2020-03-22 stsp *nentries = 0;
5729 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5730 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5731 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5732 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5734 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5735 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5736 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5739 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5740 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5743 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5744 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5746 036813ee 2019-05-09 stsp goto done;
5748 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5749 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5750 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5752 036813ee 2019-05-09 stsp goto done;
5753 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5755 afa376bf 2019-05-09 stsp goto done;
5756 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5757 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5759 2f51b5b3 2019-05-09 stsp goto done;
5760 ba580f68 2020-03-22 stsp (*nentries)++;
5762 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5763 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5764 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5765 2b496619 2019-07-10 stsp == NULL) {
5766 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5767 2b496619 2019-07-10 stsp child_path, path_base_tree,
5768 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5771 2b496619 2019-07-10 stsp goto done;
5772 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5774 2b496619 2019-07-10 stsp goto done;
5775 ba580f68 2020-03-22 stsp (*nentries)++;
5780 2f51b5b3 2019-05-09 stsp if (base_tree) {
5781 56e0773d 2019-11-28 stsp int i, nbase_entries;
5782 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5783 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5784 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5785 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5787 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5788 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5789 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5790 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5792 63c5ca5d 2019-08-24 stsp goto done;
5793 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5795 63c5ca5d 2019-08-24 stsp goto done;
5796 ba580f68 2020-03-22 stsp (*nentries)++;
5800 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5801 44d03001 2019-05-09 stsp int modified;
5802 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5804 036813ee 2019-05-09 stsp goto done;
5805 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5806 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5808 2f51b5b3 2019-05-09 stsp goto done;
5809 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5810 44d03001 2019-05-09 stsp if (modified) {
5811 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5812 ba580f68 2020-03-22 stsp int nsubentries;
5813 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5814 ba580f68 2020-03-22 stsp &nsubentries, te,
5815 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5816 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5818 44d03001 2019-05-09 stsp goto done;
5819 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5820 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5821 ba580f68 2020-03-22 stsp free(new_id);
5824 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5825 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5826 56e0773d 2019-11-28 stsp free(new_id);
5828 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5830 036813ee 2019-05-09 stsp goto done;
5831 ba580f68 2020-03-22 stsp (*nentries)++;
5835 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5836 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5838 f66c734c 2019-09-22 stsp goto done;
5840 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5841 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5842 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5843 12383673 2023-02-18 mark ct->status == GOT_STATUS_CONFLICT ||
5844 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5845 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5846 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5848 2f51b5b3 2019-05-09 stsp goto done;
5849 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5851 2f51b5b3 2019-05-09 stsp goto done;
5852 ba580f68 2020-03-22 stsp (*nentries)++;
5854 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5855 b416585c 2019-05-13 stsp status_arg);
5857 afa376bf 2019-05-09 stsp goto done;
5859 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5860 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5862 2f51b5b3 2019-05-09 stsp goto done;
5863 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5865 2f51b5b3 2019-05-09 stsp goto done;
5866 ba580f68 2020-03-22 stsp (*nentries)++;
5871 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5872 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5874 d8bacb93 2023-01-10 mark got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5875 2e1fa222 2020-07-23 stsp return err;
5878 2e1fa222 2020-07-23 stsp static const struct got_error *
5879 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5880 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5881 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5882 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5884 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5885 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5886 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5888 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5889 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5890 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5892 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5894 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5895 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5896 437adc9d 2020-12-10 yzhong if (err)
5897 437adc9d 2020-12-10 yzhong goto done;
5900 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5901 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5902 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5903 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5904 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5905 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5906 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5907 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5909 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5910 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5911 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5912 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5913 72fd46fa 2019-09-06 stsp !have_staged_files);
5915 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5916 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5917 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5918 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5919 72fd46fa 2019-09-06 stsp !have_staged_files);
5921 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5923 437adc9d 2020-12-10 yzhong goto done;
5924 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5925 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5926 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5927 3969253a 2020-03-07 stsp if (err) {
5928 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5929 437adc9d 2020-12-10 yzhong goto done;
5931 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5932 3969253a 2020-03-07 stsp if (err) {
5933 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5934 437adc9d 2020-12-10 yzhong goto done;
5937 437adc9d 2020-12-10 yzhong free(relpath);
5938 437adc9d 2020-12-10 yzhong relpath = NULL;
5941 437adc9d 2020-12-10 yzhong free(relpath);
5942 d56d26ce 2019-05-10 stsp return err;
5946 d56d26ce 2019-05-10 stsp static const struct got_error *
5947 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5948 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5949 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5950 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5951 735ef5ac 2019-08-03 stsp int ood_errcode)
5953 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5954 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5955 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5957 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5958 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5959 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5960 1a36436d 2019-06-10 stsp return NULL;
5962 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5963 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5965 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5967 a44927cc 2022-04-07 stsp goto done;
5968 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5969 9bead371 2019-07-28 stsp if (err) {
5970 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5971 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5972 1a36436d 2019-06-10 stsp goto done;
5973 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5974 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5976 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5977 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5979 a44927cc 2022-04-07 stsp goto done;
5980 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5981 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5982 1a36436d 2019-06-10 stsp goto done;
5983 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5987 a44927cc 2022-04-07 stsp if (commit)
5988 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5989 1a36436d 2019-06-10 stsp return err;
5992 336075a4 2022-06-25 op static const struct got_error *
5993 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5994 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5995 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
5996 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
5997 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
5998 2a47b1e5 2022-11-01 stsp const char *author, const char *committer, char *diff_path,
5999 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6000 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6001 afa376bf 2019-05-09 stsp struct got_repository *repo)
6003 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6004 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6005 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6006 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6007 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6008 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6009 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6010 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6011 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
6012 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6013 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6014 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6015 f8399b8f 2022-07-22 stsp time_t timestamp;
6017 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6019 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6021 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6023 588edf97 2019-05-10 stsp goto done;
6025 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6027 588edf97 2019-05-10 stsp goto done;
6029 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6030 2a47b1e5 2022-11-01 stsp err = commit_msg_cb(commitable_paths, diff_path,
6031 2a47b1e5 2022-11-01 stsp &logmsg, commit_arg);
6036 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6037 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6041 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6042 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6043 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6044 cf066bf8 2019-05-09 stsp char *ondisk_path;
6046 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6047 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6048 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6051 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6052 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6053 12383673 2023-02-18 mark ct->status != GOT_STATUS_MODE_CHANGE &&
6054 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
6057 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6058 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6059 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6060 cf066bf8 2019-05-09 stsp goto done;
6062 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6063 2e1fa222 2020-07-23 stsp free(ondisk_path);
6065 cf066bf8 2019-05-09 stsp goto done;
6068 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6069 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6070 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6072 036813ee 2019-05-09 stsp goto done;
6074 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6076 de18fc63 2019-05-09 stsp goto done;
6077 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6078 f259c4c1 2021-09-24 stsp nparents++;
6079 f259c4c1 2021-09-24 stsp if (parent_id2) {
6080 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
6082 f259c4c1 2021-09-24 stsp goto done;
6083 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6084 f259c4c1 2021-09-24 stsp nparents++;
6086 f8399b8f 2022-07-22 stsp timestamp = time(NULL);
6087 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6088 f8399b8f 2022-07-22 stsp nparents, author, timestamp, committer, timestamp, logmsg, repo);
6089 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6090 33ad4cbe 2019-05-12 jcs free(logmsg);
6092 9d40349a 2019-05-09 stsp goto done;
6094 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6095 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6096 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6097 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6098 09f5bd90 2019-05-09 stsp goto done;
6100 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6101 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6103 09f5bd90 2019-05-09 stsp goto done;
6104 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6106 ebf99748 2019-05-09 stsp goto done;
6107 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6108 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6109 09f5bd90 2019-05-09 stsp goto done;
6111 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6112 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6114 f2c16586 2019-05-09 stsp goto done;
6115 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6117 f2c16586 2019-05-09 stsp goto done;
6119 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6121 09f5bd90 2019-05-09 stsp goto done;
6123 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6125 ebf99748 2019-05-09 stsp goto done;
6127 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
6128 588edf97 2019-05-10 stsp if (head_tree)
6129 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6130 588edf97 2019-05-10 stsp if (head_commit)
6131 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6132 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6133 2f17228e 2019-05-12 stsp if (head_ref2) {
6134 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6135 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6136 2f17228e 2019-05-12 stsp err = unlockerr;
6137 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6139 39cd0ff6 2019-07-12 stsp return err;
6142 5c1e53bc 2019-07-28 stsp static const struct got_error *
6143 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6144 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6146 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6147 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6149 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6150 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6151 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6153 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6154 5c1e53bc 2019-07-28 stsp ct_path++;
6156 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6157 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6161 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6162 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6164 5c1e53bc 2019-07-28 stsp return NULL;
6167 f0b75401 2019-08-03 stsp static const struct got_error *
6168 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6170 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6172 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6173 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6174 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6177 f0b75401 2019-08-03 stsp return NULL;
6180 5f8a88c6 2019-08-03 stsp static const struct got_error *
6181 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6182 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6184 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6185 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6187 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6188 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6190 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6191 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6192 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6193 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6194 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6195 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6198 5f8a88c6 2019-08-03 stsp return NULL;
6201 39cd0ff6 2019-07-12 stsp const struct got_error *
6202 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6203 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6204 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6205 12383673 2023-02-18 mark int show_diff, int commit_conflicts,
6206 12383673 2023-02-18 mark got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6207 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6208 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6210 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6211 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6212 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6213 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6214 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6215 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6216 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6217 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6218 2a47b1e5 2022-11-01 stsp char *diff_path = NULL;
6219 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6221 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6223 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6224 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6226 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6228 39cd0ff6 2019-07-12 stsp goto done;
6230 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6232 39cd0ff6 2019-07-12 stsp goto done;
6234 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6236 39cd0ff6 2019-07-12 stsp goto done;
6238 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6240 39cd0ff6 2019-07-12 stsp goto done;
6242 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6243 5f8a88c6 2019-08-03 stsp &have_staged_files);
6244 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6245 5f8a88c6 2019-08-03 stsp goto done;
6246 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6247 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6249 5f8a88c6 2019-08-03 stsp goto done;
6252 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6253 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6254 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6255 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6256 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6257 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6258 2a47b1e5 2022-11-01 stsp cc_arg.diff_header_shown = 0;
6259 12383673 2023-02-18 mark cc_arg.commit_conflicts = commit_conflicts;
6260 2a47b1e5 2022-11-01 stsp if (show_diff) {
6261 2a47b1e5 2022-11-01 stsp err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6262 b90054ed 2022-11-01 stsp GOT_TMPDIR_STR "/got", ".diff");
6264 2a47b1e5 2022-11-01 stsp goto done;
6265 2a47b1e5 2022-11-01 stsp cc_arg.f1 = got_opentemp();
6266 2a47b1e5 2022-11-01 stsp if (cc_arg.f1 == NULL) {
6267 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6268 2a47b1e5 2022-11-01 stsp goto done;
6270 2a47b1e5 2022-11-01 stsp cc_arg.f2 = got_opentemp();
6271 2a47b1e5 2022-11-01 stsp if (cc_arg.f2 == NULL) {
6272 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6273 2a47b1e5 2022-11-01 stsp goto done;
6277 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6278 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6279 4ba2e955 2022-09-02 sh+got collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6281 5c1e53bc 2019-07-28 stsp goto done;
6284 2a47b1e5 2022-11-01 stsp if (show_diff) {
6285 2a47b1e5 2022-11-01 stsp if (fflush(cc_arg.diff_outfile) == EOF) {
6286 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fflush");
6287 2a47b1e5 2022-11-01 stsp goto done;
6291 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6292 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6293 39cd0ff6 2019-07-12 stsp goto done;
6296 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6297 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6299 5c1e53bc 2019-07-28 stsp goto done;
6302 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6303 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6304 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6306 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6307 f0b75401 2019-08-03 stsp ct_path++;
6308 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6309 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6310 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6312 b50cabdf 2019-07-12 stsp goto done;
6316 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6317 210c2321 2022-11-01 stsp head_commit_id, NULL, worktree, author, committer,
6318 210c2321 2022-11-01 stsp (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6319 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6321 39cd0ff6 2019-07-12 stsp goto done;
6323 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6324 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6325 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6326 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6327 39cd0ff6 2019-07-12 stsp err = sync_err;
6329 39cd0ff6 2019-07-12 stsp if (fileindex)
6330 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6331 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6332 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6333 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6334 39cd0ff6 2019-07-12 stsp err = unlockerr;
6335 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6336 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6338 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6340 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6341 2a47b1e5 2022-11-01 stsp if (diff_path && unlink(diff_path) == -1 && err == NULL)
6342 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("unlink", diff_path);
6343 2a47b1e5 2022-11-01 stsp free(diff_path);
6344 2a47b1e5 2022-11-01 stsp if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6345 2a47b1e5 2022-11-01 stsp err == NULL)
6346 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
6347 c4296144 2019-05-09 stsp return err;
6350 8656d6c4 2019-05-20 stsp const char *
6351 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6353 8656d6c4 2019-05-20 stsp return ct->path;
6356 8656d6c4 2019-05-20 stsp unsigned int
6357 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6359 8656d6c4 2019-05-20 stsp return ct->status;
6362 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6363 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6364 818c7501 2019-07-11 stsp struct got_repository *repo;
6367 818c7501 2019-07-11 stsp static const struct got_error *
6368 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6370 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6371 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6372 818c7501 2019-07-11 stsp unsigned char status;
6373 818c7501 2019-07-11 stsp struct stat sb;
6374 818c7501 2019-07-11 stsp char *ondisk_path;
6376 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6377 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6378 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6379 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6381 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6383 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6385 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6386 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6387 818c7501 2019-07-11 stsp free(ondisk_path);
6389 818c7501 2019-07-11 stsp return err;
6391 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6392 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6393 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6394 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6396 818c7501 2019-07-11 stsp return NULL;
6399 818c7501 2019-07-11 stsp const struct got_error *
6400 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6401 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6402 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6403 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6405 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6406 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6407 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6408 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6409 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6410 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6411 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6413 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6414 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6415 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6417 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6419 818c7501 2019-07-11 stsp return err;
6421 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6423 818c7501 2019-07-11 stsp goto done;
6425 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6426 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6427 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6430 818c7501 2019-07-11 stsp goto done;
6432 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6434 818c7501 2019-07-11 stsp goto done;
6436 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6438 818c7501 2019-07-11 stsp goto done;
6440 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6442 818c7501 2019-07-11 stsp goto done;
6444 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6447 e51d7b55 2020-01-04 stsp goto done;
6449 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6451 818c7501 2019-07-11 stsp goto done;
6452 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6453 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6454 e51d7b55 2020-01-04 stsp goto done;
6457 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6458 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6460 818c7501 2019-07-11 stsp goto done;
6461 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6463 818c7501 2019-07-11 stsp goto done;
6465 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6467 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6469 818c7501 2019-07-11 stsp goto done;
6471 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6473 818c7501 2019-07-11 stsp goto done;
6475 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6476 818c7501 2019-07-11 stsp worktree->base_commit_id);
6478 818c7501 2019-07-11 stsp goto done;
6479 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6481 818c7501 2019-07-11 stsp goto done;
6483 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6485 818c7501 2019-07-11 stsp goto done;
6487 818c7501 2019-07-11 stsp free(fileindex_path);
6488 818c7501 2019-07-11 stsp free(tmp_branch_name);
6489 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6490 818c7501 2019-07-11 stsp free(branch_ref_name);
6491 818c7501 2019-07-11 stsp if (branch_ref)
6492 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6493 818c7501 2019-07-11 stsp if (wt_branch)
6494 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6495 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6496 818c7501 2019-07-11 stsp if (err) {
6497 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6498 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6499 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6501 818c7501 2019-07-11 stsp if (*tmp_branch) {
6502 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6503 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6505 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6506 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6507 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6509 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6511 818c7501 2019-07-11 stsp return err;
6514 818c7501 2019-07-11 stsp const struct got_error *
6515 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6516 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6517 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6518 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6520 818c7501 2019-07-11 stsp const struct got_error *err;
6521 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6522 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6523 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6524 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6525 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6527 818c7501 2019-07-11 stsp *commit_id = NULL;
6528 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6529 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6530 3e3a69f1 2019-07-25 stsp *branch = NULL;
6531 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6533 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6535 3e3a69f1 2019-07-25 stsp return err;
6537 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6539 f032f1f7 2019-08-04 stsp goto done;
6541 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6542 f032f1f7 2019-08-04 stsp &have_staged_files);
6543 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6544 f032f1f7 2019-08-04 stsp goto done;
6545 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6546 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6547 3e3a69f1 2019-07-25 stsp goto done;
6550 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6552 f032f1f7 2019-08-04 stsp goto done;
6554 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6556 818c7501 2019-07-11 stsp goto done;
6558 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6560 818c7501 2019-07-11 stsp goto done;
6562 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6564 818c7501 2019-07-11 stsp goto done;
6566 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6568 818c7501 2019-07-11 stsp goto done;
6570 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6571 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6573 818c7501 2019-07-11 stsp goto done;
6575 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6577 818c7501 2019-07-11 stsp goto done;
6579 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6581 818c7501 2019-07-11 stsp goto done;
6583 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6584 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6586 818c7501 2019-07-11 stsp goto done;
6588 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6590 818c7501 2019-07-11 stsp goto done;
6592 818c7501 2019-07-11 stsp free(commit_ref_name);
6593 818c7501 2019-07-11 stsp free(branch_ref_name);
6594 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6595 818c7501 2019-07-11 stsp if (commit_ref)
6596 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6597 818c7501 2019-07-11 stsp if (branch_ref)
6598 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6599 818c7501 2019-07-11 stsp if (err) {
6600 818c7501 2019-07-11 stsp free(*commit_id);
6601 818c7501 2019-07-11 stsp *commit_id = NULL;
6602 818c7501 2019-07-11 stsp if (*tmp_branch) {
6603 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6604 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6606 818c7501 2019-07-11 stsp if (*new_base_branch) {
6607 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6608 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6610 818c7501 2019-07-11 stsp if (*branch) {
6611 818c7501 2019-07-11 stsp got_ref_close(*branch);
6612 818c7501 2019-07-11 stsp *branch = NULL;
6614 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6615 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6616 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6618 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6620 818c7501 2019-07-11 stsp return err;
6623 818c7501 2019-07-11 stsp const struct got_error *
6624 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6626 818c7501 2019-07-11 stsp const struct got_error *err;
6627 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6629 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6631 818c7501 2019-07-11 stsp return err;
6633 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6634 818c7501 2019-07-11 stsp free(tmp_branch_name);
6635 818c7501 2019-07-11 stsp return NULL;
6638 818c7501 2019-07-11 stsp static const struct got_error *
6639 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6640 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
6642 0ebf8283 2019-07-24 stsp *logmsg = arg;
6643 818c7501 2019-07-11 stsp return NULL;
6646 818c7501 2019-07-11 stsp static const struct got_error *
6647 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6648 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6649 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6650 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6652 818c7501 2019-07-11 stsp return NULL;
6655 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6656 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6657 01757395 2019-07-12 stsp void *progress_arg;
6658 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6661 01757395 2019-07-12 stsp static const struct got_error *
6662 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6664 01757395 2019-07-12 stsp const struct got_error *err;
6665 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6667 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6669 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6671 01757395 2019-07-12 stsp return err;
6673 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6674 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6675 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6676 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6677 01757395 2019-07-12 stsp return NULL;
6679 01757395 2019-07-12 stsp p = strdup(path);
6680 01757395 2019-07-12 stsp if (p == NULL)
6681 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6683 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6684 01757395 2019-07-12 stsp if (err || new == NULL)
6686 01757395 2019-07-12 stsp return err;
6689 0ebf8283 2019-07-24 stsp static const struct got_error *
6690 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6691 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6693 818c7501 2019-07-11 stsp const struct got_error *err;
6694 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6696 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6697 818c7501 2019-07-11 stsp if (err) {
6698 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6699 818c7501 2019-07-11 stsp goto done;
6700 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6702 818c7501 2019-07-11 stsp goto done;
6703 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6705 818c7501 2019-07-11 stsp goto done;
6706 de05890f 2020-03-05 stsp } else if (is_rebase) {
6707 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6710 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6712 818c7501 2019-07-11 stsp goto done;
6713 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6714 818c7501 2019-07-11 stsp free(stored_id);
6715 818c7501 2019-07-11 stsp if (cmp != 0) {
6716 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6717 818c7501 2019-07-11 stsp goto done;
6721 0ebf8283 2019-07-24 stsp if (commit_ref)
6722 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6723 0ebf8283 2019-07-24 stsp return err;
6726 0ebf8283 2019-07-24 stsp static const struct got_error *
6727 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6728 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6729 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6730 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6731 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6732 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6734 0ebf8283 2019-07-24 stsp const struct got_error *err;
6735 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6736 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6737 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6739 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6741 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6743 0ebf8283 2019-07-24 stsp return err;
6745 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6746 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6747 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6748 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6749 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6750 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6751 818c7501 2019-07-11 stsp if (commit_ref)
6752 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6753 818c7501 2019-07-11 stsp return err;
6756 818c7501 2019-07-11 stsp const struct got_error *
6757 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6758 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6759 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6760 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6761 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6762 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6764 0ebf8283 2019-07-24 stsp const struct got_error *err;
6765 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6767 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6769 0ebf8283 2019-07-24 stsp return err;
6771 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6773 0ebf8283 2019-07-24 stsp goto done;
6775 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6776 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6777 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6779 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6780 0ebf8283 2019-07-24 stsp return err;
6783 0ebf8283 2019-07-24 stsp const struct got_error *
6784 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6785 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6786 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6787 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6788 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6789 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6791 0ebf8283 2019-07-24 stsp const struct got_error *err;
6792 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6794 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6796 0ebf8283 2019-07-24 stsp return err;
6798 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6800 0ebf8283 2019-07-24 stsp goto done;
6802 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6803 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6804 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6806 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6807 0ebf8283 2019-07-24 stsp return err;
6810 0ebf8283 2019-07-24 stsp static const struct got_error *
6811 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6812 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6813 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6814 598eac43 2022-07-22 stsp struct got_reference *tmp_branch, const char *committer,
6815 598eac43 2022-07-22 stsp struct got_commit_object *orig_commit, const char *new_logmsg,
6816 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
6818 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6819 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6820 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6821 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6822 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6823 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6824 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6826 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6827 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6828 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6830 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6832 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6834 3e3a69f1 2019-07-25 stsp return err;
6836 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6837 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6838 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6839 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6840 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
6842 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6843 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6845 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6846 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6847 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6848 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6849 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6850 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6852 01757395 2019-07-12 stsp if (merged_paths) {
6853 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6854 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6855 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6856 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6859 01757395 2019-07-12 stsp goto done;
6862 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6863 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6865 01757395 2019-07-12 stsp goto done;
6868 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6869 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6870 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6872 a0e95631 2019-07-12 stsp goto done;
6873 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6874 a0e95631 2019-07-12 stsp goto done;
6877 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6879 edd02c5e 2019-07-11 stsp goto done;
6881 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6883 818c7501 2019-07-11 stsp goto done;
6885 5943eee2 2019-08-13 stsp if (new_logmsg) {
6886 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6887 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6888 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6889 5943eee2 2019-08-13 stsp goto done;
6892 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6894 5943eee2 2019-08-13 stsp goto done;
6897 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6898 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6899 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
6900 50e7a649 2022-07-22 stsp committer ? committer :
6901 2a47b1e5 2022-11-01 stsp got_object_commit_get_committer(orig_commit), NULL,
6902 50e7a649 2022-07-22 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6904 a0e95631 2019-07-12 stsp goto done;
6906 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6908 a0e95631 2019-07-12 stsp goto done;
6910 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6912 a0e95631 2019-07-12 stsp goto done;
6914 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6915 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6916 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6917 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6918 a0e95631 2019-07-12 stsp err = sync_err;
6920 a0e95631 2019-07-12 stsp free(fileindex_path);
6921 a0e95631 2019-07-12 stsp free(head_commit_id);
6922 a0e95631 2019-07-12 stsp if (head_ref)
6923 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6924 818c7501 2019-07-11 stsp if (err) {
6925 818c7501 2019-07-11 stsp free(*new_commit_id);
6926 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6928 818c7501 2019-07-11 stsp return err;
6931 818c7501 2019-07-11 stsp const struct got_error *
6932 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6933 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6934 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6935 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
6936 12383673 2023-02-18 mark struct got_object_id *orig_commit_id, int allow_conflict,
6937 12383673 2023-02-18 mark struct got_repository *repo)
6939 0ebf8283 2019-07-24 stsp const struct got_error *err;
6940 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6941 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6942 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6944 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6946 0ebf8283 2019-07-24 stsp return err;
6948 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6950 0ebf8283 2019-07-24 stsp goto done;
6951 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6953 0ebf8283 2019-07-24 stsp goto done;
6954 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6955 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6956 0ebf8283 2019-07-24 stsp goto done;
6959 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6960 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
6961 12383673 2023-02-18 mark NULL, allow_conflict, repo);
6963 0ebf8283 2019-07-24 stsp if (commit_ref)
6964 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6965 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6966 0ebf8283 2019-07-24 stsp free(commit_id);
6967 0ebf8283 2019-07-24 stsp return err;
6970 0ebf8283 2019-07-24 stsp const struct got_error *
6971 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6972 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6973 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6974 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
6975 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6976 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
6978 0ebf8283 2019-07-24 stsp const struct got_error *err;
6979 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6980 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6982 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6984 0ebf8283 2019-07-24 stsp return err;
6986 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6988 0ebf8283 2019-07-24 stsp goto done;
6990 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6991 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
6992 12383673 2023-02-18 mark new_logmsg, allow_conflict, repo);
6994 0ebf8283 2019-07-24 stsp if (commit_ref)
6995 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6996 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6997 0ebf8283 2019-07-24 stsp return err;
7000 0ebf8283 2019-07-24 stsp const struct got_error *
7001 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7002 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7004 3e3a69f1 2019-07-25 stsp if (fileindex)
7005 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7006 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7009 69844fba 2019-07-11 stsp static const struct got_error *
7010 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7012 69844fba 2019-07-11 stsp const struct got_error *err;
7013 69844fba 2019-07-11 stsp struct got_reference *ref;
7015 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7016 69844fba 2019-07-11 stsp if (err) {
7017 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7018 69844fba 2019-07-11 stsp return NULL;
7019 69844fba 2019-07-11 stsp return err;
7022 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7023 69844fba 2019-07-11 stsp got_ref_close(ref);
7024 69844fba 2019-07-11 stsp return err;
7027 69844fba 2019-07-11 stsp static const struct got_error *
7028 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7030 69844fba 2019-07-11 stsp const struct got_error *err;
7031 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7032 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7034 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7036 69844fba 2019-07-11 stsp goto done;
7037 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7039 69844fba 2019-07-11 stsp goto done;
7041 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7043 69844fba 2019-07-11 stsp goto done;
7044 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7046 69844fba 2019-07-11 stsp goto done;
7048 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7050 69844fba 2019-07-11 stsp goto done;
7051 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7053 69844fba 2019-07-11 stsp goto done;
7055 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7057 69844fba 2019-07-11 stsp goto done;
7058 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7060 69844fba 2019-07-11 stsp goto done;
7063 69844fba 2019-07-11 stsp free(tmp_branch_name);
7064 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7065 69844fba 2019-07-11 stsp free(branch_ref_name);
7066 69844fba 2019-07-11 stsp free(commit_ref_name);
7067 e600f124 2021-03-21 stsp return err;
7070 336075a4 2022-06-25 op static const struct got_error *
7071 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7072 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7074 e600f124 2021-03-21 stsp const struct got_error *err;
7075 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7076 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7077 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7078 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7079 e600f124 2021-03-21 stsp char *refname = NULL;
7081 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7082 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7083 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7084 e600f124 2021-03-21 stsp branch_name += 11;
7086 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7088 e600f124 2021-03-21 stsp return err;
7090 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7091 e600f124 2021-03-21 stsp new_id_str) == -1) {
7092 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7093 e600f124 2021-03-21 stsp goto done;
7096 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7098 e600f124 2021-03-21 stsp goto done;
7100 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7102 e600f124 2021-03-21 stsp goto done;
7104 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7106 e600f124 2021-03-21 stsp free(new_id_str);
7107 e600f124 2021-03-21 stsp free(refname);
7108 e600f124 2021-03-21 stsp free(old_commit_id);
7110 e600f124 2021-03-21 stsp got_ref_close(ref);
7111 69844fba 2019-07-11 stsp return err;
7114 818c7501 2019-07-11 stsp const struct got_error *
7115 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7116 ef85a376 2023-02-01 mark struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7117 ef85a376 2023-02-01 mark struct got_reference *rebased_branch, struct got_repository *repo,
7118 ef85a376 2023-02-01 mark int create_backup)
7120 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7121 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7122 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7124 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7126 818c7501 2019-07-11 stsp return err;
7128 e600f124 2021-03-21 stsp if (create_backup) {
7129 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7130 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7132 e600f124 2021-03-21 stsp goto done;
7135 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7137 818c7501 2019-07-11 stsp goto done;
7139 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7141 818c7501 2019-07-11 stsp goto done;
7143 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7145 818c7501 2019-07-11 stsp goto done;
7147 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7149 a615e0e7 2020-12-16 stsp goto done;
7151 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7153 a615e0e7 2020-12-16 stsp goto done;
7154 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7155 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7156 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7157 a615e0e7 2020-12-16 stsp err = sync_err;
7159 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7160 a615e0e7 2020-12-16 stsp free(fileindex_path);
7161 818c7501 2019-07-11 stsp free(new_head_commit_id);
7162 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7163 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7164 818c7501 2019-07-11 stsp err = unlockerr;
7165 818c7501 2019-07-11 stsp return err;
7168 818c7501 2019-07-11 stsp const struct got_error *
7169 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7170 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7171 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7172 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7174 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7175 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7176 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7177 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7178 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7179 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7180 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7182 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7184 818c7501 2019-07-11 stsp return err;
7186 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7187 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7189 a44927cc 2022-04-07 stsp goto done;
7191 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7192 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7194 818c7501 2019-07-11 stsp goto done;
7196 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7198 818c7501 2019-07-11 stsp goto done;
7201 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7202 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7203 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7205 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7207 818c7501 2019-07-11 stsp goto done;
7209 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7211 818c7501 2019-07-11 stsp goto done;
7213 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7214 a44927cc 2022-04-07 stsp worktree->path_prefix);
7216 ca355955 2019-07-12 stsp goto done;
7218 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7220 ca355955 2019-07-12 stsp goto done;
7222 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7224 818c7501 2019-07-11 stsp goto done;
7226 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7227 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7228 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7229 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7230 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7231 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7232 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7233 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7234 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7235 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7237 1f1abb7e 2019-08-08 stsp goto sync;
7239 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7240 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7242 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7243 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7244 ca355955 2019-07-12 stsp err = sync_err;
7246 818c7501 2019-07-11 stsp got_ref_close(resolved);
7247 a3a2faf2 2019-07-12 stsp free(tree_id);
7248 818c7501 2019-07-11 stsp free(commit_id);
7249 a44927cc 2022-04-07 stsp if (commit)
7250 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7251 0ebf8283 2019-07-24 stsp if (fileindex)
7252 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7253 0ebf8283 2019-07-24 stsp free(fileindex_path);
7255 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7256 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7257 0ebf8283 2019-07-24 stsp err = unlockerr;
7258 0ebf8283 2019-07-24 stsp return err;
7261 0ebf8283 2019-07-24 stsp const struct got_error *
7262 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7263 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7264 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7265 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7267 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7268 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7269 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7270 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7271 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7272 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7273 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7274 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7276 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7277 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7278 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7279 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7281 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7283 0ebf8283 2019-07-24 stsp return err;
7285 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7287 0ebf8283 2019-07-24 stsp goto done;
7289 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7290 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7291 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7294 0ebf8283 2019-07-24 stsp goto done;
7296 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7298 0ebf8283 2019-07-24 stsp goto done;
7300 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7302 0ebf8283 2019-07-24 stsp goto done;
7304 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7305 0ebf8283 2019-07-24 stsp worktree);
7307 0ebf8283 2019-07-24 stsp goto done;
7309 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7312 0ebf8283 2019-07-24 stsp goto done;
7314 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7316 0ebf8283 2019-07-24 stsp goto done;
7318 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7320 0ebf8283 2019-07-24 stsp goto done;
7322 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7323 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7325 0ebf8283 2019-07-24 stsp goto done;
7326 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7328 0ebf8283 2019-07-24 stsp goto done;
7329 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7330 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7331 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7332 0ebf8283 2019-07-24 stsp goto done;
7335 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7336 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7338 0ebf8283 2019-07-24 stsp goto done;
7339 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7341 0ebf8283 2019-07-24 stsp goto done;
7343 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7345 0ebf8283 2019-07-24 stsp goto done;
7347 0ebf8283 2019-07-24 stsp free(fileindex_path);
7348 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7349 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7350 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7351 0ebf8283 2019-07-24 stsp if (wt_branch)
7352 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7353 0ebf8283 2019-07-24 stsp if (err) {
7354 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7355 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7356 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7358 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7359 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7360 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7362 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7363 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7364 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7365 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7367 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7369 0ebf8283 2019-07-24 stsp return err;
7372 0ebf8283 2019-07-24 stsp const struct got_error *
7373 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7374 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7376 3e3a69f1 2019-07-25 stsp if (fileindex)
7377 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7378 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7381 0ebf8283 2019-07-24 stsp const struct got_error *
7382 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7383 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7385 0ebf8283 2019-07-24 stsp const struct got_error *err;
7386 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7388 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7390 0ebf8283 2019-07-24 stsp return err;
7392 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7393 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7394 0ebf8283 2019-07-24 stsp return NULL;
7397 0ebf8283 2019-07-24 stsp const struct got_error *
7398 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7399 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7400 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7401 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7403 0ebf8283 2019-07-24 stsp const struct got_error *err;
7404 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7405 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7406 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7407 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7408 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7409 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7411 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7412 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7413 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7414 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7416 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7418 3e3a69f1 2019-07-25 stsp return err;
7420 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7422 f032f1f7 2019-08-04 stsp goto done;
7424 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7425 f032f1f7 2019-08-04 stsp &have_staged_files);
7426 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7427 f032f1f7 2019-08-04 stsp goto done;
7428 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7429 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7430 3e3a69f1 2019-07-25 stsp goto done;
7433 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7435 f032f1f7 2019-08-04 stsp goto done;
7437 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7439 0ebf8283 2019-07-24 stsp goto done;
7441 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7443 0ebf8283 2019-07-24 stsp goto done;
7445 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7446 0ebf8283 2019-07-24 stsp worktree);
7448 0ebf8283 2019-07-24 stsp goto done;
7450 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7452 0ebf8283 2019-07-24 stsp goto done;
7454 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7456 0ebf8283 2019-07-24 stsp goto done;
7457 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7459 0ebf8283 2019-07-24 stsp goto done;
7461 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7463 0ebf8283 2019-07-24 stsp goto done;
7464 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7466 0ebf8283 2019-07-24 stsp goto done;
7468 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7470 0ebf8283 2019-07-24 stsp goto done;
7472 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7473 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7474 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7475 0ebf8283 2019-07-24 stsp if (commit_ref)
7476 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7477 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7478 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7479 0ebf8283 2019-07-24 stsp if (err) {
7480 0ebf8283 2019-07-24 stsp free(*commit_id);
7481 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7482 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7483 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7484 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7485 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7486 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7488 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7489 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7490 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7492 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7494 0ebf8283 2019-07-24 stsp return err;
7497 0ebf8283 2019-07-24 stsp static const struct got_error *
7498 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7500 0ebf8283 2019-07-24 stsp const struct got_error *err;
7501 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7502 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7504 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7506 0ebf8283 2019-07-24 stsp goto done;
7507 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7509 0ebf8283 2019-07-24 stsp goto done;
7511 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7512 0ebf8283 2019-07-24 stsp worktree);
7514 0ebf8283 2019-07-24 stsp goto done;
7515 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7517 0ebf8283 2019-07-24 stsp goto done;
7519 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7521 0ebf8283 2019-07-24 stsp goto done;
7522 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7524 0ebf8283 2019-07-24 stsp goto done;
7526 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7528 0ebf8283 2019-07-24 stsp goto done;
7529 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7531 0ebf8283 2019-07-24 stsp goto done;
7533 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7534 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7535 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7536 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7537 0ebf8283 2019-07-24 stsp return err;
7540 0ebf8283 2019-07-24 stsp const struct got_error *
7541 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7542 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7543 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7544 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7546 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7547 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7548 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7549 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7550 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7551 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7553 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7555 0ebf8283 2019-07-24 stsp return err;
7557 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7558 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7560 a44927cc 2022-04-07 stsp goto done;
7562 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7563 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7565 0ebf8283 2019-07-24 stsp goto done;
7567 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7569 0ebf8283 2019-07-24 stsp goto done;
7571 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7573 0ebf8283 2019-07-24 stsp goto done;
7575 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7576 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7578 0ebf8283 2019-07-24 stsp goto done;
7580 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7582 0ebf8283 2019-07-24 stsp goto done;
7584 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7586 0ebf8283 2019-07-24 stsp goto done;
7588 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7589 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7590 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7591 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7592 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7593 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7594 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7595 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7596 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7597 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7599 1f1abb7e 2019-08-08 stsp goto sync;
7601 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7602 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7604 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7605 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7606 0ebf8283 2019-07-24 stsp err = sync_err;
7608 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7609 0ebf8283 2019-07-24 stsp free(tree_id);
7610 818c7501 2019-07-11 stsp free(fileindex_path);
7612 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7613 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7614 818c7501 2019-07-11 stsp err = unlockerr;
7615 818c7501 2019-07-11 stsp return err;
7618 0ebf8283 2019-07-24 stsp const struct got_error *
7619 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7620 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7621 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7623 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7624 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7625 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7626 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7628 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7630 0ebf8283 2019-07-24 stsp return err;
7632 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7633 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7635 e600f124 2021-03-21 stsp goto done;
7637 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7638 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7640 0ebf8283 2019-07-24 stsp goto done;
7642 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7644 0ebf8283 2019-07-24 stsp goto done;
7646 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7648 0ebf8283 2019-07-24 stsp goto done;
7650 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7652 0ebf8283 2019-07-24 stsp goto done;
7654 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7656 a615e0e7 2020-12-16 stsp goto done;
7658 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7660 a615e0e7 2020-12-16 stsp goto done;
7661 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7662 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7663 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7664 a615e0e7 2020-12-16 stsp err = sync_err;
7666 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7667 a615e0e7 2020-12-16 stsp free(fileindex_path);
7668 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7669 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7670 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7671 0ebf8283 2019-07-24 stsp err = unlockerr;
7672 0ebf8283 2019-07-24 stsp return err;
7675 0ebf8283 2019-07-24 stsp const struct got_error *
7676 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7677 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7679 0ebf8283 2019-07-24 stsp const struct got_error *err;
7680 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7682 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7684 0ebf8283 2019-07-24 stsp return err;
7686 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7688 0ebf8283 2019-07-24 stsp goto done;
7690 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7692 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7693 2822a352 2019-10-15 stsp return err;
7696 2822a352 2019-10-15 stsp const struct got_error *
7697 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7698 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7699 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7700 2822a352 2019-10-15 stsp struct got_repository *repo)
7702 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7703 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7704 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7706 2822a352 2019-10-15 stsp *fileindex = NULL;
7707 2822a352 2019-10-15 stsp *branch_ref = NULL;
7708 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7710 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7712 2822a352 2019-10-15 stsp return err;
7714 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7715 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7716 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7717 2822a352 2019-10-15 stsp "update -b or different branch name required");
7718 2822a352 2019-10-15 stsp goto done;
7721 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7723 2822a352 2019-10-15 stsp goto done;
7725 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7726 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7727 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7728 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7731 2822a352 2019-10-15 stsp goto done;
7733 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7735 2822a352 2019-10-15 stsp goto done;
7737 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7738 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7740 2822a352 2019-10-15 stsp if (err) {
7741 2822a352 2019-10-15 stsp if (*branch_ref) {
7742 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7743 2822a352 2019-10-15 stsp *branch_ref = NULL;
7745 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7746 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7747 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7749 2822a352 2019-10-15 stsp if (*fileindex) {
7750 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7751 2822a352 2019-10-15 stsp *fileindex = NULL;
7753 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7755 0cb83759 2019-08-03 stsp return err;
7758 2822a352 2019-10-15 stsp const struct got_error *
7759 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7760 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7761 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7762 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7763 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7765 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7766 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7767 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7768 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7770 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7772 2822a352 2019-10-15 stsp goto done;
7774 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7776 2822a352 2019-10-15 stsp goto done;
7778 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7780 a44927cc 2022-04-07 stsp goto done;
7782 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7783 2822a352 2019-10-15 stsp worktree->path_prefix);
7785 2822a352 2019-10-15 stsp goto done;
7787 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7789 2822a352 2019-10-15 stsp goto done;
7791 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7792 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7794 2822a352 2019-10-15 stsp goto sync;
7796 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7798 2822a352 2019-10-15 stsp goto sync;
7800 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7802 6e210706 2021-01-22 stsp goto sync;
7804 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7806 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7807 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7808 2822a352 2019-10-15 stsp err = sync_err;
7811 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7812 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7813 2822a352 2019-10-15 stsp err = unlockerr;
7814 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7816 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7817 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7818 2822a352 2019-10-15 stsp err = unlockerr;
7819 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7821 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7822 2822a352 2019-10-15 stsp free(fileindex_path);
7823 2822a352 2019-10-15 stsp free(tree_id);
7824 a44927cc 2022-04-07 stsp if (commit)
7825 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7827 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7828 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7829 2822a352 2019-10-15 stsp err = unlockerr;
7830 2822a352 2019-10-15 stsp return err;
7833 2822a352 2019-10-15 stsp const struct got_error *
7834 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7835 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7836 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7838 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7840 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7842 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7844 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7845 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7846 8b692cd0 2019-10-21 stsp err = unlockerr;
7847 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7849 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7850 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7851 8b692cd0 2019-10-21 stsp err = unlockerr;
7852 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7854 f259c4c1 2021-09-24 stsp return err;
7857 f259c4c1 2021-09-24 stsp const struct got_error *
7858 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
7859 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
7861 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
7862 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7864 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7866 f259c4c1 2021-09-24 stsp goto done;
7868 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7870 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
7871 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7872 f259c4c1 2021-09-24 stsp err = sync_err;
7874 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7875 f259c4c1 2021-09-24 stsp free(fileindex_path);
7876 8b692cd0 2019-10-21 stsp return err;
7879 f259c4c1 2021-09-24 stsp static const struct got_error *
7880 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7882 f259c4c1 2021-09-24 stsp const struct got_error *err;
7883 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7885 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7887 f259c4c1 2021-09-24 stsp goto done;
7888 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
7890 f259c4c1 2021-09-24 stsp goto done;
7892 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7894 f259c4c1 2021-09-24 stsp goto done;
7895 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
7897 f259c4c1 2021-09-24 stsp goto done;
7900 f259c4c1 2021-09-24 stsp free(branch_refname);
7901 f259c4c1 2021-09-24 stsp free(commit_refname);
7902 f259c4c1 2021-09-24 stsp return err;
7905 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
7906 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
7907 f259c4c1 2021-09-24 stsp const char *branch_name;
7910 f259c4c1 2021-09-24 stsp static const struct got_error *
7911 2a47b1e5 2022-11-01 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7912 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
7914 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
7916 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7917 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
7918 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
7920 f259c4c1 2021-09-24 stsp return NULL;
7924 f259c4c1 2021-09-24 stsp const struct got_error *
7925 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
7926 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
7927 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
7928 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
7929 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7930 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7932 f259c4c1 2021-09-24 stsp const struct got_error *err;
7933 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7935 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7937 f259c4c1 2021-09-24 stsp goto done;
7939 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7940 f259c4c1 2021-09-24 stsp worktree);
7942 f259c4c1 2021-09-24 stsp goto done;
7944 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7945 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
7946 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
7948 f259c4c1 2021-09-24 stsp free(fileindex_path);
7949 f259c4c1 2021-09-24 stsp return err;
7952 f259c4c1 2021-09-24 stsp const struct got_error *
7953 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
7954 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7955 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
7956 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
7957 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo,
7958 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
7961 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
7962 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
7963 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
7964 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
7965 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
7966 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
7967 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7968 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
7969 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7971 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
7972 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
7974 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
7976 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7978 f259c4c1 2021-09-24 stsp goto done;
7980 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7982 f259c4c1 2021-09-24 stsp goto done;
7984 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7986 f259c4c1 2021-09-24 stsp goto done;
7988 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7989 f259c4c1 2021-09-24 stsp &have_staged_files);
7990 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7991 f259c4c1 2021-09-24 stsp goto done;
7992 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7993 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7994 f259c4c1 2021-09-24 stsp goto done;
7997 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
7998 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
7999 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
8000 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
8001 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
8002 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8003 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
8004 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8005 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8007 f259c4c1 2021-09-24 stsp goto done;
8009 f259c4c1 2021-09-24 stsp if (TAILQ_EMPTY(&commitable_paths)) {
8010 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8011 f259c4c1 2021-09-24 stsp "merge of %s cannot proceed", branch_name);
8012 f259c4c1 2021-09-24 stsp goto done;
8015 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
8016 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
8017 f259c4c1 2021-09-24 stsp const char *ct_path = ct->in_repo_path;
8019 f259c4c1 2021-09-24 stsp while (ct_path[0] == '/')
8020 f259c4c1 2021-09-24 stsp ct_path++;
8021 f259c4c1 2021-09-24 stsp err = check_out_of_date(ct_path, ct->status,
8022 f259c4c1 2021-09-24 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8023 f259c4c1 2021-09-24 stsp head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8025 f259c4c1 2021-09-24 stsp goto done;
8029 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
8030 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
8031 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
8032 2a47b1e5 2022-11-01 stsp head_commit_id, branch_tip, worktree, author, committer, NULL,
8033 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8035 f259c4c1 2021-09-24 stsp goto done;
8037 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
8038 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
8039 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8040 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8041 f259c4c1 2021-09-24 stsp err = sync_err;
8043 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
8044 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
8046 f259c4c1 2021-09-24 stsp free_commitable(ct);
8048 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8049 f259c4c1 2021-09-24 stsp free(fileindex_path);
8050 f259c4c1 2021-09-24 stsp return err;
8053 f259c4c1 2021-09-24 stsp const struct got_error *
8054 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
8055 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
8057 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8058 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8060 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8062 f259c4c1 2021-09-24 stsp goto done;
8064 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8066 f259c4c1 2021-09-24 stsp goto done;
8067 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8068 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8069 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8070 f259c4c1 2021-09-24 stsp err = sync_err;
8072 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8073 f259c4c1 2021-09-24 stsp free(fileindex_path);
8074 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8075 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8076 f259c4c1 2021-09-24 stsp err = unlockerr;
8077 f259c4c1 2021-09-24 stsp return err;
8080 f259c4c1 2021-09-24 stsp const struct got_error *
8081 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8082 f259c4c1 2021-09-24 stsp struct got_repository *repo)
8084 f259c4c1 2021-09-24 stsp const struct got_error *err;
8085 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
8086 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
8088 f259c4c1 2021-09-24 stsp *in_progress = 0;
8090 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8092 f259c4c1 2021-09-24 stsp return err;
8093 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8094 793fcac3 2021-09-24 stsp free(branch_refname);
8095 f259c4c1 2021-09-24 stsp if (err) {
8096 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
8097 f259c4c1 2021-09-24 stsp return err;
8099 f259c4c1 2021-09-24 stsp *in_progress = 1;
8101 f259c4c1 2021-09-24 stsp return NULL;
8104 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
8105 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
8106 f259c4c1 2021-09-24 stsp struct got_reference *branch, struct got_repository *repo)
8108 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
8109 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8110 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
8111 f259c4c1 2021-09-24 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8112 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL;
8113 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8114 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
8116 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8118 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8120 f259c4c1 2021-09-24 stsp return err;
8122 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8124 f259c4c1 2021-09-24 stsp goto done;
8126 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
8127 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
8128 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
8129 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8132 f259c4c1 2021-09-24 stsp goto done;
8134 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8136 f259c4c1 2021-09-24 stsp return err;
8138 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8140 f259c4c1 2021-09-24 stsp return err;
8142 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8145 f259c4c1 2021-09-24 stsp goto done;
8147 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8149 f259c4c1 2021-09-24 stsp goto done;
8151 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8152 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8153 f259c4c1 2021-09-24 stsp goto done;
8156 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
8158 f259c4c1 2021-09-24 stsp goto done;
8160 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8162 f259c4c1 2021-09-24 stsp goto done;
8163 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
8165 f259c4c1 2021-09-24 stsp goto done;
8167 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8169 f259c4c1 2021-09-24 stsp goto done;
8170 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
8172 f259c4c1 2021-09-24 stsp goto done;
8175 f259c4c1 2021-09-24 stsp free(branch_refname);
8176 f259c4c1 2021-09-24 stsp free(commit_refname);
8177 f259c4c1 2021-09-24 stsp free(fileindex_path);
8178 f259c4c1 2021-09-24 stsp if (branch_ref)
8179 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8180 f259c4c1 2021-09-24 stsp if (commit_ref)
8181 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8182 f259c4c1 2021-09-24 stsp if (wt_branch)
8183 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
8184 f259c4c1 2021-09-24 stsp free(wt_branch_tip);
8185 f259c4c1 2021-09-24 stsp if (err) {
8186 f259c4c1 2021-09-24 stsp if (*fileindex) {
8187 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8188 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8190 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8192 f259c4c1 2021-09-24 stsp return err;
8195 f259c4c1 2021-09-24 stsp const struct got_error *
8196 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
8197 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8198 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8200 f259c4c1 2021-09-24 stsp const struct got_error *err;
8201 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
8202 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8203 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8204 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8206 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8207 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8208 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8210 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8212 f259c4c1 2021-09-24 stsp return err;
8214 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8216 f259c4c1 2021-09-24 stsp goto done;
8218 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8219 f259c4c1 2021-09-24 stsp &have_staged_files);
8220 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8221 f259c4c1 2021-09-24 stsp goto done;
8222 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8223 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
8224 f259c4c1 2021-09-24 stsp goto done;
8227 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8229 f259c4c1 2021-09-24 stsp goto done;
8231 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8233 f259c4c1 2021-09-24 stsp goto done;
8235 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8237 f259c4c1 2021-09-24 stsp goto done;
8239 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
8240 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8241 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
8242 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
8243 f259c4c1 2021-09-24 stsp goto done;
8245 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8246 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
8247 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
8248 f259c4c1 2021-09-24 stsp goto done;
8251 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8253 f259c4c1 2021-09-24 stsp goto done;
8255 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
8257 f259c4c1 2021-09-24 stsp goto done;
8259 f259c4c1 2021-09-24 stsp free(commit_refname);
8260 f259c4c1 2021-09-24 stsp free(branch_refname);
8261 f259c4c1 2021-09-24 stsp free(fileindex_path);
8262 f259c4c1 2021-09-24 stsp if (commit_ref)
8263 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8264 f259c4c1 2021-09-24 stsp if (branch_ref)
8265 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8266 f259c4c1 2021-09-24 stsp if (err) {
8267 f259c4c1 2021-09-24 stsp if (*branch_name) {
8268 f259c4c1 2021-09-24 stsp free(*branch_name);
8269 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8271 f259c4c1 2021-09-24 stsp free(*branch_tip);
8272 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8273 f259c4c1 2021-09-24 stsp if (*fileindex) {
8274 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8275 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8277 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8279 f259c4c1 2021-09-24 stsp return err;
8282 f259c4c1 2021-09-24 stsp const struct got_error *
8283 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
8284 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8285 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8287 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8288 f259c4c1 2021-09-24 stsp struct got_object_id *commit_id = NULL;
8289 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8290 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8291 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
8292 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
8294 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
8295 a44927cc 2022-04-07 stsp worktree->base_commit_id);
8297 a44927cc 2022-04-07 stsp goto done;
8299 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8300 a44927cc 2022-04-07 stsp worktree->path_prefix);
8302 f259c4c1 2021-09-24 stsp goto done;
8304 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8306 f259c4c1 2021-09-24 stsp goto done;
8308 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8310 f259c4c1 2021-09-24 stsp goto done;
8312 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
8313 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
8314 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
8315 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
8316 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
8317 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
8318 f259c4c1 2021-09-24 stsp rfa.repo = repo;
8319 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
8320 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8321 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8323 f259c4c1 2021-09-24 stsp goto sync;
8325 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8326 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8328 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8329 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8330 f259c4c1 2021-09-24 stsp err = sync_err;
8332 f259c4c1 2021-09-24 stsp free(tree_id);
8333 f259c4c1 2021-09-24 stsp free(commit_id);
8334 a44927cc 2022-04-07 stsp if (commit)
8335 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8336 f259c4c1 2021-09-24 stsp if (fileindex)
8337 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8338 f259c4c1 2021-09-24 stsp free(fileindex_path);
8340 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8341 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8342 f259c4c1 2021-09-24 stsp err = unlockerr;
8343 f259c4c1 2021-09-24 stsp return err;
8346 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8347 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8348 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8349 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8350 2db2652d 2019-08-07 stsp struct got_repository *repo;
8351 2db2652d 2019-08-07 stsp int have_changes;
8354 336075a4 2022-06-25 op static const struct got_error *
8355 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8356 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8357 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8358 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8360 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8361 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8362 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8363 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8364 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8365 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8367 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8368 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8369 8b13ce36 2019-08-08 stsp return NULL;
8370 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8371 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8373 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8374 735ef5ac 2019-08-03 stsp if (ie == NULL)
8375 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8377 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8378 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8379 735ef5ac 2019-08-03 stsp relpath) == -1)
8380 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8382 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8383 b4b2adf5 2023-02-09 op base_commit_idp = got_fileindex_entry_get_commit_id(
8384 b4b2adf5 2023-02-09 op &base_commit_id, ie);
8387 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8388 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8389 3aa5969e 2019-08-06 stsp goto done;
8390 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8391 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8392 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8393 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8394 735ef5ac 2019-08-03 stsp goto done;
8397 2db2652d 2019-08-07 stsp a->have_changes = 1;
8399 735ef5ac 2019-08-03 stsp p = in_repo_path;
8400 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8402 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8403 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8404 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8406 735ef5ac 2019-08-03 stsp free(in_repo_path);
8407 dc424a06 2019-08-07 stsp return err;
8410 2db2652d 2019-08-07 stsp struct stage_path_arg {
8411 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8412 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8413 2db2652d 2019-08-07 stsp struct got_repository *repo;
8414 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8415 2db2652d 2019-08-07 stsp void *status_arg;
8416 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8417 2db2652d 2019-08-07 stsp void *patch_arg;
8418 7b5dc508 2019-10-28 stsp int staged_something;
8419 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8422 2db2652d 2019-08-07 stsp static const struct got_error *
8423 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8424 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8425 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8426 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8428 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8429 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8430 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8431 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8432 0cb83759 2019-08-03 stsp uint32_t stage;
8433 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8434 0aeb8099 2020-07-23 stsp struct stat sb;
8436 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8437 8b13ce36 2019-08-08 stsp return NULL;
8439 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8440 d3e7c587 2019-08-03 stsp if (ie == NULL)
8441 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8443 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8444 2db2652d 2019-08-07 stsp relpath)== -1)
8445 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8447 0cb83759 2019-08-03 stsp switch (status) {
8448 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8449 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8450 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8451 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8452 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8455 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8456 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8457 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8458 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8459 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8462 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8465 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8466 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8467 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8468 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8469 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8473 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8474 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8477 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8478 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8479 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8480 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8482 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8483 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8484 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8485 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8486 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8487 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8488 35213c7c 2020-07-23 stsp ssize_t target_len;
8489 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8490 35213c7c 2020-07-23 stsp sizeof(target_path));
8491 35213c7c 2020-07-23 stsp if (target_len == -1) {
8492 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8493 35213c7c 2020-07-23 stsp ondisk_path);
8496 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8497 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8498 35213c7c 2020-07-23 stsp a->worktree->root_path);
8501 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8502 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8503 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8507 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8508 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8509 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8511 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8512 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8514 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8515 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8517 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8518 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8520 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8521 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8522 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8526 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
8527 9fdde394 2022-06-04 op * implicitly unstage the file.
8529 9fdde394 2022-06-04 op if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8530 9fdde394 2022-06-04 op sizeof(ie->blob_sha1)) == 0) {
8531 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
8532 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
8535 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8536 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8538 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8539 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8540 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8541 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8544 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8546 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8547 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8551 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8552 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8553 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8554 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8556 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8557 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8560 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8562 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8563 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8565 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8566 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8569 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8573 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8574 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8575 dc424a06 2019-08-07 stsp free(path_content);
8576 2db2652d 2019-08-07 stsp free(ondisk_path);
8577 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8578 0ebf8283 2019-07-24 stsp return err;
8581 0cb83759 2019-08-03 stsp const struct got_error *
8582 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8583 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8584 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8585 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8586 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8588 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8589 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8590 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8591 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8592 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8593 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8594 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8595 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8597 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8599 0cb83759 2019-08-03 stsp return err;
8601 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8602 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8604 735ef5ac 2019-08-03 stsp goto done;
8605 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8607 735ef5ac 2019-08-03 stsp goto done;
8608 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8610 0cb83759 2019-08-03 stsp goto done;
8612 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8613 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8614 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8615 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8616 2db2652d 2019-08-07 stsp oka.repo = repo;
8617 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8618 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8619 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8620 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
8622 735ef5ac 2019-08-03 stsp goto done;
8624 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8625 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8626 2db2652d 2019-08-07 stsp goto done;
8629 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8630 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8631 2db2652d 2019-08-07 stsp spa.repo = repo;
8632 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8633 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8634 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8635 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8636 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8637 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8638 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8639 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8640 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
8642 2db2652d 2019-08-07 stsp goto done;
8644 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8645 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8646 7b5dc508 2019-10-28 stsp goto done;
8649 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8650 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8651 0cb83759 2019-08-03 stsp err = sync_err;
8653 735ef5ac 2019-08-03 stsp if (head_ref)
8654 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8655 735ef5ac 2019-08-03 stsp free(head_commit_id);
8656 0cb83759 2019-08-03 stsp free(fileindex_path);
8657 0cb83759 2019-08-03 stsp if (fileindex)
8658 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8659 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8660 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8661 0cb83759 2019-08-03 stsp err = unlockerr;
8662 0cb83759 2019-08-03 stsp return err;
8665 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8666 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8667 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8668 ad493afc 2019-08-03 stsp struct got_repository *repo;
8669 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8670 ad493afc 2019-08-03 stsp void *progress_arg;
8671 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8672 2e1f37b0 2019-08-08 stsp void *patch_arg;
8675 2e1f37b0 2019-08-08 stsp static const struct got_error *
8676 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8677 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8678 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8679 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8680 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8682 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8683 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8684 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8685 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8686 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8687 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8688 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8689 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8691 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8692 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8694 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8696 2e1f37b0 2019-08-08 stsp return err;
8698 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
8699 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
8700 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8701 eb81bc23 2022-06-28 tracey goto done;
8703 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
8704 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
8705 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8706 eb81bc23 2022-06-28 tracey goto done;
8709 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8711 2e1f37b0 2019-08-08 stsp goto done;
8713 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8715 2e1f37b0 2019-08-08 stsp goto done;
8717 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8719 2e1f37b0 2019-08-08 stsp goto done;
8721 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8724 2e1f37b0 2019-08-08 stsp goto done;
8726 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8728 2e1f37b0 2019-08-08 stsp goto done;
8730 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8732 2e1f37b0 2019-08-08 stsp goto done;
8734 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8735 4b752015 2022-06-30 stsp path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8737 2e1f37b0 2019-08-08 stsp goto done;
8739 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8740 b90054ed 2022-11-01 stsp "got-unstaged-content", "");
8742 2e1f37b0 2019-08-08 stsp goto done;
8743 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8744 b90054ed 2022-11-01 stsp "got-new-staged-content", "");
8746 2e1f37b0 2019-08-08 stsp goto done;
8748 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8749 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8750 2e1f37b0 2019-08-08 stsp goto done;
8752 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8753 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8754 2e1f37b0 2019-08-08 stsp goto done;
8756 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8757 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8758 eb81bc23 2022-06-28 tracey struct diff_chunk_context cc = {};
8759 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8760 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8761 fe621944 2020-11-10 stsp nchanges++;
8763 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8764 2e1f37b0 2019-08-08 stsp int choice;
8765 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8766 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8767 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8768 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8770 2e1f37b0 2019-08-08 stsp goto done;
8771 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8772 2e1f37b0 2019-08-08 stsp have_content = 1;
8774 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8775 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8778 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8779 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8780 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8782 2e1f37b0 2019-08-08 stsp free(label1);
8783 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8784 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8786 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8787 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8788 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8789 2e1f37b0 2019-08-08 stsp if (staged_blob)
8790 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8791 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8792 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8793 fe621944 2020-11-10 stsp err = free_err;
8794 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8795 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8796 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8797 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8798 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8799 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8800 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8801 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8802 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8803 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8804 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8805 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8806 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8807 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8808 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8809 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8810 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8811 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8812 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8814 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8815 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8816 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8817 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8818 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8819 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8820 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8822 2e1f37b0 2019-08-08 stsp free(path1);
8823 2e1f37b0 2019-08-08 stsp free(path2);
8824 fda8017d 2020-07-23 stsp return err;
8827 fda8017d 2020-07-23 stsp static const struct got_error *
8828 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8829 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8830 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8831 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8832 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8833 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8834 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8836 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8837 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8838 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8839 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8840 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8841 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8842 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8843 fda8017d 2020-07-23 stsp struct stat sb;
8845 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8846 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8847 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8849 fda8017d 2020-07-23 stsp return err;
8851 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8852 fda8017d 2020-07-23 stsp return NULL;
8854 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8855 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8856 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8858 fda8017d 2020-07-23 stsp goto done;
8861 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
8862 fda8017d 2020-07-23 stsp if (f == NULL) {
8863 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8864 fda8017d 2020-07-23 stsp path_unstaged_content);
8865 fda8017d 2020-07-23 stsp goto done;
8867 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8868 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8869 fda8017d 2020-07-23 stsp goto done;
8871 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8872 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8873 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8875 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8876 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8877 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8878 fda8017d 2020-07-23 stsp goto done;
8880 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8881 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8882 fda8017d 2020-07-23 stsp goto done;
8884 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8885 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8886 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8887 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8888 fda8017d 2020-07-23 stsp progress_arg);
8890 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8892 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8894 67a66647 2021-05-31 stsp return err;
8896 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8897 67a66647 2021-05-31 stsp parent) == -1) {
8898 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8899 67a66647 2021-05-31 stsp base_path = NULL;
8900 67a66647 2021-05-31 stsp goto done;
8903 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_base_path, &f_base,
8904 b90054ed 2022-11-01 stsp base_path, "");
8906 67a66647 2021-05-31 stsp goto done;
8907 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8908 67a66647 2021-05-31 stsp blob_base);
8910 67a66647 2021-05-31 stsp goto done;
8913 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8914 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8916 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8917 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8918 eec2f5a9 2021-06-03 stsp ondisk_path);
8920 eec2f5a9 2021-06-03 stsp goto done;
8923 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
8924 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8925 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8926 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8927 eec2f5a9 2021-06-03 stsp goto done;
8929 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8930 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8931 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8932 eec2f5a9 2021-06-03 stsp close(fd);
8933 eec2f5a9 2021-06-03 stsp goto done;
8937 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8938 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8939 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8940 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8941 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8944 fda8017d 2020-07-23 stsp goto done;
8946 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8947 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8948 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8950 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8951 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8954 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8955 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8956 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8957 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8958 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8959 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8960 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8961 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8962 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8963 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8964 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8965 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8966 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8967 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8968 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8969 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8970 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8971 67a66647 2021-05-31 stsp free(blob_base_path);
8972 67a66647 2021-05-31 stsp free(parent);
8973 67a66647 2021-05-31 stsp free(base_path);
8974 2e1f37b0 2019-08-08 stsp return err;
8977 ad493afc 2019-08-03 stsp static const struct got_error *
8978 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8979 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8980 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8981 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8983 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8984 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8985 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8986 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8987 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8988 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8989 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8990 9bc94a15 2019-08-03 stsp struct stat sb;
8991 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8993 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8994 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8995 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8996 2e1f37b0 2019-08-08 stsp return NULL;
8998 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8999 ad493afc 2019-08-03 stsp if (ie == NULL)
9000 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9002 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9004 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9006 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9007 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9009 f69721c3 2019-10-21 stsp goto done;
9010 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9011 f69721c3 2019-10-21 stsp id_str) == -1) {
9012 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9013 eb81bc23 2022-06-28 tracey goto done;
9016 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9017 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9018 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9019 eb81bc23 2022-06-28 tracey goto done;
9021 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9022 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9023 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9024 f69721c3 2019-10-21 stsp goto done;
9027 ad493afc 2019-08-03 stsp switch (staged_status) {
9028 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9029 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9030 eb81bc23 2022-06-28 tracey blob_id, 8192, fd1);
9033 ad493afc 2019-08-03 stsp /* fall through */
9034 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9035 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9036 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9037 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9038 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9039 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9042 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9045 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9046 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9047 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9048 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9049 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9050 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9053 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9054 eb81bc23 2022-06-28 tracey staged_blob_id, 8192, fd2);
9057 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9058 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9059 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9060 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9061 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9062 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9063 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9064 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9065 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9067 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9068 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9069 36bf999c 2020-07-23 stsp char *staged_target;
9070 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9071 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9073 36bf999c 2020-07-23 stsp goto done;
9074 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9075 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9076 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9077 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9078 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9079 36bf999c 2020-07-23 stsp free(staged_target);
9081 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9082 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9083 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9084 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9085 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9086 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9087 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9091 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9094 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9095 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9096 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9097 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9100 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9101 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9102 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9103 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9104 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9107 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9109 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9110 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9114 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9115 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9116 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9117 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9120 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9124 ad493afc 2019-08-03 stsp free(ondisk_path);
9125 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9126 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9127 ad493afc 2019-08-03 stsp if (blob_base)
9128 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9129 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9130 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9131 ad493afc 2019-08-03 stsp if (blob_staged)
9132 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9133 f69721c3 2019-10-21 stsp free(id_str);
9134 f69721c3 2019-10-21 stsp free(label_orig);
9135 ad493afc 2019-08-03 stsp return err;
9138 ad493afc 2019-08-03 stsp const struct got_error *
9139 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9140 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9141 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9142 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9143 ad493afc 2019-08-03 stsp struct got_repository *repo)
9145 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9146 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9147 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9148 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9149 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9151 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9153 ad493afc 2019-08-03 stsp return err;
9155 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9157 ad493afc 2019-08-03 stsp goto done;
9159 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9160 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9161 ad493afc 2019-08-03 stsp upa.repo = repo;
9162 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9163 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9164 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9165 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9166 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9167 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9168 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
9170 ad493afc 2019-08-03 stsp goto done;
9173 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9174 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9175 ad493afc 2019-08-03 stsp err = sync_err;
9177 ad493afc 2019-08-03 stsp free(fileindex_path);
9178 ad493afc 2019-08-03 stsp if (fileindex)
9179 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9180 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9181 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9182 ad493afc 2019-08-03 stsp err = unlockerr;
9183 ad493afc 2019-08-03 stsp return err;
9186 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9187 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9188 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9189 b2118c49 2020-07-28 stsp void *info_arg;
9190 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9191 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9192 b2118c49 2020-07-28 stsp void *cancel_arg;
9195 b2118c49 2020-07-28 stsp static const struct got_error *
9196 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9198 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9199 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9200 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9201 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9202 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9203 b2118c49 2020-07-28 stsp int stage;
9205 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9206 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9208 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9209 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9210 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9213 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9214 b2118c49 2020-07-28 stsp return NULL;
9216 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
9217 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9218 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9219 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9220 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9221 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9222 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
9225 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
9226 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9228 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9229 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9232 b2118c49 2020-07-28 stsp const struct got_error *
9233 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9234 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9235 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9236 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9239 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9240 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9241 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9242 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9244 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9246 b2118c49 2020-07-28 stsp return err;
9248 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9250 b2118c49 2020-07-28 stsp goto done;
9252 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9253 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9254 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9255 b2118c49 2020-07-28 stsp arg.paths = paths;
9256 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9257 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9258 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9261 b2118c49 2020-07-28 stsp free(fileindex_path);
9262 b2118c49 2020-07-28 stsp if (fileindex)
9263 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9264 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9265 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9266 b2118c49 2020-07-28 stsp err = unlockerr;
9267 b2118c49 2020-07-28 stsp return err;
9270 78f5ac24 2022-03-19 op static const struct got_error *
9271 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
9272 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
9273 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
9275 78f5ac24 2022-03-19 op const struct got_error *err;
9276 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
9277 78f5ac24 2022-03-19 op struct stat sb;
9278 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
9280 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
9284 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9285 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
9286 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
9288 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9290 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
9291 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9296 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
9297 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
9298 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
9299 78f5ac24 2022-03-19 op if (errno != ENOENT) {
9300 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
9301 78f5ac24 2022-03-19 op ondisk_path);
9304 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
9309 78f5ac24 2022-03-19 op free(ondisk_path);
9313 78f5ac24 2022-03-19 op static const struct got_error *
9314 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
9315 78f5ac24 2022-03-19 op unsigned char staged_status)
9317 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9318 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9319 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9320 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9321 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
9322 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
9323 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9324 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9325 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9326 78f5ac24 2022-03-19 op return NULL;
9329 78f5ac24 2022-03-19 op static const struct got_error *
9330 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
9332 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
9333 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9334 78f5ac24 2022-03-19 op return NULL;
9337 78f5ac24 2022-03-19 op static const struct got_error *
9338 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
9339 78f5ac24 2022-03-19 op unsigned char staged_status)
9341 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9342 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9343 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9344 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9345 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
9346 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9347 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9348 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9349 78f5ac24 2022-03-19 op return NULL;
9352 78f5ac24 2022-03-19 op const struct got_error *
9353 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
9354 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
9356 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
9359 78f5ac24 2022-03-19 op const struct got_error *
9360 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
9361 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
9362 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
9364 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
9365 78f5ac24 2022-03-19 op int file_renamed = 0;
9366 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
9367 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
9369 78f5ac24 2022-03-19 op *oldpath = NULL;
9370 78f5ac24 2022-03-19 op *newpath = NULL;
9372 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
9373 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
9377 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
9378 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
9382 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
9383 78f5ac24 2022-03-19 op file_renamed = 1;
9385 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
9386 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9387 78f5ac24 2022-03-19 op else if (file_renamed) {
9388 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9389 78f5ac24 2022-03-19 op if (err == NULL)
9390 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9391 78f5ac24 2022-03-19 op } else if (old == NULL)
9392 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9394 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
9398 78f5ac24 2022-03-19 op free(*oldpath);
9399 78f5ac24 2022-03-19 op *oldpath = NULL;
9400 78f5ac24 2022-03-19 op free(*newpath);
9401 78f5ac24 2022-03-19 op *newpath = NULL;
9406 78f5ac24 2022-03-19 op const struct got_error *
9407 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9408 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9409 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
9411 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
9413 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
9414 f2dd7807 2022-05-17 op saa.worktree = worktree;
9415 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
9416 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
9417 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
9418 f2dd7807 2022-05-17 op saa.repo = repo;
9420 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9421 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
9424 f2dd7807 2022-05-17 op const struct got_error *
9425 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9426 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9427 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
9429 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
9431 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
9432 f2dd7807 2022-05-17 op sda.worktree = worktree;
9433 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
9434 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
9435 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
9436 f2dd7807 2022-05-17 op sda.repo = repo;
9437 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
9438 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
9439 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
9440 f2dd7807 2022-05-17 op sda.status_codes = NULL;
9442 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9443 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9446 f2dd7807 2022-05-17 op const struct got_error *
9447 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
9448 4bcdc895 2022-05-17 op const char *fileindex_path)
9450 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
9452 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
9453 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);