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;
1516 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1517 6353ad76 2019-02-08 stsp static const struct got_error *
1518 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1520 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1521 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1522 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1523 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1524 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1526 7154f6ce 2019-03-27 stsp int i = 0;
1527 9bdd68dd 2021-01-02 naddy char *line = NULL;
1528 9bdd68dd 2021-01-02 naddy size_t linesize = 0;
1529 9bdd68dd 2021-01-02 naddy ssize_t linelen;
1531 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1532 9bdd68dd 2021-01-02 naddy linelen = getline(&line, &linesize, f);
1533 9bdd68dd 2021-01-02 naddy if (linelen == -1) {
1534 7154f6ce 2019-03-27 stsp if (feof(f))
1536 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1540 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1541 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1543 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1548 9bdd68dd 2021-01-02 naddy free(line);
1550 7154f6ce 2019-03-27 stsp return err;
1553 e2b1e152 2019-07-13 stsp static int
1554 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1556 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1557 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1560 1ebedb77 2019-10-19 stsp static int
1561 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1563 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1564 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1565 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1566 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1567 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1568 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1571 c363b2c1 2019-08-03 stsp static unsigned char
1572 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1574 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1575 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1576 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1577 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1578 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1579 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1580 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1582 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1586 a919d5c4 2020-07-23 stsp static const struct got_error *
1587 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1588 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1589 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1591 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1592 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1593 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1594 a919d5c4 2020-07-23 stsp ssize_t elen;
1595 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1596 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1597 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1599 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1601 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1603 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1605 a919d5c4 2020-07-23 stsp return err;
1606 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1608 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1609 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1611 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1613 a919d5c4 2020-07-23 stsp if (len > 0) {
1614 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1615 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1616 a919d5c4 2020-07-23 stsp len - hdrlen);
1617 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1618 a919d5c4 2020-07-23 stsp hdrlen = 0;
1620 a919d5c4 2020-07-23 stsp } while (len != 0);
1621 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1623 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1624 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1625 a919d5c4 2020-07-23 stsp if (elen == -1)
1626 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1628 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1629 a919d5c4 2020-07-23 stsp if (elen == -1)
1630 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1633 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1634 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1636 a919d5c4 2020-07-23 stsp return NULL;
1639 7154f6ce 2019-03-27 stsp static const struct got_error *
1640 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1641 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1642 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1644 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1645 6353ad76 2019-02-08 stsp struct got_object_id id;
1646 6353ad76 2019-02-08 stsp size_t hdrlen;
1647 eb81bc23 2022-06-28 tracey int fd = -1, fd1 = -1;
1648 6353ad76 2019-02-08 stsp FILE *f = NULL;
1649 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1650 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1651 6353ad76 2019-02-08 stsp size_t flen, blen;
1652 2c4740ad 2023-02-12 mark unsigned char staged_status;
1654 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
1655 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1656 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1659 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1660 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1661 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1663 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1664 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1665 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1666 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1667 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1669 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1670 882ef1b9 2019-12-13 stsp goto done;
1672 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1673 3d35a492 2019-12-13 stsp goto done;
1676 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1677 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1678 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1679 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1680 5c02d2a5 2021-09-26 stsp else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1681 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1682 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1683 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1684 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1685 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1686 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1688 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1689 3d35a492 2019-12-13 stsp goto done;
1691 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1692 1338848f 2019-12-13 stsp goto done;
1696 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1697 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1698 1338848f 2019-12-13 stsp goto done;
1701 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1702 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1703 1338848f 2019-12-13 stsp goto done;
1704 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1705 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1706 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1707 1338848f 2019-12-13 stsp goto done;
1710 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1711 f179e66d 2020-07-23 stsp goto done;
1713 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1714 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1715 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1716 1338848f 2019-12-13 stsp goto done;
1719 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1720 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1721 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
1723 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
1725 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1726 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1727 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1728 eb81bc23 2022-06-28 tracey goto done;
1730 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1732 1338848f 2019-12-13 stsp goto done;
1734 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1735 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1736 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1737 a919d5c4 2020-07-23 stsp goto done;
1740 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1741 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1742 ab0d4361 2019-12-13 stsp if (fd == -1) {
1743 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1744 ab0d4361 2019-12-13 stsp goto done;
1748 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1749 6353ad76 2019-02-08 stsp if (f == NULL) {
1750 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1751 6353ad76 2019-02-08 stsp goto done;
1754 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1756 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1757 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1759 7154f6ce 2019-03-27 stsp goto done;
1760 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1761 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1762 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1763 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1764 7154f6ce 2019-03-27 stsp goto done;
1766 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1767 6353ad76 2019-02-08 stsp if (flen != 0)
1768 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1770 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1771 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1772 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1774 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1775 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1776 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1777 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1781 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1784 6353ad76 2019-02-08 stsp hdrlen = 0;
1787 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1788 7154f6ce 2019-03-27 stsp rewind(f);
1789 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1790 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1791 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1793 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1794 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1796 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1797 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1798 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1799 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1800 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1801 9d31a1d8 2018-03-11 stsp return err;
1805 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1806 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1808 e2b1e152 2019-07-13 stsp static const struct got_error *
1809 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1810 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1812 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1813 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1814 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1816 e2b1e152 2019-07-13 stsp return NULL;
1819 9d31a1d8 2018-03-11 stsp static const struct got_error *
1820 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1821 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1822 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1823 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1824 0584f854 2019-04-06 stsp void *progress_arg)
1826 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1827 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1828 eb81bc23 2022-06-28 tracey char *ondisk_path = NULL;
1829 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1830 b8f41171 2019-02-10 stsp struct stat sb;
1831 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
1833 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1834 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1837 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1838 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1839 a76c42e6 2019-08-03 stsp goto done;
1841 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1844 abb4604f 2019-07-27 stsp goto done;
1845 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1846 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1848 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1849 f6764181 2021-09-24 stsp if (errno != ENOENT) {
1850 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1851 f6764181 2021-09-24 stsp ondisk_path);
1852 f6764181 2021-09-24 stsp goto done;
1854 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1855 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1857 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1858 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1860 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1864 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1866 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1867 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1868 b8f41171 2019-02-10 stsp goto done;
1870 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1872 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1873 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1875 5036ab18 2020-04-18 stsp goto done;
1878 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1879 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1880 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1882 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1883 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1884 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1885 c6e8a826 2021-04-05 stsp * updating contents of this file.
1887 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1888 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1889 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1890 c6e8a826 2021-04-05 stsp /* Same commit. */
1891 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1892 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1894 e2b1e152 2019-07-13 stsp goto done;
1895 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1897 6353ad76 2019-02-08 stsp goto done;
1899 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1900 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1901 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1902 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1903 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1904 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1906 0f58026f 2021-04-05 stsp goto done;
1907 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1909 b8f41171 2019-02-10 stsp goto done;
1913 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1914 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1915 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1916 eb81bc23 2022-06-28 tracey goto done;
1918 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1920 6353ad76 2019-02-08 stsp goto done;
1922 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1923 234035bc 2019-06-01 stsp int update_timestamps;
1924 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1925 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1926 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1927 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
1928 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
1929 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1930 eb81bc23 2022-06-28 tracey goto done;
1932 234035bc 2019-06-01 stsp struct got_object_id id2;
1933 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id2, ie);
1934 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1937 f69721c3 2019-10-21 stsp goto done;
1939 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1940 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1941 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1942 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1943 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1944 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1945 f69721c3 2019-10-21 stsp goto done;
1947 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1948 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1949 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1950 234035bc 2019-06-01 stsp goto done;
1953 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1954 36bf999c 2020-07-23 stsp char *link_target;
1955 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1957 36bf999c 2020-07-23 stsp goto done;
1958 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1959 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1960 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1961 36bf999c 2020-07-23 stsp free(link_target);
1963 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1964 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1965 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1966 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1968 f69721c3 2019-10-21 stsp free(label_orig);
1969 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1970 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1971 eb81bc23 2022-06-28 tracey goto done;
1973 234035bc 2019-06-01 stsp if (blob2)
1974 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1976 909d120e 2019-09-22 stsp goto done;
1978 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1979 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1980 234035bc 2019-06-01 stsp * unmodified files again.
1982 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1983 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1984 234035bc 2019-06-01 stsp update_timestamps);
1985 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1986 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1987 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1988 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1989 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1991 1ee397ad 2019-07-12 stsp goto done;
1992 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1993 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1995 13d9040b 2019-03-27 stsp goto done;
1997 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1998 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1999 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2000 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2001 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2002 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
2003 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
2005 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2006 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2007 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2008 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2009 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2012 13d9040b 2019-03-27 stsp goto done;
2015 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2016 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2017 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2019 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2020 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2021 054041d0 2020-06-24 stsp &blob->id);
2024 13d9040b 2019-03-27 stsp goto done;
2026 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2027 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2028 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2032 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2033 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2034 eb81bc23 2022-06-28 tracey goto done;
2036 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2038 6353ad76 2019-02-08 stsp free(ondisk_path);
2039 8da9e5f4 2019-01-12 stsp return err;
2042 90285c3b 2019-01-08 stsp static const struct got_error *
2043 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2045 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2046 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2048 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2049 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2051 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2052 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2053 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2055 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2056 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2058 1c4cdd89 2021-06-20 stsp goto done;
2059 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2060 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2061 fddefe3b 2020-10-20 stsp free(ondisk_path);
2062 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2063 1c4cdd89 2021-06-20 stsp parent = NULL;
2064 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2065 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2066 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2067 fddefe3b 2020-10-20 stsp ondisk_path);
2070 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2076 90285c3b 2019-01-08 stsp free(ondisk_path);
2077 1c4cdd89 2021-06-20 stsp free(parent);
2078 90285c3b 2019-01-08 stsp return err;
2081 708d8e67 2019-03-27 stsp static const struct got_error *
2082 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2083 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2084 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2086 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2087 708d8e67 2019-03-27 stsp unsigned char status;
2088 708d8e67 2019-03-27 stsp struct stat sb;
2089 708d8e67 2019-03-27 stsp char *ondisk_path;
2091 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2092 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2094 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2096 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2098 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2100 5a58a424 2020-06-23 stsp goto done;
2102 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2103 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2104 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2105 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2106 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2107 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2108 993e2a1b 2020-07-23 stsp goto done;
2110 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2111 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2112 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2113 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2115 993e2a1b 2020-07-23 stsp goto done;
2116 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2117 993e2a1b 2020-07-23 stsp ie->path);
2118 993e2a1b 2020-07-23 stsp goto done;
2121 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2122 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2123 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2125 5a58a424 2020-06-23 stsp goto done;
2127 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2128 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2130 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2131 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2133 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2135 5a58a424 2020-06-23 stsp goto done;
2136 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2137 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2139 5a58a424 2020-06-23 stsp goto done;
2141 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2144 5a58a424 2020-06-23 stsp free(ondisk_path);
2145 fc6346c4 2019-03-27 stsp return err;
2148 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2149 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2150 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2151 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2152 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2153 8da9e5f4 2019-01-12 stsp void *progress_arg;
2154 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2155 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2158 512f0d0e 2019-01-02 stsp static const struct got_error *
2159 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2160 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2162 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2164 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2165 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2167 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2168 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2171 8da9e5f4 2019-01-12 stsp static const struct got_error *
2172 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2174 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2176 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2177 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2179 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2180 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2183 9d31a1d8 2018-03-11 stsp static const struct got_error *
2184 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2186 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2187 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2188 8da9e5f4 2019-01-12 stsp char *path;
2190 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2191 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2193 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2194 63c5ca5d 2019-08-24 stsp return NULL;
2196 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2197 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2199 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2201 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2202 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2204 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2205 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2207 8da9e5f4 2019-01-12 stsp free(path);
2208 0cd1c46a 2019-03-11 stsp return err;
2211 b2118c49 2020-07-28 stsp const struct got_error *
2212 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2214 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2216 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2217 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2218 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2219 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2222 b2118c49 2020-07-28 stsp return NULL;
2225 818c7501 2019-07-11 stsp static const struct got_error *
2226 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2228 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2229 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2231 517bab73 2019-03-11 stsp *refname = NULL;
2233 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2235 b2118c49 2020-07-28 stsp return err;
2237 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2238 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2239 0647c563 2019-03-11 stsp *refname = NULL;
2241 517bab73 2019-03-11 stsp free(uuidstr);
2242 517bab73 2019-03-11 stsp return err;
2245 818c7501 2019-07-11 stsp const struct got_error *
2246 9587e6cc 2023-01-28 mark got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2247 9587e6cc 2023-01-28 mark const char *prefix)
2249 9587e6cc 2023-01-28 mark return get_ref_name(refname, worktree, prefix);
2252 9587e6cc 2023-01-28 mark const struct got_error *
2253 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2255 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2258 818c7501 2019-07-11 stsp static const struct got_error *
2259 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2261 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2262 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2265 818c7501 2019-07-11 stsp static const struct got_error *
2266 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2268 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2271 818c7501 2019-07-11 stsp static const struct got_error *
2272 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2274 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2275 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2278 818c7501 2019-07-11 stsp static const struct got_error *
2279 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2281 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2282 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2285 0ebf8283 2019-07-24 stsp static const struct got_error *
2286 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2288 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2289 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2292 0ebf8283 2019-07-24 stsp static const struct got_error *
2293 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2295 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2296 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2299 0ebf8283 2019-07-24 stsp static const struct got_error *
2300 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2302 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2303 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2306 0ebf8283 2019-07-24 stsp static const struct got_error *
2307 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2309 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2310 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2313 0ebf8283 2019-07-24 stsp const struct got_error *
2314 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2315 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2317 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2318 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2319 0ebf8283 2019-07-24 stsp *path = NULL;
2320 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2322 0ebf8283 2019-07-24 stsp return NULL;
2325 f259c4c1 2021-09-24 stsp static const struct got_error *
2326 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2328 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2329 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2332 f259c4c1 2021-09-24 stsp static const struct got_error *
2333 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2335 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2336 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2340 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2341 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2343 517bab73 2019-03-11 stsp static const struct got_error *
2344 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2346 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2347 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2348 517bab73 2019-03-11 stsp char *refname;
2350 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2352 517bab73 2019-03-11 stsp return err;
2354 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2356 0cd1c46a 2019-03-11 stsp goto done;
2358 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2360 0cd1c46a 2019-03-11 stsp free(refname);
2362 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2363 3e3a69f1 2019-07-25 stsp return err;
2366 3e3a69f1 2019-07-25 stsp static const struct got_error *
2367 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2369 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2371 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2372 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2373 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2374 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2376 9d31a1d8 2018-03-11 stsp return err;
2380 ebf99748 2019-05-09 stsp static const struct got_error *
2381 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2382 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2384 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2385 ebf99748 2019-05-09 stsp FILE *index = NULL;
2387 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2388 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2389 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2390 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2392 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2394 ebf99748 2019-05-09 stsp goto done;
2396 00fe21f2 2021-12-31 stsp index = fopen(*fileindex_path, "rbe");
2397 ebf99748 2019-05-09 stsp if (index == NULL) {
2398 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2399 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2401 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2402 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2403 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2406 ebf99748 2019-05-09 stsp if (err) {
2407 ebf99748 2019-05-09 stsp free(*fileindex_path);
2408 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2409 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2410 ebf99748 2019-05-09 stsp *fileindex = NULL;
2412 ebf99748 2019-05-09 stsp return err;
2415 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2416 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2417 c932eeeb 2019-05-22 stsp const char *path;
2418 c932eeeb 2019-05-22 stsp size_t path_len;
2419 c932eeeb 2019-05-22 stsp const char *entry_name;
2420 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2421 a484d721 2019-06-10 stsp void *progress_arg;
2424 c932eeeb 2019-05-22 stsp static const struct got_error *
2425 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2427 1ee397ad 2019-07-12 stsp const struct got_error *err;
2428 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2430 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2431 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2432 c932eeeb 2019-05-22 stsp return NULL;
2433 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2434 102ff934 2019-06-11 stsp return NULL;
2436 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2437 a769b60b 2021-06-27 stsp return NULL;
2439 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2440 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2441 c932eeeb 2019-05-22 stsp return NULL;
2443 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2444 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2445 edd02c5e 2019-07-11 stsp ie->path);
2447 1ee397ad 2019-07-12 stsp return err;
2449 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2450 c932eeeb 2019-05-22 stsp return NULL;
2453 b0a0c9ed 2023-02-06 op /* Bump base commit ID of all files within an updated part of the work tree. */
2454 a615e0e7 2020-12-16 stsp static const struct got_error *
2455 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2456 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2457 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2459 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2461 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2462 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2463 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2464 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2465 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2466 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2468 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2469 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2472 9c6338c4 2019-06-02 stsp static const struct got_error *
2473 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2475 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2476 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2477 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2478 867630bb 2020-01-17 stsp struct timespec timeout;
2480 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2481 b90054ed 2022-11-01 stsp fileindex_path, "");
2483 9c6338c4 2019-06-02 stsp goto done;
2485 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2487 9c6338c4 2019-06-02 stsp goto done;
2489 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2490 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2491 9c6338c4 2019-06-02 stsp fileindex_path);
2492 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2496 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2497 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2498 867630bb 2020-01-17 stsp * was recorded in the file index.
2500 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2501 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2502 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2504 9c6338c4 2019-06-02 stsp if (new_index)
2505 9c6338c4 2019-06-02 stsp fclose(new_index);
2506 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2507 9c6338c4 2019-06-02 stsp return err;
2510 6ced4176 2019-07-12 stsp static const struct got_error *
2511 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2512 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2513 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit, struct got_worktree *worktree,
2514 a44927cc 2022-04-07 stsp struct got_repository *repo)
2516 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2517 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2518 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2519 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2521 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2522 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2523 6ced4176 2019-07-12 stsp *tree_id = NULL;
2525 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2526 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2527 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2528 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2529 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2530 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2531 6ced4176 2019-07-12 stsp goto done;
2533 a44927cc 2022-04-07 stsp err = got_object_id_by_path(tree_id, repo, base_commit,
2534 a44927cc 2022-04-07 stsp worktree->path_prefix);
2536 6ced4176 2019-07-12 stsp goto done;
2537 6ced4176 2019-07-12 stsp return NULL;
2540 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2542 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2543 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2544 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2545 6ced4176 2019-07-12 stsp goto done;
2548 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2550 6ced4176 2019-07-12 stsp goto done;
2552 6ced4176 2019-07-12 stsp free(in_repo_path);
2553 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2555 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2557 6ced4176 2019-07-12 stsp goto done;
2559 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2560 6ced4176 2019-07-12 stsp /* Check out a single file. */
2561 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2562 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2563 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2564 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2565 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2566 6ced4176 2019-07-12 stsp goto done;
2568 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2569 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2570 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2571 6ced4176 2019-07-12 stsp goto done;
2574 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2575 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2577 6ced4176 2019-07-12 stsp return err;
2578 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2579 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2580 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2581 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2582 6ced4176 2019-07-12 stsp goto done;
2585 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2586 a44927cc 2022-04-07 stsp base_commit, in_repo_path);
2588 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2589 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2590 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2591 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2592 6ced4176 2019-07-12 stsp goto done;
2594 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2595 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2596 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2597 6ced4176 2019-07-12 stsp goto done;
2602 6ced4176 2019-07-12 stsp free(in_repo_path);
2603 6ced4176 2019-07-12 stsp if (err) {
2604 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2605 6ced4176 2019-07-12 stsp free(*tree_relpath);
2606 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2607 6ced4176 2019-07-12 stsp free(*tree_id);
2608 6ced4176 2019-07-12 stsp *tree_id = NULL;
2610 07ed472d 2019-07-12 stsp return err;
2613 07ed472d 2019-07-12 stsp static const struct got_error *
2614 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2615 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2616 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2617 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2619 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2620 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2621 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2622 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2623 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2625 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2626 7f47418f 2019-12-20 stsp if (err) {
2627 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2628 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2629 7f47418f 2019-12-20 stsp goto done;
2630 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2631 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2633 7f47418f 2019-12-20 stsp return err;
2636 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2637 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2639 07ed472d 2019-07-12 stsp goto done;
2641 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2643 07ed472d 2019-07-12 stsp goto done;
2645 07ed472d 2019-07-12 stsp if (entry_name &&
2646 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2647 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2648 07ed472d 2019-07-12 stsp goto done;
2651 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2652 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2653 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2654 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2655 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2656 07ed472d 2019-07-12 stsp arg.repo = repo;
2657 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2658 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2659 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2660 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2661 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2662 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2665 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2666 07ed472d 2019-07-12 stsp if (commit)
2667 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2668 6ced4176 2019-07-12 stsp return err;
2671 9d31a1d8 2018-03-11 stsp const struct got_error *
2672 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2673 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2674 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2675 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2677 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2678 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2679 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2680 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2681 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2682 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2683 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2684 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2685 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2686 f2ea84fa 2019-07-27 stsp int entry_type;
2687 f2ea84fa 2019-07-27 stsp char *relpath;
2688 f2ea84fa 2019-07-27 stsp char *entry_name;
2689 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2690 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2692 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2694 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2696 9d31a1d8 2018-03-11 stsp return err;
2698 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
2699 a44927cc 2022-04-07 stsp worktree->base_commit_id);
2701 a44927cc 2022-04-07 stsp goto done;
2703 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2704 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2705 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2706 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2707 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2708 f2ea84fa 2019-07-27 stsp goto done;
2711 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2712 a44927cc 2022-04-07 stsp &tpd->relpath, &tpd->tree_id, pe->path, commit,
2713 a44927cc 2022-04-07 stsp worktree, repo);
2714 f2ea84fa 2019-07-27 stsp if (err) {
2715 f2ea84fa 2019-07-27 stsp free(tpd);
2716 6ced4176 2019-07-12 stsp goto done;
2719 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2720 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2721 f2ea84fa 2019-07-27 stsp if (err) {
2722 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2723 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2724 f2ea84fa 2019-07-27 stsp free(tpd);
2725 f2ea84fa 2019-07-27 stsp goto done;
2728 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2730 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2734 51514078 2018-12-25 stsp * Read the file index.
2735 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2736 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2738 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2740 c4cdcb68 2019-04-03 stsp goto done;
2742 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2743 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2744 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2746 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2747 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2748 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2752 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2753 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2754 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2755 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2756 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2757 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2758 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2759 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2763 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2765 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2766 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2767 af12c6b9 2019-06-04 stsp err = sync_err;
2769 9c6338c4 2019-06-02 stsp free(fileindex_path);
2771 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2772 9d31a1d8 2018-03-11 stsp if (commit)
2773 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2774 5ade8233 2019-07-12 stsp if (fileindex)
2775 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2776 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2777 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2778 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2779 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2780 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2781 f2ea84fa 2019-07-27 stsp free(tpd);
2783 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2784 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2785 9d31a1d8 2018-03-11 stsp err = unlockerr;
2786 f8d1f275 2019-02-04 stsp return err;
2789 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2790 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2791 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2792 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2793 234035bc 2019-06-01 stsp void *progress_arg;
2794 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2795 234035bc 2019-06-01 stsp void *cancel_arg;
2796 f69721c3 2019-10-21 stsp const char *label_orig;
2797 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2798 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2801 234035bc 2019-06-01 stsp static const struct got_error *
2802 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2803 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
2804 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
2805 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
2806 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2808 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2809 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2810 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2811 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2812 234035bc 2019-06-01 stsp struct stat sb;
2813 234035bc 2019-06-01 stsp unsigned char status;
2814 234035bc 2019-06-01 stsp int local_changes_subsumed;
2815 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2816 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2818 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2819 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2820 d6c87207 2019-08-02 stsp strlen(path2));
2821 1ee397ad 2019-07-12 stsp if (ie == NULL)
2822 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2823 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2825 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2826 234035bc 2019-06-01 stsp path2) == -1)
2827 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2829 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2832 234035bc 2019-06-01 stsp goto done;
2834 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2835 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2836 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2837 234035bc 2019-06-01 stsp goto done;
2839 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2840 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2841 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2842 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2843 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2844 234035bc 2019-06-01 stsp goto done;
2847 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2848 36bf999c 2020-07-23 stsp char *link_target2;
2849 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2851 36bf999c 2020-07-23 stsp goto done;
2852 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2853 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2854 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2855 36bf999c 2020-07-23 stsp free(link_target2);
2859 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2860 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2861 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2862 1af628f4 2021-06-03 stsp goto done;
2864 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2865 1af628f4 2021-06-03 stsp f_orig, blob1);
2867 1af628f4 2021-06-03 stsp goto done;
2869 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2870 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2871 1af628f4 2021-06-03 stsp goto done;
2872 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2873 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2875 1af628f4 2021-06-03 stsp goto done;
2877 c0df5966 2021-12-31 stsp fd = open(ondisk_path,
2878 c0df5966 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2879 1af628f4 2021-06-03 stsp if (fd == -1) {
2880 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2881 1af628f4 2021-06-03 stsp ondisk_path);
2882 1af628f4 2021-06-03 stsp goto done;
2884 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2885 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2886 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2887 1af628f4 2021-06-03 stsp ondisk_path);
2888 1af628f4 2021-06-03 stsp close(fd);
2889 1af628f4 2021-06-03 stsp goto done;
2891 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2893 1af628f4 2021-06-03 stsp goto done;
2894 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2895 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2896 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2897 1af628f4 2021-06-03 stsp goto done;
2899 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2900 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2901 c48f94a4 2023-01-29 stsp mode2, a->label_orig, NULL, label_deriv2,
2902 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2903 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2905 234035bc 2019-06-01 stsp } else if (blob1) {
2906 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2907 d6c87207 2019-08-02 stsp strlen(path1));
2908 1ee397ad 2019-07-12 stsp if (ie == NULL)
2909 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2910 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2912 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2913 2b92fad7 2019-06-02 stsp path1) == -1)
2914 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2916 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2919 2b92fad7 2019-06-02 stsp goto done;
2921 2b92fad7 2019-06-02 stsp switch (status) {
2922 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2923 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2924 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2926 1ee397ad 2019-07-12 stsp goto done;
2927 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2929 2b92fad7 2019-06-02 stsp goto done;
2931 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2933 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2934 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2935 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2936 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2938 1ee397ad 2019-07-12 stsp goto done;
2940 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2942 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2943 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2944 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2945 72840534 2022-01-19 stsp off_t blob1_size;
2947 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2948 0a22ca1a 2020-09-23 stsp * exists in the repository.
2950 72840534 2022-01-19 stsp err = got_object_blob_file_create(&id, &blob1_f,
2951 72840534 2022-01-19 stsp &blob1_size, path1);
2953 0a22ca1a 2020-09-23 stsp goto done;
2954 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2955 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2956 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2958 0a22ca1a 2020-09-23 stsp goto done;
2959 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2962 0a22ca1a 2020-09-23 stsp goto done;
2964 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2967 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2968 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2970 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2971 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2974 0a22ca1a 2020-09-23 stsp goto done;
2977 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2978 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2979 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2980 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2982 1ee397ad 2019-07-12 stsp goto done;
2984 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2985 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2987 1ee397ad 2019-07-12 stsp goto done;
2992 234035bc 2019-06-01 stsp } else if (blob2) {
2993 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2994 234035bc 2019-06-01 stsp path2) == -1)
2995 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2996 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2997 d6c87207 2019-08-02 stsp strlen(path2));
2999 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3000 7f91a133 2019-12-13 stsp -1, NULL, repo);
3002 234035bc 2019-06-01 stsp goto done;
3003 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3004 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3005 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3006 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3007 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3008 1ee397ad 2019-07-12 stsp status, path2);
3009 234035bc 2019-06-01 stsp goto done;
3011 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3012 36bf999c 2020-07-23 stsp char *link_target2;
3013 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3016 36bf999c 2020-07-23 stsp goto done;
3017 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3018 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3019 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3020 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3021 36bf999c 2020-07-23 stsp free(link_target2);
3022 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3023 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3024 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3025 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3026 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3027 526a746f 2020-07-23 stsp a->progress_arg);
3029 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3030 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3033 dfe9fba0 2020-07-23 stsp goto done;
3034 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3035 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3036 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3037 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3039 234035bc 2019-06-01 stsp goto done;
3042 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3043 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3044 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3045 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3046 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3047 5267b9e4 2021-09-26 stsp 0, 1, a->allow_bad_symlinks, repo,
3048 5267b9e4 2021-09-26 stsp a->progress_cb, a->progress_arg);
3050 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3051 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3052 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3055 234035bc 2019-06-01 stsp goto done;
3056 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3058 234035bc 2019-06-01 stsp goto done;
3059 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3060 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3061 3969253a 2020-03-07 stsp if (err) {
3062 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3063 3969253a 2020-03-07 stsp goto done;
3065 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3066 2b92fad7 2019-06-02 stsp if (err) {
3067 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3068 2b92fad7 2019-06-02 stsp goto done;
3070 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3071 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3072 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3077 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3078 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3079 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3080 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3081 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3082 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3083 1af628f4 2021-06-03 stsp free(id_str);
3084 54d5be07 2021-06-03 stsp free(label_deriv2);
3085 234035bc 2019-06-01 stsp free(ondisk_path);
3086 234035bc 2019-06-01 stsp return err;
3089 69de9dd4 2021-09-03 stsp static const struct got_error *
3090 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3092 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3094 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3095 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3096 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3097 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3098 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3100 69de9dd4 2021-09-03 stsp return NULL;
3103 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3104 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3105 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3106 234035bc 2019-06-01 stsp struct got_repository *repo;
3109 234035bc 2019-06-01 stsp static const struct got_error *
3110 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3111 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3112 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3113 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3114 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3116 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3117 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3118 234035bc 2019-06-01 stsp unsigned char status;
3119 234035bc 2019-06-01 stsp struct stat sb;
3120 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3121 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3122 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3123 234035bc 2019-06-01 stsp char *ondisk_path;
3125 69de9dd4 2021-09-03 stsp if (id == NULL)
3126 69de9dd4 2021-09-03 stsp return NULL;
3128 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3129 69de9dd4 2021-09-03 stsp if (ie == NULL)
3130 69de9dd4 2021-09-03 stsp return NULL;
3132 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3134 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3136 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3137 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3138 5546d466 2021-09-02 stsp free(ondisk_path);
3140 234035bc 2019-06-01 stsp return err;
3141 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3142 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3144 234035bc 2019-06-01 stsp return NULL;
3147 818c7501 2019-07-11 stsp static const struct got_error *
3148 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3149 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3150 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3151 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3152 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3154 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3155 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3156 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3157 a44927cc 2022-04-07 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3158 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3159 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3160 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3161 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
3162 f9d37699 2022-06-28 stsp int fd1 = -1, fd2 = -1;
3164 03415a1a 2019-06-02 stsp if (commit_id1) {
3165 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit1, repo, commit_id1);
3167 a44927cc 2022-04-07 stsp goto done;
3168 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id1, repo, commit1,
3169 03415a1a 2019-06-02 stsp worktree->path_prefix);
3170 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3171 03415a1a 2019-06-02 stsp goto done;
3173 69d57f3d 2020-07-31 stsp if (tree_id1) {
3174 69d57f3d 2020-07-31 stsp char *id_str;
3176 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3178 03415a1a 2019-06-02 stsp goto done;
3180 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3182 f69721c3 2019-10-21 stsp goto done;
3184 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3185 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3186 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3187 f69721c3 2019-10-21 stsp free(id_str);
3188 f69721c3 2019-10-21 stsp goto done;
3190 f69721c3 2019-10-21 stsp free(id_str);
3192 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3193 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3194 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3195 b72706c3 2022-06-01 stsp goto done;
3199 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit2, repo, commit_id2);
3201 a44927cc 2022-04-07 stsp goto done;
3203 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id2, repo, commit2,
3204 234035bc 2019-06-01 stsp worktree->path_prefix);
3206 234035bc 2019-06-01 stsp goto done;
3208 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3210 234035bc 2019-06-01 stsp goto done;
3212 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3213 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3214 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3215 f9d37699 2022-06-28 stsp goto done;
3218 f9d37699 2022-06-28 stsp fd1 = got_opentempfd();
3219 f9d37699 2022-06-28 stsp if (fd1 == -1) {
3220 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3221 b72706c3 2022-06-01 stsp goto done;
3224 f9d37699 2022-06-28 stsp fd2 = got_opentempfd();
3225 f9d37699 2022-06-28 stsp if (fd2 == -1) {
3226 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3227 f9d37699 2022-06-28 stsp goto done;
3230 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3231 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3232 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3233 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3234 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3236 69de9dd4 2021-09-03 stsp goto done;
3238 234035bc 2019-06-01 stsp arg.worktree = worktree;
3239 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3240 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3241 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3242 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3243 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3244 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3245 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3246 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3247 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3248 b72706c3 2022-06-01 stsp merge_file_cb, &arg, 1);
3249 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3250 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3251 af12c6b9 2019-06-04 stsp err = sync_err;
3253 a44927cc 2022-04-07 stsp if (commit1)
3254 a44927cc 2022-04-07 stsp got_object_commit_close(commit1);
3255 a44927cc 2022-04-07 stsp if (commit2)
3256 a44927cc 2022-04-07 stsp got_object_commit_close(commit2);
3257 234035bc 2019-06-01 stsp if (tree1)
3258 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3259 234035bc 2019-06-01 stsp if (tree2)
3260 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3261 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3262 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3263 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3264 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3265 f9d37699 2022-06-28 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3266 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3267 f9d37699 2022-06-28 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3268 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3269 f69721c3 2019-10-21 stsp free(label_orig);
3270 818c7501 2019-07-11 stsp return err;
3273 818c7501 2019-07-11 stsp const struct got_error *
3274 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3275 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3276 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3277 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3279 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3280 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3281 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3283 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3285 818c7501 2019-07-11 stsp return err;
3287 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3289 818c7501 2019-07-11 stsp goto done;
3291 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3292 69de9dd4 2021-09-03 stsp worktree);
3294 818c7501 2019-07-11 stsp goto done;
3296 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3297 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3298 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3300 818c7501 2019-07-11 stsp if (fileindex)
3301 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3302 818c7501 2019-07-11 stsp free(fileindex_path);
3303 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3304 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3305 234035bc 2019-06-01 stsp err = unlockerr;
3306 234035bc 2019-06-01 stsp return err;
3309 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3310 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3311 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3312 927df6b7 2019-02-10 stsp const char *status_path;
3313 927df6b7 2019-02-10 stsp size_t status_path_len;
3314 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3315 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3316 f8d1f275 2019-02-04 stsp void *status_arg;
3317 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3318 f8d1f275 2019-02-04 stsp void *cancel_arg;
3319 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3320 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3321 f2a9dc41 2019-12-13 tracey int report_unchanged;
3322 3143d852 2020-06-25 stsp int no_ignores;
3325 f8d1f275 2019-02-04 stsp static const struct got_error *
3326 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3327 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3328 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3329 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3331 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3332 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3333 2c4740ad 2023-02-12 mark unsigned char staged_status;
3334 927df6b7 2019-02-10 stsp struct stat sb;
3335 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3336 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3337 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3339 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
3340 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3342 98eaaa12 2019-08-03 stsp return err;
3344 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3345 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3346 98eaaa12 2019-08-03 stsp return NULL;
3348 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
3349 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3350 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
3351 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3352 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3353 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3354 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3355 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
3358 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3359 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3362 927df6b7 2019-02-10 stsp static const struct got_error *
3363 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3364 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3366 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3367 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3368 f8d1f275 2019-02-04 stsp char *abspath;
3370 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3371 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3373 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3374 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3375 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3376 927df6b7 2019-02-10 stsp return NULL;
3378 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3379 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3380 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3381 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3383 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3384 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3385 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3388 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3389 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3390 f8d1f275 2019-02-04 stsp free(abspath);
3391 9d31a1d8 2018-03-11 stsp return err;
3394 f8d1f275 2019-02-04 stsp static const struct got_error *
3395 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3397 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3398 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3399 2ec1f75b 2019-03-26 stsp unsigned char status;
3401 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3402 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3404 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3405 927df6b7 2019-02-10 stsp return NULL;
3407 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&blob_id, ie);
3408 b4b2adf5 2023-02-09 op got_fileindex_entry_get_commit_id(&commit_id, ie);
3409 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3410 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3412 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3413 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3414 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3418 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3420 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3422 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3423 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3425 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3427 d8bacb93 2023-01-10 mark got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3430 6841da00 2019-08-08 stsp static const struct got_error *
3431 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3433 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3434 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3435 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3436 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3437 6841da00 2019-08-08 stsp size_t linesize = 0;
3438 6841da00 2019-08-08 stsp ssize_t linelen;
3440 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3441 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3442 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3443 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3445 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3446 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3447 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3449 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3450 bd8de430 2019-10-04 stsp if (line[0] == '#')
3453 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3454 bd8de430 2019-10-04 stsp if (line[0] == '!')
3457 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3458 6841da00 2019-08-08 stsp line) == -1) {
3459 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3460 6841da00 2019-08-08 stsp goto done;
3462 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3464 6841da00 2019-08-08 stsp goto done;
3466 6841da00 2019-08-08 stsp if (ferror(f)) {
3467 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3468 6841da00 2019-08-08 stsp goto done;
3471 6841da00 2019-08-08 stsp dirpath = strdup(path);
3472 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3473 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3474 6841da00 2019-08-08 stsp goto done;
3476 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3478 6841da00 2019-08-08 stsp free(line);
3479 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3480 6841da00 2019-08-08 stsp free(dirpath);
3481 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3483 6841da00 2019-08-08 stsp return err;
3487 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3489 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3491 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3492 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3493 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3494 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3496 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3497 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3499 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3501 bd8de430 2019-10-04 stsp pattern += 3;
3503 bd8de430 2019-10-04 stsp while (*p) {
3504 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3505 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3506 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3507 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3509 bd8de430 2019-10-04 stsp while (*p == '/')
3519 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3520 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3521 6841da00 2019-08-08 stsp * ignores backwards.
3523 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3524 6841da00 2019-08-08 stsp while (pe) {
3525 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3526 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3527 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3528 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3529 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3530 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3531 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3532 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3533 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3538 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3544 6841da00 2019-08-08 stsp static const struct got_error *
3545 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3546 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3548 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3549 6841da00 2019-08-08 stsp char *ignorespath;
3550 886cec17 2019-12-15 stsp int fd = -1;
3551 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3553 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3554 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3555 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3557 886cec17 2019-12-15 stsp if (dirfd != -1) {
3558 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3559 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3560 886cec17 2019-12-15 stsp if (fd == -1) {
3561 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3562 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3563 886cec17 2019-12-15 stsp ignorespath);
3565 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3566 5e91dae4 2022-08-30 stsp if (ignoresfile == NULL)
3567 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3568 886cec17 2019-12-15 stsp ignorespath);
3571 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3575 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3576 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3577 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3578 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3579 886cec17 2019-12-15 stsp ignorespath);
3581 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3584 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3585 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3586 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3587 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3588 6841da00 2019-08-08 stsp free(ignorespath);
3589 6841da00 2019-08-08 stsp return err;
3592 f8d1f275 2019-02-04 stsp static const struct got_error *
3593 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3594 62da3196 2021-10-01 stsp int dirfd)
3596 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3597 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3598 f8d1f275 2019-02-04 stsp char *path = NULL;
3600 62da3196 2021-10-01 stsp if (ignore != NULL)
3601 62da3196 2021-10-01 stsp *ignore = 0;
3603 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3604 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3606 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3607 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3608 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3610 f8d1f275 2019-02-04 stsp path = de->d_name;
3613 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3614 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3615 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3616 62da3196 2021-10-01 stsp *ignore = 1;
3617 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3618 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3619 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3620 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3621 f8d1f275 2019-02-04 stsp if (parent_path[0])
3622 f8d1f275 2019-02-04 stsp free(path);
3623 b72f483a 2019-02-05 stsp return err;
3626 347d1d3e 2019-07-12 stsp static const struct got_error *
3627 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3629 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3630 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3632 3143d852 2020-06-25 stsp if (a->no_ignores)
3633 3143d852 2020-06-25 stsp return NULL;
3635 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3636 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3638 3143d852 2020-06-25 stsp return err;
3640 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3641 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3643 3143d852 2020-06-25 stsp return err;
3646 3143d852 2020-06-25 stsp static const struct got_error *
3647 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3648 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3649 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3650 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3652 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3653 abb4604f 2019-07-27 stsp struct stat sb;
3655 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3657 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3658 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3660 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3661 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3662 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3663 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3664 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3667 916237f3 2022-02-11 stsp if (!no_ignores && match_ignores(ignores, path))
3668 916237f3 2022-02-11 stsp return NULL;
3670 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3671 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3672 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3674 abb4604f 2019-07-27 stsp return NULL;
3677 3143d852 2020-06-25 stsp static const struct got_error *
3678 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3679 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3681 3143d852 2020-06-25 stsp const struct got_error *err;
3682 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3684 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3685 3143d852 2020-06-25 stsp ".cvsignore");
3687 3143d852 2020-06-25 stsp return err;
3689 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3690 3143d852 2020-06-25 stsp ".gitignore");
3692 3143d852 2020-06-25 stsp return err;
3694 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3695 3143d852 2020-06-25 stsp if (err) {
3696 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3697 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3698 3143d852 2020-06-25 stsp return err;
3700 3143d852 2020-06-25 stsp for (;;) {
3701 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3702 3143d852 2020-06-25 stsp ".cvsignore");
3705 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3706 3143d852 2020-06-25 stsp ".gitignore");
3709 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3710 3143d852 2020-06-25 stsp if (err) {
3711 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3712 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3715 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3717 b737c85a 2020-06-26 stsp free(parent_path);
3718 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3719 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3722 b737c85a 2020-06-26 stsp free(parent_path);
3723 b737c85a 2020-06-26 stsp free(next_parent_path);
3724 3143d852 2020-06-25 stsp return err;
3727 abb4604f 2019-07-27 stsp static const struct got_error *
3728 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3729 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3730 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3731 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3732 f2a9dc41 2019-12-13 tracey int report_unchanged)
3734 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3735 6fc93f37 2019-12-13 stsp int fd = -1;
3736 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3737 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3738 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3739 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3740 a84c0d30 2022-03-12 stsp struct got_fileindex_entry *ie;
3742 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3744 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3745 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3746 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3748 a84c0d30 2022-03-12 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3750 a84c0d30 2022-03-12 stsp err = report_single_file_status(path, ondisk_path,
3751 a84c0d30 2022-03-12 stsp fileindex, status_cb, status_arg, repo,
3752 a84c0d30 2022-03-12 stsp report_unchanged, &ignores, no_ignores);
3753 a84c0d30 2022-03-12 stsp goto done;
3756 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3757 6fc93f37 2019-12-13 stsp if (fd == -1) {
3758 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3759 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
3760 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3762 ff56836b 2021-07-08 stsp if (!no_ignores) {
3763 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3764 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3766 ff56836b 2021-07-08 stsp goto done;
3768 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3769 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3770 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3773 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3774 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3775 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3776 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3777 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3778 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3779 abb4604f 2019-07-27 stsp arg.status_path = path;
3780 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3781 abb4604f 2019-07-27 stsp arg.repo = repo;
3782 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3783 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3784 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3785 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3786 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3787 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3788 022fae89 2019-12-06 tracey if (!no_ignores) {
3789 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3790 3143d852 2020-06-25 stsp worktree->root_path, path);
3792 3143d852 2020-06-25 stsp goto done;
3794 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3795 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3796 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3799 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3800 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3801 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3802 927df6b7 2019-02-10 stsp free(ondisk_path);
3803 347d1d3e 2019-07-12 stsp return err;
3806 347d1d3e 2019-07-12 stsp const struct got_error *
3807 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3808 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3809 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3810 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3812 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3813 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3814 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3815 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3817 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3819 347d1d3e 2019-07-12 stsp return err;
3821 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3822 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3823 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3824 f6343036 2021-06-22 stsp no_ignores, 0);
3828 f8d1f275 2019-02-04 stsp free(fileindex_path);
3829 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3830 f8d1f275 2019-02-04 stsp return err;
3833 6c7ab921 2019-03-18 stsp const struct got_error *
3834 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3835 6c7ab921 2019-03-18 stsp const char *arg)
3837 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3838 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3839 6c7ab921 2019-03-18 stsp size_t len;
3840 00bb5ea0 2020-07-23 stsp struct stat sb;
3841 01740607 2020-11-04 stsp char *abspath = NULL;
3842 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3844 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3846 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3847 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3848 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3850 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3851 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3852 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3853 00bb5ea0 2020-07-23 stsp goto done;
3855 727173c3 2020-11-06 stsp sb.st_mode = 0;
3857 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3859 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3860 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3861 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3862 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3864 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3865 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3866 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3867 00bb5ea0 2020-07-23 stsp goto done;
3871 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3872 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3874 d0710d08 2019-07-22 stsp goto done;
3875 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3876 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3877 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3878 00bb5ea0 2020-07-23 stsp goto done;
3881 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3882 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3883 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3884 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3885 00bb5ea0 2020-07-23 stsp goto done;
3887 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3888 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3889 01740607 2020-11-04 stsp goto done;
3891 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3892 01740607 2020-11-04 stsp sizeof(canonpath));
3894 00bb5ea0 2020-07-23 stsp goto done;
3895 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3896 01740607 2020-11-04 stsp if (resolved == NULL) {
3897 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3898 01740607 2020-11-04 stsp goto done;
3903 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3904 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3905 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3906 6c7ab921 2019-03-18 stsp goto done;
3909 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3910 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3911 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3913 f6d88e1a 2019-05-29 stsp goto done;
3915 f6d88e1a 2019-05-29 stsp path = strdup("");
3916 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3917 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3918 f6d88e1a 2019-05-29 stsp goto done;
3922 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3923 6c7ab921 2019-03-18 stsp len = strlen(path);
3924 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3925 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3929 01740607 2020-11-04 stsp free(abspath);
3930 6c7ab921 2019-03-18 stsp free(resolved);
3931 d0710d08 2019-07-22 stsp free(cwd);
3932 6c7ab921 2019-03-18 stsp if (err == NULL)
3933 6c7ab921 2019-03-18 stsp *wt_path = path;
3935 6c7ab921 2019-03-18 stsp free(path);
3936 d00136be 2019-03-26 stsp return err;
3939 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3940 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3941 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3942 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3943 4e68cba3 2019-11-23 stsp void *progress_arg;
3944 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3947 4e68cba3 2019-11-23 stsp static const struct got_error *
3948 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3949 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3950 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3951 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3953 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3954 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3955 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3956 6d022e97 2019-08-04 stsp struct stat sb;
3957 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3959 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3960 dbb83fbd 2019-12-12 stsp relpath) == -1)
3961 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3963 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3965 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3966 12463d8b 2019-12-13 stsp de_name, a->repo);
3968 dbb83fbd 2019-12-12 stsp goto done;
3969 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3970 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3971 dbb83fbd 2019-12-12 stsp goto done;
3972 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3974 dbb83fbd 2019-12-12 stsp goto done;
3977 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3978 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
3979 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
3981 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3982 dbb83fbd 2019-12-12 stsp goto done;
3985 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3987 4e68cba3 2019-11-23 stsp goto done;
3988 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3989 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3990 3969253a 2020-03-07 stsp if (err) {
3991 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3992 3969253a 2020-03-07 stsp goto done;
3994 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3995 07f5b47a 2019-06-02 stsp if (err) {
3996 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3997 4e68cba3 2019-11-23 stsp goto done;
4000 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4002 dbb83fbd 2019-12-12 stsp return err;
4003 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4004 dbb83fbd 2019-12-12 stsp return NULL;
4005 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4008 d00136be 2019-03-26 stsp const struct got_error *
4009 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4010 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4011 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4012 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4014 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4015 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4016 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4017 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4018 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4020 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4022 d00136be 2019-03-26 stsp return err;
4024 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4026 d00136be 2019-03-26 stsp goto done;
4028 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4029 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4030 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4031 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4032 4e68cba3 2019-11-23 stsp saa.repo = repo;
4034 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4035 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4036 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4040 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4041 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4042 af12c6b9 2019-06-04 stsp err = sync_err;
4044 fb399478 2019-07-12 stsp free(fileindex_path);
4045 d00136be 2019-03-26 stsp if (fileindex)
4046 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4047 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4048 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4049 d00136be 2019-03-26 stsp err = unlockerr;
4050 6c7ab921 2019-03-18 stsp return err;
4053 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4054 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4055 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4056 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4057 f2a9dc41 2019-12-13 tracey void *progress_arg;
4058 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4059 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4060 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4061 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4062 766841c2 2020-08-13 stsp const char *status_codes;
4065 f2a9dc41 2019-12-13 tracey static const struct got_error *
4066 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4067 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4068 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4069 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4071 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4072 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4073 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4074 17ed4618 2019-06-02 stsp struct stat sb;
4075 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4077 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4078 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4079 4e12cd97 2022-01-25 stsp return NULL;
4080 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4083 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4084 17ed4618 2019-06-02 stsp if (ie == NULL)
4085 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4087 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4088 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4089 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4090 9acbc4fa 2019-08-03 stsp return NULL;
4091 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4094 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4095 f2a9dc41 2019-12-13 tracey relpath) == -1)
4096 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4098 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4101 f2a9dc41 2019-12-13 tracey goto done;
4103 766841c2 2020-08-13 stsp if (a->status_codes) {
4104 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4106 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4107 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4110 5e91dae4 2022-08-30 stsp if (i == ncodes) {
4111 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4112 766841c2 2020-08-13 stsp free(ondisk_path);
4113 766841c2 2020-08-13 stsp return NULL;
4115 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4116 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4117 766841c2 2020-08-13 stsp static char msg[64];
4118 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4119 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4120 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4121 766841c2 2020-08-13 stsp goto done;
4125 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4126 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4127 f2a9dc41 2019-12-13 tracey goto done;
4128 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4129 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4130 f2a9dc41 2019-12-13 tracey goto done;
4132 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4133 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4134 4e12cd97 2022-01-25 stsp goto done;
4136 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4137 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4138 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4139 f2a9dc41 2019-12-13 tracey goto done;
4143 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4144 20a2ad1c 2020-10-20 stsp size_t root_len;
4146 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4147 a06ca3f7 2022-10-15 stsp if (unlinkat(dirfd, de_name, 0) == -1) {
4148 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4149 12463d8b 2019-12-13 stsp ondisk_path);
4150 12463d8b 2019-12-13 stsp goto done;
4152 a06ca3f7 2022-10-15 stsp } else if (unlink(ondisk_path) == -1) {
4153 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4154 12463d8b 2019-12-13 stsp goto done;
4157 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4159 20a2ad1c 2020-10-20 stsp char *parent;
4160 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4162 2513f20a 2020-10-20 stsp goto done;
4163 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4164 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4165 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4166 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4167 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4168 20a2ad1c 2020-10-20 stsp ondisk_path);
4171 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4172 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4175 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4177 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4178 f2a9dc41 2019-12-13 tracey if (err)
4179 f2a9dc41 2019-12-13 tracey return err;
4180 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4181 f2a9dc41 2019-12-13 tracey return NULL;
4182 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4183 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4186 2ec1f75b 2019-03-26 stsp const struct got_error *
4187 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4188 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4189 766841c2 2020-08-13 stsp const char *status_codes,
4190 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4191 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4193 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4194 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4195 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4196 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4197 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4199 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4201 2ec1f75b 2019-03-26 stsp return err;
4203 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4205 2ec1f75b 2019-03-26 stsp goto done;
4207 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4208 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4209 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4210 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4211 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4212 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4213 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4214 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4215 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4217 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4218 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4219 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4223 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4224 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4225 af12c6b9 2019-06-04 stsp err = sync_err;
4227 fb399478 2019-07-12 stsp free(fileindex_path);
4228 2ec1f75b 2019-03-26 stsp if (fileindex)
4229 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4230 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4231 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4232 2ec1f75b 2019-03-26 stsp err = unlockerr;
4233 33aa809d 2019-08-08 stsp return err;
4236 33aa809d 2019-08-08 stsp static const struct got_error *
4237 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4239 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4240 33aa809d 2019-08-08 stsp char *line = NULL;
4241 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4242 33aa809d 2019-08-08 stsp ssize_t linelen;
4244 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4245 33aa809d 2019-08-08 stsp if (linelen == -1) {
4246 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4247 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4248 33aa809d 2019-08-08 stsp goto done;
4250 33aa809d 2019-08-08 stsp return NULL;
4252 33aa809d 2019-08-08 stsp if (outfile) {
4253 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4254 33aa809d 2019-08-08 stsp if (n != linelen) {
4255 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4256 33aa809d 2019-08-08 stsp goto done;
4259 33aa809d 2019-08-08 stsp if (rejectfile) {
4260 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4261 33aa809d 2019-08-08 stsp if (n != linelen)
4262 910d235d 2023-01-10 mark err = got_ferror(rejectfile, GOT_ERR_IO);
4265 33aa809d 2019-08-08 stsp free(line);
4266 2ec1f75b 2019-03-26 stsp return err;
4269 33aa809d 2019-08-08 stsp static const struct got_error *
4270 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4272 33aa809d 2019-08-08 stsp char *line = NULL;
4273 33aa809d 2019-08-08 stsp size_t linesize = 0;
4274 33aa809d 2019-08-08 stsp ssize_t linelen;
4276 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4277 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4278 500467ff 2019-09-25 hiltjo if (ferror(f))
4279 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4280 500467ff 2019-09-25 hiltjo return NULL;
4282 33aa809d 2019-08-08 stsp free(line);
4283 33aa809d 2019-08-08 stsp return NULL;
4286 33aa809d 2019-08-08 stsp static const struct got_error *
4287 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4288 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4289 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4291 33aa809d 2019-08-08 stsp const struct got_error *err;
4293 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4294 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4295 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4297 33aa809d 2019-08-08 stsp return err;
4298 33aa809d 2019-08-08 stsp (*line_cur1)++;
4300 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4301 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4302 33aa809d 2019-08-08 stsp if (rejectfile)
4303 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4305 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4307 33aa809d 2019-08-08 stsp return err;
4308 33aa809d 2019-08-08 stsp (*line_cur2)++;
4310 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4311 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4312 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4314 33aa809d 2019-08-08 stsp return err;
4315 33aa809d 2019-08-08 stsp (*line_cur2)++;
4317 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4318 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4319 33aa809d 2019-08-08 stsp if (rejectfile)
4320 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4322 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4324 33aa809d 2019-08-08 stsp return err;
4325 33aa809d 2019-08-08 stsp (*line_cur1)++;
4328 f1e81a05 2019-08-10 stsp return NULL;
4331 f1e81a05 2019-08-10 stsp static const struct got_error *
4332 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4333 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4335 f1e81a05 2019-08-10 stsp const struct got_error *err;
4337 f1e81a05 2019-08-10 stsp if (outfile) {
4338 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4339 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4340 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4342 f1e81a05 2019-08-10 stsp return err;
4343 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4346 f1e81a05 2019-08-10 stsp if (rejectfile) {
4347 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4348 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4349 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4351 f1e81a05 2019-08-10 stsp return err;
4352 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4356 33aa809d 2019-08-08 stsp return NULL;
4359 33aa809d 2019-08-08 stsp static const struct got_error *
4360 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4361 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4362 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4363 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4364 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4366 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4367 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4368 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4369 33aa809d 2019-08-08 stsp FILE *hunkfile;
4370 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4371 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4374 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4376 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4377 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4378 fe621944 2020-11-10 stsp start_old = cc.left.start;
4379 fe621944 2020-11-10 stsp end_old = cc.left.end;
4380 fe621944 2020-11-10 stsp start_new = cc.right.start;
4381 fe621944 2020-11-10 stsp end_new = cc.right.end;
4383 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4384 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4385 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4387 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4388 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4389 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4391 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4392 fe621944 2020-11-10 stsp if (diff_state == NULL)
4393 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4394 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4396 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4397 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4398 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4399 33aa809d 2019-08-08 stsp goto done;
4402 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4403 fe621944 2020-11-10 stsp diff_result, &cc);
4404 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4405 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4406 33aa809d 2019-08-08 stsp goto done;
4409 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4410 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4411 33aa809d 2019-08-08 stsp goto done;
4414 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4415 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4417 33aa809d 2019-08-08 stsp goto done;
4419 33aa809d 2019-08-08 stsp switch (*choice) {
4420 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4421 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4422 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4424 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4425 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4426 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4428 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4431 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4435 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4436 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4437 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4438 33aa809d 2019-08-08 stsp return err;
4441 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4442 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4443 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4444 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4445 1f1abb7e 2019-08-08 stsp void *progress_arg;
4446 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4447 33aa809d 2019-08-08 stsp void *patch_arg;
4448 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4449 f259c4c1 2021-09-24 stsp int unlink_added_files;
4452 e20a8b6f 2019-06-04 stsp static const struct got_error *
4453 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4454 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4455 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4456 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4457 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4459 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4460 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4461 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4462 eb81bc23 2022-06-28 tracey int fd = -1, fd2 = -1;
4463 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4464 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4465 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4466 fe621944 2020-11-10 stsp struct stat sb2;
4467 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4468 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4469 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4471 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4473 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4475 33aa809d 2019-08-08 stsp return err;
4477 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4478 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4479 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4480 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4481 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4482 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4483 fa3cef63 2020-07-23 stsp goto done;
4485 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4486 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4487 c0df5966 2021-12-31 stsp if (link_len == -1) {
4488 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4491 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4492 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4495 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4496 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4497 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4498 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4499 fa3cef63 2020-07-23 stsp goto done;
4501 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4502 fa3cef63 2020-07-23 stsp sizeof(link_target));
4503 fa3cef63 2020-07-23 stsp if (link_len == -1)
4504 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4505 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4506 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4509 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4510 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4511 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4512 fa3cef63 2020-07-23 stsp goto done;
4515 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4516 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4517 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4518 fa3cef63 2020-07-23 stsp goto done;
4523 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4524 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4525 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4526 fa3cef63 2020-07-23 stsp goto done;
4528 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4529 fa3cef63 2020-07-23 stsp if (n != link_len) {
4530 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4531 fa3cef63 2020-07-23 stsp goto done;
4533 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4534 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4535 fa3cef63 2020-07-23 stsp goto done;
4537 fa3cef63 2020-07-23 stsp rewind(f2);
4540 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4541 eb81bc23 2022-06-28 tracey if (fd == -1) {
4542 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4543 eb81bc23 2022-06-28 tracey goto done;
4546 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4548 33aa809d 2019-08-08 stsp goto done;
4550 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4552 33aa809d 2019-08-08 stsp goto done;
4554 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4556 33aa809d 2019-08-08 stsp goto done;
4558 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4559 4b752015 2022-06-30 stsp 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4561 33aa809d 2019-08-08 stsp goto done;
4563 b90054ed 2022-11-01 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4566 33aa809d 2019-08-08 stsp goto done;
4568 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4569 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4570 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4571 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4573 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4574 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4575 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4576 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4577 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4578 fe621944 2020-11-10 stsp nchanges++;
4580 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4581 33aa809d 2019-08-08 stsp int choice;
4582 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4583 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4584 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4585 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4586 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4587 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4589 33aa809d 2019-08-08 stsp goto done;
4590 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4591 33aa809d 2019-08-08 stsp have_content = 1;
4592 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4595 1ebedb77 2019-10-19 stsp if (have_content) {
4596 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4597 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4598 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4600 1ebedb77 2019-10-19 stsp goto done;
4602 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4603 b2b3fce1 2022-10-29 op mode_t mode;
4605 b2b3fce1 2022-10-29 op mode = apply_umask(sb2.st_mode);
4606 b2b3fce1 2022-10-29 op if (fchmod(fileno(outfile), mode) == -1) {
4607 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4608 fa3cef63 2020-07-23 stsp goto done;
4613 33aa809d 2019-08-08 stsp free(id_str);
4614 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4615 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4617 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4618 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4619 fe621944 2020-11-10 stsp if (err == NULL)
4620 fe621944 2020-11-10 stsp err = free_err;
4621 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4622 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4623 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4624 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4625 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4626 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4627 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4628 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4629 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4630 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4631 33aa809d 2019-08-08 stsp if (err || !have_content) {
4632 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4633 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4634 33aa809d 2019-08-08 stsp free(*path_outfile);
4635 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4637 33aa809d 2019-08-08 stsp free(path1);
4638 33aa809d 2019-08-08 stsp return err;
4641 33aa809d 2019-08-08 stsp static const struct got_error *
4642 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4643 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4644 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4645 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4647 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4648 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4649 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4650 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4651 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
4652 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4653 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4654 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4655 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4656 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4657 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4658 eb81bc23 2022-06-28 tracey int fd = -1;
4660 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4661 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4662 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4663 d3bcc3d1 2019-08-08 stsp return NULL;
4665 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4666 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4667 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4669 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4670 65084dad 2019-08-08 stsp if (ie == NULL)
4671 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4673 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4674 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4675 a129376b 2019-03-28 stsp if (err) {
4676 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4677 a129376b 2019-03-28 stsp goto done;
4678 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4679 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4680 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4681 e20a8b6f 2019-06-04 stsp goto done;
4684 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4685 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4686 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4687 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4688 a129376b 2019-03-28 stsp goto done;
4691 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4692 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4693 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4694 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4695 a129376b 2019-03-28 stsp goto done;
4698 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4699 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4700 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4701 a129376b 2019-03-28 stsp goto done;
4706 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
4707 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
4709 a44927cc 2022-04-07 stsp goto done;
4711 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4712 a9fa2909 2019-07-27 stsp if (err) {
4713 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4714 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4715 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4716 a9fa2909 2019-07-27 stsp goto done;
4718 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4720 a9fa2909 2019-07-27 stsp goto done;
4722 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4724 a9fa2909 2019-07-27 stsp goto done;
4726 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4727 1233e6b6 2020-10-19 stsp free(te_name);
4728 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4729 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4730 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4731 a9fa2909 2019-07-27 stsp goto done;
4735 a129376b 2019-03-28 stsp switch (status) {
4736 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4737 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4738 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4739 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4740 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4742 33aa809d 2019-08-08 stsp goto done;
4743 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4746 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4747 1f1abb7e 2019-08-08 stsp ie->path);
4749 1ee397ad 2019-07-12 stsp goto done;
4750 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4751 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
4752 f259c4c1 2021-09-24 stsp if (asprintf(&ondisk_path, "%s/%s",
4753 f259c4c1 2021-09-24 stsp got_worktree_get_root_path(a->worktree),
4754 f259c4c1 2021-09-24 stsp relpath) == -1) {
4755 f259c4c1 2021-09-24 stsp err = got_error_from_errno("asprintf");
4756 f259c4c1 2021-09-24 stsp goto done;
4758 f259c4c1 2021-09-24 stsp if (unlink(ondisk_path) == -1) {
4759 f259c4c1 2021-09-24 stsp err = got_error_from_errno2("unlink",
4760 f259c4c1 2021-09-24 stsp ondisk_path);
4765 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4766 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4767 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4768 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4769 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4771 33aa809d 2019-08-08 stsp goto done;
4772 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4775 33aa809d 2019-08-08 stsp /* fall through */
4776 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4777 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4778 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4779 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4780 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4781 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4782 b4b2adf5 2023-02-09 op staged_status == GOT_STATUS_MODIFY)
4783 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
4785 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
4786 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4787 eb81bc23 2022-06-28 tracey if (fd == -1) {
4788 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4789 eb81bc23 2022-06-28 tracey goto done;
4792 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4794 65084dad 2019-08-08 stsp goto done;
4796 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4797 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4798 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4799 a129376b 2019-03-28 stsp goto done;
4802 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4803 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4804 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4805 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4806 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4807 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4808 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4810 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4811 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4812 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4813 369fd7e5 2020-07-23 stsp path_content);
4816 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4817 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4818 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4819 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4821 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4822 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4823 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4824 369fd7e5 2020-07-23 stsp goto done;
4828 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4829 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4830 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4831 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4832 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4833 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4835 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4837 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4838 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4839 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4840 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4843 a129376b 2019-03-28 stsp goto done;
4844 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4845 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4846 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4847 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4848 437adc9d 2020-12-10 yzhong blob->id.sha1,
4849 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4851 2e1fa222 2020-07-23 stsp goto done;
4853 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4854 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4855 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4864 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4865 33aa809d 2019-08-08 stsp free(path_content);
4866 e20a8b6f 2019-06-04 stsp free(parent_path);
4867 a129376b 2019-03-28 stsp free(tree_path);
4868 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4869 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4871 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4873 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4874 a129376b 2019-03-28 stsp free(tree_id);
4875 a44927cc 2022-04-07 stsp if (base_commit)
4876 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
4877 e20a8b6f 2019-06-04 stsp return err;
4880 e20a8b6f 2019-06-04 stsp const struct got_error *
4881 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4882 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4883 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4884 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4885 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4887 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4888 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4889 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4890 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4891 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4892 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4894 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4896 e20a8b6f 2019-06-04 stsp return err;
4898 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4900 e20a8b6f 2019-06-04 stsp goto done;
4902 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4903 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4904 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4905 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4906 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4907 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4908 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4909 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
4910 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4911 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4912 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
4916 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4917 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4918 e20a8b6f 2019-06-04 stsp err = sync_err;
4920 fb399478 2019-07-12 stsp free(fileindex_path);
4921 a129376b 2019-03-28 stsp if (fileindex)
4922 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4923 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4924 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4925 a129376b 2019-03-28 stsp err = unlockerr;
4926 c4296144 2019-05-09 stsp return err;
4929 cf066bf8 2019-05-09 stsp static void
4930 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4932 24519714 2019-05-09 stsp free(ct->path);
4933 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4934 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4935 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4936 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4937 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4938 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4942 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4943 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4944 24519714 2019-05-09 stsp struct got_repository *repo;
4945 24519714 2019-05-09 stsp struct got_worktree *worktree;
4946 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4947 5f8a88c6 2019-08-03 stsp int have_staged_files;
4948 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4949 2a47b1e5 2022-11-01 stsp int diff_header_shown;
4950 2a47b1e5 2022-11-01 stsp FILE *diff_outfile;
4956 2a47b1e5 2022-11-01 stsp * Create a file which contains the target path of a symlink so we can feed
4957 2a47b1e5 2022-11-01 stsp * it as content to the diff engine.
4959 2a47b1e5 2022-11-01 stsp static const struct got_error *
4960 2a47b1e5 2022-11-01 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4961 2a47b1e5 2022-11-01 stsp const char *abspath)
4963 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
4964 2a47b1e5 2022-11-01 stsp char target_path[PATH_MAX];
4965 2a47b1e5 2022-11-01 stsp ssize_t target_len, outlen;
4969 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
4970 2a47b1e5 2022-11-01 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4971 2a47b1e5 2022-11-01 stsp if (target_len == -1)
4972 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlinkat", abspath);
4974 2a47b1e5 2022-11-01 stsp target_len = readlink(abspath, target_path, PATH_MAX);
4975 2a47b1e5 2022-11-01 stsp if (target_len == -1)
4976 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlink", abspath);
4979 2a47b1e5 2022-11-01 stsp *fd = got_opentempfd();
4980 2a47b1e5 2022-11-01 stsp if (*fd == -1)
4981 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentempfd");
4983 2a47b1e5 2022-11-01 stsp outlen = write(*fd, target_path, target_len);
4984 2a47b1e5 2022-11-01 stsp if (outlen == -1) {
4985 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
4986 2a47b1e5 2022-11-01 stsp goto done;
4989 2a47b1e5 2022-11-01 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
4990 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("lseek", abspath);
4991 2a47b1e5 2022-11-01 stsp goto done;
4994 2a47b1e5 2022-11-01 stsp if (err) {
4995 2a47b1e5 2022-11-01 stsp close(*fd);
4998 2a47b1e5 2022-11-01 stsp return err;
5001 2a47b1e5 2022-11-01 stsp static const struct got_error *
5002 2a47b1e5 2022-11-01 stsp append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5003 2a47b1e5 2022-11-01 stsp FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5004 6d15dc69 2022-11-01 stsp int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5006 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5007 2a47b1e5 2022-11-01 stsp struct got_blob_object *blob1 = NULL;
5008 2a47b1e5 2022-11-01 stsp int fd = -1, fd1 = -1, fd2 = -1;
5009 2a47b1e5 2022-11-01 stsp FILE *ondisk_file = NULL;
5010 2a47b1e5 2022-11-01 stsp char *label1 = NULL;
5011 2a47b1e5 2022-11-01 stsp struct stat sb;
5012 2a47b1e5 2022-11-01 stsp off_t size1 = 0;
5013 2a47b1e5 2022-11-01 stsp int f2_exists = 0;
5014 2a47b1e5 2022-11-01 stsp char *id_str = NULL;
5016 2a47b1e5 2022-11-01 stsp memset(&sb, 0, sizeof(sb));
5018 4ba5cca9 2022-11-01 stsp if (diff_staged) {
5019 4ba5cca9 2022-11-01 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5020 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_ADD &&
5021 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_DELETE)
5022 4ba5cca9 2022-11-01 stsp return NULL;
5024 4ba5cca9 2022-11-01 stsp if (ct->status != GOT_STATUS_MODIFY &&
5025 4ba5cca9 2022-11-01 stsp ct->status != GOT_STATUS_ADD &&
5026 4ba5cca9 2022-11-01 stsp ct->status != GOT_STATUS_DELETE)
5027 4ba5cca9 2022-11-01 stsp return NULL;
5030 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f1);
5032 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5033 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f2);
5035 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5037 2a47b1e5 2022-11-01 stsp if (!*diff_header_shown) {
5038 2a47b1e5 2022-11-01 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
5040 2a47b1e5 2022-11-01 stsp return err;
5041 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5042 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree));
5043 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "commit - %s\n", id_str);
5044 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "path + %s%s\n",
5045 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree),
5046 2a47b1e5 2022-11-01 stsp diff_staged ? " (staged changes)" : "");
5047 2a47b1e5 2022-11-01 stsp *diff_header_shown = 1;
5050 2a47b1e5 2022-11-01 stsp if (diff_staged) {
5051 2a47b1e5 2022-11-01 stsp const char *label1 = NULL, *label2 = NULL;
5052 2a47b1e5 2022-11-01 stsp switch (ct->staged_status) {
5053 2a47b1e5 2022-11-01 stsp case GOT_STATUS_MODIFY:
5054 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5055 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5057 2a47b1e5 2022-11-01 stsp case GOT_STATUS_ADD:
5058 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5060 2a47b1e5 2022-11-01 stsp case GOT_STATUS_DELETE:
5061 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5064 2a47b1e5 2022-11-01 stsp return got_error(GOT_ERR_FILE_STATUS);
5066 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5067 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5068 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5069 2a47b1e5 2022-11-01 stsp goto done;
5071 2a47b1e5 2022-11-01 stsp fd2 = got_opentempfd();
5072 2a47b1e5 2022-11-01 stsp if (fd2 == -1) {
5073 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5074 2a47b1e5 2022-11-01 stsp goto done;
5076 2a47b1e5 2022-11-01 stsp err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5077 2a47b1e5 2022-11-01 stsp fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5078 1f3405c9 2023-01-17 mark label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5079 a76e88e5 2023-01-10 mark NULL, repo, diff_outfile);
5080 2a47b1e5 2022-11-01 stsp goto done;
5083 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5084 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5085 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5086 2a47b1e5 2022-11-01 stsp goto done;
5089 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_ADD) {
5090 2a47b1e5 2022-11-01 stsp err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5091 2a47b1e5 2022-11-01 stsp 8192, fd1);
5093 2a47b1e5 2022-11-01 stsp goto done;
5096 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_DELETE) {
5097 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5098 2a47b1e5 2022-11-01 stsp fd = openat(dirfd, de_name,
5099 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5100 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5101 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5102 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("openat",
5103 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5104 2a47b1e5 2022-11-01 stsp goto done;
5106 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5107 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5109 2a47b1e5 2022-11-01 stsp goto done;
5112 2a47b1e5 2022-11-01 stsp fd = open(ct->ondisk_path,
5113 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5114 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5115 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5116 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("open",
5117 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5118 2a47b1e5 2022-11-01 stsp goto done;
5120 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5121 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5123 2a47b1e5 2022-11-01 stsp goto done;
5126 2a47b1e5 2022-11-01 stsp if (fstatat(fd, ct->ondisk_path, &sb,
5127 2a47b1e5 2022-11-01 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5128 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fstatat", ct->ondisk_path);
5129 2a47b1e5 2022-11-01 stsp goto done;
5131 2a47b1e5 2022-11-01 stsp ondisk_file = fdopen(fd, "r");
5132 2a47b1e5 2022-11-01 stsp if (ondisk_file == NULL) {
5133 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fdopen", ct->ondisk_path);
5134 2a47b1e5 2022-11-01 stsp goto done;
5137 2a47b1e5 2022-11-01 stsp f2_exists = 1;
5140 2a47b1e5 2022-11-01 stsp if (blob1) {
5141 2a47b1e5 2022-11-01 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5142 2a47b1e5 2022-11-01 stsp f1, blob1);
5144 2a47b1e5 2022-11-01 stsp goto done;
5147 2a47b1e5 2022-11-01 stsp err = got_diff_blob_file(blob1, f1, size1, label1,
5148 2a47b1e5 2022-11-01 stsp ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5149 1f3405c9 2023-01-17 mark GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5151 2a47b1e5 2022-11-01 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5152 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5153 2a47b1e5 2022-11-01 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5154 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5155 2a47b1e5 2022-11-01 stsp if (blob1)
5156 2a47b1e5 2022-11-01 stsp got_object_blob_close(blob1);
5157 2a47b1e5 2022-11-01 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5158 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5159 2a47b1e5 2022-11-01 stsp if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5160 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
5161 2a47b1e5 2022-11-01 stsp return err;
5164 c4296144 2019-05-09 stsp static const struct got_error *
5165 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5166 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5167 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5168 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5170 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5171 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5172 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5173 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5174 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5175 768aea60 2019-05-09 stsp struct stat sb;
5177 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5178 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5179 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5180 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5181 5f8a88c6 2019-08-03 stsp return NULL;
5183 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
5184 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5186 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5187 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5188 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5189 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
5190 5f8a88c6 2019-08-03 stsp return NULL;
5193 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5194 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5195 036813ee 2019-05-09 stsp goto done;
5197 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5198 036813ee 2019-05-09 stsp parent_path = strdup("");
5199 69960a46 2019-05-09 stsp if (parent_path == NULL)
5200 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5202 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5204 69960a46 2019-05-09 stsp return err;
5207 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5208 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5209 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5210 c4296144 2019-05-09 stsp goto done;
5213 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5214 768aea60 2019-05-09 stsp relpath) == -1) {
5215 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5216 768aea60 2019-05-09 stsp goto done;
5219 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5220 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5221 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5222 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5223 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5224 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5225 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5226 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5228 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5229 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5232 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5233 0aeb8099 2020-07-23 stsp goto done;
5235 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5236 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5237 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5238 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5239 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5240 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5241 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5242 12463d8b 2019-12-13 stsp ct->ondisk_path);
5243 12463d8b 2019-12-13 stsp goto done;
5245 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5246 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5247 768aea60 2019-05-09 stsp goto done;
5249 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5252 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5253 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5254 44d03001 2019-05-09 stsp relpath) == -1) {
5255 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5256 44d03001 2019-05-09 stsp goto done;
5259 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5260 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5261 35213c7c 2020-07-23 stsp int is_bad_symlink;
5262 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5263 35213c7c 2020-07-23 stsp ssize_t target_len;
5264 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5265 35213c7c 2020-07-23 stsp sizeof(target_path));
5266 35213c7c 2020-07-23 stsp if (target_len == -1) {
5267 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5268 35213c7c 2020-07-23 stsp ct->ondisk_path);
5269 35213c7c 2020-07-23 stsp goto done;
5271 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5272 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5274 35213c7c 2020-07-23 stsp goto done;
5275 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5276 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5277 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5278 35213c7c 2020-07-23 stsp goto done;
5283 cf066bf8 2019-05-09 stsp ct->status = status;
5284 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5285 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5286 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5287 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5288 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5289 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5290 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5291 b416585c 2019-05-13 stsp goto done;
5293 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5294 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5295 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5296 f0b75401 2019-08-03 stsp goto done;
5299 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5300 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5301 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5302 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5303 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5304 036813ee 2019-05-09 stsp goto done;
5307 24519714 2019-05-09 stsp ct->path = strdup(path);
5308 24519714 2019-05-09 stsp if (ct->path == NULL) {
5309 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5310 24519714 2019-05-09 stsp goto done;
5312 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5314 2a47b1e5 2022-11-01 stsp goto done;
5316 2a47b1e5 2022-11-01 stsp if (a->diff_outfile && ct && new != NULL) {
5317 2a47b1e5 2022-11-01 stsp err = append_ct_diff(ct, &a->diff_header_shown,
5318 2a47b1e5 2022-11-01 stsp a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5319 6d15dc69 2022-11-01 stsp a->have_staged_files, a->repo, a->worktree);
5321 2a47b1e5 2022-11-01 stsp goto done;
5324 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5325 ed175427 2019-05-09 stsp free_commitable(ct);
5326 24519714 2019-05-09 stsp free(parent_path);
5327 036813ee 2019-05-09 stsp free(path);
5328 a129376b 2019-03-28 stsp return err;
5331 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5332 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5333 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5334 036813ee 2019-05-09 stsp struct got_repository *);
5336 ed175427 2019-05-09 stsp static const struct got_error *
5337 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5338 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5339 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5340 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5341 afa376bf 2019-05-09 stsp struct got_repository *repo)
5343 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5344 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5345 036813ee 2019-05-09 stsp char *subpath;
5347 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5348 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5349 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5351 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5353 ed175427 2019-05-09 stsp return err;
5355 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5356 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5357 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5358 036813ee 2019-05-09 stsp free(subpath);
5359 036813ee 2019-05-09 stsp return err;
5362 036813ee 2019-05-09 stsp static const struct got_error *
5363 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5365 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5366 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5368 036813ee 2019-05-09 stsp *match = 0;
5370 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5371 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5372 0f63689d 2019-05-10 stsp return NULL;
5375 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5377 0f63689d 2019-05-10 stsp return err;
5378 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5379 036813ee 2019-05-09 stsp free(ct_parent_path);
5380 036813ee 2019-05-09 stsp return err;
5383 768aea60 2019-05-09 stsp static mode_t
5384 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5386 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5387 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5389 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5392 036813ee 2019-05-09 stsp static const struct got_error *
5393 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5394 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5396 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5398 036813ee 2019-05-09 stsp *new_te = NULL;
5400 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5402 036813ee 2019-05-09 stsp goto done;
5404 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5406 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5407 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5408 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5410 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5412 036813ee 2019-05-09 stsp if (err && *new_te) {
5413 56e0773d 2019-11-28 stsp free(*new_te);
5414 036813ee 2019-05-09 stsp *new_te = NULL;
5416 036813ee 2019-05-09 stsp return err;
5419 036813ee 2019-05-09 stsp static const struct got_error *
5420 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5421 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5423 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5424 102b254e 2020-10-19 stsp char *ct_name = NULL;
5426 036813ee 2019-05-09 stsp *new_te = NULL;
5428 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5429 036813ee 2019-05-09 stsp if (*new_te == NULL)
5430 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5432 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5434 036813ee 2019-05-09 stsp goto done;
5435 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5436 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5437 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5438 036813ee 2019-05-09 stsp goto done;
5441 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5443 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5444 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5445 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5447 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5449 102b254e 2020-10-19 stsp free(ct_name);
5450 036813ee 2019-05-09 stsp if (err && *new_te) {
5451 56e0773d 2019-11-28 stsp free(*new_te);
5452 036813ee 2019-05-09 stsp *new_te = NULL;
5454 036813ee 2019-05-09 stsp return err;
5457 036813ee 2019-05-09 stsp static const struct got_error *
5458 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5459 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5461 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5462 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5464 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5466 036813ee 2019-05-09 stsp return err;
5467 036813ee 2019-05-09 stsp if (new_pe == NULL)
5468 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5469 036813ee 2019-05-09 stsp return NULL;
5472 afa376bf 2019-05-09 stsp static const struct got_error *
5473 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5474 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5476 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5477 5f8a88c6 2019-08-03 stsp unsigned char status;
5479 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5480 0ff8d236 2021-09-28 stsp return NULL;
5482 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5483 afa376bf 2019-05-09 stsp ct_path++;
5485 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5486 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5488 5f8a88c6 2019-08-03 stsp status = ct->status;
5490 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5491 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5494 036813ee 2019-05-09 stsp static const struct got_error *
5495 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5496 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5498 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5499 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5500 44d03001 2019-05-09 stsp char *te_path;
5502 44d03001 2019-05-09 stsp *modified = 0;
5504 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5505 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5506 44d03001 2019-05-09 stsp te->name) == -1)
5507 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5509 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5510 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5511 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5512 44d03001 2019-05-09 stsp strlen(te_path));
5513 62d463ca 2020-10-20 naddy if (*modified)
5517 44d03001 2019-05-09 stsp free(te_path);
5518 44d03001 2019-05-09 stsp return err;
5521 44d03001 2019-05-09 stsp static const struct got_error *
5522 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5523 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5524 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5526 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5527 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5529 036813ee 2019-05-09 stsp *ctp = NULL;
5531 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5532 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5533 036813ee 2019-05-09 stsp char *ct_name = NULL;
5534 036813ee 2019-05-09 stsp int path_matches;
5536 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5537 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5538 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5539 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5542 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5543 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5547 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5550 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5552 036813ee 2019-05-09 stsp return err;
5553 036813ee 2019-05-09 stsp if (!path_matches)
5556 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5558 d34b633e 2020-10-19 stsp return err;
5560 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5561 d34b633e 2020-10-19 stsp free(ct_name);
5564 d34b633e 2020-10-19 stsp free(ct_name);
5566 036813ee 2019-05-09 stsp *ctp = ct;
5570 2b496619 2019-07-10 stsp return err;
5573 2b496619 2019-07-10 stsp static const struct got_error *
5574 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5575 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5576 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5577 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5578 2b496619 2019-07-10 stsp struct got_repository *repo)
5580 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5581 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5582 2b496619 2019-07-10 stsp char *subtree_path;
5583 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5584 ba580f68 2020-03-22 stsp int nentries;
5586 2b496619 2019-07-10 stsp *new_tep = NULL;
5588 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5589 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5590 2b496619 2019-07-10 stsp child_path) == -1)
5591 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5593 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5594 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5595 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5596 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5598 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5599 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5600 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5601 2b496619 2019-07-10 stsp goto done;
5603 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5604 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5605 2b496619 2019-07-10 stsp if (err) {
5606 56e0773d 2019-11-28 stsp free(new_te);
5607 2b496619 2019-07-10 stsp goto done;
5609 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5612 2b496619 2019-07-10 stsp free(subtree_path);
5613 2b496619 2019-07-10 stsp if (err == NULL)
5614 2b496619 2019-07-10 stsp *new_tep = new_te;
5615 036813ee 2019-05-09 stsp return err;
5618 036813ee 2019-05-09 stsp static const struct got_error *
5619 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5620 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5621 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5622 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5623 036813ee 2019-05-09 stsp struct got_repository *repo)
5625 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5626 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5627 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5628 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5630 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5631 ba580f68 2020-03-22 stsp *nentries = 0;
5633 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5634 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5635 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5636 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5638 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5639 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5640 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5643 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5644 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5647 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5648 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5650 036813ee 2019-05-09 stsp goto done;
5652 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5653 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5654 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5656 036813ee 2019-05-09 stsp goto done;
5657 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5659 afa376bf 2019-05-09 stsp goto done;
5660 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5661 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5663 2f51b5b3 2019-05-09 stsp goto done;
5664 ba580f68 2020-03-22 stsp (*nentries)++;
5666 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5667 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5668 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5669 2b496619 2019-07-10 stsp == NULL) {
5670 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5671 2b496619 2019-07-10 stsp child_path, path_base_tree,
5672 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5675 2b496619 2019-07-10 stsp goto done;
5676 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5678 2b496619 2019-07-10 stsp goto done;
5679 ba580f68 2020-03-22 stsp (*nentries)++;
5684 2f51b5b3 2019-05-09 stsp if (base_tree) {
5685 56e0773d 2019-11-28 stsp int i, nbase_entries;
5686 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5687 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5688 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5689 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5691 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5692 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5693 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5694 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5696 63c5ca5d 2019-08-24 stsp goto done;
5697 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5699 63c5ca5d 2019-08-24 stsp goto done;
5700 ba580f68 2020-03-22 stsp (*nentries)++;
5704 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5705 44d03001 2019-05-09 stsp int modified;
5706 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5708 036813ee 2019-05-09 stsp goto done;
5709 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5710 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5712 2f51b5b3 2019-05-09 stsp goto done;
5713 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5714 44d03001 2019-05-09 stsp if (modified) {
5715 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5716 ba580f68 2020-03-22 stsp int nsubentries;
5717 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5718 ba580f68 2020-03-22 stsp &nsubentries, te,
5719 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5720 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5722 44d03001 2019-05-09 stsp goto done;
5723 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5724 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5725 ba580f68 2020-03-22 stsp free(new_id);
5728 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5729 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5730 56e0773d 2019-11-28 stsp free(new_id);
5732 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5734 036813ee 2019-05-09 stsp goto done;
5735 ba580f68 2020-03-22 stsp (*nentries)++;
5739 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5740 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5742 f66c734c 2019-09-22 stsp goto done;
5744 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5745 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5746 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5747 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5748 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5749 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5751 2f51b5b3 2019-05-09 stsp goto done;
5752 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5754 2f51b5b3 2019-05-09 stsp goto done;
5755 ba580f68 2020-03-22 stsp (*nentries)++;
5757 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5758 b416585c 2019-05-13 stsp status_arg);
5760 afa376bf 2019-05-09 stsp goto done;
5762 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5763 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5765 2f51b5b3 2019-05-09 stsp goto done;
5766 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5768 2f51b5b3 2019-05-09 stsp goto done;
5769 ba580f68 2020-03-22 stsp (*nentries)++;
5774 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5775 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5777 d8bacb93 2023-01-10 mark got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5778 2e1fa222 2020-07-23 stsp return err;
5781 2e1fa222 2020-07-23 stsp static const struct got_error *
5782 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5783 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5784 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5785 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5787 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5788 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5789 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5791 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5792 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5793 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5795 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5797 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5798 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5799 437adc9d 2020-12-10 yzhong if (err)
5800 437adc9d 2020-12-10 yzhong goto done;
5803 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5804 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5805 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5806 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5807 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5808 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5809 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5810 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5812 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5813 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5814 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5815 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5816 72fd46fa 2019-09-06 stsp !have_staged_files);
5818 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5819 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5820 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5821 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5822 72fd46fa 2019-09-06 stsp !have_staged_files);
5824 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5826 437adc9d 2020-12-10 yzhong goto done;
5827 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5828 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5829 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5830 3969253a 2020-03-07 stsp if (err) {
5831 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5832 437adc9d 2020-12-10 yzhong goto done;
5834 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5835 3969253a 2020-03-07 stsp if (err) {
5836 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5837 437adc9d 2020-12-10 yzhong goto done;
5840 437adc9d 2020-12-10 yzhong free(relpath);
5841 437adc9d 2020-12-10 yzhong relpath = NULL;
5844 437adc9d 2020-12-10 yzhong free(relpath);
5845 d56d26ce 2019-05-10 stsp return err;
5849 d56d26ce 2019-05-10 stsp static const struct got_error *
5850 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5851 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5852 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5853 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5854 735ef5ac 2019-08-03 stsp int ood_errcode)
5856 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5857 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5858 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5860 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5861 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5862 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5863 1a36436d 2019-06-10 stsp return NULL;
5865 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5866 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5868 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5870 a44927cc 2022-04-07 stsp goto done;
5871 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5872 9bead371 2019-07-28 stsp if (err) {
5873 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5874 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5875 1a36436d 2019-06-10 stsp goto done;
5876 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5877 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5879 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5880 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5882 a44927cc 2022-04-07 stsp goto done;
5883 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5884 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5885 1a36436d 2019-06-10 stsp goto done;
5886 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5890 a44927cc 2022-04-07 stsp if (commit)
5891 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5892 1a36436d 2019-06-10 stsp return err;
5895 336075a4 2022-06-25 op static const struct got_error *
5896 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5897 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5898 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
5899 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
5900 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
5901 2a47b1e5 2022-11-01 stsp const char *author, const char *committer, char *diff_path,
5902 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5903 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5904 afa376bf 2019-05-09 stsp struct got_repository *repo)
5906 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5907 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5908 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5909 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5910 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5911 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5912 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5913 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5914 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
5915 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5916 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5917 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5918 f8399b8f 2022-07-22 stsp time_t timestamp;
5920 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5922 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5924 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5926 588edf97 2019-05-10 stsp goto done;
5928 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5930 588edf97 2019-05-10 stsp goto done;
5932 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5933 2a47b1e5 2022-11-01 stsp err = commit_msg_cb(commitable_paths, diff_path,
5934 2a47b1e5 2022-11-01 stsp &logmsg, commit_arg);
5939 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5940 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5944 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5945 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5946 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5947 cf066bf8 2019-05-09 stsp char *ondisk_path;
5949 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5950 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5951 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5954 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5955 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5956 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5959 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5960 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5961 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5962 cf066bf8 2019-05-09 stsp goto done;
5964 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5965 2e1fa222 2020-07-23 stsp free(ondisk_path);
5967 cf066bf8 2019-05-09 stsp goto done;
5970 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5971 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5972 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5974 036813ee 2019-05-09 stsp goto done;
5976 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5978 de18fc63 2019-05-09 stsp goto done;
5979 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5980 f259c4c1 2021-09-24 stsp nparents++;
5981 f259c4c1 2021-09-24 stsp if (parent_id2) {
5982 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
5984 f259c4c1 2021-09-24 stsp goto done;
5985 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5986 f259c4c1 2021-09-24 stsp nparents++;
5988 f8399b8f 2022-07-22 stsp timestamp = time(NULL);
5989 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5990 f8399b8f 2022-07-22 stsp nparents, author, timestamp, committer, timestamp, logmsg, repo);
5991 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5992 33ad4cbe 2019-05-12 jcs free(logmsg);
5994 9d40349a 2019-05-09 stsp goto done;
5996 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5997 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5998 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5999 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6000 09f5bd90 2019-05-09 stsp goto done;
6002 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6003 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6005 09f5bd90 2019-05-09 stsp goto done;
6006 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6008 ebf99748 2019-05-09 stsp goto done;
6009 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6010 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6011 09f5bd90 2019-05-09 stsp goto done;
6013 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6014 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6016 f2c16586 2019-05-09 stsp goto done;
6017 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6019 f2c16586 2019-05-09 stsp goto done;
6021 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6023 09f5bd90 2019-05-09 stsp goto done;
6025 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6027 ebf99748 2019-05-09 stsp goto done;
6029 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
6030 588edf97 2019-05-10 stsp if (head_tree)
6031 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6032 588edf97 2019-05-10 stsp if (head_commit)
6033 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6034 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6035 2f17228e 2019-05-12 stsp if (head_ref2) {
6036 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6037 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6038 2f17228e 2019-05-12 stsp err = unlockerr;
6039 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6041 39cd0ff6 2019-07-12 stsp return err;
6044 5c1e53bc 2019-07-28 stsp static const struct got_error *
6045 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6046 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6048 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6049 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6051 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6052 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6053 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6055 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6056 5c1e53bc 2019-07-28 stsp ct_path++;
6058 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6059 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6063 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6064 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6066 5c1e53bc 2019-07-28 stsp return NULL;
6069 f0b75401 2019-08-03 stsp static const struct got_error *
6070 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6072 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6074 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6075 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6076 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6079 f0b75401 2019-08-03 stsp return NULL;
6082 5f8a88c6 2019-08-03 stsp static const struct got_error *
6083 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6084 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6086 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6087 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6089 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6090 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6092 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6093 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6094 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6095 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6096 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6097 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6100 5f8a88c6 2019-08-03 stsp return NULL;
6103 39cd0ff6 2019-07-12 stsp const struct got_error *
6104 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6105 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6106 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6107 2a47b1e5 2022-11-01 stsp int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6108 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6109 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6111 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6112 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6113 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6114 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6115 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6116 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6117 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6118 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6119 2a47b1e5 2022-11-01 stsp char *diff_path = NULL;
6120 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6122 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6124 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6125 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6127 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6129 39cd0ff6 2019-07-12 stsp goto done;
6131 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6133 39cd0ff6 2019-07-12 stsp goto done;
6135 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6137 39cd0ff6 2019-07-12 stsp goto done;
6139 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6141 39cd0ff6 2019-07-12 stsp goto done;
6143 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6144 5f8a88c6 2019-08-03 stsp &have_staged_files);
6145 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6146 5f8a88c6 2019-08-03 stsp goto done;
6147 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6148 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6150 5f8a88c6 2019-08-03 stsp goto done;
6153 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6154 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6155 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6156 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6157 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6158 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6159 2a47b1e5 2022-11-01 stsp cc_arg.diff_header_shown = 0;
6160 2a47b1e5 2022-11-01 stsp if (show_diff) {
6161 2a47b1e5 2022-11-01 stsp err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6162 b90054ed 2022-11-01 stsp GOT_TMPDIR_STR "/got", ".diff");
6164 2a47b1e5 2022-11-01 stsp goto done;
6165 2a47b1e5 2022-11-01 stsp cc_arg.f1 = got_opentemp();
6166 2a47b1e5 2022-11-01 stsp if (cc_arg.f1 == NULL) {
6167 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6168 2a47b1e5 2022-11-01 stsp goto done;
6170 2a47b1e5 2022-11-01 stsp cc_arg.f2 = got_opentemp();
6171 2a47b1e5 2022-11-01 stsp if (cc_arg.f2 == NULL) {
6172 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6173 2a47b1e5 2022-11-01 stsp goto done;
6177 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6178 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6179 4ba2e955 2022-09-02 sh+got collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6181 5c1e53bc 2019-07-28 stsp goto done;
6184 2a47b1e5 2022-11-01 stsp if (show_diff) {
6185 2a47b1e5 2022-11-01 stsp if (fflush(cc_arg.diff_outfile) == EOF) {
6186 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fflush");
6187 2a47b1e5 2022-11-01 stsp goto done;
6191 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6192 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6193 39cd0ff6 2019-07-12 stsp goto done;
6196 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6197 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6199 5c1e53bc 2019-07-28 stsp goto done;
6202 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6203 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6204 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6206 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6207 f0b75401 2019-08-03 stsp ct_path++;
6208 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6209 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6210 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6212 b50cabdf 2019-07-12 stsp goto done;
6216 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6217 210c2321 2022-11-01 stsp head_commit_id, NULL, worktree, author, committer,
6218 210c2321 2022-11-01 stsp (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6219 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6221 39cd0ff6 2019-07-12 stsp goto done;
6223 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6224 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6225 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6226 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6227 39cd0ff6 2019-07-12 stsp err = sync_err;
6229 39cd0ff6 2019-07-12 stsp if (fileindex)
6230 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6231 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6232 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6233 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6234 39cd0ff6 2019-07-12 stsp err = unlockerr;
6235 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6236 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6238 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6240 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6241 2a47b1e5 2022-11-01 stsp if (diff_path && unlink(diff_path) == -1 && err == NULL)
6242 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("unlink", diff_path);
6243 2a47b1e5 2022-11-01 stsp free(diff_path);
6244 2a47b1e5 2022-11-01 stsp if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6245 2a47b1e5 2022-11-01 stsp err == NULL)
6246 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
6247 c4296144 2019-05-09 stsp return err;
6250 8656d6c4 2019-05-20 stsp const char *
6251 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6253 8656d6c4 2019-05-20 stsp return ct->path;
6256 8656d6c4 2019-05-20 stsp unsigned int
6257 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6259 8656d6c4 2019-05-20 stsp return ct->status;
6262 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6263 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6264 818c7501 2019-07-11 stsp struct got_repository *repo;
6267 818c7501 2019-07-11 stsp static const struct got_error *
6268 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6270 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6271 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6272 818c7501 2019-07-11 stsp unsigned char status;
6273 818c7501 2019-07-11 stsp struct stat sb;
6274 818c7501 2019-07-11 stsp char *ondisk_path;
6276 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6277 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6278 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6279 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6281 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6283 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6285 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6286 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6287 818c7501 2019-07-11 stsp free(ondisk_path);
6289 818c7501 2019-07-11 stsp return err;
6291 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6292 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6293 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6294 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6296 818c7501 2019-07-11 stsp return NULL;
6299 818c7501 2019-07-11 stsp const struct got_error *
6300 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6301 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6302 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6303 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6305 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6306 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6307 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6308 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6309 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6310 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6311 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6313 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6314 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6315 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6317 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6319 818c7501 2019-07-11 stsp return err;
6321 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6323 818c7501 2019-07-11 stsp goto done;
6325 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6326 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6327 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6330 818c7501 2019-07-11 stsp goto done;
6332 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6334 818c7501 2019-07-11 stsp goto done;
6336 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6338 818c7501 2019-07-11 stsp goto done;
6340 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6342 818c7501 2019-07-11 stsp goto done;
6344 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6347 e51d7b55 2020-01-04 stsp goto done;
6349 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6351 818c7501 2019-07-11 stsp goto done;
6352 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6353 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6354 e51d7b55 2020-01-04 stsp goto done;
6357 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6358 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6360 818c7501 2019-07-11 stsp goto done;
6361 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6363 818c7501 2019-07-11 stsp goto done;
6365 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6367 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6369 818c7501 2019-07-11 stsp goto done;
6371 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6373 818c7501 2019-07-11 stsp goto done;
6375 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6376 818c7501 2019-07-11 stsp worktree->base_commit_id);
6378 818c7501 2019-07-11 stsp goto done;
6379 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6381 818c7501 2019-07-11 stsp goto done;
6383 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6385 818c7501 2019-07-11 stsp goto done;
6387 818c7501 2019-07-11 stsp free(fileindex_path);
6388 818c7501 2019-07-11 stsp free(tmp_branch_name);
6389 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6390 818c7501 2019-07-11 stsp free(branch_ref_name);
6391 818c7501 2019-07-11 stsp if (branch_ref)
6392 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6393 818c7501 2019-07-11 stsp if (wt_branch)
6394 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6395 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6396 818c7501 2019-07-11 stsp if (err) {
6397 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6398 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6399 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6401 818c7501 2019-07-11 stsp if (*tmp_branch) {
6402 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6403 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6405 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6406 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6407 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6409 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6411 818c7501 2019-07-11 stsp return err;
6414 818c7501 2019-07-11 stsp const struct got_error *
6415 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6416 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6417 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6418 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6420 818c7501 2019-07-11 stsp const struct got_error *err;
6421 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6422 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6423 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6424 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6425 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6427 818c7501 2019-07-11 stsp *commit_id = NULL;
6428 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6429 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6430 3e3a69f1 2019-07-25 stsp *branch = NULL;
6431 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6433 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6435 3e3a69f1 2019-07-25 stsp return err;
6437 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6439 f032f1f7 2019-08-04 stsp goto done;
6441 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6442 f032f1f7 2019-08-04 stsp &have_staged_files);
6443 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6444 f032f1f7 2019-08-04 stsp goto done;
6445 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6446 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6447 3e3a69f1 2019-07-25 stsp goto done;
6450 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6452 f032f1f7 2019-08-04 stsp goto done;
6454 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6456 818c7501 2019-07-11 stsp goto done;
6458 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6460 818c7501 2019-07-11 stsp goto done;
6462 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6464 818c7501 2019-07-11 stsp goto done;
6466 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6468 818c7501 2019-07-11 stsp goto done;
6470 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6471 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6473 818c7501 2019-07-11 stsp goto done;
6475 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6477 818c7501 2019-07-11 stsp goto done;
6479 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6481 818c7501 2019-07-11 stsp goto done;
6483 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6484 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6486 818c7501 2019-07-11 stsp goto done;
6488 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6490 818c7501 2019-07-11 stsp goto done;
6492 818c7501 2019-07-11 stsp free(commit_ref_name);
6493 818c7501 2019-07-11 stsp free(branch_ref_name);
6494 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6495 818c7501 2019-07-11 stsp if (commit_ref)
6496 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6497 818c7501 2019-07-11 stsp if (branch_ref)
6498 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6499 818c7501 2019-07-11 stsp if (err) {
6500 818c7501 2019-07-11 stsp free(*commit_id);
6501 818c7501 2019-07-11 stsp *commit_id = NULL;
6502 818c7501 2019-07-11 stsp if (*tmp_branch) {
6503 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6504 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6506 818c7501 2019-07-11 stsp if (*new_base_branch) {
6507 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6508 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6510 818c7501 2019-07-11 stsp if (*branch) {
6511 818c7501 2019-07-11 stsp got_ref_close(*branch);
6512 818c7501 2019-07-11 stsp *branch = NULL;
6514 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6515 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6516 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6518 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6520 818c7501 2019-07-11 stsp return err;
6523 818c7501 2019-07-11 stsp const struct got_error *
6524 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6526 818c7501 2019-07-11 stsp const struct got_error *err;
6527 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6529 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6531 818c7501 2019-07-11 stsp return err;
6533 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6534 818c7501 2019-07-11 stsp free(tmp_branch_name);
6535 818c7501 2019-07-11 stsp return NULL;
6538 818c7501 2019-07-11 stsp static const struct got_error *
6539 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6540 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
6542 0ebf8283 2019-07-24 stsp *logmsg = arg;
6543 818c7501 2019-07-11 stsp return NULL;
6546 818c7501 2019-07-11 stsp static const struct got_error *
6547 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6548 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6549 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6550 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6552 818c7501 2019-07-11 stsp return NULL;
6555 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6556 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6557 01757395 2019-07-12 stsp void *progress_arg;
6558 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6561 01757395 2019-07-12 stsp static const struct got_error *
6562 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6564 01757395 2019-07-12 stsp const struct got_error *err;
6565 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6567 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6569 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6571 01757395 2019-07-12 stsp return err;
6573 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6574 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6575 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6576 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6577 01757395 2019-07-12 stsp return NULL;
6579 01757395 2019-07-12 stsp p = strdup(path);
6580 01757395 2019-07-12 stsp if (p == NULL)
6581 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6583 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6584 01757395 2019-07-12 stsp if (err || new == NULL)
6586 01757395 2019-07-12 stsp return err;
6589 0ebf8283 2019-07-24 stsp static const struct got_error *
6590 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6591 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6593 818c7501 2019-07-11 stsp const struct got_error *err;
6594 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6596 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6597 818c7501 2019-07-11 stsp if (err) {
6598 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6599 818c7501 2019-07-11 stsp goto done;
6600 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6602 818c7501 2019-07-11 stsp goto done;
6603 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6605 818c7501 2019-07-11 stsp goto done;
6606 de05890f 2020-03-05 stsp } else if (is_rebase) {
6607 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6610 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6612 818c7501 2019-07-11 stsp goto done;
6613 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6614 818c7501 2019-07-11 stsp free(stored_id);
6615 818c7501 2019-07-11 stsp if (cmp != 0) {
6616 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6617 818c7501 2019-07-11 stsp goto done;
6621 0ebf8283 2019-07-24 stsp if (commit_ref)
6622 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6623 0ebf8283 2019-07-24 stsp return err;
6626 0ebf8283 2019-07-24 stsp static const struct got_error *
6627 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6628 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6629 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6630 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6631 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6632 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6634 0ebf8283 2019-07-24 stsp const struct got_error *err;
6635 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6636 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6637 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6639 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6641 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6643 0ebf8283 2019-07-24 stsp return err;
6645 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6646 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6647 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6648 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6649 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6650 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6651 818c7501 2019-07-11 stsp if (commit_ref)
6652 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6653 818c7501 2019-07-11 stsp return err;
6656 818c7501 2019-07-11 stsp const struct got_error *
6657 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6658 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6659 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6660 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6661 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6662 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6664 0ebf8283 2019-07-24 stsp const struct got_error *err;
6665 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6667 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6669 0ebf8283 2019-07-24 stsp return err;
6671 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6673 0ebf8283 2019-07-24 stsp goto done;
6675 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6676 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6677 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6679 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6680 0ebf8283 2019-07-24 stsp return err;
6683 0ebf8283 2019-07-24 stsp const struct got_error *
6684 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6685 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6686 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6687 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6688 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6689 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6691 0ebf8283 2019-07-24 stsp const struct got_error *err;
6692 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6694 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6696 0ebf8283 2019-07-24 stsp return err;
6698 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6700 0ebf8283 2019-07-24 stsp goto done;
6702 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6703 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6704 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6706 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6707 0ebf8283 2019-07-24 stsp return err;
6710 0ebf8283 2019-07-24 stsp static const struct got_error *
6711 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6712 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6713 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6714 598eac43 2022-07-22 stsp struct got_reference *tmp_branch, const char *committer,
6715 598eac43 2022-07-22 stsp struct got_commit_object *orig_commit, const char *new_logmsg,
6716 598eac43 2022-07-22 stsp struct got_repository *repo)
6718 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6719 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6720 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6721 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6722 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6723 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6724 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6726 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6727 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6728 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6730 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6732 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6734 3e3a69f1 2019-07-25 stsp return err;
6736 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6737 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6738 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6739 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6741 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6742 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6744 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6745 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6746 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6747 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6748 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6749 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6751 01757395 2019-07-12 stsp if (merged_paths) {
6752 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6753 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6754 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6755 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6758 01757395 2019-07-12 stsp goto done;
6761 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6762 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6764 01757395 2019-07-12 stsp goto done;
6767 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6768 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6769 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6771 a0e95631 2019-07-12 stsp goto done;
6772 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6773 a0e95631 2019-07-12 stsp goto done;
6776 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6778 edd02c5e 2019-07-11 stsp goto done;
6780 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6782 818c7501 2019-07-11 stsp goto done;
6784 5943eee2 2019-08-13 stsp if (new_logmsg) {
6785 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6786 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6787 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6788 5943eee2 2019-08-13 stsp goto done;
6791 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6793 5943eee2 2019-08-13 stsp goto done;
6796 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6797 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6798 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
6799 50e7a649 2022-07-22 stsp committer ? committer :
6800 2a47b1e5 2022-11-01 stsp got_object_commit_get_committer(orig_commit), NULL,
6801 50e7a649 2022-07-22 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6803 a0e95631 2019-07-12 stsp goto done;
6805 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6807 a0e95631 2019-07-12 stsp goto done;
6809 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6811 a0e95631 2019-07-12 stsp goto done;
6813 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6814 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6815 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6816 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6817 a0e95631 2019-07-12 stsp err = sync_err;
6819 a0e95631 2019-07-12 stsp free(fileindex_path);
6820 a0e95631 2019-07-12 stsp free(head_commit_id);
6821 a0e95631 2019-07-12 stsp if (head_ref)
6822 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6823 818c7501 2019-07-11 stsp if (err) {
6824 818c7501 2019-07-11 stsp free(*new_commit_id);
6825 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6827 818c7501 2019-07-11 stsp return err;
6830 818c7501 2019-07-11 stsp const struct got_error *
6831 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6832 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6833 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6834 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
6835 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6837 0ebf8283 2019-07-24 stsp const struct got_error *err;
6838 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6839 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6840 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6842 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6844 0ebf8283 2019-07-24 stsp return err;
6846 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6848 0ebf8283 2019-07-24 stsp goto done;
6849 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6851 0ebf8283 2019-07-24 stsp goto done;
6852 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6853 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6854 0ebf8283 2019-07-24 stsp goto done;
6857 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6858 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
6859 598eac43 2022-07-22 stsp NULL, repo);
6861 0ebf8283 2019-07-24 stsp if (commit_ref)
6862 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6863 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6864 0ebf8283 2019-07-24 stsp free(commit_id);
6865 0ebf8283 2019-07-24 stsp return err;
6868 0ebf8283 2019-07-24 stsp const struct got_error *
6869 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6870 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6871 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6872 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
6873 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6874 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6876 0ebf8283 2019-07-24 stsp const struct got_error *err;
6877 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6878 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6880 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6882 0ebf8283 2019-07-24 stsp return err;
6884 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6886 0ebf8283 2019-07-24 stsp goto done;
6888 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6889 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
6890 598eac43 2022-07-22 stsp new_logmsg, repo);
6892 0ebf8283 2019-07-24 stsp if (commit_ref)
6893 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6894 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6895 0ebf8283 2019-07-24 stsp return err;
6898 0ebf8283 2019-07-24 stsp const struct got_error *
6899 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6900 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6902 3e3a69f1 2019-07-25 stsp if (fileindex)
6903 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6904 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6907 69844fba 2019-07-11 stsp static const struct got_error *
6908 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6910 69844fba 2019-07-11 stsp const struct got_error *err;
6911 69844fba 2019-07-11 stsp struct got_reference *ref;
6913 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6914 69844fba 2019-07-11 stsp if (err) {
6915 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6916 69844fba 2019-07-11 stsp return NULL;
6917 69844fba 2019-07-11 stsp return err;
6920 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6921 69844fba 2019-07-11 stsp got_ref_close(ref);
6922 69844fba 2019-07-11 stsp return err;
6925 69844fba 2019-07-11 stsp static const struct got_error *
6926 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6928 69844fba 2019-07-11 stsp const struct got_error *err;
6929 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6930 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6932 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6934 69844fba 2019-07-11 stsp goto done;
6935 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6937 69844fba 2019-07-11 stsp goto done;
6939 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6941 69844fba 2019-07-11 stsp goto done;
6942 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6944 69844fba 2019-07-11 stsp goto done;
6946 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6948 69844fba 2019-07-11 stsp goto done;
6949 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6951 69844fba 2019-07-11 stsp goto done;
6953 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6955 69844fba 2019-07-11 stsp goto done;
6956 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6958 69844fba 2019-07-11 stsp goto done;
6961 69844fba 2019-07-11 stsp free(tmp_branch_name);
6962 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6963 69844fba 2019-07-11 stsp free(branch_ref_name);
6964 69844fba 2019-07-11 stsp free(commit_ref_name);
6965 e600f124 2021-03-21 stsp return err;
6968 336075a4 2022-06-25 op static const struct got_error *
6969 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6970 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6972 e600f124 2021-03-21 stsp const struct got_error *err;
6973 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6974 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6975 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6976 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6977 e600f124 2021-03-21 stsp char *refname = NULL;
6979 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6980 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6981 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6982 e600f124 2021-03-21 stsp branch_name += 11;
6984 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6986 e600f124 2021-03-21 stsp return err;
6988 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6989 e600f124 2021-03-21 stsp new_id_str) == -1) {
6990 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6991 e600f124 2021-03-21 stsp goto done;
6994 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6996 e600f124 2021-03-21 stsp goto done;
6998 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7000 e600f124 2021-03-21 stsp goto done;
7002 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7004 e600f124 2021-03-21 stsp free(new_id_str);
7005 e600f124 2021-03-21 stsp free(refname);
7006 e600f124 2021-03-21 stsp free(old_commit_id);
7008 e600f124 2021-03-21 stsp got_ref_close(ref);
7009 69844fba 2019-07-11 stsp return err;
7012 818c7501 2019-07-11 stsp const struct got_error *
7013 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7014 ef85a376 2023-02-01 mark struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7015 ef85a376 2023-02-01 mark struct got_reference *rebased_branch, struct got_repository *repo,
7016 ef85a376 2023-02-01 mark int create_backup)
7018 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7019 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7020 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7022 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7024 818c7501 2019-07-11 stsp return err;
7026 e600f124 2021-03-21 stsp if (create_backup) {
7027 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7028 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7030 e600f124 2021-03-21 stsp goto done;
7033 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7035 818c7501 2019-07-11 stsp goto done;
7037 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7039 818c7501 2019-07-11 stsp goto done;
7041 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7043 818c7501 2019-07-11 stsp goto done;
7045 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7047 a615e0e7 2020-12-16 stsp goto done;
7049 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7051 a615e0e7 2020-12-16 stsp goto done;
7052 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7053 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7054 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7055 a615e0e7 2020-12-16 stsp err = sync_err;
7057 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7058 a615e0e7 2020-12-16 stsp free(fileindex_path);
7059 818c7501 2019-07-11 stsp free(new_head_commit_id);
7060 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7061 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7062 818c7501 2019-07-11 stsp err = unlockerr;
7063 818c7501 2019-07-11 stsp return err;
7066 818c7501 2019-07-11 stsp const struct got_error *
7067 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7068 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7069 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7070 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7072 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7073 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7074 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7075 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7076 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7077 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7078 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7080 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7082 818c7501 2019-07-11 stsp return err;
7084 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7085 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7087 a44927cc 2022-04-07 stsp goto done;
7089 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7090 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7092 818c7501 2019-07-11 stsp goto done;
7094 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7096 818c7501 2019-07-11 stsp goto done;
7099 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7100 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7101 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7103 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7105 818c7501 2019-07-11 stsp goto done;
7107 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7109 818c7501 2019-07-11 stsp goto done;
7111 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7112 a44927cc 2022-04-07 stsp worktree->path_prefix);
7114 ca355955 2019-07-12 stsp goto done;
7116 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7118 ca355955 2019-07-12 stsp goto done;
7120 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7122 818c7501 2019-07-11 stsp goto done;
7124 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7125 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7126 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7127 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7128 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7129 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7130 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7131 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7132 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7133 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7135 1f1abb7e 2019-08-08 stsp goto sync;
7137 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7138 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7140 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7141 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7142 ca355955 2019-07-12 stsp err = sync_err;
7144 818c7501 2019-07-11 stsp got_ref_close(resolved);
7145 a3a2faf2 2019-07-12 stsp free(tree_id);
7146 818c7501 2019-07-11 stsp free(commit_id);
7147 a44927cc 2022-04-07 stsp if (commit)
7148 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7149 0ebf8283 2019-07-24 stsp if (fileindex)
7150 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7151 0ebf8283 2019-07-24 stsp free(fileindex_path);
7153 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7154 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7155 0ebf8283 2019-07-24 stsp err = unlockerr;
7156 0ebf8283 2019-07-24 stsp return err;
7159 0ebf8283 2019-07-24 stsp const struct got_error *
7160 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7161 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7162 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7163 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7165 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7166 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7167 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7168 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7169 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7170 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7171 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7172 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7174 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7175 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7176 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7177 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7179 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7181 0ebf8283 2019-07-24 stsp return err;
7183 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7185 0ebf8283 2019-07-24 stsp goto done;
7187 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7188 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7189 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7192 0ebf8283 2019-07-24 stsp goto done;
7194 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7196 0ebf8283 2019-07-24 stsp goto done;
7198 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7200 0ebf8283 2019-07-24 stsp goto done;
7202 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7203 0ebf8283 2019-07-24 stsp worktree);
7205 0ebf8283 2019-07-24 stsp goto done;
7207 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7210 0ebf8283 2019-07-24 stsp goto done;
7212 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7214 0ebf8283 2019-07-24 stsp goto done;
7216 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7218 0ebf8283 2019-07-24 stsp goto done;
7220 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7221 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7223 0ebf8283 2019-07-24 stsp goto done;
7224 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7226 0ebf8283 2019-07-24 stsp goto done;
7227 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7228 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7229 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7230 0ebf8283 2019-07-24 stsp goto done;
7233 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7234 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7236 0ebf8283 2019-07-24 stsp goto done;
7237 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7239 0ebf8283 2019-07-24 stsp goto done;
7241 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7243 0ebf8283 2019-07-24 stsp goto done;
7245 0ebf8283 2019-07-24 stsp free(fileindex_path);
7246 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7247 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7248 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7249 0ebf8283 2019-07-24 stsp if (wt_branch)
7250 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7251 0ebf8283 2019-07-24 stsp if (err) {
7252 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7253 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7254 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7256 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7257 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7258 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7260 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7261 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7262 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7263 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7265 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7267 0ebf8283 2019-07-24 stsp return err;
7270 0ebf8283 2019-07-24 stsp const struct got_error *
7271 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7272 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7274 3e3a69f1 2019-07-25 stsp if (fileindex)
7275 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7276 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7279 0ebf8283 2019-07-24 stsp const struct got_error *
7280 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7281 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7283 0ebf8283 2019-07-24 stsp const struct got_error *err;
7284 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7286 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7288 0ebf8283 2019-07-24 stsp return err;
7290 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7291 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7292 0ebf8283 2019-07-24 stsp return NULL;
7295 0ebf8283 2019-07-24 stsp const struct got_error *
7296 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7297 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7298 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7299 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7301 0ebf8283 2019-07-24 stsp const struct got_error *err;
7302 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7303 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7304 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7305 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7306 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7307 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7309 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7310 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7311 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7312 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7314 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7316 3e3a69f1 2019-07-25 stsp return err;
7318 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7320 f032f1f7 2019-08-04 stsp goto done;
7322 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7323 f032f1f7 2019-08-04 stsp &have_staged_files);
7324 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7325 f032f1f7 2019-08-04 stsp goto done;
7326 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7327 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7328 3e3a69f1 2019-07-25 stsp goto done;
7331 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7333 f032f1f7 2019-08-04 stsp goto done;
7335 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7337 0ebf8283 2019-07-24 stsp goto done;
7339 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7341 0ebf8283 2019-07-24 stsp goto done;
7343 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7344 0ebf8283 2019-07-24 stsp worktree);
7346 0ebf8283 2019-07-24 stsp goto done;
7348 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7350 0ebf8283 2019-07-24 stsp goto done;
7352 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7354 0ebf8283 2019-07-24 stsp goto done;
7355 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7357 0ebf8283 2019-07-24 stsp goto done;
7359 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7361 0ebf8283 2019-07-24 stsp goto done;
7362 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7364 0ebf8283 2019-07-24 stsp goto done;
7366 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7368 0ebf8283 2019-07-24 stsp goto done;
7370 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7371 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7372 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7373 0ebf8283 2019-07-24 stsp if (commit_ref)
7374 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7375 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7376 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7377 0ebf8283 2019-07-24 stsp if (err) {
7378 0ebf8283 2019-07-24 stsp free(*commit_id);
7379 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7380 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7381 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7382 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7383 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7384 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7386 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7387 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7388 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7390 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7392 0ebf8283 2019-07-24 stsp return err;
7395 0ebf8283 2019-07-24 stsp static const struct got_error *
7396 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7398 0ebf8283 2019-07-24 stsp const struct got_error *err;
7399 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7400 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7402 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7404 0ebf8283 2019-07-24 stsp goto done;
7405 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7407 0ebf8283 2019-07-24 stsp goto done;
7409 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7410 0ebf8283 2019-07-24 stsp worktree);
7412 0ebf8283 2019-07-24 stsp goto done;
7413 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7415 0ebf8283 2019-07-24 stsp goto done;
7417 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7419 0ebf8283 2019-07-24 stsp goto done;
7420 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7422 0ebf8283 2019-07-24 stsp goto done;
7424 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7426 0ebf8283 2019-07-24 stsp goto done;
7427 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7429 0ebf8283 2019-07-24 stsp goto done;
7431 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7432 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7433 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7434 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7435 0ebf8283 2019-07-24 stsp return err;
7438 0ebf8283 2019-07-24 stsp const struct got_error *
7439 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7440 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7441 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7442 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7444 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7445 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7446 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7447 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7448 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7449 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7451 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7453 0ebf8283 2019-07-24 stsp return err;
7455 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7456 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7458 a44927cc 2022-04-07 stsp goto done;
7460 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7461 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7463 0ebf8283 2019-07-24 stsp goto done;
7465 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7467 0ebf8283 2019-07-24 stsp goto done;
7469 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7471 0ebf8283 2019-07-24 stsp goto done;
7473 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7474 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7476 0ebf8283 2019-07-24 stsp goto done;
7478 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7480 0ebf8283 2019-07-24 stsp goto done;
7482 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7484 0ebf8283 2019-07-24 stsp goto done;
7486 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7487 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7488 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7489 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7490 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7491 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7492 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7493 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7494 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7495 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7497 1f1abb7e 2019-08-08 stsp goto sync;
7499 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7500 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7502 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7503 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7504 0ebf8283 2019-07-24 stsp err = sync_err;
7506 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7507 0ebf8283 2019-07-24 stsp free(tree_id);
7508 818c7501 2019-07-11 stsp free(fileindex_path);
7510 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7511 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7512 818c7501 2019-07-11 stsp err = unlockerr;
7513 818c7501 2019-07-11 stsp return err;
7516 0ebf8283 2019-07-24 stsp const struct got_error *
7517 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7518 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7519 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7521 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7522 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7523 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7524 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7526 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7528 0ebf8283 2019-07-24 stsp return err;
7530 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7531 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7533 e600f124 2021-03-21 stsp goto done;
7535 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7536 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7538 0ebf8283 2019-07-24 stsp goto done;
7540 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7542 0ebf8283 2019-07-24 stsp goto done;
7544 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7546 0ebf8283 2019-07-24 stsp goto done;
7548 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7550 0ebf8283 2019-07-24 stsp goto done;
7552 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7554 a615e0e7 2020-12-16 stsp goto done;
7556 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7558 a615e0e7 2020-12-16 stsp goto done;
7559 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7560 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7561 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7562 a615e0e7 2020-12-16 stsp err = sync_err;
7564 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7565 a615e0e7 2020-12-16 stsp free(fileindex_path);
7566 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7567 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7568 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7569 0ebf8283 2019-07-24 stsp err = unlockerr;
7570 0ebf8283 2019-07-24 stsp return err;
7573 0ebf8283 2019-07-24 stsp const struct got_error *
7574 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7575 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7577 0ebf8283 2019-07-24 stsp const struct got_error *err;
7578 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7580 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7582 0ebf8283 2019-07-24 stsp return err;
7584 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7586 0ebf8283 2019-07-24 stsp goto done;
7588 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7590 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7591 2822a352 2019-10-15 stsp return err;
7594 2822a352 2019-10-15 stsp const struct got_error *
7595 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7596 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7597 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7598 2822a352 2019-10-15 stsp struct got_repository *repo)
7600 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7601 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7602 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7604 2822a352 2019-10-15 stsp *fileindex = NULL;
7605 2822a352 2019-10-15 stsp *branch_ref = NULL;
7606 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7608 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7610 2822a352 2019-10-15 stsp return err;
7612 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7613 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7614 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7615 2822a352 2019-10-15 stsp "update -b or different branch name required");
7616 2822a352 2019-10-15 stsp goto done;
7619 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7621 2822a352 2019-10-15 stsp goto done;
7623 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7624 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7625 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7626 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7629 2822a352 2019-10-15 stsp goto done;
7631 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7633 2822a352 2019-10-15 stsp goto done;
7635 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7636 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7638 2822a352 2019-10-15 stsp if (err) {
7639 2822a352 2019-10-15 stsp if (*branch_ref) {
7640 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7641 2822a352 2019-10-15 stsp *branch_ref = NULL;
7643 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7644 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7645 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7647 2822a352 2019-10-15 stsp if (*fileindex) {
7648 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7649 2822a352 2019-10-15 stsp *fileindex = NULL;
7651 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7653 0cb83759 2019-08-03 stsp return err;
7656 2822a352 2019-10-15 stsp const struct got_error *
7657 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7658 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7659 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7660 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7661 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7663 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7664 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7665 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7666 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7668 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7670 2822a352 2019-10-15 stsp goto done;
7672 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7674 2822a352 2019-10-15 stsp goto done;
7676 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7678 a44927cc 2022-04-07 stsp goto done;
7680 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7681 2822a352 2019-10-15 stsp worktree->path_prefix);
7683 2822a352 2019-10-15 stsp goto done;
7685 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7687 2822a352 2019-10-15 stsp goto done;
7689 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7690 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7692 2822a352 2019-10-15 stsp goto sync;
7694 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7696 2822a352 2019-10-15 stsp goto sync;
7698 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7700 6e210706 2021-01-22 stsp goto sync;
7702 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7704 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7705 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7706 2822a352 2019-10-15 stsp err = sync_err;
7709 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7710 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7711 2822a352 2019-10-15 stsp err = unlockerr;
7712 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7714 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7715 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7716 2822a352 2019-10-15 stsp err = unlockerr;
7717 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7719 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7720 2822a352 2019-10-15 stsp free(fileindex_path);
7721 2822a352 2019-10-15 stsp free(tree_id);
7722 a44927cc 2022-04-07 stsp if (commit)
7723 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7725 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7726 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7727 2822a352 2019-10-15 stsp err = unlockerr;
7728 2822a352 2019-10-15 stsp return err;
7731 2822a352 2019-10-15 stsp const struct got_error *
7732 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7733 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7734 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7736 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7738 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7740 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7742 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7743 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7744 8b692cd0 2019-10-21 stsp err = unlockerr;
7745 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7747 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7748 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7749 8b692cd0 2019-10-21 stsp err = unlockerr;
7750 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7752 f259c4c1 2021-09-24 stsp return err;
7755 f259c4c1 2021-09-24 stsp const struct got_error *
7756 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
7757 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
7759 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
7760 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7762 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7764 f259c4c1 2021-09-24 stsp goto done;
7766 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7768 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
7769 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7770 f259c4c1 2021-09-24 stsp err = sync_err;
7772 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7773 f259c4c1 2021-09-24 stsp free(fileindex_path);
7774 8b692cd0 2019-10-21 stsp return err;
7777 f259c4c1 2021-09-24 stsp static const struct got_error *
7778 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7780 f259c4c1 2021-09-24 stsp const struct got_error *err;
7781 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7783 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7785 f259c4c1 2021-09-24 stsp goto done;
7786 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
7788 f259c4c1 2021-09-24 stsp goto done;
7790 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7792 f259c4c1 2021-09-24 stsp goto done;
7793 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
7795 f259c4c1 2021-09-24 stsp goto done;
7798 f259c4c1 2021-09-24 stsp free(branch_refname);
7799 f259c4c1 2021-09-24 stsp free(commit_refname);
7800 f259c4c1 2021-09-24 stsp return err;
7803 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
7804 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
7805 f259c4c1 2021-09-24 stsp const char *branch_name;
7808 f259c4c1 2021-09-24 stsp static const struct got_error *
7809 2a47b1e5 2022-11-01 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7810 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
7812 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
7814 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7815 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
7816 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
7818 f259c4c1 2021-09-24 stsp return NULL;
7822 f259c4c1 2021-09-24 stsp const struct got_error *
7823 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
7824 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
7825 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
7826 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
7827 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7828 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7830 f259c4c1 2021-09-24 stsp const struct got_error *err;
7831 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7833 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7835 f259c4c1 2021-09-24 stsp goto done;
7837 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7838 f259c4c1 2021-09-24 stsp worktree);
7840 f259c4c1 2021-09-24 stsp goto done;
7842 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7843 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
7844 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
7846 f259c4c1 2021-09-24 stsp free(fileindex_path);
7847 f259c4c1 2021-09-24 stsp return err;
7850 f259c4c1 2021-09-24 stsp const struct got_error *
7851 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
7852 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7853 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
7854 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
7855 0ff8d236 2021-09-28 stsp struct got_repository *repo,
7856 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
7859 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
7860 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
7861 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
7862 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
7863 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
7864 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
7865 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7866 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
7867 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7869 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
7870 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
7872 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
7874 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7876 f259c4c1 2021-09-24 stsp goto done;
7878 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7880 f259c4c1 2021-09-24 stsp goto done;
7882 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7884 f259c4c1 2021-09-24 stsp goto done;
7886 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7887 f259c4c1 2021-09-24 stsp &have_staged_files);
7888 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7889 f259c4c1 2021-09-24 stsp goto done;
7890 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7891 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7892 f259c4c1 2021-09-24 stsp goto done;
7895 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
7896 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
7897 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
7898 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
7899 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
7900 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7901 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7902 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7904 f259c4c1 2021-09-24 stsp goto done;
7906 f259c4c1 2021-09-24 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7907 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7908 f259c4c1 2021-09-24 stsp "merge of %s cannot proceed", branch_name);
7909 f259c4c1 2021-09-24 stsp goto done;
7912 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7913 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7914 f259c4c1 2021-09-24 stsp const char *ct_path = ct->in_repo_path;
7916 f259c4c1 2021-09-24 stsp while (ct_path[0] == '/')
7917 f259c4c1 2021-09-24 stsp ct_path++;
7918 f259c4c1 2021-09-24 stsp err = check_out_of_date(ct_path, ct->status,
7919 f259c4c1 2021-09-24 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7920 f259c4c1 2021-09-24 stsp head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7922 f259c4c1 2021-09-24 stsp goto done;
7926 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
7927 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
7928 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
7929 2a47b1e5 2022-11-01 stsp head_commit_id, branch_tip, worktree, author, committer, NULL,
7930 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7932 f259c4c1 2021-09-24 stsp goto done;
7934 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
7935 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
7936 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7937 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7938 f259c4c1 2021-09-24 stsp err = sync_err;
7940 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7941 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7943 f259c4c1 2021-09-24 stsp free_commitable(ct);
7945 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
7946 f259c4c1 2021-09-24 stsp free(fileindex_path);
7947 f259c4c1 2021-09-24 stsp return err;
7950 f259c4c1 2021-09-24 stsp const struct got_error *
7951 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
7952 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
7954 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7955 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7957 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7959 f259c4c1 2021-09-24 stsp goto done;
7961 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7963 f259c4c1 2021-09-24 stsp goto done;
7964 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7965 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7966 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7967 f259c4c1 2021-09-24 stsp err = sync_err;
7969 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7970 f259c4c1 2021-09-24 stsp free(fileindex_path);
7971 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7972 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7973 f259c4c1 2021-09-24 stsp err = unlockerr;
7974 f259c4c1 2021-09-24 stsp return err;
7977 f259c4c1 2021-09-24 stsp const struct got_error *
7978 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7979 f259c4c1 2021-09-24 stsp struct got_repository *repo)
7981 f259c4c1 2021-09-24 stsp const struct got_error *err;
7982 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
7983 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
7985 f259c4c1 2021-09-24 stsp *in_progress = 0;
7987 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7989 f259c4c1 2021-09-24 stsp return err;
7990 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7991 793fcac3 2021-09-24 stsp free(branch_refname);
7992 f259c4c1 2021-09-24 stsp if (err) {
7993 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
7994 f259c4c1 2021-09-24 stsp return err;
7996 f259c4c1 2021-09-24 stsp *in_progress = 1;
7998 f259c4c1 2021-09-24 stsp return NULL;
8001 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
8002 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
8003 f259c4c1 2021-09-24 stsp struct got_reference *branch, struct got_repository *repo)
8005 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
8006 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8007 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
8008 f259c4c1 2021-09-24 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8009 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL;
8010 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8011 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
8013 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8015 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8017 f259c4c1 2021-09-24 stsp return err;
8019 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8021 f259c4c1 2021-09-24 stsp goto done;
8023 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
8024 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
8025 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
8026 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8029 f259c4c1 2021-09-24 stsp goto done;
8031 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8033 f259c4c1 2021-09-24 stsp return err;
8035 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8037 f259c4c1 2021-09-24 stsp return err;
8039 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8042 f259c4c1 2021-09-24 stsp goto done;
8044 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8046 f259c4c1 2021-09-24 stsp goto done;
8048 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8049 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8050 f259c4c1 2021-09-24 stsp goto done;
8053 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
8055 f259c4c1 2021-09-24 stsp goto done;
8057 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8059 f259c4c1 2021-09-24 stsp goto done;
8060 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
8062 f259c4c1 2021-09-24 stsp goto done;
8064 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8066 f259c4c1 2021-09-24 stsp goto done;
8067 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
8069 f259c4c1 2021-09-24 stsp goto done;
8072 f259c4c1 2021-09-24 stsp free(branch_refname);
8073 f259c4c1 2021-09-24 stsp free(commit_refname);
8074 f259c4c1 2021-09-24 stsp free(fileindex_path);
8075 f259c4c1 2021-09-24 stsp if (branch_ref)
8076 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8077 f259c4c1 2021-09-24 stsp if (commit_ref)
8078 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8079 f259c4c1 2021-09-24 stsp if (wt_branch)
8080 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
8081 f259c4c1 2021-09-24 stsp free(wt_branch_tip);
8082 f259c4c1 2021-09-24 stsp if (err) {
8083 f259c4c1 2021-09-24 stsp if (*fileindex) {
8084 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8085 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8087 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8089 f259c4c1 2021-09-24 stsp return err;
8092 f259c4c1 2021-09-24 stsp const struct got_error *
8093 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
8094 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8095 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8097 f259c4c1 2021-09-24 stsp const struct got_error *err;
8098 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
8099 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8100 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8101 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8103 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8104 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8105 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8107 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8109 f259c4c1 2021-09-24 stsp return err;
8111 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8113 f259c4c1 2021-09-24 stsp goto done;
8115 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8116 f259c4c1 2021-09-24 stsp &have_staged_files);
8117 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8118 f259c4c1 2021-09-24 stsp goto done;
8119 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8120 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
8121 f259c4c1 2021-09-24 stsp goto done;
8124 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8126 f259c4c1 2021-09-24 stsp goto done;
8128 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8130 f259c4c1 2021-09-24 stsp goto done;
8132 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8134 f259c4c1 2021-09-24 stsp goto done;
8136 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
8137 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8138 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
8139 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
8140 f259c4c1 2021-09-24 stsp goto done;
8142 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8143 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
8144 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
8145 f259c4c1 2021-09-24 stsp goto done;
8148 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8150 f259c4c1 2021-09-24 stsp goto done;
8152 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
8154 f259c4c1 2021-09-24 stsp goto done;
8156 f259c4c1 2021-09-24 stsp free(commit_refname);
8157 f259c4c1 2021-09-24 stsp free(branch_refname);
8158 f259c4c1 2021-09-24 stsp free(fileindex_path);
8159 f259c4c1 2021-09-24 stsp if (commit_ref)
8160 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8161 f259c4c1 2021-09-24 stsp if (branch_ref)
8162 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8163 f259c4c1 2021-09-24 stsp if (err) {
8164 f259c4c1 2021-09-24 stsp if (*branch_name) {
8165 f259c4c1 2021-09-24 stsp free(*branch_name);
8166 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8168 f259c4c1 2021-09-24 stsp free(*branch_tip);
8169 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8170 f259c4c1 2021-09-24 stsp if (*fileindex) {
8171 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8172 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8174 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8176 f259c4c1 2021-09-24 stsp return err;
8179 f259c4c1 2021-09-24 stsp const struct got_error *
8180 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
8181 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8182 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8184 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8185 f259c4c1 2021-09-24 stsp struct got_object_id *commit_id = NULL;
8186 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8187 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8188 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
8189 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
8191 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
8192 a44927cc 2022-04-07 stsp worktree->base_commit_id);
8194 a44927cc 2022-04-07 stsp goto done;
8196 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8197 a44927cc 2022-04-07 stsp worktree->path_prefix);
8199 f259c4c1 2021-09-24 stsp goto done;
8201 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8203 f259c4c1 2021-09-24 stsp goto done;
8205 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8207 f259c4c1 2021-09-24 stsp goto done;
8209 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
8210 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
8211 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
8212 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
8213 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
8214 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
8215 f259c4c1 2021-09-24 stsp rfa.repo = repo;
8216 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
8217 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8218 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8220 f259c4c1 2021-09-24 stsp goto sync;
8222 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8223 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8225 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8226 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8227 f259c4c1 2021-09-24 stsp err = sync_err;
8229 f259c4c1 2021-09-24 stsp free(tree_id);
8230 f259c4c1 2021-09-24 stsp free(commit_id);
8231 a44927cc 2022-04-07 stsp if (commit)
8232 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8233 f259c4c1 2021-09-24 stsp if (fileindex)
8234 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8235 f259c4c1 2021-09-24 stsp free(fileindex_path);
8237 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8238 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8239 f259c4c1 2021-09-24 stsp err = unlockerr;
8240 f259c4c1 2021-09-24 stsp return err;
8243 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8244 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8245 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8246 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8247 2db2652d 2019-08-07 stsp struct got_repository *repo;
8248 2db2652d 2019-08-07 stsp int have_changes;
8251 336075a4 2022-06-25 op static const struct got_error *
8252 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8253 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8254 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8255 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8257 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8258 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8259 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8260 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8261 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8262 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8264 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8265 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8266 8b13ce36 2019-08-08 stsp return NULL;
8267 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8268 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8270 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8271 735ef5ac 2019-08-03 stsp if (ie == NULL)
8272 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8274 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8275 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8276 735ef5ac 2019-08-03 stsp relpath) == -1)
8277 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8279 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8280 b4b2adf5 2023-02-09 op base_commit_idp = got_fileindex_entry_get_commit_id(
8281 b4b2adf5 2023-02-09 op &base_commit_id, ie);
8284 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8285 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8286 3aa5969e 2019-08-06 stsp goto done;
8287 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8288 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8289 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8290 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8291 735ef5ac 2019-08-03 stsp goto done;
8294 2db2652d 2019-08-07 stsp a->have_changes = 1;
8296 735ef5ac 2019-08-03 stsp p = in_repo_path;
8297 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8299 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8300 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8301 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8303 735ef5ac 2019-08-03 stsp free(in_repo_path);
8304 dc424a06 2019-08-07 stsp return err;
8307 2db2652d 2019-08-07 stsp struct stage_path_arg {
8308 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8309 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8310 2db2652d 2019-08-07 stsp struct got_repository *repo;
8311 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8312 2db2652d 2019-08-07 stsp void *status_arg;
8313 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8314 2db2652d 2019-08-07 stsp void *patch_arg;
8315 7b5dc508 2019-10-28 stsp int staged_something;
8316 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8319 2db2652d 2019-08-07 stsp static const struct got_error *
8320 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8321 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8322 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8323 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8325 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8326 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8327 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8328 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8329 0cb83759 2019-08-03 stsp uint32_t stage;
8330 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8331 0aeb8099 2020-07-23 stsp struct stat sb;
8333 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8334 8b13ce36 2019-08-08 stsp return NULL;
8336 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8337 d3e7c587 2019-08-03 stsp if (ie == NULL)
8338 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8340 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8341 2db2652d 2019-08-07 stsp relpath)== -1)
8342 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8344 0cb83759 2019-08-03 stsp switch (status) {
8345 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8346 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8347 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8348 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8349 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8352 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8353 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8354 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8355 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8356 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8359 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8362 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8363 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8364 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8365 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8366 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8370 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8371 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8374 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8375 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8376 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8377 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8379 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8380 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8381 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8382 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8383 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8384 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8385 35213c7c 2020-07-23 stsp ssize_t target_len;
8386 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8387 35213c7c 2020-07-23 stsp sizeof(target_path));
8388 35213c7c 2020-07-23 stsp if (target_len == -1) {
8389 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8390 35213c7c 2020-07-23 stsp ondisk_path);
8393 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8394 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8395 35213c7c 2020-07-23 stsp a->worktree->root_path);
8398 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8399 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8400 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8404 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8405 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8406 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8408 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8409 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8411 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8412 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8414 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8415 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8417 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8418 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8419 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8423 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
8424 9fdde394 2022-06-04 op * implicitly unstage the file.
8426 9fdde394 2022-06-04 op if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8427 9fdde394 2022-06-04 op sizeof(ie->blob_sha1)) == 0) {
8428 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
8429 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
8432 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8433 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8435 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8436 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8437 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8438 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8441 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8443 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8444 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8448 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8449 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8450 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8451 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8453 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8454 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8457 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8459 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8460 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8462 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8463 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8466 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8470 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8471 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8472 dc424a06 2019-08-07 stsp free(path_content);
8473 2db2652d 2019-08-07 stsp free(ondisk_path);
8474 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8475 0ebf8283 2019-07-24 stsp return err;
8478 0cb83759 2019-08-03 stsp const struct got_error *
8479 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8480 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8481 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8482 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8483 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8485 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8486 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8487 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8488 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8489 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8490 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8491 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8492 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8494 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8496 0cb83759 2019-08-03 stsp return err;
8498 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8499 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8501 735ef5ac 2019-08-03 stsp goto done;
8502 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8504 735ef5ac 2019-08-03 stsp goto done;
8505 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8507 0cb83759 2019-08-03 stsp goto done;
8509 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8510 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8511 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8512 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8513 2db2652d 2019-08-07 stsp oka.repo = repo;
8514 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8515 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8516 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8517 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
8519 735ef5ac 2019-08-03 stsp goto done;
8521 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8522 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8523 2db2652d 2019-08-07 stsp goto done;
8526 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8527 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8528 2db2652d 2019-08-07 stsp spa.repo = repo;
8529 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8530 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8531 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8532 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8533 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8534 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8535 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8536 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8537 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
8539 2db2652d 2019-08-07 stsp goto done;
8541 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8542 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8543 7b5dc508 2019-10-28 stsp goto done;
8546 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8547 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8548 0cb83759 2019-08-03 stsp err = sync_err;
8550 735ef5ac 2019-08-03 stsp if (head_ref)
8551 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8552 735ef5ac 2019-08-03 stsp free(head_commit_id);
8553 0cb83759 2019-08-03 stsp free(fileindex_path);
8554 0cb83759 2019-08-03 stsp if (fileindex)
8555 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8556 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8557 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8558 0cb83759 2019-08-03 stsp err = unlockerr;
8559 0cb83759 2019-08-03 stsp return err;
8562 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8563 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8564 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8565 ad493afc 2019-08-03 stsp struct got_repository *repo;
8566 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8567 ad493afc 2019-08-03 stsp void *progress_arg;
8568 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8569 2e1f37b0 2019-08-08 stsp void *patch_arg;
8572 2e1f37b0 2019-08-08 stsp static const struct got_error *
8573 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8574 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8575 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8576 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8577 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8579 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8580 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8581 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8582 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8583 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8584 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8585 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8586 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8588 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8589 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8591 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8593 2e1f37b0 2019-08-08 stsp return err;
8595 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
8596 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
8597 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8598 eb81bc23 2022-06-28 tracey goto done;
8600 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
8601 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
8602 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8603 eb81bc23 2022-06-28 tracey goto done;
8606 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8608 2e1f37b0 2019-08-08 stsp goto done;
8610 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8612 2e1f37b0 2019-08-08 stsp goto done;
8614 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8616 2e1f37b0 2019-08-08 stsp goto done;
8618 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8621 2e1f37b0 2019-08-08 stsp goto done;
8623 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8625 2e1f37b0 2019-08-08 stsp goto done;
8627 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8629 2e1f37b0 2019-08-08 stsp goto done;
8631 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8632 4b752015 2022-06-30 stsp path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8634 2e1f37b0 2019-08-08 stsp goto done;
8636 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8637 b90054ed 2022-11-01 stsp "got-unstaged-content", "");
8639 2e1f37b0 2019-08-08 stsp goto done;
8640 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8641 b90054ed 2022-11-01 stsp "got-new-staged-content", "");
8643 2e1f37b0 2019-08-08 stsp goto done;
8645 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8646 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8647 2e1f37b0 2019-08-08 stsp goto done;
8649 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8650 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8651 2e1f37b0 2019-08-08 stsp goto done;
8653 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8654 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8655 eb81bc23 2022-06-28 tracey struct diff_chunk_context cc = {};
8656 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8657 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8658 fe621944 2020-11-10 stsp nchanges++;
8660 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8661 2e1f37b0 2019-08-08 stsp int choice;
8662 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8663 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8664 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8665 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8667 2e1f37b0 2019-08-08 stsp goto done;
8668 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8669 2e1f37b0 2019-08-08 stsp have_content = 1;
8671 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8672 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8675 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8676 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8677 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8679 2e1f37b0 2019-08-08 stsp free(label1);
8680 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8681 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8683 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8684 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8685 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8686 2e1f37b0 2019-08-08 stsp if (staged_blob)
8687 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8688 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8689 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8690 fe621944 2020-11-10 stsp err = free_err;
8691 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8692 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8693 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8694 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8695 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8696 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8697 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8698 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8699 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8700 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8701 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8702 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8703 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8704 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8705 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8706 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8707 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8708 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8709 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8711 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8712 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8713 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8714 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8715 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8716 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8717 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8719 2e1f37b0 2019-08-08 stsp free(path1);
8720 2e1f37b0 2019-08-08 stsp free(path2);
8721 fda8017d 2020-07-23 stsp return err;
8724 fda8017d 2020-07-23 stsp static const struct got_error *
8725 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8726 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8727 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8728 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8729 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8730 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8731 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8733 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8734 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8735 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8736 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8737 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8738 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8739 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8740 fda8017d 2020-07-23 stsp struct stat sb;
8742 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8743 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8744 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8746 fda8017d 2020-07-23 stsp return err;
8748 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8749 fda8017d 2020-07-23 stsp return NULL;
8751 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8752 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8753 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8755 fda8017d 2020-07-23 stsp goto done;
8758 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
8759 fda8017d 2020-07-23 stsp if (f == NULL) {
8760 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8761 fda8017d 2020-07-23 stsp path_unstaged_content);
8762 fda8017d 2020-07-23 stsp goto done;
8764 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8765 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8766 fda8017d 2020-07-23 stsp goto done;
8768 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8769 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8770 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8772 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8773 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8774 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8775 fda8017d 2020-07-23 stsp goto done;
8777 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8778 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8779 fda8017d 2020-07-23 stsp goto done;
8781 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8782 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8783 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8784 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8785 fda8017d 2020-07-23 stsp progress_arg);
8787 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8789 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8791 67a66647 2021-05-31 stsp return err;
8793 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8794 67a66647 2021-05-31 stsp parent) == -1) {
8795 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8796 67a66647 2021-05-31 stsp base_path = NULL;
8797 67a66647 2021-05-31 stsp goto done;
8800 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_base_path, &f_base,
8801 b90054ed 2022-11-01 stsp base_path, "");
8803 67a66647 2021-05-31 stsp goto done;
8804 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8805 67a66647 2021-05-31 stsp blob_base);
8807 67a66647 2021-05-31 stsp goto done;
8810 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8811 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8813 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8814 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8815 eec2f5a9 2021-06-03 stsp ondisk_path);
8817 eec2f5a9 2021-06-03 stsp goto done;
8820 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
8821 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8822 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8823 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8824 eec2f5a9 2021-06-03 stsp goto done;
8826 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8827 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8828 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8829 eec2f5a9 2021-06-03 stsp close(fd);
8830 eec2f5a9 2021-06-03 stsp goto done;
8834 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8835 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8836 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8837 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8838 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8841 fda8017d 2020-07-23 stsp goto done;
8843 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8844 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8845 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8847 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8848 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8851 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8852 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8853 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8854 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8855 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8856 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8857 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8858 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8859 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8860 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8861 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8862 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8863 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8864 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8865 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8866 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8867 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8868 67a66647 2021-05-31 stsp free(blob_base_path);
8869 67a66647 2021-05-31 stsp free(parent);
8870 67a66647 2021-05-31 stsp free(base_path);
8871 2e1f37b0 2019-08-08 stsp return err;
8874 ad493afc 2019-08-03 stsp static const struct got_error *
8875 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8876 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8877 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8878 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8880 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8881 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8882 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8883 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8884 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8885 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8886 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8887 9bc94a15 2019-08-03 stsp struct stat sb;
8888 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8890 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8891 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8892 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8893 2e1f37b0 2019-08-08 stsp return NULL;
8895 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8896 ad493afc 2019-08-03 stsp if (ie == NULL)
8897 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8899 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8901 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8903 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8904 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8906 f69721c3 2019-10-21 stsp goto done;
8907 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8908 f69721c3 2019-10-21 stsp id_str) == -1) {
8909 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8910 eb81bc23 2022-06-28 tracey goto done;
8913 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
8914 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
8915 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8916 eb81bc23 2022-06-28 tracey goto done;
8918 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
8919 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
8920 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8921 f69721c3 2019-10-21 stsp goto done;
8924 ad493afc 2019-08-03 stsp switch (staged_status) {
8925 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8926 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8927 eb81bc23 2022-06-28 tracey blob_id, 8192, fd1);
8930 ad493afc 2019-08-03 stsp /* fall through */
8931 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8932 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8933 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8934 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8935 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8936 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8939 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8942 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8943 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8944 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8945 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8946 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8947 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8950 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8951 eb81bc23 2022-06-28 tracey staged_blob_id, 8192, fd2);
8954 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8955 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8956 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8957 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8958 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8959 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8960 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8961 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8962 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8964 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8965 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8966 36bf999c 2020-07-23 stsp char *staged_target;
8967 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8968 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8970 36bf999c 2020-07-23 stsp goto done;
8971 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8972 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8973 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8974 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8975 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8976 36bf999c 2020-07-23 stsp free(staged_target);
8978 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8979 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8980 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8981 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8982 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8983 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8984 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8988 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8991 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8992 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8993 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8994 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8997 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8998 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8999 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9000 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9001 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9004 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9006 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9007 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9011 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9012 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9013 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9014 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9017 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9021 ad493afc 2019-08-03 stsp free(ondisk_path);
9022 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9023 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9024 ad493afc 2019-08-03 stsp if (blob_base)
9025 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9026 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9027 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9028 ad493afc 2019-08-03 stsp if (blob_staged)
9029 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9030 f69721c3 2019-10-21 stsp free(id_str);
9031 f69721c3 2019-10-21 stsp free(label_orig);
9032 ad493afc 2019-08-03 stsp return err;
9035 ad493afc 2019-08-03 stsp const struct got_error *
9036 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9037 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9038 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9039 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9040 ad493afc 2019-08-03 stsp struct got_repository *repo)
9042 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9043 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9044 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9045 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9046 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9048 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9050 ad493afc 2019-08-03 stsp return err;
9052 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9054 ad493afc 2019-08-03 stsp goto done;
9056 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9057 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9058 ad493afc 2019-08-03 stsp upa.repo = repo;
9059 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9060 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9061 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9062 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9063 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9064 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9065 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
9067 ad493afc 2019-08-03 stsp goto done;
9070 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9071 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9072 ad493afc 2019-08-03 stsp err = sync_err;
9074 ad493afc 2019-08-03 stsp free(fileindex_path);
9075 ad493afc 2019-08-03 stsp if (fileindex)
9076 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9077 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9078 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9079 ad493afc 2019-08-03 stsp err = unlockerr;
9080 ad493afc 2019-08-03 stsp return err;
9083 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9084 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9085 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9086 b2118c49 2020-07-28 stsp void *info_arg;
9087 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9088 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9089 b2118c49 2020-07-28 stsp void *cancel_arg;
9092 b2118c49 2020-07-28 stsp static const struct got_error *
9093 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9095 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9096 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9097 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9098 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9099 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9100 b2118c49 2020-07-28 stsp int stage;
9102 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9103 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9105 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9106 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9107 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9110 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9111 b2118c49 2020-07-28 stsp return NULL;
9113 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
9114 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9115 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9116 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9117 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9118 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9119 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
9122 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
9123 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9125 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9126 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9129 b2118c49 2020-07-28 stsp const struct got_error *
9130 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9131 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9132 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9133 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9136 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9137 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9138 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9139 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9141 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9143 b2118c49 2020-07-28 stsp return err;
9145 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9147 b2118c49 2020-07-28 stsp goto done;
9149 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9150 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9151 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9152 b2118c49 2020-07-28 stsp arg.paths = paths;
9153 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9154 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9155 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9158 b2118c49 2020-07-28 stsp free(fileindex_path);
9159 b2118c49 2020-07-28 stsp if (fileindex)
9160 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9161 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9162 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9163 b2118c49 2020-07-28 stsp err = unlockerr;
9164 b2118c49 2020-07-28 stsp return err;
9167 78f5ac24 2022-03-19 op static const struct got_error *
9168 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
9169 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
9170 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
9172 78f5ac24 2022-03-19 op const struct got_error *err;
9173 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
9174 78f5ac24 2022-03-19 op struct stat sb;
9175 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
9177 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
9181 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9182 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
9183 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
9185 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9187 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
9188 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9193 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
9194 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
9195 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
9196 78f5ac24 2022-03-19 op if (errno != ENOENT) {
9197 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
9198 78f5ac24 2022-03-19 op ondisk_path);
9201 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
9206 78f5ac24 2022-03-19 op free(ondisk_path);
9210 78f5ac24 2022-03-19 op static const struct got_error *
9211 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
9212 78f5ac24 2022-03-19 op unsigned char staged_status)
9214 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9215 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9216 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9217 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9218 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
9219 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
9220 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9221 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9222 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9223 78f5ac24 2022-03-19 op return NULL;
9226 78f5ac24 2022-03-19 op static const struct got_error *
9227 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
9229 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
9230 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9231 78f5ac24 2022-03-19 op return NULL;
9234 78f5ac24 2022-03-19 op static const struct got_error *
9235 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
9236 78f5ac24 2022-03-19 op unsigned char staged_status)
9238 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9239 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9240 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9241 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9242 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
9243 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9244 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9245 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9246 78f5ac24 2022-03-19 op return NULL;
9249 78f5ac24 2022-03-19 op const struct got_error *
9250 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
9251 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
9253 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
9256 78f5ac24 2022-03-19 op const struct got_error *
9257 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
9258 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
9259 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
9261 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
9262 78f5ac24 2022-03-19 op int file_renamed = 0;
9263 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
9264 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
9266 78f5ac24 2022-03-19 op *oldpath = NULL;
9267 78f5ac24 2022-03-19 op *newpath = NULL;
9269 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
9270 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
9274 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
9275 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
9279 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
9280 78f5ac24 2022-03-19 op file_renamed = 1;
9282 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
9283 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9284 78f5ac24 2022-03-19 op else if (file_renamed) {
9285 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9286 78f5ac24 2022-03-19 op if (err == NULL)
9287 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9288 78f5ac24 2022-03-19 op } else if (old == NULL)
9289 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9291 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
9295 78f5ac24 2022-03-19 op free(*oldpath);
9296 78f5ac24 2022-03-19 op *oldpath = NULL;
9297 78f5ac24 2022-03-19 op free(*newpath);
9298 78f5ac24 2022-03-19 op *newpath = NULL;
9303 78f5ac24 2022-03-19 op const struct got_error *
9304 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9305 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9306 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
9308 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
9310 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
9311 f2dd7807 2022-05-17 op saa.worktree = worktree;
9312 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
9313 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
9314 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
9315 f2dd7807 2022-05-17 op saa.repo = repo;
9317 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9318 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
9321 f2dd7807 2022-05-17 op const struct got_error *
9322 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9323 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9324 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
9326 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
9328 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
9329 f2dd7807 2022-05-17 op sda.worktree = worktree;
9330 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
9331 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
9332 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
9333 f2dd7807 2022-05-17 op sda.repo = repo;
9334 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
9335 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
9336 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
9337 f2dd7807 2022-05-17 op sda.status_codes = NULL;
9339 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9340 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9343 f2dd7807 2022-05-17 op const struct got_error *
9344 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
9345 4bcdc895 2022-05-17 op const char *fileindex_path)
9347 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
9349 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
9350 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);