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>
19 d1f6d47b 2019-02-04 stsp #include <dirent.h>
20 12ce7a6c 2019-08-12 stsp #include <limits.h>
21 f5c49f82 2019-01-06 stsp #include <stddef.h>
22 86c3caaf 2018-03-09 stsp #include <string.h>
23 86c3caaf 2018-03-09 stsp #include <stdio.h>
24 86c3caaf 2018-03-09 stsp #include <stdlib.h>
25 303e14b5 2019-09-23 stsp #include <time.h>
26 86c3caaf 2018-03-09 stsp #include <fcntl.h>
27 86c3caaf 2018-03-09 stsp #include <errno.h>
28 86c3caaf 2018-03-09 stsp #include <unistd.h>
29 9d31a1d8 2018-03-11 stsp #include <zlib.h>
30 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
31 512f0d0e 2019-01-02 stsp #include <libgen.h>
33 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
35 86c3caaf 2018-03-09 stsp #include "got_error.h"
36 86c3caaf 2018-03-09 stsp #include "got_repository.h"
37 5261c201 2018-04-01 stsp #include "got_reference.h"
38 9d31a1d8 2018-03-11 stsp #include "got_object.h"
39 1dd54920 2019-05-11 stsp #include "got_path.h"
40 e6209546 2019-08-22 stsp #include "got_cancel.h"
41 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
42 511a516b 2018-05-19 stsp #include "got_opentemp.h"
43 234035bc 2019-06-01 stsp #include "got_diff.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
48 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
51 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
52 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
53 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
54 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
55 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
58 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
62 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
64 99724ed4 2018-03-10 stsp static const struct got_error *
65 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
67 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
70 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
71 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
73 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
78 507dc3bb 2018-12-29 stsp static const struct got_error *
79 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
81 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
82 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
83 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
84 507dc3bb 2018-12-29 stsp char *path = NULL;
86 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
87 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
88 507dc3bb 2018-12-29 stsp path = NULL;
92 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
96 507dc3bb 2018-12-29 stsp if (content) {
97 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
98 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
99 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
104 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
105 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
106 2a57020b 2019-02-20 stsp unlink(tmppath);
111 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
112 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
113 230a42bd 2019-05-11 jcs free(tmppath);
114 09fe317a 2018-03-11 stsp return err;
117 024e9686 2019-05-14 stsp static const struct got_error *
118 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
120 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
121 024e9686 2019-05-14 stsp char *refstr = NULL;
123 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
124 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
125 024e9686 2019-05-14 stsp if (refstr == NULL)
126 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
128 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
129 024e9686 2019-05-14 stsp if (refstr == NULL)
130 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
132 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
133 024e9686 2019-05-14 stsp free(refstr);
134 024e9686 2019-05-14 stsp return err;
137 86c3caaf 2018-03-09 stsp const struct got_error *
138 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
139 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
141 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
142 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
143 ec22038e 2019-03-10 stsp uuid_t uuid;
144 ec22038e 2019-03-10 stsp uint32_t uuid_status;
145 65596e15 2018-12-24 stsp int obj_type;
146 7ac97322 2018-03-11 stsp char *path_got = NULL;
147 1451e70d 2018-03-10 stsp char *formatstr = NULL;
148 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
149 65596e15 2018-12-24 stsp char *basestr = NULL;
150 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
152 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
153 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
157 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
159 65596e15 2018-12-24 stsp return err;
160 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
162 65596e15 2018-12-24 stsp return err;
163 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
164 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
166 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
167 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
168 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
171 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
172 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
173 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
177 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
178 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
179 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
182 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
183 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
187 056e7441 2018-03-11 stsp /* Create an empty lock file. */
188 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
193 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
198 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
202 65596e15 2018-12-24 stsp /* Record our base commit. */
203 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
206 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 1451e70d 2018-03-10 stsp /* Store path to repository. */
211 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
212 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
216 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
217 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
218 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
222 ec22038e 2019-03-10 stsp /* Generate UUID. */
223 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
224 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
225 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
228 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &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_to_string");
233 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
238 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
239 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
242 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
247 65596e15 2018-12-24 stsp free(commit_id);
248 7ac97322 2018-03-11 stsp free(path_got);
249 1451e70d 2018-03-10 stsp free(formatstr);
250 0bb8a95e 2018-03-12 stsp free(absprefix);
251 65596e15 2018-12-24 stsp free(basestr);
252 ec22038e 2019-03-10 stsp free(uuidstr);
253 86c3caaf 2018-03-09 stsp return err;
256 247140b2 2019-02-05 stsp const struct got_error *
257 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
258 e5dc7198 2018-12-29 stsp const char *path_prefix)
260 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
262 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
263 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
264 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
266 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
267 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
268 e5dc7198 2018-12-29 stsp free(absprefix);
269 e5dc7198 2018-12-29 stsp return NULL;
272 bc70eb79 2019-05-09 stsp const char *
273 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
275 36a38700 2019-05-10 stsp return worktree->head_ref_name;
278 024e9686 2019-05-14 stsp const struct got_error *
279 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
280 024e9686 2019-05-14 stsp struct got_reference *head_ref)
282 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
283 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
285 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
286 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
287 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
288 024e9686 2019-05-14 stsp path_got = NULL;
292 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
293 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
294 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
298 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
302 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
303 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
305 024e9686 2019-05-14 stsp free(path_got);
307 024e9686 2019-05-14 stsp free(head_ref_name);
308 024e9686 2019-05-14 stsp return err;
311 b72f483a 2019-02-05 stsp struct got_object_id *
312 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
314 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
317 507dc3bb 2018-12-29 stsp const struct got_error *
318 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
319 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
321 507dc3bb 2018-12-29 stsp const struct got_error *err;
322 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
323 507dc3bb 2018-12-29 stsp char *id_str = NULL;
324 507dc3bb 2018-12-29 stsp char *path_got = NULL;
326 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
327 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
328 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
329 507dc3bb 2018-12-29 stsp path_got = NULL;
333 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
335 507dc3bb 2018-12-29 stsp return err;
337 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
338 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
342 507dc3bb 2018-12-29 stsp /* Record our base commit. */
343 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
346 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
351 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
352 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
353 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
358 507dc3bb 2018-12-29 stsp got_object_close(obj);
359 507dc3bb 2018-12-29 stsp free(id_str);
360 507dc3bb 2018-12-29 stsp free(path_got);
361 507dc3bb 2018-12-29 stsp return err;
364 50b0790e 2020-09-11 stsp const struct got_gotconfig *
365 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
367 50b0790e 2020-09-11 stsp return worktree->gotconfig;
370 9d31a1d8 2018-03-11 stsp static const struct got_error *
371 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
373 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
374 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
375 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
376 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
377 86c3caaf 2018-03-09 stsp return NULL;
380 21908da4 2019-01-13 stsp static const struct got_error *
381 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
383 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
384 4a1ddfc2 2019-01-12 stsp char *abspath;
386 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
387 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
389 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
390 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
391 ddcd8544 2019-03-11 stsp struct stat sb;
392 ddcd8544 2019-03-11 stsp err = NULL;
393 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
394 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
395 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
396 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
397 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
400 4a1ddfc2 2019-01-12 stsp free(abspath);
401 68c76935 2019-02-19 stsp return err;
404 68c76935 2019-02-19 stsp static const struct got_error *
405 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
407 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
408 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
409 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
410 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
415 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
416 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
417 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
420 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
421 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
422 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
425 68c76935 2019-02-19 stsp if (flen1 == 0) {
426 68c76935 2019-02-19 stsp if (flen2 != 0)
429 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
430 68c76935 2019-02-19 stsp if (flen1 != 0)
433 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
434 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
444 68c76935 2019-02-19 stsp return err;
447 68c76935 2019-02-19 stsp static const struct got_error *
448 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
450 68c76935 2019-02-19 stsp struct stat sb;
451 68c76935 2019-02-19 stsp size_t size1, size2;
455 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
456 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
457 68c76935 2019-02-19 stsp size1 = sb.st_size;
459 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
460 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
461 68c76935 2019-02-19 stsp size2 = sb.st_size;
463 68c76935 2019-02-19 stsp if (size1 != size2) {
465 68c76935 2019-02-19 stsp return NULL;
468 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
469 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
470 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
471 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
473 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
476 24f136e0 2021-11-20 thomas static const struct got_error *
477 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
479 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
480 24f136e0 2021-11-20 thomas size_t flen;
481 24f136e0 2021-11-20 thomas ssize_t outlen;
483 24f136e0 2021-11-20 thomas *outsize = 0;
485 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
486 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
488 24f136e0 2021-11-20 thomas for (;;) {
489 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
490 24f136e0 2021-11-20 thomas if (flen == 0) {
491 24f136e0 2021-11-20 thomas if (ferror(f))
492 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
493 24f136e0 2021-11-20 thomas if (feof(f))
496 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
497 24f136e0 2021-11-20 thomas if (outlen == -1)
498 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
499 24f136e0 2021-11-20 thomas if (outlen != flen)
500 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
501 24f136e0 2021-11-20 thomas *outsize += outlen;
504 24f136e0 2021-11-20 thomas return NULL;
507 24f136e0 2021-11-20 thomas static const struct got_error *
508 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
509 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
510 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
511 24f136e0 2021-11-20 thomas const char *ondisk_path)
513 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
514 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
515 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
516 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
517 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
518 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
519 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
521 24f136e0 2021-11-20 thomas *overlapcnt = 0;
523 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
525 24f136e0 2021-11-20 thomas return err;
527 24f136e0 2021-11-20 thomas if (same_content)
528 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
530 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
532 24f136e0 2021-11-20 thomas return err;
533 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
534 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
536 24f136e0 2021-11-20 thomas return err;
537 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
539 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
540 24f136e0 2021-11-20 thomas *overlapcnt = 1;
541 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
542 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
543 24f136e0 2021-11-20 thomas goto done;
545 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
546 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
547 24f136e0 2021-11-20 thomas goto done;
549 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
550 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
551 24f136e0 2021-11-20 thomas goto done;
553 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
554 24f136e0 2021-11-20 thomas base_path_orig);
556 24f136e0 2021-11-20 thomas goto done;
557 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
558 24f136e0 2021-11-20 thomas base_path_deriv);
560 24f136e0 2021-11-20 thomas goto done;
561 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
562 24f136e0 2021-11-20 thomas base_path_deriv2);
564 24f136e0 2021-11-20 thomas goto done;
565 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
567 24f136e0 2021-11-20 thomas goto done;
568 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
570 24f136e0 2021-11-20 thomas goto done;
571 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
573 24f136e0 2021-11-20 thomas goto done;
574 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
575 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
576 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
577 24f136e0 2021-11-20 thomas goto done;
579 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
580 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
581 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
582 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
583 24f136e0 2021-11-20 thomas path_deriv) < 0) {
584 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
585 24f136e0 2021-11-20 thomas goto done;
587 24f136e0 2021-11-20 thomas if (size_orig > 0) {
588 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
589 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
590 24f136e0 2021-11-20 thomas label_orig ? " " : "",
591 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
592 24f136e0 2021-11-20 thomas path_orig) < 0) {
593 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
594 24f136e0 2021-11-20 thomas goto done;
597 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
598 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
599 24f136e0 2021-11-20 thomas path_deriv2,
600 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
601 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
602 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
603 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
604 24f136e0 2021-11-20 thomas goto done;
606 24f136e0 2021-11-20 thomas } else if (changed_deriv)
607 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
608 24f136e0 2021-11-20 thomas else if (changed_deriv2)
609 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
611 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
612 24f136e0 2021-11-20 thomas err == NULL)
613 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
614 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
615 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
616 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
617 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
618 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
619 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
620 24f136e0 2021-11-20 thomas free(path_orig);
621 24f136e0 2021-11-20 thomas free(path_deriv);
622 24f136e0 2021-11-20 thomas free(path_deriv2);
623 24f136e0 2021-11-20 thomas free(base_path_orig);
624 24f136e0 2021-11-20 thomas free(base_path_deriv);
625 24f136e0 2021-11-20 thomas free(base_path_deriv2);
626 24f136e0 2021-11-20 thomas return err;
630 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
631 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
632 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
633 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
634 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
636 6353ad76 2019-02-08 stsp static const struct got_error *
637 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
638 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
639 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
640 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
641 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
642 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
644 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
645 6353ad76 2019-02-08 stsp int merged_fd = -1;
646 db590691 2021-06-02 stsp FILE *f_merged = NULL;
647 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
648 234035bc 2019-06-01 stsp int overlapcnt = 0;
649 3524bbf9 2020-10-20 stsp char *parent = NULL;
651 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
653 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
655 3524bbf9 2020-10-20 stsp return err;
657 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
658 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
662 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
666 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
667 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
668 24f136e0 2021-11-20 thomas if (err) {
669 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
670 24f136e0 2021-11-20 thomas goto done;
671 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
672 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
673 24f136e0 2021-11-20 thomas ondisk_path);
675 24f136e0 2021-11-20 thomas goto done;
678 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
679 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
684 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
688 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
689 db590691 2021-06-02 stsp if (f_merged == NULL) {
690 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
693 db590691 2021-06-02 stsp merged_fd = -1;
695 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
696 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
697 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
703 db590691 2021-06-02 stsp if (fchmod(fileno(f_merged), st_mode) != 0) {
704 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
708 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
709 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
710 230a42bd 2019-05-11 jcs ondisk_path);
715 fdcb7daf 2019-12-15 stsp if (merged_path)
716 fdcb7daf 2019-12-15 stsp unlink(merged_path);
718 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
719 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
720 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
721 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
722 6353ad76 2019-02-08 stsp free(merged_path);
723 af54ae4a 2019-02-19 stsp free(base_path);
724 3524bbf9 2020-10-20 stsp free(parent);
725 af57b12a 2020-07-23 stsp return err;
728 af57b12a 2020-07-23 stsp static const struct got_error *
729 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
730 af57b12a 2020-07-23 stsp size_t target_len)
732 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
733 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
734 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
735 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
736 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
737 af57b12a 2020-07-23 stsp ondisk_path);
738 af57b12a 2020-07-23 stsp return NULL;
742 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
743 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
744 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
745 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
747 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
748 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
750 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
751 993e2a1b 2020-07-23 stsp * has deleted this symlink.
753 11cc08c1 2020-07-23 stsp static const struct got_error *
754 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
755 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
756 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
758 11cc08c1 2020-07-23 stsp const struct got_error *err;
759 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
760 11cc08c1 2020-07-23 stsp FILE *f = NULL;
762 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
764 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
766 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
767 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
768 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
772 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
774 3818e3c4 2020-11-01 naddy goto done;
776 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
777 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
781 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
782 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
783 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
784 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
785 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
786 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
787 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
788 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
789 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
790 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
794 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
795 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
798 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
799 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
803 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
804 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
805 11cc08c1 2020-07-23 stsp free(path);
806 11cc08c1 2020-07-23 stsp free(id_str);
807 11cc08c1 2020-07-23 stsp free(label_deriv);
808 11cc08c1 2020-07-23 stsp return err;
811 d219f183 2020-07-23 stsp /* forward declaration */
812 d219f183 2020-07-23 stsp static const struct got_error *
813 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
814 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
815 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
816 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
819 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
820 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
821 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
822 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
824 af57b12a 2020-07-23 stsp static const struct got_error *
825 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
826 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
827 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
828 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
829 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
831 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
832 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
833 af57b12a 2020-07-23 stsp struct stat sb;
834 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
835 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
836 11cc08c1 2020-07-23 stsp int have_local_change = 0;
837 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
839 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
840 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
842 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
843 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
844 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
845 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
846 af57b12a 2020-07-23 stsp ondisk_path);
849 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
851 526a746f 2020-07-23 stsp if (blob_orig) {
852 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
857 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
858 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
859 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
860 11cc08c1 2020-07-23 stsp have_local_change = 1;
862 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
863 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
864 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
865 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
866 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
868 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
869 11cc08c1 2020-07-23 stsp if (ancestor_target) {
870 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
871 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
873 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
874 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
875 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
876 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
879 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
880 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
881 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
882 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
885 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
888 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
889 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
890 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
891 af57b12a 2020-07-23 stsp strlen(deriv_target));
894 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
895 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
896 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
897 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
898 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
899 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
902 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
903 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
904 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
907 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
913 af57b12a 2020-07-23 stsp free(ancestor_target);
914 eec2f5a9 2021-06-03 stsp return err;
917 eec2f5a9 2021-06-03 stsp static const struct got_error *
918 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
920 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
921 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
922 eec2f5a9 2021-06-03 stsp ssize_t target_len;
926 eec2f5a9 2021-06-03 stsp *outfile = NULL;
928 eec2f5a9 2021-06-03 stsp f = got_opentemp();
929 eec2f5a9 2021-06-03 stsp if (f == NULL)
930 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
931 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
932 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
933 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
936 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
937 eec2f5a9 2021-06-03 stsp if (n != target_len) {
938 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
941 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
942 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
945 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
946 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
953 eec2f5a9 2021-06-03 stsp *outfile = f;
954 14c901f1 2019-08-08 stsp return err;
958 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
959 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
960 14c901f1 2019-08-08 stsp * acts as the second derived version.
962 14c901f1 2019-08-08 stsp static const struct got_error *
963 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
964 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
965 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
966 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
967 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
968 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
970 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
971 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
972 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
973 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
974 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
976 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
978 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
980 ed6b5030 2020-10-20 stsp return err;
982 67a66647 2021-05-31 stsp if (blob_orig) {
983 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
984 67a66647 2021-05-31 stsp parent) == -1) {
985 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
986 67a66647 2021-05-31 stsp base_path = NULL;
990 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
993 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
994 67a66647 2021-05-31 stsp blob_orig);
997 67a66647 2021-05-31 stsp free(base_path);
1000 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1001 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1002 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1004 db590691 2021-06-02 stsp f_orig = got_opentemp();
1005 db590691 2021-06-02 stsp if (f_orig == NULL) {
1006 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1007 db590691 2021-06-02 stsp goto done;
1011 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1012 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1013 14c901f1 2019-08-08 stsp base_path = NULL;
1014 14c901f1 2019-08-08 stsp goto done;
1017 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1019 14c901f1 2019-08-08 stsp goto done;
1020 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1021 14c901f1 2019-08-08 stsp blob_deriv);
1023 14c901f1 2019-08-08 stsp goto done;
1025 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1027 14c901f1 2019-08-08 stsp goto done;
1028 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1029 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1030 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1031 14c901f1 2019-08-08 stsp goto done;
1035 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1036 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1038 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1039 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1041 eec2f5a9 2021-06-03 stsp goto done;
1044 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1045 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1046 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1047 eec2f5a9 2021-06-03 stsp goto done;
1049 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1050 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1051 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1052 eec2f5a9 2021-06-03 stsp close(fd);
1053 eec2f5a9 2021-06-03 stsp goto done;
1057 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1058 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1059 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1061 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1062 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1063 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1064 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1065 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1066 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1067 14c901f1 2019-08-08 stsp free(base_path);
1068 67a66647 2021-05-31 stsp if (blob_orig_path) {
1069 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1070 67a66647 2021-05-31 stsp free(blob_orig_path);
1072 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1073 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1074 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1076 6353ad76 2019-02-08 stsp free(id_str);
1077 818c7501 2019-07-11 stsp free(label_deriv);
1078 ed6b5030 2020-10-20 stsp free(parent);
1079 4a1ddfc2 2019-01-12 stsp return err;
1082 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1083 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1084 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1085 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1087 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1088 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1090 65b05cec 2020-07-23 stsp *new_iep = NULL;
1092 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1094 054041d0 2020-06-24 stsp return err;
1096 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1097 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1099 054041d0 2020-06-24 stsp goto done;
1101 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1104 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1106 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1107 13d9040b 2019-03-27 stsp return err;
1110 1ebedb77 2019-10-19 stsp static mode_t
1111 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1113 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1115 1ebedb77 2019-10-19 stsp if (executable) {
1116 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1117 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1118 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1119 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1120 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1121 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1124 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1127 8ba819a3 2020-07-23 stsp /* forward declaration */
1128 13d9040b 2019-03-27 stsp static const struct got_error *
1129 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1130 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1131 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1132 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1133 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1134 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1137 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1138 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1140 8ba819a3 2020-07-23 stsp static const struct got_error *
1141 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1142 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1144 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1145 5a1fbc73 2020-07-23 stsp ssize_t elen;
1146 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1149 c6e8a826 2021-04-05 stsp *did_something = 0;
1152 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1153 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1154 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1155 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1156 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1157 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1159 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1160 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1161 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1162 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1164 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1165 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1166 5a1fbc73 2020-07-23 stsp if (elen == -1)
1167 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1169 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1170 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1171 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1174 c6e8a826 2021-04-05 stsp *did_something = 1;
1175 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1176 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1177 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1178 5a1fbc73 2020-07-23 stsp return err;
1181 3c1ec782 2020-07-23 stsp static const struct got_error *
1182 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1183 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1185 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1186 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1187 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1189 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1191 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1192 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1193 3c1ec782 2020-07-23 stsp return NULL;
1197 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1198 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1199 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1200 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1201 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1203 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1204 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1205 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1207 ce031e9e 2020-10-20 stsp return err;
1208 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1209 ce031e9e 2020-10-20 stsp free(parent);
1210 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1212 ce031e9e 2020-10-20 stsp free(parent);
1213 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1214 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1215 3c1ec782 2020-07-23 stsp free(abspath);
1216 3c1ec782 2020-07-23 stsp return err;
1218 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1219 3c1ec782 2020-07-23 stsp free(abspath);
1221 3c1ec782 2020-07-23 stsp return err;
1223 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1225 3c1ec782 2020-07-23 stsp return err;
1228 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1229 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1230 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1231 3c1ec782 2020-07-23 stsp return NULL;
1234 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1235 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1236 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1237 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1238 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1239 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1241 3c1ec782 2020-07-23 stsp free(path_got);
1242 3c1ec782 2020-07-23 stsp return NULL;
1245 5a1fbc73 2020-07-23 stsp static const struct got_error *
1246 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1247 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1248 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1249 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1250 ace90326 2021-09-27 thomas struct got_repository *repo,
1251 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1253 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1254 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1255 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1256 906c123b 2020-07-23 stsp char *path_got = NULL;
1257 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1258 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1260 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1263 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1264 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1265 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1266 8ba819a3 2020-07-23 stsp * in the blob object.
1269 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1270 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1271 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1272 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1273 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1274 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1275 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1276 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1277 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1278 3b9f0f87 2020-07-23 stsp progress_arg);
1280 8ba819a3 2020-07-23 stsp if (len > 0) {
1281 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1282 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1283 8ba819a3 2020-07-23 stsp len - hdrlen);
1284 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1285 8ba819a3 2020-07-23 stsp hdrlen = 0;
1287 8ba819a3 2020-07-23 stsp } while (len != 0);
1288 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1290 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1291 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1293 3c1ec782 2020-07-23 stsp return err;
1295 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1296 906c123b 2020-07-23 stsp /* install as a regular file */
1297 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1298 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1299 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1300 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1301 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1302 906c123b 2020-07-23 stsp goto done;
1305 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1306 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1307 c6e8a826 2021-04-05 stsp int symlink_replaced;
1308 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1309 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1310 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1311 c90c8ce3 2020-07-23 stsp goto done;
1313 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1314 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1316 f35fa46a 2020-07-23 stsp goto done;
1317 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1318 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1319 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1320 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1321 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1322 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1324 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1325 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1328 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1331 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1332 f4994adc 2020-10-20 stsp char *parent;
1333 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1335 f4994adc 2020-10-20 stsp goto done;
1336 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1337 f4994adc 2020-10-20 stsp free(parent);
1339 f35fa46a 2020-07-23 stsp goto done;
1341 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1342 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1344 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1345 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1346 f35fa46a 2020-07-23 stsp goto done;
1350 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1351 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1352 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1353 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1354 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1355 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1356 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1357 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1358 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1359 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1360 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1361 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1362 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1364 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1365 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1367 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1368 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1369 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1371 906c123b 2020-07-23 stsp free(path_got);
1372 8ba819a3 2020-07-23 stsp return err;
1375 8ba819a3 2020-07-23 stsp static const struct got_error *
1376 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1377 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1378 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1379 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1380 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1381 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1383 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1384 507dc3bb 2018-12-29 stsp int fd = -1;
1385 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1386 507dc3bb 2018-12-29 stsp int update = 0;
1387 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1389 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1390 06340621 2021-12-31 thomas O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1391 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1392 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1393 f5375317 2020-10-20 stsp char *parent;
1394 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1396 f5375317 2020-10-20 stsp return err;
1397 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1398 f5375317 2020-10-20 stsp free(parent);
1400 21908da4 2019-01-13 stsp return err;
1401 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1402 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1403 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1404 21908da4 2019-01-13 stsp if (fd == -1)
1405 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1406 230a42bd 2019-05-11 jcs ondisk_path);
1407 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1408 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1409 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1410 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1411 3b9f0f87 2020-07-23 stsp goto done;
1413 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1414 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1415 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1416 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1417 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1418 507dc3bb 2018-12-29 stsp goto done;
1420 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1421 507dc3bb 2018-12-29 stsp ondisk_path);
1423 507dc3bb 2018-12-29 stsp goto done;
1424 507dc3bb 2018-12-29 stsp update = 1;
1427 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1430 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1431 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1432 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1433 3818e3c4 2020-11-01 naddy goto done;
1436 bd6aa359 2020-07-23 stsp if (progress_cb) {
1437 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1438 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1440 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1441 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1444 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1445 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1447 bd6aa359 2020-07-23 stsp goto done;
1450 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1452 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1453 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1456 9d31a1d8 2018-03-11 stsp if (len > 0) {
1457 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1458 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1459 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1460 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1461 b87c6f83 2018-12-24 stsp goto done;
1462 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1463 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1464 b87c6f83 2018-12-24 stsp goto done;
1466 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1468 9d31a1d8 2018-03-11 stsp } while (len != 0);
1470 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1471 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1472 816dc654 2019-02-16 stsp goto done;
1475 507dc3bb 2018-12-29 stsp if (update) {
1476 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1477 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1478 f6d8c0ac 2020-11-09 stsp goto done;
1480 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1481 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1482 230a42bd 2019-05-11 jcs ondisk_path);
1483 68ed9ba5 2019-02-10 stsp goto done;
1485 63df146d 2020-11-09 stsp free(tmppath);
1486 63df146d 2020-11-09 stsp tmppath = NULL;
1490 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1491 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1492 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1493 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1494 507dc3bb 2018-12-29 stsp free(tmppath);
1495 6353ad76 2019-02-08 stsp return err;
1498 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1499 6353ad76 2019-02-08 stsp static const struct got_error *
1500 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1502 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1503 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1504 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1505 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1506 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1508 7154f6ce 2019-03-27 stsp int i = 0;
1509 9bdd68dd 2020-01-02 naddy char *line = NULL;
1510 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1511 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1513 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1514 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1515 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1516 7154f6ce 2019-03-27 stsp if (feof(f))
1518 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1522 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1523 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1525 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1530 9bdd68dd 2020-01-02 naddy free(line);
1532 7154f6ce 2019-03-27 stsp return err;
1535 e2b1e152 2019-07-13 stsp static int
1536 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1538 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1539 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1542 1ebedb77 2019-10-19 stsp static int
1543 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1545 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1546 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1547 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1548 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1549 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1550 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1553 c363b2c1 2019-08-03 stsp static unsigned char
1554 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1556 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1557 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1558 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1559 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1560 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1561 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1562 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1564 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1568 a919d5c4 2020-07-23 stsp static const struct got_error *
1569 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1570 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1571 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1573 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1574 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1575 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1576 a919d5c4 2020-07-23 stsp ssize_t elen;
1577 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1578 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1579 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1581 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1583 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1585 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1587 a919d5c4 2020-07-23 stsp return err;
1588 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1590 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1591 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1593 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1595 a919d5c4 2020-07-23 stsp if (len > 0) {
1596 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1597 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1598 a919d5c4 2020-07-23 stsp len - hdrlen);
1599 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1600 a919d5c4 2020-07-23 stsp hdrlen = 0;
1602 a919d5c4 2020-07-23 stsp } while (len != 0);
1603 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1605 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1606 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1607 a919d5c4 2020-07-23 stsp if (elen == -1)
1608 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1610 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1611 a919d5c4 2020-07-23 stsp if (elen == -1)
1612 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1615 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1616 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1618 a919d5c4 2020-07-23 stsp return NULL;
1621 7154f6ce 2019-03-27 stsp static const struct got_error *
1622 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1623 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1624 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1626 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1627 6353ad76 2019-02-08 stsp struct got_object_id id;
1628 6353ad76 2019-02-08 stsp size_t hdrlen;
1629 1338848f 2019-12-13 stsp int fd = -1;
1630 6353ad76 2019-02-08 stsp FILE *f = NULL;
1631 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1632 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1633 6353ad76 2019-02-08 stsp size_t flen, blen;
1634 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1636 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1637 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1640 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1641 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1642 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1644 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1645 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1646 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1647 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1648 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1650 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1651 882ef1b9 2019-12-13 stsp goto done;
1653 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1654 3d35a492 2019-12-13 stsp goto done;
1657 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1658 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1659 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1660 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1661 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1662 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1663 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1664 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1665 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1666 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1667 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1669 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1670 3d35a492 2019-12-13 stsp goto done;
1672 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1673 1338848f 2019-12-13 stsp goto done;
1677 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1678 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1679 1338848f 2019-12-13 stsp goto done;
1682 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1683 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1684 1338848f 2019-12-13 stsp goto done;
1685 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1686 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1687 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1688 1338848f 2019-12-13 stsp goto done;
1691 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1692 f179e66d 2020-07-23 stsp goto done;
1694 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1695 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1696 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1697 1338848f 2019-12-13 stsp goto done;
1700 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1701 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1702 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1704 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1706 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1708 1338848f 2019-12-13 stsp goto done;
1710 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1711 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1712 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1713 a919d5c4 2020-07-23 stsp goto done;
1716 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1717 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1718 ab0d4361 2019-12-13 stsp if (fd == -1) {
1719 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1720 ab0d4361 2019-12-13 stsp goto done;
1724 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1725 6353ad76 2019-02-08 stsp if (f == NULL) {
1726 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1727 6353ad76 2019-02-08 stsp goto done;
1730 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1732 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1733 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1735 7154f6ce 2019-03-27 stsp goto done;
1736 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1737 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1738 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1739 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1740 7154f6ce 2019-03-27 stsp goto done;
1742 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1743 6353ad76 2019-02-08 stsp if (flen != 0)
1744 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1746 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1747 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1748 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1750 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1751 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1752 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1753 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1757 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1760 6353ad76 2019-02-08 stsp hdrlen = 0;
1763 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1764 7154f6ce 2019-03-27 stsp rewind(f);
1765 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1766 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1767 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1770 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1771 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1772 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1773 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1774 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1775 9d31a1d8 2018-03-11 stsp return err;
1779 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1780 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1782 e2b1e152 2019-07-13 stsp static const struct got_error *
1783 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1784 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1786 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1787 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1788 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1790 e2b1e152 2019-07-13 stsp return NULL;
1793 9d31a1d8 2018-03-11 stsp static const struct got_error *
1794 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1795 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1796 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1797 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1798 0584f854 2019-04-06 stsp void *progress_arg)
1800 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1801 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1802 6353ad76 2019-02-08 stsp char *ondisk_path;
1803 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1804 b8f41171 2019-02-10 stsp struct stat sb;
1806 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1807 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1810 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1811 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1812 a76c42e6 2019-08-03 stsp goto done;
1814 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1817 abb4604f 2019-07-27 stsp goto done;
1818 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1819 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1821 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1822 e6f4ba31 2021-09-24 thomas if (errno != ENOENT) {
1823 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1824 e6f4ba31 2021-09-24 thomas ondisk_path);
1825 e6f4ba31 2021-09-24 thomas goto done;
1827 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1828 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1829 e6f4ba31 2021-09-24 thomas } else {
1830 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1831 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1833 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1837 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1839 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1840 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1841 b8f41171 2019-02-10 stsp goto done;
1843 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1845 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1846 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1848 5036ab18 2020-04-18 stsp goto done;
1851 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1852 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1853 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1855 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1856 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1857 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1858 c6e8a826 2021-04-05 stsp * updating contents of this file.
1860 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1861 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1862 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1863 c6e8a826 2021-04-05 stsp /* Same commit. */
1864 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1865 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1867 e2b1e152 2019-07-13 stsp goto done;
1868 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1870 6353ad76 2019-02-08 stsp goto done;
1872 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1873 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1874 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1875 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1876 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1877 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1879 0f58026f 2021-04-05 stsp goto done;
1880 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1882 b8f41171 2019-02-10 stsp goto done;
1886 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1888 6353ad76 2019-02-08 stsp goto done;
1890 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1891 234035bc 2019-06-01 stsp int update_timestamps;
1892 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1893 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1894 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1895 234035bc 2019-06-01 stsp struct got_object_id id2;
1896 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1897 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1899 f69721c3 2019-10-21 stsp goto done;
1901 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1902 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1903 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1904 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1905 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1906 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1907 f69721c3 2019-10-21 stsp goto done;
1909 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1910 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1911 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1912 234035bc 2019-06-01 stsp goto done;
1915 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1916 36bf999c 2020-07-23 stsp char *link_target;
1917 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1919 36bf999c 2020-07-23 stsp goto done;
1920 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1921 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1922 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1923 36bf999c 2020-07-23 stsp free(link_target);
1925 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1926 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1927 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1928 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1930 f69721c3 2019-10-21 stsp free(label_orig);
1931 234035bc 2019-06-01 stsp if (blob2)
1932 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1934 909d120e 2019-09-22 stsp goto done;
1936 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1937 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1938 234035bc 2019-06-01 stsp * unmodified files again.
1940 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1941 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1942 234035bc 2019-06-01 stsp update_timestamps);
1943 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1944 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1945 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1946 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1947 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1949 1ee397ad 2019-07-12 stsp goto done;
1950 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1951 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1953 13d9040b 2019-03-27 stsp goto done;
1955 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1956 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1957 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1958 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1959 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1960 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
1961 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
1963 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1964 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
1965 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
1966 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1967 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1970 13d9040b 2019-03-27 stsp goto done;
1973 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
1974 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
1975 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
1977 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
1978 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
1979 054041d0 2020-06-24 stsp &blob->id);
1982 13d9040b 2019-03-27 stsp goto done;
1984 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
1985 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
1986 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
1989 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1991 6353ad76 2019-02-08 stsp free(ondisk_path);
1992 8da9e5f4 2019-01-12 stsp return err;
1995 90285c3b 2019-01-08 stsp static const struct got_error *
1996 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1998 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1999 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2001 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2002 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2004 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2005 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2006 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2008 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2009 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2011 1c4cdd89 2021-06-20 stsp goto done;
2012 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2013 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2014 fddefe3b 2020-10-20 stsp free(ondisk_path);
2015 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2016 1c4cdd89 2021-06-20 stsp parent = NULL;
2017 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2018 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2019 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2020 fddefe3b 2020-10-20 stsp ondisk_path);
2023 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2029 90285c3b 2019-01-08 stsp free(ondisk_path);
2030 1c4cdd89 2021-06-20 stsp free(parent);
2031 90285c3b 2019-01-08 stsp return err;
2034 708d8e67 2019-03-27 stsp static const struct got_error *
2035 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2036 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2037 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2039 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2040 708d8e67 2019-03-27 stsp unsigned char status;
2041 708d8e67 2019-03-27 stsp struct stat sb;
2042 708d8e67 2019-03-27 stsp char *ondisk_path;
2044 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2045 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2047 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2049 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2051 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2053 5a58a424 2020-06-23 stsp goto done;
2055 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2056 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2057 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2058 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2059 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2060 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2061 993e2a1b 2020-07-23 stsp goto done;
2063 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2064 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2065 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2066 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2068 993e2a1b 2020-07-23 stsp goto done;
2069 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2070 993e2a1b 2020-07-23 stsp ie->path);
2071 993e2a1b 2020-07-23 stsp goto done;
2074 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2075 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2076 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2078 5a58a424 2020-06-23 stsp goto done;
2080 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2081 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2083 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2084 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2086 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2088 5a58a424 2020-06-23 stsp goto done;
2089 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2090 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2092 5a58a424 2020-06-23 stsp goto done;
2094 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2097 5a58a424 2020-06-23 stsp free(ondisk_path);
2098 fc6346c4 2019-03-27 stsp return err;
2101 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2102 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2103 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2104 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2105 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2106 8da9e5f4 2019-01-12 stsp void *progress_arg;
2107 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2108 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2111 512f0d0e 2019-01-02 stsp static const struct got_error *
2112 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2113 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2115 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2117 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2118 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2120 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2121 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2124 8da9e5f4 2019-01-12 stsp static const struct got_error *
2125 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2127 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2129 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2130 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2132 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2133 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2136 9d31a1d8 2018-03-11 stsp static const struct got_error *
2137 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2139 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2140 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2141 8da9e5f4 2019-01-12 stsp char *path;
2143 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2144 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2146 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2147 63c5ca5d 2019-08-24 stsp return NULL;
2149 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2150 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2152 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2154 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2155 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2157 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2158 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2160 8da9e5f4 2019-01-12 stsp free(path);
2161 0cd1c46a 2019-03-11 stsp return err;
2164 b2118c49 2020-07-28 stsp const struct got_error *
2165 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2167 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2169 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2170 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2171 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2172 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2175 b2118c49 2020-07-28 stsp return NULL;
2178 818c7501 2019-07-11 stsp static const struct got_error *
2179 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2181 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2182 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2184 517bab73 2019-03-11 stsp *refname = NULL;
2186 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2188 b2118c49 2020-07-28 stsp return err;
2190 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2191 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2192 0647c563 2019-03-11 stsp *refname = NULL;
2194 517bab73 2019-03-11 stsp free(uuidstr);
2195 517bab73 2019-03-11 stsp return err;
2198 818c7501 2019-07-11 stsp const struct got_error *
2199 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2201 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2204 818c7501 2019-07-11 stsp static const struct got_error *
2205 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2207 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2208 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2211 818c7501 2019-07-11 stsp static const struct got_error *
2212 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2214 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2217 818c7501 2019-07-11 stsp static const struct got_error *
2218 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2220 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2221 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2224 818c7501 2019-07-11 stsp static const struct got_error *
2225 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2227 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2228 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2231 0ebf8283 2019-07-24 stsp static const struct got_error *
2232 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2234 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2235 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2238 0ebf8283 2019-07-24 stsp static const struct got_error *
2239 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2241 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2242 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2245 0ebf8283 2019-07-24 stsp static const struct got_error *
2246 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2248 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2249 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2252 0ebf8283 2019-07-24 stsp static const struct got_error *
2253 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2255 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2256 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2259 0ebf8283 2019-07-24 stsp const struct got_error *
2260 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2261 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2263 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2264 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2265 0ebf8283 2019-07-24 stsp *path = NULL;
2266 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2268 0ebf8283 2019-07-24 stsp return NULL;
2271 10604dce 2021-09-24 thomas static const struct got_error *
2272 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2274 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2275 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2278 10604dce 2021-09-24 thomas static const struct got_error *
2279 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2281 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2282 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2286 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2287 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2289 517bab73 2019-03-11 stsp static const struct got_error *
2290 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2292 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2293 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2294 517bab73 2019-03-11 stsp char *refname;
2296 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2298 517bab73 2019-03-11 stsp return err;
2300 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2302 0cd1c46a 2019-03-11 stsp goto done;
2304 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2306 0cd1c46a 2019-03-11 stsp free(refname);
2308 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2309 3e3a69f1 2019-07-25 stsp return err;
2312 3e3a69f1 2019-07-25 stsp static const struct got_error *
2313 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2315 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2317 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2318 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2319 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2320 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2322 9d31a1d8 2018-03-11 stsp return err;
2326 ebf99748 2019-05-09 stsp static const struct got_error *
2327 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2328 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2330 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2331 ebf99748 2019-05-09 stsp FILE *index = NULL;
2333 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2334 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2335 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2336 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2338 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2340 ebf99748 2019-05-09 stsp goto done;
2342 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2343 ebf99748 2019-05-09 stsp if (index == NULL) {
2344 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2345 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2347 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2348 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2349 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2352 ebf99748 2019-05-09 stsp if (err) {
2353 ebf99748 2019-05-09 stsp free(*fileindex_path);
2354 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2355 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2356 ebf99748 2019-05-09 stsp *fileindex = NULL;
2358 ebf99748 2019-05-09 stsp return err;
2361 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2362 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2363 c932eeeb 2019-05-22 stsp const char *path;
2364 c932eeeb 2019-05-22 stsp size_t path_len;
2365 c932eeeb 2019-05-22 stsp const char *entry_name;
2366 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2367 a484d721 2019-06-10 stsp void *progress_arg;
2370 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2371 c932eeeb 2019-05-22 stsp static const struct got_error *
2372 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2374 1ee397ad 2019-07-12 stsp const struct got_error *err;
2375 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2377 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2378 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2379 c932eeeb 2019-05-22 stsp return NULL;
2380 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2381 102ff934 2019-06-11 stsp return NULL;
2383 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2384 a769b60b 2021-06-27 stsp return NULL;
2386 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2387 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2388 c932eeeb 2019-05-22 stsp return NULL;
2390 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2391 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2392 edd02c5e 2019-07-11 stsp ie->path);
2394 1ee397ad 2019-07-12 stsp return err;
2396 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2397 c932eeeb 2019-05-22 stsp return NULL;
2400 a615e0e7 2020-12-16 stsp static const struct got_error *
2401 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2402 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2403 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2405 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2407 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2408 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2409 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2410 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2411 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2412 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2414 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2415 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2418 9c6338c4 2019-06-02 stsp static const struct got_error *
2419 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2421 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2422 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2423 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2424 867630bb 2020-01-17 stsp struct timespec timeout;
2426 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2427 9c6338c4 2019-06-02 stsp fileindex_path);
2429 9c6338c4 2019-06-02 stsp goto done;
2431 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2433 9c6338c4 2019-06-02 stsp goto done;
2435 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2436 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2437 9c6338c4 2019-06-02 stsp fileindex_path);
2438 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2442 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2443 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2444 867630bb 2020-01-17 stsp * was recorded in the file index.
2446 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2447 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2448 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2450 9c6338c4 2019-06-02 stsp if (new_index)
2451 9c6338c4 2019-06-02 stsp fclose(new_index);
2452 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2453 9c6338c4 2019-06-02 stsp return err;
2456 6ced4176 2019-07-12 stsp static const struct got_error *
2457 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2458 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2459 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2461 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2462 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2463 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2464 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2466 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2467 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2468 6ced4176 2019-07-12 stsp *tree_id = NULL;
2470 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2471 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2472 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2473 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2474 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2475 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2476 6ced4176 2019-07-12 stsp goto done;
2478 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2479 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2481 6ced4176 2019-07-12 stsp goto done;
2482 6ced4176 2019-07-12 stsp return NULL;
2485 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2487 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2488 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2489 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2490 6ced4176 2019-07-12 stsp goto done;
2493 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2494 6ced4176 2019-07-12 stsp in_repo_path);
2496 6ced4176 2019-07-12 stsp goto done;
2498 6ced4176 2019-07-12 stsp free(in_repo_path);
2499 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2501 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2503 6ced4176 2019-07-12 stsp goto done;
2505 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2506 6ced4176 2019-07-12 stsp /* Check out a single file. */
2507 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2508 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2509 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2510 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2511 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2512 6ced4176 2019-07-12 stsp goto done;
2514 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2515 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2516 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2517 6ced4176 2019-07-12 stsp goto done;
2520 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2521 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2523 6ced4176 2019-07-12 stsp return err;
2524 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2525 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2526 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2527 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2528 6ced4176 2019-07-12 stsp goto done;
2531 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2532 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2534 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2535 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2536 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2537 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2538 6ced4176 2019-07-12 stsp goto done;
2540 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2541 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2542 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2543 6ced4176 2019-07-12 stsp goto done;
2548 6ced4176 2019-07-12 stsp free(in_repo_path);
2549 6ced4176 2019-07-12 stsp if (err) {
2550 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2551 6ced4176 2019-07-12 stsp free(*tree_relpath);
2552 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2553 6ced4176 2019-07-12 stsp free(*tree_id);
2554 6ced4176 2019-07-12 stsp *tree_id = NULL;
2556 07ed472d 2019-07-12 stsp return err;
2559 07ed472d 2019-07-12 stsp static const struct got_error *
2560 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2561 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2562 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2563 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2565 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2566 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2567 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2568 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2569 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2571 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2572 7f47418f 2019-12-20 stsp if (err) {
2573 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2574 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2575 7f47418f 2019-12-20 stsp goto done;
2576 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2577 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2579 7f47418f 2019-12-20 stsp return err;
2582 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2583 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2585 07ed472d 2019-07-12 stsp goto done;
2587 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2589 07ed472d 2019-07-12 stsp goto done;
2591 07ed472d 2019-07-12 stsp if (entry_name &&
2592 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2593 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2594 07ed472d 2019-07-12 stsp goto done;
2597 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2598 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2599 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2600 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2601 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2602 07ed472d 2019-07-12 stsp arg.repo = repo;
2603 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2604 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2605 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2606 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2607 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2608 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2611 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2612 07ed472d 2019-07-12 stsp if (commit)
2613 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2614 6ced4176 2019-07-12 stsp return err;
2617 9d31a1d8 2018-03-11 stsp const struct got_error *
2618 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2619 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2620 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2621 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2623 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2624 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2625 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2626 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2627 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2628 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2629 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2630 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2631 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2632 f2ea84fa 2019-07-27 stsp int entry_type;
2633 f2ea84fa 2019-07-27 stsp char *relpath;
2634 f2ea84fa 2019-07-27 stsp char *entry_name;
2635 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2636 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2638 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2640 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2642 9d31a1d8 2018-03-11 stsp return err;
2644 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2645 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2646 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2647 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2648 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2649 f2ea84fa 2019-07-27 stsp goto done;
2652 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2653 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2654 f2ea84fa 2019-07-27 stsp if (err) {
2655 f2ea84fa 2019-07-27 stsp free(tpd);
2656 6ced4176 2019-07-12 stsp goto done;
2659 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2660 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2661 f2ea84fa 2019-07-27 stsp if (err) {
2662 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2663 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2664 f2ea84fa 2019-07-27 stsp free(tpd);
2665 f2ea84fa 2019-07-27 stsp goto done;
2668 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2670 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2674 51514078 2018-12-25 stsp * Read the file index.
2675 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2676 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2678 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2680 c4cdcb68 2019-04-03 stsp goto done;
2682 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2683 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2684 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2686 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2687 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2688 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2692 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2693 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2694 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2695 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2696 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2697 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2698 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2699 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2703 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2705 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2706 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2707 af12c6b9 2019-06-04 stsp err = sync_err;
2709 9c6338c4 2019-06-02 stsp free(fileindex_path);
2711 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2712 9d31a1d8 2018-03-11 stsp if (commit)
2713 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2714 5ade8233 2019-07-12 stsp if (fileindex)
2715 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2716 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2717 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2718 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2719 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2720 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2721 f2ea84fa 2019-07-27 stsp free(tpd);
2723 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2724 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2725 9d31a1d8 2018-03-11 stsp err = unlockerr;
2726 f8d1f275 2019-02-04 stsp return err;
2729 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2730 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2731 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2732 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2733 234035bc 2019-06-01 stsp void *progress_arg;
2734 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2735 234035bc 2019-06-01 stsp void *cancel_arg;
2736 f69721c3 2019-10-21 stsp const char *label_orig;
2737 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2738 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2741 234035bc 2019-06-01 stsp static const struct got_error *
2742 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2743 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2744 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2745 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2747 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2748 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2749 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2750 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2751 234035bc 2019-06-01 stsp struct stat sb;
2752 234035bc 2019-06-01 stsp unsigned char status;
2753 234035bc 2019-06-01 stsp int local_changes_subsumed;
2754 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2755 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2757 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2758 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2759 d6c87207 2019-08-02 stsp strlen(path2));
2760 1ee397ad 2019-07-12 stsp if (ie == NULL)
2761 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2762 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2764 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2765 234035bc 2019-06-01 stsp path2) == -1)
2766 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2768 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2771 234035bc 2019-06-01 stsp goto done;
2773 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2774 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2775 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2776 234035bc 2019-06-01 stsp goto done;
2778 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2779 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2780 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2781 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2782 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2783 234035bc 2019-06-01 stsp goto done;
2786 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2787 36bf999c 2020-07-23 stsp char *link_target2;
2788 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2790 36bf999c 2020-07-23 stsp goto done;
2791 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2792 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2793 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2794 36bf999c 2020-07-23 stsp free(link_target2);
2798 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2799 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2800 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2801 1af628f4 2021-06-03 stsp goto done;
2803 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2804 1af628f4 2021-06-03 stsp f_orig, blob1);
2806 1af628f4 2021-06-03 stsp goto done;
2808 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2809 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2810 1af628f4 2021-06-03 stsp goto done;
2811 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2812 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2814 1af628f4 2021-06-03 stsp goto done;
2816 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
2817 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2818 1af628f4 2021-06-03 stsp if (fd == -1) {
2819 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2820 1af628f4 2021-06-03 stsp ondisk_path);
2821 1af628f4 2021-06-03 stsp goto done;
2823 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2824 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2825 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2826 1af628f4 2021-06-03 stsp ondisk_path);
2827 1af628f4 2021-06-03 stsp close(fd);
2828 1af628f4 2021-06-03 stsp goto done;
2830 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2832 1af628f4 2021-06-03 stsp goto done;
2833 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2834 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2835 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2836 1af628f4 2021-06-03 stsp goto done;
2838 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2839 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2840 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2841 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2842 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2844 234035bc 2019-06-01 stsp } else if (blob1) {
2845 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2846 d6c87207 2019-08-02 stsp strlen(path1));
2847 1ee397ad 2019-07-12 stsp if (ie == NULL)
2848 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2849 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2851 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2852 2b92fad7 2019-06-02 stsp path1) == -1)
2853 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2855 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2858 2b92fad7 2019-06-02 stsp goto done;
2860 2b92fad7 2019-06-02 stsp switch (status) {
2861 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2862 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2863 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2865 1ee397ad 2019-07-12 stsp goto done;
2866 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2868 2b92fad7 2019-06-02 stsp goto done;
2870 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2872 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2873 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2874 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2875 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2877 1ee397ad 2019-07-12 stsp goto done;
2879 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2881 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2882 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2883 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2884 e8f02263 2022-01-23 thomas off_t blob1_size;
2886 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2887 0a22ca1a 2020-09-23 stsp * exists in the repository.
2889 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
2890 e8f02263 2022-01-23 thomas &blob1_size, path1);
2892 0a22ca1a 2020-09-23 stsp goto done;
2893 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2894 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2895 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2897 0a22ca1a 2020-09-23 stsp goto done;
2898 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2901 0a22ca1a 2020-09-23 stsp goto done;
2903 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2906 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2907 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2909 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2910 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2913 0a22ca1a 2020-09-23 stsp goto done;
2916 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2917 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2918 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2919 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2921 1ee397ad 2019-07-12 stsp goto done;
2923 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2924 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2926 1ee397ad 2019-07-12 stsp goto done;
2931 234035bc 2019-06-01 stsp } else if (blob2) {
2932 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2933 234035bc 2019-06-01 stsp path2) == -1)
2934 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2935 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2936 d6c87207 2019-08-02 stsp strlen(path2));
2938 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2939 7f91a133 2019-12-13 stsp -1, NULL, repo);
2941 234035bc 2019-06-01 stsp goto done;
2942 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2943 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2944 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2945 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2946 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2947 1ee397ad 2019-07-12 stsp status, path2);
2948 234035bc 2019-06-01 stsp goto done;
2950 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2951 36bf999c 2020-07-23 stsp char *link_target2;
2952 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2955 36bf999c 2020-07-23 stsp goto done;
2956 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2957 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2958 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2959 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2960 36bf999c 2020-07-23 stsp free(link_target2);
2961 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2962 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2963 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2964 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2965 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2966 526a746f 2020-07-23 stsp a->progress_arg);
2968 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2969 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2972 dfe9fba0 2020-07-23 stsp goto done;
2973 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2974 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2975 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
2976 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2978 234035bc 2019-06-01 stsp goto done;
2981 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2982 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2983 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2984 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2985 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2986 ace90326 2021-09-27 thomas 0, 1, a->allow_bad_symlinks, repo,
2987 ace90326 2021-09-27 thomas a->progress_cb, a->progress_arg);
2989 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2990 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2991 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2994 234035bc 2019-06-01 stsp goto done;
2995 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2997 234035bc 2019-06-01 stsp goto done;
2998 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2999 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3000 3969253a 2020-03-07 stsp if (err) {
3001 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3002 3969253a 2020-03-07 stsp goto done;
3004 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3005 2b92fad7 2019-06-02 stsp if (err) {
3006 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3007 2b92fad7 2019-06-02 stsp goto done;
3009 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3010 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3011 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3016 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3017 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3018 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3019 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3020 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3021 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3022 1af628f4 2021-06-03 stsp free(id_str);
3023 54d5be07 2021-06-03 stsp free(label_deriv2);
3024 234035bc 2019-06-01 stsp free(ondisk_path);
3025 234035bc 2019-06-01 stsp return err;
3028 69de9dd4 2021-09-03 stsp static const struct got_error *
3029 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3031 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3033 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3034 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3035 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3036 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3037 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3039 69de9dd4 2021-09-03 stsp return NULL;
3042 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3043 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3044 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3045 234035bc 2019-06-01 stsp struct got_repository *repo;
3048 234035bc 2019-06-01 stsp static const struct got_error *
3049 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3050 69de9dd4 2021-09-03 stsp struct got_blob_object *blob2, struct got_object_id *id1,
3051 69de9dd4 2021-09-03 stsp struct got_object_id *id2, const char *path1, const char *path2,
3052 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3054 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3055 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3056 234035bc 2019-06-01 stsp unsigned char status;
3057 234035bc 2019-06-01 stsp struct stat sb;
3058 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3059 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3060 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3061 234035bc 2019-06-01 stsp char *ondisk_path;
3063 69de9dd4 2021-09-03 stsp if (id == NULL)
3064 69de9dd4 2021-09-03 stsp return NULL;
3066 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3067 69de9dd4 2021-09-03 stsp if (ie == NULL)
3068 69de9dd4 2021-09-03 stsp return NULL;
3070 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3072 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3074 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3075 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3076 5546d466 2021-09-02 stsp free(ondisk_path);
3078 234035bc 2019-06-01 stsp return err;
3079 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3080 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3082 234035bc 2019-06-01 stsp return NULL;
3085 818c7501 2019-07-11 stsp static const struct got_error *
3086 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3087 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3088 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3089 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3090 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3092 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3093 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3094 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3095 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3096 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3097 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3099 03415a1a 2019-06-02 stsp if (commit_id1) {
3100 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3101 03415a1a 2019-06-02 stsp worktree->path_prefix);
3102 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3103 03415a1a 2019-06-02 stsp goto done;
3105 69d57f3d 2020-07-31 stsp if (tree_id1) {
3106 69d57f3d 2020-07-31 stsp char *id_str;
3108 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3110 03415a1a 2019-06-02 stsp goto done;
3112 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3114 f69721c3 2019-10-21 stsp goto done;
3116 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3117 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3118 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3119 f69721c3 2019-10-21 stsp free(id_str);
3120 f69721c3 2019-10-21 stsp goto done;
3122 f69721c3 2019-10-21 stsp free(id_str);
3125 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3126 234035bc 2019-06-01 stsp worktree->path_prefix);
3128 234035bc 2019-06-01 stsp goto done;
3130 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3132 234035bc 2019-06-01 stsp goto done;
3134 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3135 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3136 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3137 69de9dd4 2021-09-03 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3138 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3140 69de9dd4 2021-09-03 stsp goto done;
3142 234035bc 2019-06-01 stsp arg.worktree = worktree;
3143 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3144 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3145 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3146 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3147 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3148 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3149 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3150 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3151 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3152 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3153 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3154 af12c6b9 2019-06-04 stsp err = sync_err;
3156 234035bc 2019-06-01 stsp if (tree1)
3157 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3158 234035bc 2019-06-01 stsp if (tree2)
3159 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3160 f69721c3 2019-10-21 stsp free(label_orig);
3161 818c7501 2019-07-11 stsp return err;
3164 818c7501 2019-07-11 stsp const struct got_error *
3165 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3166 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3167 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3168 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3170 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3171 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3172 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3174 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3176 818c7501 2019-07-11 stsp return err;
3178 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3180 818c7501 2019-07-11 stsp goto done;
3182 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3183 69de9dd4 2021-09-03 stsp worktree);
3185 818c7501 2019-07-11 stsp goto done;
3187 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3188 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3189 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3191 818c7501 2019-07-11 stsp if (fileindex)
3192 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3193 818c7501 2019-07-11 stsp free(fileindex_path);
3194 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3195 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3196 234035bc 2019-06-01 stsp err = unlockerr;
3197 234035bc 2019-06-01 stsp return err;
3200 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3201 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3202 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3203 927df6b7 2019-02-10 stsp const char *status_path;
3204 927df6b7 2019-02-10 stsp size_t status_path_len;
3205 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3206 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3207 f8d1f275 2019-02-04 stsp void *status_arg;
3208 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3209 f8d1f275 2019-02-04 stsp void *cancel_arg;
3210 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3211 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3212 f2a9dc41 2019-12-13 tracey int report_unchanged;
3213 3143d852 2020-06-25 stsp int no_ignores;
3216 f8d1f275 2019-02-04 stsp static const struct got_error *
3217 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3218 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3219 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3220 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3222 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3223 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3224 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3225 927df6b7 2019-02-10 stsp struct stat sb;
3226 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3227 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3228 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3230 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3232 98eaaa12 2019-08-03 stsp return err;
3234 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3235 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3236 98eaaa12 2019-08-03 stsp return NULL;
3238 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3239 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3240 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3242 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3243 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3244 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3246 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3247 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3248 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3249 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3250 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3253 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3254 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3257 927df6b7 2019-02-10 stsp static const struct got_error *
3258 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3259 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3261 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3262 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3263 f8d1f275 2019-02-04 stsp char *abspath;
3265 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3266 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3268 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3269 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3270 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3271 927df6b7 2019-02-10 stsp return NULL;
3273 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3274 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3275 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3276 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3278 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3279 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3280 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3283 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3284 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3285 f8d1f275 2019-02-04 stsp free(abspath);
3286 9d31a1d8 2018-03-11 stsp return err;
3289 f8d1f275 2019-02-04 stsp static const struct got_error *
3290 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3292 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3293 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3294 2ec1f75b 2019-03-26 stsp unsigned char status;
3296 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3297 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3299 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3300 927df6b7 2019-02-10 stsp return NULL;
3302 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3303 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3304 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3305 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3307 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3308 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3309 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3313 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3315 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3317 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3318 6841da00 2019-08-08 stsp free((char *)pe->path);
3319 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3323 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3325 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3327 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3328 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3329 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3330 6841da00 2019-08-08 stsp free((char *)pe->path);
3332 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3335 6841da00 2019-08-08 stsp static const struct got_error *
3336 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3338 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3339 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3340 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3341 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3342 6841da00 2019-08-08 stsp size_t linesize = 0;
3343 6841da00 2019-08-08 stsp ssize_t linelen;
3345 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3346 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3347 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3348 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3350 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3351 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3352 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3354 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3355 bd8de430 2019-10-04 stsp if (line[0] == '#')
3358 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3359 bd8de430 2019-10-04 stsp if (line[0] == '!')
3362 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3363 6841da00 2019-08-08 stsp line) == -1) {
3364 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3365 6841da00 2019-08-08 stsp goto done;
3367 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3369 6841da00 2019-08-08 stsp goto done;
3371 6841da00 2019-08-08 stsp if (ferror(f)) {
3372 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3373 6841da00 2019-08-08 stsp goto done;
3376 6841da00 2019-08-08 stsp dirpath = strdup(path);
3377 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3378 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3379 6841da00 2019-08-08 stsp goto done;
3381 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3383 6841da00 2019-08-08 stsp free(line);
3384 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3385 6841da00 2019-08-08 stsp free(dirpath);
3386 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3388 6841da00 2019-08-08 stsp return err;
3392 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3394 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3396 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3397 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3398 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3399 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3401 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3402 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3404 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3406 bd8de430 2019-10-04 stsp pattern += 3;
3408 bd8de430 2019-10-04 stsp while (*p) {
3409 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3410 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3411 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3412 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3414 bd8de430 2019-10-04 stsp while (*p == '/')
3424 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3425 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3426 6841da00 2019-08-08 stsp * ignores backwards.
3428 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3429 6841da00 2019-08-08 stsp while (pe) {
3430 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3431 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3432 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3433 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3434 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3435 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3436 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3437 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3438 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3443 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3449 6841da00 2019-08-08 stsp static const struct got_error *
3450 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3451 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3453 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3454 6841da00 2019-08-08 stsp char *ignorespath;
3455 886cec17 2019-12-15 stsp int fd = -1;
3456 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3458 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3459 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3460 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3462 886cec17 2019-12-15 stsp if (dirfd != -1) {
3463 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3464 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3465 886cec17 2019-12-15 stsp if (fd == -1) {
3466 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3467 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3468 886cec17 2019-12-15 stsp ignorespath);
3470 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3471 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3472 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3473 886cec17 2019-12-15 stsp ignorespath);
3476 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3480 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3481 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3482 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3483 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3484 886cec17 2019-12-15 stsp ignorespath);
3486 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3489 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3490 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3491 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3492 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3493 6841da00 2019-08-08 stsp free(ignorespath);
3494 6841da00 2019-08-08 stsp return err;
3497 f8d1f275 2019-02-04 stsp static const struct got_error *
3498 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3499 6092c299 2021-10-04 thomas int dirfd)
3501 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3502 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3503 f8d1f275 2019-02-04 stsp char *path = NULL;
3505 6092c299 2021-10-04 thomas if (ignore != NULL)
3506 6092c299 2021-10-04 thomas *ignore = 0;
3508 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3509 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3511 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3512 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3513 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3515 f8d1f275 2019-02-04 stsp path = de->d_name;
3518 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3519 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3520 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3521 6092c299 2021-10-04 thomas *ignore = 1;
3522 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3523 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3524 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3525 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3526 f8d1f275 2019-02-04 stsp if (parent_path[0])
3527 f8d1f275 2019-02-04 stsp free(path);
3528 b72f483a 2019-02-05 stsp return err;
3531 347d1d3e 2019-07-12 stsp static const struct got_error *
3532 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3534 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3535 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3537 3143d852 2020-06-25 stsp if (a->no_ignores)
3538 3143d852 2020-06-25 stsp return NULL;
3540 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3541 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3543 3143d852 2020-06-25 stsp return err;
3545 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3546 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3548 3143d852 2020-06-25 stsp return err;
3551 3143d852 2020-06-25 stsp static const struct got_error *
3552 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3553 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3554 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3555 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3557 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3558 abb4604f 2019-07-27 stsp struct stat sb;
3560 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3562 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3563 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3565 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3566 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3567 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3568 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3569 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3572 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3573 fe1d8685 2022-02-12 thomas return NULL;
3575 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3576 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3577 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3579 abb4604f 2019-07-27 stsp return NULL;
3582 3143d852 2020-06-25 stsp static const struct got_error *
3583 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3584 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3586 3143d852 2020-06-25 stsp const struct got_error *err;
3587 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3589 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3590 3143d852 2020-06-25 stsp ".cvsignore");
3592 3143d852 2020-06-25 stsp return err;
3594 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3595 3143d852 2020-06-25 stsp ".gitignore");
3597 3143d852 2020-06-25 stsp return err;
3599 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3600 3143d852 2020-06-25 stsp if (err) {
3601 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3602 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3603 3143d852 2020-06-25 stsp return err;
3605 3143d852 2020-06-25 stsp for (;;) {
3606 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3607 3143d852 2020-06-25 stsp ".cvsignore");
3610 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3611 3143d852 2020-06-25 stsp ".gitignore");
3614 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3615 3143d852 2020-06-25 stsp if (err) {
3616 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3617 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3620 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3622 b737c85a 2020-06-26 stsp free(parent_path);
3623 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3624 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3627 b737c85a 2020-06-26 stsp free(parent_path);
3628 b737c85a 2020-06-26 stsp free(next_parent_path);
3629 3143d852 2020-06-25 stsp return err;
3632 abb4604f 2019-07-27 stsp static const struct got_error *
3633 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3634 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3635 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3636 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3637 f2a9dc41 2019-12-13 tracey int report_unchanged)
3639 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3640 6fc93f37 2019-12-13 stsp int fd = -1;
3641 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3642 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3643 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3644 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3646 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3648 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3649 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3650 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3652 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3653 6fc93f37 2019-12-13 stsp if (fd == -1) {
3654 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3655 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3656 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3658 ff56836b 2021-07-08 stsp if (!no_ignores) {
3659 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3660 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3662 ff56836b 2021-07-08 stsp goto done;
3664 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3665 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3666 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3669 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3670 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3671 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3672 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3673 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3674 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3675 abb4604f 2019-07-27 stsp arg.status_path = path;
3676 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3677 abb4604f 2019-07-27 stsp arg.repo = repo;
3678 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3679 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3680 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3681 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3682 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3683 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3684 022fae89 2019-12-06 tracey if (!no_ignores) {
3685 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3686 3143d852 2020-06-25 stsp worktree->root_path, path);
3688 3143d852 2020-06-25 stsp goto done;
3690 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3691 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3692 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3695 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3696 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3697 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3698 927df6b7 2019-02-10 stsp free(ondisk_path);
3699 347d1d3e 2019-07-12 stsp return err;
3702 347d1d3e 2019-07-12 stsp const struct got_error *
3703 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3704 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3705 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3706 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3708 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3709 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3710 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3711 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3713 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3715 347d1d3e 2019-07-12 stsp return err;
3717 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3718 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3719 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3720 f6343036 2021-06-22 stsp no_ignores, 0);
3724 f8d1f275 2019-02-04 stsp free(fileindex_path);
3725 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3726 f8d1f275 2019-02-04 stsp return err;
3729 6c7ab921 2019-03-18 stsp const struct got_error *
3730 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3731 6c7ab921 2019-03-18 stsp const char *arg)
3733 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3734 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3735 6c7ab921 2019-03-18 stsp size_t len;
3736 00bb5ea0 2020-07-23 stsp struct stat sb;
3737 01740607 2020-11-04 stsp char *abspath = NULL;
3738 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3740 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3742 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3743 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3744 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3746 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3747 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3748 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3749 00bb5ea0 2020-07-23 stsp goto done;
3751 727173c3 2020-11-06 stsp sb.st_mode = 0;
3753 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3755 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3756 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3757 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3758 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3760 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3761 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3762 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3763 00bb5ea0 2020-07-23 stsp goto done;
3767 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3768 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3770 d0710d08 2019-07-22 stsp goto done;
3771 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3772 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3773 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3774 00bb5ea0 2020-07-23 stsp goto done;
3777 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3778 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3779 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3780 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3781 00bb5ea0 2020-07-23 stsp goto done;
3783 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3784 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3785 01740607 2020-11-04 stsp goto done;
3787 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3788 01740607 2020-11-04 stsp sizeof(canonpath));
3790 00bb5ea0 2020-07-23 stsp goto done;
3791 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3792 01740607 2020-11-04 stsp if (resolved == NULL) {
3793 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3794 01740607 2020-11-04 stsp goto done;
3799 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3800 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3801 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3802 6c7ab921 2019-03-18 stsp goto done;
3805 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3806 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3807 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3809 f6d88e1a 2019-05-29 stsp goto done;
3811 f6d88e1a 2019-05-29 stsp path = strdup("");
3812 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3813 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3814 f6d88e1a 2019-05-29 stsp goto done;
3818 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3819 6c7ab921 2019-03-18 stsp len = strlen(path);
3820 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3821 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3825 01740607 2020-11-04 stsp free(abspath);
3826 6c7ab921 2019-03-18 stsp free(resolved);
3827 d0710d08 2019-07-22 stsp free(cwd);
3828 6c7ab921 2019-03-18 stsp if (err == NULL)
3829 6c7ab921 2019-03-18 stsp *wt_path = path;
3831 6c7ab921 2019-03-18 stsp free(path);
3832 d00136be 2019-03-26 stsp return err;
3835 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3836 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3837 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3838 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3839 4e68cba3 2019-11-23 stsp void *progress_arg;
3840 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3843 4e68cba3 2019-11-23 stsp static const struct got_error *
3844 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3845 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3846 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3847 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3849 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3850 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3851 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3852 6d022e97 2019-08-04 stsp struct stat sb;
3853 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3855 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3856 dbb83fbd 2019-12-12 stsp relpath) == -1)
3857 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3859 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3861 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3862 12463d8b 2019-12-13 stsp de_name, a->repo);
3864 dbb83fbd 2019-12-12 stsp goto done;
3865 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3866 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3867 dbb83fbd 2019-12-12 stsp goto done;
3868 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3870 dbb83fbd 2019-12-12 stsp goto done;
3873 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3874 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
3875 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
3877 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3878 dbb83fbd 2019-12-12 stsp goto done;
3881 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3883 4e68cba3 2019-11-23 stsp goto done;
3884 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3885 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3886 3969253a 2020-03-07 stsp if (err) {
3887 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3888 3969253a 2020-03-07 stsp goto done;
3890 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3891 07f5b47a 2019-06-02 stsp if (err) {
3892 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3893 4e68cba3 2019-11-23 stsp goto done;
3896 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3898 dbb83fbd 2019-12-12 stsp return err;
3899 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3900 dbb83fbd 2019-12-12 stsp return NULL;
3901 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3904 d00136be 2019-03-26 stsp const struct got_error *
3905 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3906 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3907 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3908 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3910 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3911 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3912 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3913 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3914 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3916 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3918 d00136be 2019-03-26 stsp return err;
3920 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3922 d00136be 2019-03-26 stsp goto done;
3924 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3925 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3926 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3927 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3928 4e68cba3 2019-11-23 stsp saa.repo = repo;
3930 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3931 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3932 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3936 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3937 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3938 af12c6b9 2019-06-04 stsp err = sync_err;
3940 fb399478 2019-07-12 stsp free(fileindex_path);
3941 d00136be 2019-03-26 stsp if (fileindex)
3942 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3943 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3944 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3945 d00136be 2019-03-26 stsp err = unlockerr;
3946 6c7ab921 2019-03-18 stsp return err;
3949 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3950 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3951 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3952 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3953 f2a9dc41 2019-12-13 tracey void *progress_arg;
3954 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3955 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3956 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3957 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
3958 766841c2 2020-08-13 stsp const char *status_codes;
3961 f2a9dc41 2019-12-13 tracey static const struct got_error *
3962 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3963 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3964 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3965 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3967 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3968 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3969 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3970 17ed4618 2019-06-02 stsp struct stat sb;
3971 20a2ad1c 2020-10-20 stsp char *ondisk_path;
3973 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
3974 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
3975 d64cc78a 2022-01-25 thomas return NULL;
3976 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
3979 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3980 17ed4618 2019-06-02 stsp if (ie == NULL)
3981 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
3983 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3984 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3985 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3986 9acbc4fa 2019-08-03 stsp return NULL;
3987 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3990 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3991 f2a9dc41 2019-12-13 tracey relpath) == -1)
3992 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3994 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3997 f2a9dc41 2019-12-13 tracey goto done;
3999 766841c2 2020-08-13 stsp if (a->status_codes) {
4000 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4002 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4003 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4006 766841c2 2020-08-13 stsp if (i == ncodes) {
4007 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4008 766841c2 2020-08-13 stsp free(ondisk_path);
4009 766841c2 2020-08-13 stsp return NULL;
4011 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4012 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4013 766841c2 2020-08-13 stsp static char msg[64];
4014 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4015 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4016 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4017 766841c2 2020-08-13 stsp goto done;
4021 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4022 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4023 f2a9dc41 2019-12-13 tracey goto done;
4024 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4025 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4026 f2a9dc41 2019-12-13 tracey goto done;
4028 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4029 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4030 d64cc78a 2022-01-25 thomas goto done;
4032 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4033 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4034 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4035 f2a9dc41 2019-12-13 tracey goto done;
4039 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4040 20a2ad1c 2020-10-20 stsp size_t root_len;
4042 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4043 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
4044 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4045 12463d8b 2019-12-13 stsp ondisk_path);
4046 12463d8b 2019-12-13 stsp goto done;
4048 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
4049 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4050 12463d8b 2019-12-13 stsp goto done;
4053 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4055 20a2ad1c 2020-10-20 stsp char *parent;
4056 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4058 2513f20a 2020-10-20 stsp goto done;
4059 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4060 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4061 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4062 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4063 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4064 20a2ad1c 2020-10-20 stsp ondisk_path);
4067 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4068 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4071 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4073 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4074 f2a9dc41 2019-12-13 tracey if (err)
4075 f2a9dc41 2019-12-13 tracey return err;
4076 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4077 f2a9dc41 2019-12-13 tracey return NULL;
4078 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4079 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4082 2ec1f75b 2019-03-26 stsp const struct got_error *
4083 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4084 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4085 766841c2 2020-08-13 stsp const char *status_codes,
4086 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4087 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4089 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4090 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4091 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4092 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4093 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4095 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4097 2ec1f75b 2019-03-26 stsp return err;
4099 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4101 2ec1f75b 2019-03-26 stsp goto done;
4103 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4104 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4105 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4106 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4107 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4108 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4109 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4110 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4111 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4113 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4114 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4115 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4119 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4120 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4121 af12c6b9 2019-06-04 stsp err = sync_err;
4123 fb399478 2019-07-12 stsp free(fileindex_path);
4124 2ec1f75b 2019-03-26 stsp if (fileindex)
4125 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4126 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4127 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4128 2ec1f75b 2019-03-26 stsp err = unlockerr;
4129 33aa809d 2019-08-08 stsp return err;
4132 33aa809d 2019-08-08 stsp static const struct got_error *
4133 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4135 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4136 33aa809d 2019-08-08 stsp char *line = NULL;
4137 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4138 33aa809d 2019-08-08 stsp ssize_t linelen;
4140 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4141 33aa809d 2019-08-08 stsp if (linelen == -1) {
4142 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4143 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4144 33aa809d 2019-08-08 stsp goto done;
4146 33aa809d 2019-08-08 stsp return NULL;
4148 33aa809d 2019-08-08 stsp if (outfile) {
4149 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4150 33aa809d 2019-08-08 stsp if (n != linelen) {
4151 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4152 33aa809d 2019-08-08 stsp goto done;
4155 33aa809d 2019-08-08 stsp if (rejectfile) {
4156 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4157 33aa809d 2019-08-08 stsp if (n != linelen)
4158 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4161 33aa809d 2019-08-08 stsp free(line);
4162 2ec1f75b 2019-03-26 stsp return err;
4165 33aa809d 2019-08-08 stsp static const struct got_error *
4166 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4168 33aa809d 2019-08-08 stsp char *line = NULL;
4169 33aa809d 2019-08-08 stsp size_t linesize = 0;
4170 33aa809d 2019-08-08 stsp ssize_t linelen;
4172 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4173 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4174 500467ff 2019-09-25 hiltjo if (ferror(f))
4175 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4176 500467ff 2019-09-25 hiltjo return NULL;
4178 33aa809d 2019-08-08 stsp free(line);
4179 33aa809d 2019-08-08 stsp return NULL;
4182 33aa809d 2019-08-08 stsp static const struct got_error *
4183 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4184 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4185 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4187 33aa809d 2019-08-08 stsp const struct got_error *err;
4189 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4190 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4191 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4193 33aa809d 2019-08-08 stsp return err;
4194 33aa809d 2019-08-08 stsp (*line_cur1)++;
4196 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4197 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4198 33aa809d 2019-08-08 stsp if (rejectfile)
4199 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4201 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4203 33aa809d 2019-08-08 stsp return err;
4204 33aa809d 2019-08-08 stsp (*line_cur2)++;
4206 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4207 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4208 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4210 33aa809d 2019-08-08 stsp return err;
4211 33aa809d 2019-08-08 stsp (*line_cur2)++;
4213 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4214 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4215 33aa809d 2019-08-08 stsp if (rejectfile)
4216 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4218 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4220 33aa809d 2019-08-08 stsp return err;
4221 33aa809d 2019-08-08 stsp (*line_cur1)++;
4224 f1e81a05 2019-08-10 stsp return NULL;
4227 f1e81a05 2019-08-10 stsp static const struct got_error *
4228 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4229 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4231 f1e81a05 2019-08-10 stsp const struct got_error *err;
4233 f1e81a05 2019-08-10 stsp if (outfile) {
4234 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4235 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4236 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4238 f1e81a05 2019-08-10 stsp return err;
4239 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4242 f1e81a05 2019-08-10 stsp if (rejectfile) {
4243 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4244 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4245 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4247 f1e81a05 2019-08-10 stsp return err;
4248 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4252 33aa809d 2019-08-08 stsp return NULL;
4255 33aa809d 2019-08-08 stsp static const struct got_error *
4256 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4257 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4258 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4259 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4260 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4262 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4263 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4264 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4265 33aa809d 2019-08-08 stsp FILE *hunkfile;
4266 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4267 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4270 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4272 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4273 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4274 fe621944 2020-11-10 stsp start_old = cc.left.start;
4275 fe621944 2020-11-10 stsp end_old = cc.left.end;
4276 fe621944 2020-11-10 stsp start_new = cc.right.start;
4277 fe621944 2020-11-10 stsp end_new = cc.right.end;
4279 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4280 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4281 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4283 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4284 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4285 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4287 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4288 fe621944 2020-11-10 stsp if (diff_state == NULL)
4289 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4290 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4292 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4293 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4294 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4295 33aa809d 2019-08-08 stsp goto done;
4298 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4299 fe621944 2020-11-10 stsp diff_result, &cc);
4300 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4301 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4302 33aa809d 2019-08-08 stsp goto done;
4305 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4306 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4307 33aa809d 2019-08-08 stsp goto done;
4310 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4311 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4313 33aa809d 2019-08-08 stsp goto done;
4315 33aa809d 2019-08-08 stsp switch (*choice) {
4316 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4317 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4318 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4320 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4321 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4322 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4324 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4327 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4331 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4332 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4333 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4334 33aa809d 2019-08-08 stsp return err;
4337 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4338 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4339 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4340 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4341 1f1abb7e 2019-08-08 stsp void *progress_arg;
4342 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4343 33aa809d 2019-08-08 stsp void *patch_arg;
4344 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4345 10604dce 2021-09-24 thomas int unlink_added_files;
4348 e20a8b6f 2019-06-04 stsp static const struct got_error *
4349 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4350 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4351 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4352 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4353 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4355 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4356 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4357 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4358 1ebedb77 2019-10-19 stsp int fd2 = -1;
4359 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4360 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4361 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4362 fe621944 2020-11-10 stsp struct stat sb2;
4363 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4364 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4365 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4367 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4369 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4371 33aa809d 2019-08-08 stsp return err;
4373 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4374 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4375 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4376 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4377 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4378 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4379 fa3cef63 2020-07-23 stsp goto done;
4381 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4382 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4383 48b4f239 2021-12-31 thomas if (link_len == -1) {
4384 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4387 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4388 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4391 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4392 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4393 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4394 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4395 fa3cef63 2020-07-23 stsp goto done;
4397 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4398 fa3cef63 2020-07-23 stsp sizeof(link_target));
4399 fa3cef63 2020-07-23 stsp if (link_len == -1)
4400 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4401 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4402 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4405 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4406 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4407 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4408 fa3cef63 2020-07-23 stsp goto done;
4411 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4412 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4413 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4414 fa3cef63 2020-07-23 stsp goto done;
4419 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4420 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4421 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4422 fa3cef63 2020-07-23 stsp goto done;
4424 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4425 fa3cef63 2020-07-23 stsp if (n != link_len) {
4426 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4427 fa3cef63 2020-07-23 stsp goto done;
4429 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4430 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4431 fa3cef63 2020-07-23 stsp goto done;
4433 fa3cef63 2020-07-23 stsp rewind(f2);
4436 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4438 33aa809d 2019-08-08 stsp goto done;
4440 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4442 33aa809d 2019-08-08 stsp goto done;
4444 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4446 33aa809d 2019-08-08 stsp goto done;
4448 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4451 33aa809d 2019-08-08 stsp goto done;
4453 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4455 33aa809d 2019-08-08 stsp goto done;
4457 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4458 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4459 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4460 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4462 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4463 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4464 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4465 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4466 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4467 fe621944 2020-11-10 stsp nchanges++;
4469 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4470 33aa809d 2019-08-08 stsp int choice;
4471 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4472 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4473 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4474 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4475 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4476 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4478 33aa809d 2019-08-08 stsp goto done;
4479 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4480 33aa809d 2019-08-08 stsp have_content = 1;
4481 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4484 1ebedb77 2019-10-19 stsp if (have_content) {
4485 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4486 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4487 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4489 1ebedb77 2019-10-19 stsp goto done;
4491 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4492 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4493 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4494 fa3cef63 2020-07-23 stsp goto done;
4499 33aa809d 2019-08-08 stsp free(id_str);
4501 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4502 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4503 fe621944 2020-11-10 stsp if (err == NULL)
4504 fe621944 2020-11-10 stsp err = free_err;
4505 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4506 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4507 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4508 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4509 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4510 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4511 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4512 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4513 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4514 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4515 33aa809d 2019-08-08 stsp if (err || !have_content) {
4516 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4517 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4518 33aa809d 2019-08-08 stsp free(*path_outfile);
4519 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4521 33aa809d 2019-08-08 stsp free(path1);
4522 33aa809d 2019-08-08 stsp return err;
4525 33aa809d 2019-08-08 stsp static const struct got_error *
4526 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4527 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4528 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4529 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4531 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4532 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4533 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4534 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4535 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4536 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4537 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4538 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4539 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4540 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4542 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4543 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4544 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4545 d3bcc3d1 2019-08-08 stsp return NULL;
4547 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4548 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4549 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4551 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4552 65084dad 2019-08-08 stsp if (ie == NULL)
4553 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4555 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4556 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4557 a129376b 2019-03-28 stsp if (err) {
4558 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4559 a129376b 2019-03-28 stsp goto done;
4560 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4561 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4562 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4563 e20a8b6f 2019-06-04 stsp goto done;
4566 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4567 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4568 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4569 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4570 a129376b 2019-03-28 stsp goto done;
4573 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4574 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4575 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4576 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4577 a129376b 2019-03-28 stsp goto done;
4580 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4581 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4582 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4583 a129376b 2019-03-28 stsp goto done;
4588 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4589 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4590 a9fa2909 2019-07-27 stsp if (err) {
4591 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4592 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4593 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4594 a9fa2909 2019-07-27 stsp goto done;
4596 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4598 a9fa2909 2019-07-27 stsp goto done;
4600 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4602 a9fa2909 2019-07-27 stsp goto done;
4604 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4605 1233e6b6 2020-10-19 stsp free(te_name);
4606 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4607 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4608 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4609 a9fa2909 2019-07-27 stsp goto done;
4613 a129376b 2019-03-28 stsp switch (status) {
4614 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4615 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4616 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4617 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4618 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4620 33aa809d 2019-08-08 stsp goto done;
4621 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4624 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4625 1f1abb7e 2019-08-08 stsp ie->path);
4627 1ee397ad 2019-07-12 stsp goto done;
4628 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4629 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
4630 10604dce 2021-09-24 thomas if (asprintf(&ondisk_path, "%s/%s",
4631 10604dce 2021-09-24 thomas got_worktree_get_root_path(a->worktree),
4632 10604dce 2021-09-24 thomas relpath) == -1) {
4633 10604dce 2021-09-24 thomas err = got_error_from_errno("asprintf");
4634 10604dce 2021-09-24 thomas goto done;
4636 10604dce 2021-09-24 thomas if (unlink(ondisk_path) == -1) {
4637 10604dce 2021-09-24 thomas err = got_error_from_errno2("unlink",
4638 10604dce 2021-09-24 thomas ondisk_path);
4643 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4644 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4645 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4646 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4647 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4649 33aa809d 2019-08-08 stsp goto done;
4650 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4653 33aa809d 2019-08-08 stsp /* fall through */
4654 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4655 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4656 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4657 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4658 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4659 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4660 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4661 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4662 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4664 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4665 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4666 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4668 65084dad 2019-08-08 stsp goto done;
4670 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4671 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4672 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4673 a129376b 2019-03-28 stsp goto done;
4676 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4677 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4678 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4679 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4680 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4681 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4682 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4684 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4685 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4686 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4687 369fd7e5 2020-07-23 stsp path_content);
4690 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4691 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4692 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4693 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4695 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4696 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4697 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4698 369fd7e5 2020-07-23 stsp goto done;
4702 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4703 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4704 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4705 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4706 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4707 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4709 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4711 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4712 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4713 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4714 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4717 a129376b 2019-03-28 stsp goto done;
4718 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4719 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4720 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4721 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4722 437adc9d 2020-12-10 yzhong blob->id.sha1,
4723 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4725 2e1fa222 2020-07-23 stsp goto done;
4727 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4728 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4729 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4738 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4739 33aa809d 2019-08-08 stsp free(path_content);
4740 e20a8b6f 2019-06-04 stsp free(parent_path);
4741 a129376b 2019-03-28 stsp free(tree_path);
4743 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4745 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4746 a129376b 2019-03-28 stsp free(tree_id);
4747 e20a8b6f 2019-06-04 stsp return err;
4750 e20a8b6f 2019-06-04 stsp const struct got_error *
4751 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4752 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4753 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4754 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4755 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4757 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4758 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4759 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4760 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4761 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4762 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4764 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4766 e20a8b6f 2019-06-04 stsp return err;
4768 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4770 e20a8b6f 2019-06-04 stsp goto done;
4772 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4773 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4774 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4775 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4776 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4777 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4778 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4779 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
4780 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4781 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4782 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
4786 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4787 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4788 e20a8b6f 2019-06-04 stsp err = sync_err;
4790 fb399478 2019-07-12 stsp free(fileindex_path);
4791 a129376b 2019-03-28 stsp if (fileindex)
4792 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4793 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4794 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4795 a129376b 2019-03-28 stsp err = unlockerr;
4796 c4296144 2019-05-09 stsp return err;
4799 cf066bf8 2019-05-09 stsp static void
4800 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4802 24519714 2019-05-09 stsp free(ct->path);
4803 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4804 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4805 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4806 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4807 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4808 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4812 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4813 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4814 24519714 2019-05-09 stsp struct got_repository *repo;
4815 24519714 2019-05-09 stsp struct got_worktree *worktree;
4816 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4817 5f8a88c6 2019-08-03 stsp int have_staged_files;
4818 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4821 c4296144 2019-05-09 stsp static const struct got_error *
4822 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
4823 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
4824 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4825 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4827 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
4828 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4829 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4830 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4831 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4832 768aea60 2019-05-09 stsp struct stat sb;
4834 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
4835 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4836 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4837 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4838 5f8a88c6 2019-08-03 stsp return NULL;
4840 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4841 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4843 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4844 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4845 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4846 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4847 5f8a88c6 2019-08-03 stsp return NULL;
4850 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4851 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4852 036813ee 2019-05-09 stsp goto done;
4854 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4855 036813ee 2019-05-09 stsp parent_path = strdup("");
4856 69960a46 2019-05-09 stsp if (parent_path == NULL)
4857 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4859 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4861 69960a46 2019-05-09 stsp return err;
4864 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4865 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4866 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4867 c4296144 2019-05-09 stsp goto done;
4870 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4871 768aea60 2019-05-09 stsp relpath) == -1) {
4872 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4873 768aea60 2019-05-09 stsp goto done;
4876 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4877 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4878 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4879 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4880 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4881 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4882 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4883 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4885 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4886 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4889 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4890 0aeb8099 2020-07-23 stsp goto done;
4892 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4893 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4894 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4895 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4896 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4897 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4898 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4899 12463d8b 2019-12-13 stsp ct->ondisk_path);
4900 12463d8b 2019-12-13 stsp goto done;
4902 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4903 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4904 768aea60 2019-05-09 stsp goto done;
4906 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4909 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4910 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4911 44d03001 2019-05-09 stsp relpath) == -1) {
4912 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4913 44d03001 2019-05-09 stsp goto done;
4916 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4917 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4918 35213c7c 2020-07-23 stsp int is_bad_symlink;
4919 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4920 35213c7c 2020-07-23 stsp ssize_t target_len;
4921 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4922 35213c7c 2020-07-23 stsp sizeof(target_path));
4923 35213c7c 2020-07-23 stsp if (target_len == -1) {
4924 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4925 35213c7c 2020-07-23 stsp ct->ondisk_path);
4926 35213c7c 2020-07-23 stsp goto done;
4928 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4929 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4931 35213c7c 2020-07-23 stsp goto done;
4932 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4933 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4934 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4935 35213c7c 2020-07-23 stsp goto done;
4940 cf066bf8 2019-05-09 stsp ct->status = status;
4941 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4942 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4943 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4944 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4945 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4946 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4947 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4948 b416585c 2019-05-13 stsp goto done;
4950 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4951 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4952 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4953 f0b75401 2019-08-03 stsp goto done;
4956 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4957 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4958 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4959 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4960 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4961 036813ee 2019-05-09 stsp goto done;
4964 24519714 2019-05-09 stsp ct->path = strdup(path);
4965 24519714 2019-05-09 stsp if (ct->path == NULL) {
4966 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4967 24519714 2019-05-09 stsp goto done;
4969 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4971 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4972 ed175427 2019-05-09 stsp free_commitable(ct);
4973 24519714 2019-05-09 stsp free(parent_path);
4974 036813ee 2019-05-09 stsp free(path);
4975 a129376b 2019-03-28 stsp return err;
4978 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4979 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4980 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4981 036813ee 2019-05-09 stsp struct got_repository *);
4983 ed175427 2019-05-09 stsp static const struct got_error *
4984 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4985 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4986 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4987 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4988 afa376bf 2019-05-09 stsp struct got_repository *repo)
4990 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4991 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4992 036813ee 2019-05-09 stsp char *subpath;
4994 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4995 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4996 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4998 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5000 ed175427 2019-05-09 stsp return err;
5002 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5003 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5004 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5005 036813ee 2019-05-09 stsp free(subpath);
5006 036813ee 2019-05-09 stsp return err;
5009 036813ee 2019-05-09 stsp static const struct got_error *
5010 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5012 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5013 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5015 036813ee 2019-05-09 stsp *match = 0;
5017 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5018 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5019 0f63689d 2019-05-10 stsp return NULL;
5022 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5024 0f63689d 2019-05-10 stsp return err;
5025 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5026 036813ee 2019-05-09 stsp free(ct_parent_path);
5027 036813ee 2019-05-09 stsp return err;
5030 768aea60 2019-05-09 stsp static mode_t
5031 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5033 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5034 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5036 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5039 036813ee 2019-05-09 stsp static const struct got_error *
5040 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5041 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5043 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5045 036813ee 2019-05-09 stsp *new_te = NULL;
5047 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5049 036813ee 2019-05-09 stsp goto done;
5051 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5053 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5054 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5055 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5057 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5059 036813ee 2019-05-09 stsp if (err && *new_te) {
5060 56e0773d 2019-11-28 stsp free(*new_te);
5061 036813ee 2019-05-09 stsp *new_te = NULL;
5063 036813ee 2019-05-09 stsp return err;
5066 036813ee 2019-05-09 stsp static const struct got_error *
5067 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5068 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5070 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5071 102b254e 2020-10-19 stsp char *ct_name = NULL;
5073 036813ee 2019-05-09 stsp *new_te = NULL;
5075 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5076 036813ee 2019-05-09 stsp if (*new_te == NULL)
5077 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5079 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5081 036813ee 2019-05-09 stsp goto done;
5082 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5083 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5084 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5085 036813ee 2019-05-09 stsp goto done;
5088 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5090 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5091 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5092 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5094 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5096 102b254e 2020-10-19 stsp free(ct_name);
5097 036813ee 2019-05-09 stsp if (err && *new_te) {
5098 56e0773d 2019-11-28 stsp free(*new_te);
5099 036813ee 2019-05-09 stsp *new_te = NULL;
5101 036813ee 2019-05-09 stsp return err;
5104 036813ee 2019-05-09 stsp static const struct got_error *
5105 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5106 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5108 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5109 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5111 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5113 036813ee 2019-05-09 stsp return err;
5114 036813ee 2019-05-09 stsp if (new_pe == NULL)
5115 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5116 036813ee 2019-05-09 stsp return NULL;
5119 afa376bf 2019-05-09 stsp static const struct got_error *
5120 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5121 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5123 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5124 5f8a88c6 2019-08-03 stsp unsigned char status;
5126 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5127 ae1e948a 2021-09-28 thomas return NULL;
5129 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5130 afa376bf 2019-05-09 stsp ct_path++;
5132 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5133 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5135 5f8a88c6 2019-08-03 stsp status = ct->status;
5137 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5138 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5141 036813ee 2019-05-09 stsp static const struct got_error *
5142 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5143 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5145 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5146 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5147 44d03001 2019-05-09 stsp char *te_path;
5149 44d03001 2019-05-09 stsp *modified = 0;
5151 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5152 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5153 44d03001 2019-05-09 stsp te->name) == -1)
5154 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5156 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5157 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5158 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5159 44d03001 2019-05-09 stsp strlen(te_path));
5160 62d463ca 2020-10-20 naddy if (*modified)
5164 44d03001 2019-05-09 stsp free(te_path);
5165 44d03001 2019-05-09 stsp return err;
5168 44d03001 2019-05-09 stsp static const struct got_error *
5169 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5170 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5171 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5173 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5174 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5176 036813ee 2019-05-09 stsp *ctp = NULL;
5178 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5179 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5180 036813ee 2019-05-09 stsp char *ct_name = NULL;
5181 036813ee 2019-05-09 stsp int path_matches;
5183 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5184 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5185 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5186 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5189 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5190 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5194 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5197 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5199 036813ee 2019-05-09 stsp return err;
5200 036813ee 2019-05-09 stsp if (!path_matches)
5203 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5205 d34b633e 2020-10-19 stsp return err;
5207 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5208 d34b633e 2020-10-19 stsp free(ct_name);
5211 d34b633e 2020-10-19 stsp free(ct_name);
5213 036813ee 2019-05-09 stsp *ctp = ct;
5217 2b496619 2019-07-10 stsp return err;
5220 2b496619 2019-07-10 stsp static const struct got_error *
5221 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5222 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5223 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5224 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5225 2b496619 2019-07-10 stsp struct got_repository *repo)
5227 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5228 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5229 2b496619 2019-07-10 stsp char *subtree_path;
5230 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5231 ba580f68 2020-03-22 stsp int nentries;
5233 2b496619 2019-07-10 stsp *new_tep = NULL;
5235 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5236 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5237 2b496619 2019-07-10 stsp child_path) == -1)
5238 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5240 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5241 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5242 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5243 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5245 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5246 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5247 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5248 2b496619 2019-07-10 stsp goto done;
5250 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5251 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5252 2b496619 2019-07-10 stsp if (err) {
5253 56e0773d 2019-11-28 stsp free(new_te);
5254 2b496619 2019-07-10 stsp goto done;
5256 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5259 2b496619 2019-07-10 stsp free(subtree_path);
5260 2b496619 2019-07-10 stsp if (err == NULL)
5261 2b496619 2019-07-10 stsp *new_tep = new_te;
5262 036813ee 2019-05-09 stsp return err;
5265 036813ee 2019-05-09 stsp static const struct got_error *
5266 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5267 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5268 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5269 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5270 036813ee 2019-05-09 stsp struct got_repository *repo)
5272 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5273 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5274 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5275 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5277 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5278 ba580f68 2020-03-22 stsp *nentries = 0;
5280 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5281 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5282 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5283 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5285 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5286 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5287 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5290 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5291 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5294 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5295 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5297 036813ee 2019-05-09 stsp goto done;
5299 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5300 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5301 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5303 036813ee 2019-05-09 stsp goto done;
5304 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5306 afa376bf 2019-05-09 stsp goto done;
5307 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5308 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5310 2f51b5b3 2019-05-09 stsp goto done;
5311 ba580f68 2020-03-22 stsp (*nentries)++;
5313 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5314 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5315 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5316 2b496619 2019-07-10 stsp == NULL) {
5317 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5318 2b496619 2019-07-10 stsp child_path, path_base_tree,
5319 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5322 2b496619 2019-07-10 stsp goto done;
5323 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5325 2b496619 2019-07-10 stsp goto done;
5326 ba580f68 2020-03-22 stsp (*nentries)++;
5331 2f51b5b3 2019-05-09 stsp if (base_tree) {
5332 56e0773d 2019-11-28 stsp int i, nbase_entries;
5333 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5334 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5335 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5336 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5338 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5339 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5340 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5341 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5343 63c5ca5d 2019-08-24 stsp goto done;
5344 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5346 63c5ca5d 2019-08-24 stsp goto done;
5347 ba580f68 2020-03-22 stsp (*nentries)++;
5351 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5352 44d03001 2019-05-09 stsp int modified;
5353 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5355 036813ee 2019-05-09 stsp goto done;
5356 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5357 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5359 2f51b5b3 2019-05-09 stsp goto done;
5360 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5361 44d03001 2019-05-09 stsp if (modified) {
5362 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5363 ba580f68 2020-03-22 stsp int nsubentries;
5364 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5365 ba580f68 2020-03-22 stsp &nsubentries, te,
5366 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5367 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5369 44d03001 2019-05-09 stsp goto done;
5370 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5371 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5372 ba580f68 2020-03-22 stsp free(new_id);
5375 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5376 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5377 56e0773d 2019-11-28 stsp free(new_id);
5379 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5381 036813ee 2019-05-09 stsp goto done;
5382 ba580f68 2020-03-22 stsp (*nentries)++;
5386 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5387 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5389 f66c734c 2019-09-22 stsp goto done;
5391 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5392 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5393 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5394 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5395 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5396 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5398 2f51b5b3 2019-05-09 stsp goto done;
5399 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5401 2f51b5b3 2019-05-09 stsp goto done;
5402 ba580f68 2020-03-22 stsp (*nentries)++;
5404 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5405 b416585c 2019-05-13 stsp status_arg);
5407 afa376bf 2019-05-09 stsp goto done;
5409 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5410 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5412 2f51b5b3 2019-05-09 stsp goto done;
5413 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5415 2f51b5b3 2019-05-09 stsp goto done;
5416 ba580f68 2020-03-22 stsp (*nentries)++;
5421 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5422 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5424 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5425 2e1fa222 2020-07-23 stsp return err;
5428 2e1fa222 2020-07-23 stsp static const struct got_error *
5429 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5430 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5431 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5432 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5434 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5435 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5436 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5438 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5439 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5440 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5442 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5444 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5445 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5446 437adc9d 2020-12-10 yzhong if (err)
5447 437adc9d 2020-12-10 yzhong goto done;
5450 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5451 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5452 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5453 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5454 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5455 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5456 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5457 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5459 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5460 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5461 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5462 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5463 72fd46fa 2019-09-06 stsp !have_staged_files);
5465 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5466 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5467 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5468 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5469 72fd46fa 2019-09-06 stsp !have_staged_files);
5471 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5473 437adc9d 2020-12-10 yzhong goto done;
5474 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5475 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5476 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5477 3969253a 2020-03-07 stsp if (err) {
5478 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5479 437adc9d 2020-12-10 yzhong goto done;
5481 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5482 3969253a 2020-03-07 stsp if (err) {
5483 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5484 437adc9d 2020-12-10 yzhong goto done;
5487 437adc9d 2020-12-10 yzhong free(relpath);
5488 437adc9d 2020-12-10 yzhong relpath = NULL;
5491 437adc9d 2020-12-10 yzhong free(relpath);
5492 d56d26ce 2019-05-10 stsp return err;
5496 d56d26ce 2019-05-10 stsp static const struct got_error *
5497 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5498 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5499 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5500 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5501 735ef5ac 2019-08-03 stsp int ood_errcode)
5503 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5504 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5506 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5507 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5508 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5509 1a36436d 2019-06-10 stsp return NULL;
5511 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5512 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5514 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5515 735ef5ac 2019-08-03 stsp in_repo_path);
5516 9bead371 2019-07-28 stsp if (err) {
5517 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5518 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5519 1a36436d 2019-06-10 stsp goto done;
5520 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5521 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5523 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5524 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5525 735ef5ac 2019-08-03 stsp in_repo_path);
5526 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5527 1a36436d 2019-06-10 stsp goto done;
5528 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5532 1a36436d 2019-06-10 stsp return err;
5535 c4296144 2019-05-09 stsp const struct got_error *
5536 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5537 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5538 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
5539 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
5540 10604dce 2021-09-24 thomas struct got_worktree *worktree,
5541 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5542 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5543 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5544 afa376bf 2019-05-09 stsp struct got_repository *repo)
5546 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5547 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5548 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5549 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5550 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5551 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5552 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5553 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5554 10604dce 2021-09-24 thomas int nentries, nparents = 0;
5555 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5556 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5557 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5559 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5561 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5563 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5565 588edf97 2019-05-10 stsp goto done;
5567 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5569 588edf97 2019-05-10 stsp goto done;
5571 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5572 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5577 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5578 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5582 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5583 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5584 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5585 cf066bf8 2019-05-09 stsp char *ondisk_path;
5587 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5588 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5589 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5592 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5593 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5594 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5597 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5598 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5599 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5600 cf066bf8 2019-05-09 stsp goto done;
5602 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5603 2e1fa222 2020-07-23 stsp free(ondisk_path);
5605 cf066bf8 2019-05-09 stsp goto done;
5608 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5609 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5610 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5612 036813ee 2019-05-09 stsp goto done;
5614 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5616 de18fc63 2019-05-09 stsp goto done;
5617 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5618 10604dce 2021-09-24 thomas nparents++;
5619 10604dce 2021-09-24 thomas if (parent_id2) {
5620 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
5621 10604dce 2021-09-24 thomas if (err)
5622 10604dce 2021-09-24 thomas goto done;
5623 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5624 10604dce 2021-09-24 thomas nparents++;
5626 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5627 10604dce 2021-09-24 thomas nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5628 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5629 33ad4cbe 2019-05-12 jcs free(logmsg);
5631 9d40349a 2019-05-09 stsp goto done;
5633 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5634 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5635 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5636 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5637 09f5bd90 2019-05-09 stsp goto done;
5639 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5640 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5642 09f5bd90 2019-05-09 stsp goto done;
5643 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5645 ebf99748 2019-05-09 stsp goto done;
5646 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5647 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5648 09f5bd90 2019-05-09 stsp goto done;
5650 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5651 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5653 f2c16586 2019-05-09 stsp goto done;
5654 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5656 f2c16586 2019-05-09 stsp goto done;
5658 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5660 09f5bd90 2019-05-09 stsp goto done;
5662 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5664 ebf99748 2019-05-09 stsp goto done;
5666 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
5667 588edf97 2019-05-10 stsp if (head_tree)
5668 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5669 588edf97 2019-05-10 stsp if (head_commit)
5670 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5671 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5672 2f17228e 2019-05-12 stsp if (head_ref2) {
5673 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5674 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5675 2f17228e 2019-05-12 stsp err = unlockerr;
5676 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5678 39cd0ff6 2019-07-12 stsp return err;
5681 5c1e53bc 2019-07-28 stsp static const struct got_error *
5682 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5683 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5685 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5686 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5688 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5689 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5690 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5692 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5693 5c1e53bc 2019-07-28 stsp ct_path++;
5695 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5696 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5700 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5701 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5703 5c1e53bc 2019-07-28 stsp return NULL;
5706 f0b75401 2019-08-03 stsp static const struct got_error *
5707 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5709 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5711 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5712 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5713 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5716 f0b75401 2019-08-03 stsp return NULL;
5719 5f8a88c6 2019-08-03 stsp static const struct got_error *
5720 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5721 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5723 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5724 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5726 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5727 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5729 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5730 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5731 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5732 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5733 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5734 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5737 5f8a88c6 2019-08-03 stsp return NULL;
5740 39cd0ff6 2019-07-12 stsp const struct got_error *
5741 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5742 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5743 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5744 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5745 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5746 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5748 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5749 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5750 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5751 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5752 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5753 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5754 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5755 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5756 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5758 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5760 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5762 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5764 39cd0ff6 2019-07-12 stsp goto done;
5766 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5768 39cd0ff6 2019-07-12 stsp goto done;
5770 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5772 39cd0ff6 2019-07-12 stsp goto done;
5774 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5776 39cd0ff6 2019-07-12 stsp goto done;
5778 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5779 5f8a88c6 2019-08-03 stsp &have_staged_files);
5780 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5781 5f8a88c6 2019-08-03 stsp goto done;
5782 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5783 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5785 5f8a88c6 2019-08-03 stsp goto done;
5788 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5789 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5790 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5791 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5792 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5793 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5794 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5795 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5796 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5798 5c1e53bc 2019-07-28 stsp goto done;
5801 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5802 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5803 39cd0ff6 2019-07-12 stsp goto done;
5806 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5807 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5809 5c1e53bc 2019-07-28 stsp goto done;
5812 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5813 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5814 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5816 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5817 f0b75401 2019-08-03 stsp ct_path++;
5818 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5819 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5820 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5822 b50cabdf 2019-07-12 stsp goto done;
5826 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5827 10604dce 2021-09-24 thomas head_commit_id, NULL, worktree, author, committer,
5828 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5830 39cd0ff6 2019-07-12 stsp goto done;
5832 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5833 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5834 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5835 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5836 39cd0ff6 2019-07-12 stsp err = sync_err;
5838 39cd0ff6 2019-07-12 stsp if (fileindex)
5839 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5840 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5841 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5842 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5843 39cd0ff6 2019-07-12 stsp err = unlockerr;
5844 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5845 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5846 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5848 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5849 c4296144 2019-05-09 stsp return err;
5852 8656d6c4 2019-05-20 stsp const char *
5853 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5855 8656d6c4 2019-05-20 stsp return ct->path;
5858 8656d6c4 2019-05-20 stsp unsigned int
5859 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5861 8656d6c4 2019-05-20 stsp return ct->status;
5864 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5865 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5866 818c7501 2019-07-11 stsp struct got_repository *repo;
5869 818c7501 2019-07-11 stsp static const struct got_error *
5870 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5872 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5873 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5874 818c7501 2019-07-11 stsp unsigned char status;
5875 818c7501 2019-07-11 stsp struct stat sb;
5876 818c7501 2019-07-11 stsp char *ondisk_path;
5878 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5879 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5880 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5881 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5883 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5885 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5887 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5888 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5889 818c7501 2019-07-11 stsp free(ondisk_path);
5891 818c7501 2019-07-11 stsp return err;
5893 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5894 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5895 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5896 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5898 818c7501 2019-07-11 stsp return NULL;
5901 818c7501 2019-07-11 stsp const struct got_error *
5902 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5903 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5904 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5905 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5907 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5908 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5909 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5910 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5911 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5912 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5913 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5915 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5916 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5917 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5919 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5921 818c7501 2019-07-11 stsp return err;
5923 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5925 818c7501 2019-07-11 stsp goto done;
5927 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5928 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5929 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5932 818c7501 2019-07-11 stsp goto done;
5934 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5936 818c7501 2019-07-11 stsp goto done;
5938 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5940 818c7501 2019-07-11 stsp goto done;
5942 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5944 818c7501 2019-07-11 stsp goto done;
5946 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5949 e51d7b55 2020-01-04 stsp goto done;
5951 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5953 818c7501 2019-07-11 stsp goto done;
5954 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5955 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5956 e51d7b55 2020-01-04 stsp goto done;
5959 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5960 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5962 818c7501 2019-07-11 stsp goto done;
5963 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5965 818c7501 2019-07-11 stsp goto done;
5967 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5969 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5971 818c7501 2019-07-11 stsp goto done;
5973 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5975 818c7501 2019-07-11 stsp goto done;
5977 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5978 818c7501 2019-07-11 stsp worktree->base_commit_id);
5980 818c7501 2019-07-11 stsp goto done;
5981 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5983 818c7501 2019-07-11 stsp goto done;
5985 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5987 818c7501 2019-07-11 stsp goto done;
5989 818c7501 2019-07-11 stsp free(fileindex_path);
5990 818c7501 2019-07-11 stsp free(tmp_branch_name);
5991 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5992 818c7501 2019-07-11 stsp free(branch_ref_name);
5993 818c7501 2019-07-11 stsp if (branch_ref)
5994 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5995 818c7501 2019-07-11 stsp if (wt_branch)
5996 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5997 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5998 818c7501 2019-07-11 stsp if (err) {
5999 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6000 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6001 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6003 818c7501 2019-07-11 stsp if (*tmp_branch) {
6004 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6005 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6007 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6008 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6009 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6011 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6013 818c7501 2019-07-11 stsp return err;
6016 818c7501 2019-07-11 stsp const struct got_error *
6017 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6018 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6019 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6020 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6022 818c7501 2019-07-11 stsp const struct got_error *err;
6023 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6024 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6025 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6026 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6027 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6029 818c7501 2019-07-11 stsp *commit_id = NULL;
6030 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6031 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6032 3e3a69f1 2019-07-25 stsp *branch = NULL;
6033 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6035 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6037 3e3a69f1 2019-07-25 stsp return err;
6039 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6041 f032f1f7 2019-08-04 stsp goto done;
6043 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6044 f032f1f7 2019-08-04 stsp &have_staged_files);
6045 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6046 f032f1f7 2019-08-04 stsp goto done;
6047 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6048 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6049 3e3a69f1 2019-07-25 stsp goto done;
6052 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6054 f032f1f7 2019-08-04 stsp goto done;
6056 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6058 818c7501 2019-07-11 stsp goto done;
6060 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6062 818c7501 2019-07-11 stsp goto done;
6064 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6066 818c7501 2019-07-11 stsp goto done;
6068 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6070 818c7501 2019-07-11 stsp goto done;
6072 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6073 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6075 818c7501 2019-07-11 stsp goto done;
6077 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6079 818c7501 2019-07-11 stsp goto done;
6081 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6083 818c7501 2019-07-11 stsp goto done;
6085 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6086 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6088 818c7501 2019-07-11 stsp goto done;
6090 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6092 818c7501 2019-07-11 stsp goto done;
6094 818c7501 2019-07-11 stsp free(commit_ref_name);
6095 818c7501 2019-07-11 stsp free(branch_ref_name);
6096 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6097 818c7501 2019-07-11 stsp if (commit_ref)
6098 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6099 818c7501 2019-07-11 stsp if (branch_ref)
6100 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6101 818c7501 2019-07-11 stsp if (err) {
6102 818c7501 2019-07-11 stsp free(*commit_id);
6103 818c7501 2019-07-11 stsp *commit_id = NULL;
6104 818c7501 2019-07-11 stsp if (*tmp_branch) {
6105 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6106 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6108 818c7501 2019-07-11 stsp if (*new_base_branch) {
6109 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6110 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6112 818c7501 2019-07-11 stsp if (*branch) {
6113 818c7501 2019-07-11 stsp got_ref_close(*branch);
6114 818c7501 2019-07-11 stsp *branch = NULL;
6116 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6117 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6118 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6120 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6122 818c7501 2019-07-11 stsp return err;
6125 818c7501 2019-07-11 stsp const struct got_error *
6126 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6128 818c7501 2019-07-11 stsp const struct got_error *err;
6129 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6131 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6133 818c7501 2019-07-11 stsp return err;
6135 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6136 818c7501 2019-07-11 stsp free(tmp_branch_name);
6137 818c7501 2019-07-11 stsp return NULL;
6140 818c7501 2019-07-11 stsp static const struct got_error *
6141 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6142 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6144 0ebf8283 2019-07-24 stsp *logmsg = arg;
6145 818c7501 2019-07-11 stsp return NULL;
6148 818c7501 2019-07-11 stsp static const struct got_error *
6149 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6150 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6151 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6152 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6154 818c7501 2019-07-11 stsp return NULL;
6157 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6158 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6159 01757395 2019-07-12 stsp void *progress_arg;
6160 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6163 01757395 2019-07-12 stsp static const struct got_error *
6164 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6166 01757395 2019-07-12 stsp const struct got_error *err;
6167 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6169 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6171 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6173 01757395 2019-07-12 stsp return err;
6175 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6176 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6177 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6178 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6179 01757395 2019-07-12 stsp return NULL;
6181 01757395 2019-07-12 stsp p = strdup(path);
6182 01757395 2019-07-12 stsp if (p == NULL)
6183 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6185 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6186 01757395 2019-07-12 stsp if (err || new == NULL)
6188 01757395 2019-07-12 stsp return err;
6192 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6194 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6196 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6197 01757395 2019-07-12 stsp free((char *)pe->path);
6199 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6202 0ebf8283 2019-07-24 stsp static const struct got_error *
6203 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6204 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6206 818c7501 2019-07-11 stsp const struct got_error *err;
6207 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6209 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6210 818c7501 2019-07-11 stsp if (err) {
6211 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6212 818c7501 2019-07-11 stsp goto done;
6213 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6215 818c7501 2019-07-11 stsp goto done;
6216 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6218 818c7501 2019-07-11 stsp goto done;
6219 de05890f 2020-03-05 stsp } else if (is_rebase) {
6220 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6223 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6225 818c7501 2019-07-11 stsp goto done;
6226 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6227 818c7501 2019-07-11 stsp free(stored_id);
6228 818c7501 2019-07-11 stsp if (cmp != 0) {
6229 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6230 818c7501 2019-07-11 stsp goto done;
6234 0ebf8283 2019-07-24 stsp if (commit_ref)
6235 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6236 0ebf8283 2019-07-24 stsp return err;
6239 0ebf8283 2019-07-24 stsp static const struct got_error *
6240 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6241 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6242 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6243 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6244 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6245 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6247 0ebf8283 2019-07-24 stsp const struct got_error *err;
6248 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6249 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6250 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6252 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6254 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6256 0ebf8283 2019-07-24 stsp return err;
6258 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6259 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6260 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6261 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6262 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6263 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6264 818c7501 2019-07-11 stsp if (commit_ref)
6265 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6266 818c7501 2019-07-11 stsp return err;
6269 818c7501 2019-07-11 stsp const struct got_error *
6270 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6271 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6272 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6273 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6274 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6275 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6277 0ebf8283 2019-07-24 stsp const struct got_error *err;
6278 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6280 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6282 0ebf8283 2019-07-24 stsp return err;
6284 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6286 0ebf8283 2019-07-24 stsp goto done;
6288 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6289 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6290 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6292 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6293 0ebf8283 2019-07-24 stsp return err;
6296 0ebf8283 2019-07-24 stsp const struct got_error *
6297 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6298 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6299 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6300 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6301 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6302 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6304 0ebf8283 2019-07-24 stsp const struct got_error *err;
6305 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6307 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6309 0ebf8283 2019-07-24 stsp return err;
6311 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6313 0ebf8283 2019-07-24 stsp goto done;
6315 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6316 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6317 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6319 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6320 0ebf8283 2019-07-24 stsp return err;
6323 0ebf8283 2019-07-24 stsp static const struct got_error *
6324 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6325 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6326 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6327 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6328 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6330 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6331 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6332 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6333 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6334 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6335 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6336 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6338 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6339 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6341 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6343 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6345 3e3a69f1 2019-07-25 stsp return err;
6347 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6348 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6349 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6350 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6352 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6353 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6355 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6356 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6357 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6358 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6359 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6360 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6362 01757395 2019-07-12 stsp if (merged_paths) {
6363 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6364 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6365 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6366 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6369 01757395 2019-07-12 stsp goto done;
6372 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6373 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6375 01757395 2019-07-12 stsp goto done;
6378 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6379 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6380 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6382 a0e95631 2019-07-12 stsp goto done;
6383 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6384 a0e95631 2019-07-12 stsp goto done;
6387 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6389 edd02c5e 2019-07-11 stsp goto done;
6391 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6393 818c7501 2019-07-11 stsp goto done;
6395 5943eee2 2019-08-13 stsp if (new_logmsg) {
6396 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6397 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6398 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6399 5943eee2 2019-08-13 stsp goto done;
6402 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6404 5943eee2 2019-08-13 stsp goto done;
6407 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6408 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6409 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
6410 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6411 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6413 a0e95631 2019-07-12 stsp goto done;
6415 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6417 a0e95631 2019-07-12 stsp goto done;
6419 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6421 a0e95631 2019-07-12 stsp goto done;
6423 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6424 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6425 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6426 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6427 a0e95631 2019-07-12 stsp err = sync_err;
6429 a0e95631 2019-07-12 stsp free(fileindex_path);
6430 a0e95631 2019-07-12 stsp free(head_commit_id);
6431 a0e95631 2019-07-12 stsp if (head_ref)
6432 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6433 818c7501 2019-07-11 stsp if (err) {
6434 818c7501 2019-07-11 stsp free(*new_commit_id);
6435 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6437 818c7501 2019-07-11 stsp return err;
6440 818c7501 2019-07-11 stsp const struct got_error *
6441 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6442 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6443 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6444 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6445 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6447 0ebf8283 2019-07-24 stsp const struct got_error *err;
6448 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6449 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6450 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6452 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6454 0ebf8283 2019-07-24 stsp return err;
6456 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6458 0ebf8283 2019-07-24 stsp goto done;
6459 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6461 0ebf8283 2019-07-24 stsp goto done;
6462 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6463 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6464 0ebf8283 2019-07-24 stsp goto done;
6467 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6468 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6470 0ebf8283 2019-07-24 stsp if (commit_ref)
6471 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6472 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6473 0ebf8283 2019-07-24 stsp free(commit_id);
6474 0ebf8283 2019-07-24 stsp return err;
6477 0ebf8283 2019-07-24 stsp const struct got_error *
6478 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6479 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6480 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6481 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6482 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6483 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6485 0ebf8283 2019-07-24 stsp const struct got_error *err;
6486 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6487 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6489 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6491 0ebf8283 2019-07-24 stsp return err;
6493 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6495 0ebf8283 2019-07-24 stsp goto done;
6497 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6498 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6500 0ebf8283 2019-07-24 stsp if (commit_ref)
6501 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6502 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6503 0ebf8283 2019-07-24 stsp return err;
6506 0ebf8283 2019-07-24 stsp const struct got_error *
6507 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6508 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6510 3e3a69f1 2019-07-25 stsp if (fileindex)
6511 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6512 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6515 69844fba 2019-07-11 stsp static const struct got_error *
6516 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6518 69844fba 2019-07-11 stsp const struct got_error *err;
6519 69844fba 2019-07-11 stsp struct got_reference *ref;
6521 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6522 69844fba 2019-07-11 stsp if (err) {
6523 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6524 69844fba 2019-07-11 stsp return NULL;
6525 69844fba 2019-07-11 stsp return err;
6528 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6529 69844fba 2019-07-11 stsp got_ref_close(ref);
6530 69844fba 2019-07-11 stsp return err;
6533 69844fba 2019-07-11 stsp static const struct got_error *
6534 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6536 69844fba 2019-07-11 stsp const struct got_error *err;
6537 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6538 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6540 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6542 69844fba 2019-07-11 stsp goto done;
6543 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6545 69844fba 2019-07-11 stsp goto done;
6547 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6549 69844fba 2019-07-11 stsp goto done;
6550 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6552 69844fba 2019-07-11 stsp goto done;
6554 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6556 69844fba 2019-07-11 stsp goto done;
6557 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6559 69844fba 2019-07-11 stsp goto done;
6561 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6563 69844fba 2019-07-11 stsp goto done;
6564 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6566 69844fba 2019-07-11 stsp goto done;
6569 69844fba 2019-07-11 stsp free(tmp_branch_name);
6570 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6571 69844fba 2019-07-11 stsp free(branch_ref_name);
6572 69844fba 2019-07-11 stsp free(commit_ref_name);
6573 e600f124 2021-03-21 stsp return err;
6576 e600f124 2021-03-21 stsp const struct got_error *
6577 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6578 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6580 e600f124 2021-03-21 stsp const struct got_error *err;
6581 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6582 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6583 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6584 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6585 e600f124 2021-03-21 stsp char *refname = NULL;
6587 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6588 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6589 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6590 e600f124 2021-03-21 stsp branch_name += 11;
6592 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6594 e600f124 2021-03-21 stsp return err;
6596 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6597 e600f124 2021-03-21 stsp new_id_str) == -1) {
6598 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6599 e600f124 2021-03-21 stsp goto done;
6602 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6604 e600f124 2021-03-21 stsp goto done;
6606 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6608 e600f124 2021-03-21 stsp goto done;
6610 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6612 e600f124 2021-03-21 stsp free(new_id_str);
6613 e600f124 2021-03-21 stsp free(refname);
6614 e600f124 2021-03-21 stsp free(old_commit_id);
6616 e600f124 2021-03-21 stsp got_ref_close(ref);
6617 69844fba 2019-07-11 stsp return err;
6620 818c7501 2019-07-11 stsp const struct got_error *
6621 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6622 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6623 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6624 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6626 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6627 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6628 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6630 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6632 818c7501 2019-07-11 stsp return err;
6634 e600f124 2021-03-21 stsp if (create_backup) {
6635 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6636 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6638 e600f124 2021-03-21 stsp goto done;
6641 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6643 818c7501 2019-07-11 stsp goto done;
6645 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6647 818c7501 2019-07-11 stsp goto done;
6649 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6651 818c7501 2019-07-11 stsp goto done;
6653 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6655 a615e0e7 2020-12-16 stsp goto done;
6657 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6659 a615e0e7 2020-12-16 stsp goto done;
6660 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6661 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6662 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6663 a615e0e7 2020-12-16 stsp err = sync_err;
6665 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6666 a615e0e7 2020-12-16 stsp free(fileindex_path);
6667 818c7501 2019-07-11 stsp free(new_head_commit_id);
6668 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6669 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6670 818c7501 2019-07-11 stsp err = unlockerr;
6671 818c7501 2019-07-11 stsp return err;
6674 818c7501 2019-07-11 stsp const struct got_error *
6675 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6676 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6677 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6678 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
6680 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6681 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6682 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6683 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6684 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6685 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6687 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6689 818c7501 2019-07-11 stsp return err;
6691 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6692 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6694 818c7501 2019-07-11 stsp goto done;
6696 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6698 818c7501 2019-07-11 stsp goto done;
6701 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6702 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6703 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6705 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6707 818c7501 2019-07-11 stsp goto done;
6709 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6711 818c7501 2019-07-11 stsp goto done;
6713 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6714 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6716 ca355955 2019-07-12 stsp goto done;
6718 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6720 ca355955 2019-07-12 stsp goto done;
6722 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6724 818c7501 2019-07-11 stsp goto done;
6726 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6727 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6728 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6729 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6730 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6731 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6732 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6733 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
6734 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6735 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
6737 1f1abb7e 2019-08-08 stsp goto sync;
6739 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6740 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6742 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6743 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6744 ca355955 2019-07-12 stsp err = sync_err;
6746 818c7501 2019-07-11 stsp got_ref_close(resolved);
6747 a3a2faf2 2019-07-12 stsp free(tree_id);
6748 818c7501 2019-07-11 stsp free(commit_id);
6749 0ebf8283 2019-07-24 stsp if (fileindex)
6750 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6751 0ebf8283 2019-07-24 stsp free(fileindex_path);
6753 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6754 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6755 0ebf8283 2019-07-24 stsp err = unlockerr;
6756 0ebf8283 2019-07-24 stsp return err;
6759 0ebf8283 2019-07-24 stsp const struct got_error *
6760 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6761 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6762 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6763 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6765 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6766 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6767 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6768 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6769 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6770 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6771 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6772 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6774 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6775 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6776 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6777 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6779 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6781 0ebf8283 2019-07-24 stsp return err;
6783 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6785 0ebf8283 2019-07-24 stsp goto done;
6787 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6788 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6789 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6792 0ebf8283 2019-07-24 stsp goto done;
6794 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6796 0ebf8283 2019-07-24 stsp goto done;
6798 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6800 0ebf8283 2019-07-24 stsp goto done;
6802 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6803 0ebf8283 2019-07-24 stsp worktree);
6805 0ebf8283 2019-07-24 stsp goto done;
6807 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6810 0ebf8283 2019-07-24 stsp goto done;
6812 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6814 0ebf8283 2019-07-24 stsp goto done;
6816 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6818 0ebf8283 2019-07-24 stsp goto done;
6820 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6821 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6823 0ebf8283 2019-07-24 stsp goto done;
6824 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6826 0ebf8283 2019-07-24 stsp goto done;
6827 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6828 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6829 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6830 0ebf8283 2019-07-24 stsp goto done;
6833 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6834 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6836 0ebf8283 2019-07-24 stsp goto done;
6837 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6839 0ebf8283 2019-07-24 stsp goto done;
6841 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6843 0ebf8283 2019-07-24 stsp goto done;
6845 0ebf8283 2019-07-24 stsp free(fileindex_path);
6846 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6847 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6848 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6849 0ebf8283 2019-07-24 stsp if (wt_branch)
6850 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6851 0ebf8283 2019-07-24 stsp if (err) {
6852 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6853 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6854 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6856 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6857 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6858 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6860 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6861 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6862 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6863 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6865 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6867 0ebf8283 2019-07-24 stsp return err;
6870 0ebf8283 2019-07-24 stsp const struct got_error *
6871 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6872 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6874 3e3a69f1 2019-07-25 stsp if (fileindex)
6875 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6876 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6879 0ebf8283 2019-07-24 stsp const struct got_error *
6880 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6881 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6883 0ebf8283 2019-07-24 stsp const struct got_error *err;
6884 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6886 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6888 0ebf8283 2019-07-24 stsp return err;
6890 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6891 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6892 0ebf8283 2019-07-24 stsp return NULL;
6895 0ebf8283 2019-07-24 stsp const struct got_error *
6896 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6897 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6898 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6899 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6901 0ebf8283 2019-07-24 stsp const struct got_error *err;
6902 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6903 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6904 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6905 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6906 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6907 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6909 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6910 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6911 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6912 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6914 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6916 3e3a69f1 2019-07-25 stsp return err;
6918 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6920 f032f1f7 2019-08-04 stsp goto done;
6922 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6923 f032f1f7 2019-08-04 stsp &have_staged_files);
6924 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6925 f032f1f7 2019-08-04 stsp goto done;
6926 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6927 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6928 3e3a69f1 2019-07-25 stsp goto done;
6931 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6933 f032f1f7 2019-08-04 stsp goto done;
6935 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6937 0ebf8283 2019-07-24 stsp goto done;
6939 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6941 0ebf8283 2019-07-24 stsp goto done;
6943 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6944 0ebf8283 2019-07-24 stsp worktree);
6946 0ebf8283 2019-07-24 stsp goto done;
6948 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6950 0ebf8283 2019-07-24 stsp goto done;
6952 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6954 0ebf8283 2019-07-24 stsp goto done;
6955 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6957 0ebf8283 2019-07-24 stsp goto done;
6959 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6961 0ebf8283 2019-07-24 stsp goto done;
6962 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6964 0ebf8283 2019-07-24 stsp goto done;
6966 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6968 0ebf8283 2019-07-24 stsp goto done;
6970 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6971 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6972 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6973 0ebf8283 2019-07-24 stsp if (commit_ref)
6974 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6975 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6976 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6977 0ebf8283 2019-07-24 stsp if (err) {
6978 0ebf8283 2019-07-24 stsp free(*commit_id);
6979 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6980 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6981 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6982 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6983 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6984 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6986 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6987 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6988 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6990 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6992 0ebf8283 2019-07-24 stsp return err;
6995 0ebf8283 2019-07-24 stsp static const struct got_error *
6996 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6998 0ebf8283 2019-07-24 stsp const struct got_error *err;
6999 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7000 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7002 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7004 0ebf8283 2019-07-24 stsp goto done;
7005 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7007 0ebf8283 2019-07-24 stsp goto done;
7009 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7010 0ebf8283 2019-07-24 stsp worktree);
7012 0ebf8283 2019-07-24 stsp goto done;
7013 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7015 0ebf8283 2019-07-24 stsp goto done;
7017 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7019 0ebf8283 2019-07-24 stsp goto done;
7020 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7022 0ebf8283 2019-07-24 stsp goto done;
7024 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7026 0ebf8283 2019-07-24 stsp goto done;
7027 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7029 0ebf8283 2019-07-24 stsp goto done;
7031 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7032 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7033 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7034 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7035 0ebf8283 2019-07-24 stsp return err;
7038 0ebf8283 2019-07-24 stsp const struct got_error *
7039 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7040 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7041 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7042 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7044 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7045 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7046 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7047 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7048 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7050 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7052 0ebf8283 2019-07-24 stsp return err;
7054 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7055 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7057 0ebf8283 2019-07-24 stsp goto done;
7059 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7061 0ebf8283 2019-07-24 stsp goto done;
7063 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7065 0ebf8283 2019-07-24 stsp goto done;
7067 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7068 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7070 0ebf8283 2019-07-24 stsp goto done;
7072 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7074 0ebf8283 2019-07-24 stsp goto done;
7076 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7078 0ebf8283 2019-07-24 stsp goto done;
7080 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7081 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7082 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7083 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7084 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7085 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7086 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7087 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7088 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7089 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7091 1f1abb7e 2019-08-08 stsp goto sync;
7093 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7094 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7096 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7097 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7098 0ebf8283 2019-07-24 stsp err = sync_err;
7100 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7101 0ebf8283 2019-07-24 stsp free(tree_id);
7102 818c7501 2019-07-11 stsp free(fileindex_path);
7104 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7105 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7106 818c7501 2019-07-11 stsp err = unlockerr;
7107 818c7501 2019-07-11 stsp return err;
7110 0ebf8283 2019-07-24 stsp const struct got_error *
7111 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7112 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7113 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7115 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7116 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7117 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7118 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7120 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7122 0ebf8283 2019-07-24 stsp return err;
7124 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7125 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7127 e600f124 2021-03-21 stsp goto done;
7129 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7130 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7132 0ebf8283 2019-07-24 stsp goto done;
7134 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7136 0ebf8283 2019-07-24 stsp goto done;
7138 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7140 0ebf8283 2019-07-24 stsp goto done;
7142 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7144 0ebf8283 2019-07-24 stsp goto done;
7146 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7148 a615e0e7 2020-12-16 stsp goto done;
7150 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7152 a615e0e7 2020-12-16 stsp goto done;
7153 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7154 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7155 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7156 a615e0e7 2020-12-16 stsp err = sync_err;
7158 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7159 a615e0e7 2020-12-16 stsp free(fileindex_path);
7160 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7161 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7162 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7163 0ebf8283 2019-07-24 stsp err = unlockerr;
7164 0ebf8283 2019-07-24 stsp return err;
7167 0ebf8283 2019-07-24 stsp const struct got_error *
7168 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7169 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7171 0ebf8283 2019-07-24 stsp const struct got_error *err;
7172 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7174 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7176 0ebf8283 2019-07-24 stsp return err;
7178 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7180 0ebf8283 2019-07-24 stsp goto done;
7182 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7184 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7185 2822a352 2019-10-15 stsp return err;
7188 2822a352 2019-10-15 stsp const struct got_error *
7189 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7190 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7191 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7192 2822a352 2019-10-15 stsp struct got_repository *repo)
7194 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7195 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7196 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7198 2822a352 2019-10-15 stsp *fileindex = NULL;
7199 2822a352 2019-10-15 stsp *branch_ref = NULL;
7200 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7202 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7204 2822a352 2019-10-15 stsp return err;
7206 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7207 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7208 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7209 2822a352 2019-10-15 stsp "update -b or different branch name required");
7210 2822a352 2019-10-15 stsp goto done;
7213 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7215 2822a352 2019-10-15 stsp goto done;
7217 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7218 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7219 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7220 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7223 2822a352 2019-10-15 stsp goto done;
7225 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7227 2822a352 2019-10-15 stsp goto done;
7229 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7230 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7232 2822a352 2019-10-15 stsp if (err) {
7233 2822a352 2019-10-15 stsp if (*branch_ref) {
7234 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7235 2822a352 2019-10-15 stsp *branch_ref = NULL;
7237 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7238 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7239 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7241 2822a352 2019-10-15 stsp if (*fileindex) {
7242 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7243 2822a352 2019-10-15 stsp *fileindex = NULL;
7245 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7247 0cb83759 2019-08-03 stsp return err;
7250 2822a352 2019-10-15 stsp const struct got_error *
7251 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7252 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7253 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7254 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7255 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7257 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7258 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7259 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7261 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7263 2822a352 2019-10-15 stsp goto done;
7265 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7267 2822a352 2019-10-15 stsp goto done;
7269 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7270 2822a352 2019-10-15 stsp worktree->path_prefix);
7272 2822a352 2019-10-15 stsp goto done;
7274 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7276 2822a352 2019-10-15 stsp goto done;
7278 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7279 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7281 2822a352 2019-10-15 stsp goto sync;
7283 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7285 2822a352 2019-10-15 stsp goto sync;
7287 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7289 6e210706 2021-01-22 stsp goto sync;
7291 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7293 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7294 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7295 2822a352 2019-10-15 stsp err = sync_err;
7298 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7299 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7300 2822a352 2019-10-15 stsp err = unlockerr;
7301 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7303 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7304 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7305 2822a352 2019-10-15 stsp err = unlockerr;
7306 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7308 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7309 2822a352 2019-10-15 stsp free(fileindex_path);
7310 2822a352 2019-10-15 stsp free(tree_id);
7312 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7313 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7314 2822a352 2019-10-15 stsp err = unlockerr;
7315 2822a352 2019-10-15 stsp return err;
7318 2822a352 2019-10-15 stsp const struct got_error *
7319 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7320 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7321 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7323 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7325 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7327 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7329 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7330 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7331 8b692cd0 2019-10-21 stsp err = unlockerr;
7332 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7334 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7335 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7336 8b692cd0 2019-10-21 stsp err = unlockerr;
7337 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7339 10604dce 2021-09-24 thomas return err;
7342 10604dce 2021-09-24 thomas const struct got_error *
7343 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
7344 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
7346 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
7347 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7349 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7350 10604dce 2021-09-24 thomas if (err)
7351 10604dce 2021-09-24 thomas goto done;
7353 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7355 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
7356 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7357 10604dce 2021-09-24 thomas err = sync_err;
7359 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7360 10604dce 2021-09-24 thomas free(fileindex_path);
7361 8b692cd0 2019-10-21 stsp return err;
7364 10604dce 2021-09-24 thomas static const struct got_error *
7365 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7367 10604dce 2021-09-24 thomas const struct got_error *err;
7368 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7370 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7371 10604dce 2021-09-24 thomas if (err)
7372 10604dce 2021-09-24 thomas goto done;
7373 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
7374 10604dce 2021-09-24 thomas if (err)
7375 10604dce 2021-09-24 thomas goto done;
7377 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7378 10604dce 2021-09-24 thomas if (err)
7379 10604dce 2021-09-24 thomas goto done;
7380 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
7381 10604dce 2021-09-24 thomas if (err)
7382 10604dce 2021-09-24 thomas goto done;
7385 10604dce 2021-09-24 thomas free(branch_refname);
7386 10604dce 2021-09-24 thomas free(commit_refname);
7387 10604dce 2021-09-24 thomas return err;
7390 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
7391 10604dce 2021-09-24 thomas struct got_worktree *worktree;
7392 10604dce 2021-09-24 thomas const char *branch_name;
7395 10604dce 2021-09-24 thomas static const struct got_error *
7396 10604dce 2021-09-24 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7397 10604dce 2021-09-24 thomas void *arg)
7399 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
7401 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7402 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
7403 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
7405 10604dce 2021-09-24 thomas return NULL;
7409 10604dce 2021-09-24 thomas const struct got_error *
7410 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
7411 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
7412 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
7413 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
7414 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7415 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7417 10604dce 2021-09-24 thomas const struct got_error *err;
7418 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7420 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7421 10604dce 2021-09-24 thomas if (err)
7422 10604dce 2021-09-24 thomas goto done;
7424 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7425 10604dce 2021-09-24 thomas worktree);
7426 10604dce 2021-09-24 thomas if (err)
7427 10604dce 2021-09-24 thomas goto done;
7429 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7430 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
7431 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
7433 10604dce 2021-09-24 thomas free(fileindex_path);
7434 10604dce 2021-09-24 thomas return err;
7437 10604dce 2021-09-24 thomas const struct got_error *
7438 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
7439 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
7440 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
7441 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
7442 ae1e948a 2021-09-28 thomas struct got_repository *repo,
7443 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
7446 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
7447 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
7448 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
7449 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
7450 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
7451 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
7452 10604dce 2021-09-24 thomas int have_staged_files = 0;
7453 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
7454 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7456 10604dce 2021-09-24 thomas *new_commit_id = NULL;
7458 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
7460 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7461 10604dce 2021-09-24 thomas if (err)
7462 10604dce 2021-09-24 thomas goto done;
7464 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7465 10604dce 2021-09-24 thomas if (err)
7466 10604dce 2021-09-24 thomas goto done;
7468 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
7469 10604dce 2021-09-24 thomas if (err)
7470 10604dce 2021-09-24 thomas goto done;
7472 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7473 10604dce 2021-09-24 thomas &have_staged_files);
7474 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7475 10604dce 2021-09-24 thomas goto done;
7476 10604dce 2021-09-24 thomas if (have_staged_files) {
7477 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7478 10604dce 2021-09-24 thomas goto done;
7481 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
7482 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
7483 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
7484 10604dce 2021-09-24 thomas cc_arg.repo = repo;
7485 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
7486 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7487 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7488 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7489 10604dce 2021-09-24 thomas if (err)
7490 10604dce 2021-09-24 thomas goto done;
7492 10604dce 2021-09-24 thomas if (TAILQ_EMPTY(&commitable_paths)) {
7493 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7494 10604dce 2021-09-24 thomas "merge of %s cannot proceed", branch_name);
7495 10604dce 2021-09-24 thomas goto done;
7498 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7499 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7500 10604dce 2021-09-24 thomas const char *ct_path = ct->in_repo_path;
7502 10604dce 2021-09-24 thomas while (ct_path[0] == '/')
7503 10604dce 2021-09-24 thomas ct_path++;
7504 10604dce 2021-09-24 thomas err = check_out_of_date(ct_path, ct->status,
7505 10604dce 2021-09-24 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7506 10604dce 2021-09-24 thomas head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7507 10604dce 2021-09-24 thomas if (err)
7508 10604dce 2021-09-24 thomas goto done;
7512 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
7513 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
7514 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
7515 10604dce 2021-09-24 thomas head_commit_id, branch_tip, worktree, author, committer,
7516 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7517 10604dce 2021-09-24 thomas if (err)
7518 10604dce 2021-09-24 thomas goto done;
7520 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
7521 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
7522 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7523 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7524 10604dce 2021-09-24 thomas err = sync_err;
7526 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7527 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7528 10604dce 2021-09-24 thomas free_commitable(ct);
7530 10604dce 2021-09-24 thomas got_pathlist_free(&commitable_paths);
7531 10604dce 2021-09-24 thomas free(fileindex_path);
7532 10604dce 2021-09-24 thomas return err;
7535 10604dce 2021-09-24 thomas const struct got_error *
7536 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
7537 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
7539 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
7540 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7542 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
7543 10604dce 2021-09-24 thomas if (err)
7544 10604dce 2021-09-24 thomas goto done;
7546 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7547 10604dce 2021-09-24 thomas if (err)
7548 10604dce 2021-09-24 thomas goto done;
7549 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7550 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7551 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7552 10604dce 2021-09-24 thomas err = sync_err;
7554 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7555 10604dce 2021-09-24 thomas free(fileindex_path);
7556 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
7557 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
7558 10604dce 2021-09-24 thomas err = unlockerr;
7559 10604dce 2021-09-24 thomas return err;
7562 10604dce 2021-09-24 thomas const struct got_error *
7563 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7564 10604dce 2021-09-24 thomas struct got_repository *repo)
7566 10604dce 2021-09-24 thomas const struct got_error *err;
7567 10604dce 2021-09-24 thomas char *branch_refname = NULL;
7568 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
7570 10604dce 2021-09-24 thomas *in_progress = 0;
7572 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7573 10604dce 2021-09-24 thomas if (err)
7574 10604dce 2021-09-24 thomas return err;
7575 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7576 dfe86d1f 2021-09-24 thomas free(branch_refname);
7577 10604dce 2021-09-24 thomas if (err) {
7578 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
7579 10604dce 2021-09-24 thomas return err;
7581 10604dce 2021-09-24 thomas *in_progress = 1;
7583 10604dce 2021-09-24 thomas return NULL;
7586 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
7587 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
7588 10604dce 2021-09-24 thomas struct got_reference *branch, struct got_repository *repo)
7590 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
7591 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7592 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7593 10604dce 2021-09-24 thomas struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7594 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL;
7595 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7596 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
7598 10604dce 2021-09-24 thomas *fileindex = NULL;
7600 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
7601 10604dce 2021-09-24 thomas if (err)
7602 10604dce 2021-09-24 thomas return err;
7604 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
7605 10604dce 2021-09-24 thomas if (err)
7606 10604dce 2021-09-24 thomas goto done;
7608 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
7609 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
7610 10604dce 2021-09-24 thomas ok_arg.repo = repo;
7611 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7612 10604dce 2021-09-24 thomas &ok_arg);
7613 10604dce 2021-09-24 thomas if (err)
7614 10604dce 2021-09-24 thomas goto done;
7616 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7617 10604dce 2021-09-24 thomas if (err)
7618 10604dce 2021-09-24 thomas return err;
7620 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7621 10604dce 2021-09-24 thomas if (err)
7622 10604dce 2021-09-24 thomas return err;
7624 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7626 10604dce 2021-09-24 thomas if (err)
7627 10604dce 2021-09-24 thomas goto done;
7629 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7630 10604dce 2021-09-24 thomas if (err)
7631 10604dce 2021-09-24 thomas goto done;
7633 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7634 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7635 10604dce 2021-09-24 thomas goto done;
7638 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
7639 10604dce 2021-09-24 thomas if (err)
7640 10604dce 2021-09-24 thomas goto done;
7642 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7643 10604dce 2021-09-24 thomas if (err)
7644 10604dce 2021-09-24 thomas goto done;
7645 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
7646 10604dce 2021-09-24 thomas if (err)
7647 10604dce 2021-09-24 thomas goto done;
7649 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7650 10604dce 2021-09-24 thomas if (err)
7651 10604dce 2021-09-24 thomas goto done;
7652 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
7653 10604dce 2021-09-24 thomas if (err)
7654 10604dce 2021-09-24 thomas goto done;
7657 10604dce 2021-09-24 thomas free(branch_refname);
7658 10604dce 2021-09-24 thomas free(commit_refname);
7659 10604dce 2021-09-24 thomas free(fileindex_path);
7660 10604dce 2021-09-24 thomas if (branch_ref)
7661 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
7662 10604dce 2021-09-24 thomas if (commit_ref)
7663 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
7664 10604dce 2021-09-24 thomas if (wt_branch)
7665 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
7666 10604dce 2021-09-24 thomas free(wt_branch_tip);
7667 10604dce 2021-09-24 thomas if (err) {
7668 10604dce 2021-09-24 thomas if (*fileindex) {
7669 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
7670 10604dce 2021-09-24 thomas *fileindex = NULL;
7672 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
7674 10604dce 2021-09-24 thomas return err;
7677 10604dce 2021-09-24 thomas const struct got_error *
7678 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
7679 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7680 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
7682 10604dce 2021-09-24 thomas const struct got_error *err;
7683 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
7684 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7685 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7686 10604dce 2021-09-24 thomas int have_staged_files = 0;
7688 10604dce 2021-09-24 thomas *branch_name = NULL;
7689 10604dce 2021-09-24 thomas *branch_tip = NULL;
7690 10604dce 2021-09-24 thomas *fileindex = NULL;
7692 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
7693 10604dce 2021-09-24 thomas if (err)
7694 10604dce 2021-09-24 thomas return err;
7696 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
7697 10604dce 2021-09-24 thomas if (err)
7698 10604dce 2021-09-24 thomas goto done;
7700 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7701 10604dce 2021-09-24 thomas &have_staged_files);
7702 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7703 10604dce 2021-09-24 thomas goto done;
7704 10604dce 2021-09-24 thomas if (have_staged_files) {
7705 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
7706 10604dce 2021-09-24 thomas goto done;
7709 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7710 10604dce 2021-09-24 thomas if (err)
7711 10604dce 2021-09-24 thomas goto done;
7713 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7714 10604dce 2021-09-24 thomas if (err)
7715 10604dce 2021-09-24 thomas goto done;
7717 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7718 10604dce 2021-09-24 thomas if (err)
7719 10604dce 2021-09-24 thomas goto done;
7721 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
7722 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7723 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
7724 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
7725 10604dce 2021-09-24 thomas goto done;
7727 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7728 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
7729 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
7730 10604dce 2021-09-24 thomas goto done;
7733 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7734 10604dce 2021-09-24 thomas if (err)
7735 10604dce 2021-09-24 thomas goto done;
7737 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
7738 10604dce 2021-09-24 thomas if (err)
7739 10604dce 2021-09-24 thomas goto done;
7741 10604dce 2021-09-24 thomas free(commit_refname);
7742 10604dce 2021-09-24 thomas free(branch_refname);
7743 10604dce 2021-09-24 thomas free(fileindex_path);
7744 10604dce 2021-09-24 thomas if (commit_ref)
7745 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
7746 10604dce 2021-09-24 thomas if (branch_ref)
7747 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
7748 10604dce 2021-09-24 thomas if (err) {
7749 10604dce 2021-09-24 thomas if (*branch_name) {
7750 10604dce 2021-09-24 thomas free(*branch_name);
7751 10604dce 2021-09-24 thomas *branch_name = NULL;
7753 10604dce 2021-09-24 thomas free(*branch_tip);
7754 10604dce 2021-09-24 thomas *branch_tip = NULL;
7755 10604dce 2021-09-24 thomas if (*fileindex) {
7756 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
7757 10604dce 2021-09-24 thomas *fileindex = NULL;
7759 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
7761 10604dce 2021-09-24 thomas return err;
7764 10604dce 2021-09-24 thomas const struct got_error *
7765 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
7766 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
7767 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
7769 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
7770 10604dce 2021-09-24 thomas struct got_object_id *commit_id = NULL;
7771 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7772 10604dce 2021-09-24 thomas struct revert_file_args rfa;
7773 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
7775 10604dce 2021-09-24 thomas err = got_object_id_by_path(&tree_id, repo,
7776 10604dce 2021-09-24 thomas worktree->base_commit_id, worktree->path_prefix);
7777 10604dce 2021-09-24 thomas if (err)
7778 10604dce 2021-09-24 thomas goto done;
7780 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
7781 10604dce 2021-09-24 thomas if (err)
7782 10604dce 2021-09-24 thomas goto done;
7784 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7785 10604dce 2021-09-24 thomas if (err)
7786 10604dce 2021-09-24 thomas goto done;
7788 10604dce 2021-09-24 thomas rfa.worktree = worktree;
7789 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
7790 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
7791 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
7792 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
7793 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
7794 10604dce 2021-09-24 thomas rfa.repo = repo;
7795 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
7796 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7797 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7798 10604dce 2021-09-24 thomas if (err)
7799 10604dce 2021-09-24 thomas goto sync;
7801 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7802 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
7804 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7805 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7806 10604dce 2021-09-24 thomas err = sync_err;
7808 10604dce 2021-09-24 thomas free(tree_id);
7809 10604dce 2021-09-24 thomas free(commit_id);
7810 10604dce 2021-09-24 thomas if (fileindex)
7811 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7812 10604dce 2021-09-24 thomas free(fileindex_path);
7814 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
7815 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
7816 10604dce 2021-09-24 thomas err = unlockerr;
7817 10604dce 2021-09-24 thomas return err;
7820 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7821 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7822 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7823 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7824 2db2652d 2019-08-07 stsp struct got_repository *repo;
7825 2db2652d 2019-08-07 stsp int have_changes;
7828 2db2652d 2019-08-07 stsp const struct got_error *
7829 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7830 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7831 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7832 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7834 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7835 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7836 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7837 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7838 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7839 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7841 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7842 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7843 8b13ce36 2019-08-08 stsp return NULL;
7844 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7845 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7847 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7848 735ef5ac 2019-08-03 stsp if (ie == NULL)
7849 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7851 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7852 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7853 735ef5ac 2019-08-03 stsp relpath) == -1)
7854 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7856 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7857 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7858 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7859 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7862 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7863 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7864 3aa5969e 2019-08-06 stsp goto done;
7865 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7866 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7867 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7868 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7869 735ef5ac 2019-08-03 stsp goto done;
7872 2db2652d 2019-08-07 stsp a->have_changes = 1;
7874 735ef5ac 2019-08-03 stsp p = in_repo_path;
7875 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7877 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7878 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7879 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7881 735ef5ac 2019-08-03 stsp free(in_repo_path);
7882 dc424a06 2019-08-07 stsp return err;
7885 2db2652d 2019-08-07 stsp struct stage_path_arg {
7886 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7887 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7888 2db2652d 2019-08-07 stsp struct got_repository *repo;
7889 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7890 2db2652d 2019-08-07 stsp void *status_arg;
7891 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7892 2db2652d 2019-08-07 stsp void *patch_arg;
7893 7b5dc508 2019-10-28 stsp int staged_something;
7894 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7897 2db2652d 2019-08-07 stsp static const struct got_error *
7898 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7899 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7900 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7901 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7903 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7904 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7905 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7906 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7907 0cb83759 2019-08-03 stsp uint32_t stage;
7908 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7909 0aeb8099 2020-07-23 stsp struct stat sb;
7911 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7912 8b13ce36 2019-08-08 stsp return NULL;
7914 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7915 d3e7c587 2019-08-03 stsp if (ie == NULL)
7916 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7918 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7919 2db2652d 2019-08-07 stsp relpath)== -1)
7920 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7922 0cb83759 2019-08-03 stsp switch (status) {
7923 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7924 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7925 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7926 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7927 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7930 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7931 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7932 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7933 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7934 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7937 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7940 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7941 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7942 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7943 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7944 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7948 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7949 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7952 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7953 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7954 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7955 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7957 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7958 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7959 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7960 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7961 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7962 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7963 35213c7c 2020-07-23 stsp ssize_t target_len;
7964 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7965 35213c7c 2020-07-23 stsp sizeof(target_path));
7966 35213c7c 2020-07-23 stsp if (target_len == -1) {
7967 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7968 35213c7c 2020-07-23 stsp ondisk_path);
7971 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7972 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7973 35213c7c 2020-07-23 stsp a->worktree->root_path);
7976 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7977 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7978 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7982 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7983 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7984 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7986 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7987 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7989 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7990 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7992 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7993 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7995 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7996 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7997 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7999 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8000 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8002 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8003 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8004 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8005 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8008 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8010 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8011 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8015 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8016 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8017 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8018 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8020 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8021 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8024 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8026 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8027 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8029 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8030 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8033 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8037 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8038 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8039 dc424a06 2019-08-07 stsp free(path_content);
8040 2db2652d 2019-08-07 stsp free(ondisk_path);
8041 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8042 0ebf8283 2019-07-24 stsp return err;
8045 0cb83759 2019-08-03 stsp const struct got_error *
8046 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8047 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8048 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8049 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8050 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8052 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8053 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8054 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8055 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8056 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8057 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8058 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8059 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8061 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8063 0cb83759 2019-08-03 stsp return err;
8065 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8066 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8068 735ef5ac 2019-08-03 stsp goto done;
8069 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8071 735ef5ac 2019-08-03 stsp goto done;
8072 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8074 0cb83759 2019-08-03 stsp goto done;
8076 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8077 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8078 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8079 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8080 2db2652d 2019-08-07 stsp oka.repo = repo;
8081 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8082 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8083 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8084 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
8086 735ef5ac 2019-08-03 stsp goto done;
8088 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8089 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8090 2db2652d 2019-08-07 stsp goto done;
8093 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8094 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8095 2db2652d 2019-08-07 stsp spa.repo = repo;
8096 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8097 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8098 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8099 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8100 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8101 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8102 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8103 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8104 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
8106 2db2652d 2019-08-07 stsp goto done;
8108 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8109 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8110 7b5dc508 2019-10-28 stsp goto done;
8113 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8114 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8115 0cb83759 2019-08-03 stsp err = sync_err;
8117 735ef5ac 2019-08-03 stsp if (head_ref)
8118 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8119 735ef5ac 2019-08-03 stsp free(head_commit_id);
8120 0cb83759 2019-08-03 stsp free(fileindex_path);
8121 0cb83759 2019-08-03 stsp if (fileindex)
8122 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8123 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8124 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8125 0cb83759 2019-08-03 stsp err = unlockerr;
8126 0cb83759 2019-08-03 stsp return err;
8129 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8130 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8131 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8132 ad493afc 2019-08-03 stsp struct got_repository *repo;
8133 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8134 ad493afc 2019-08-03 stsp void *progress_arg;
8135 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8136 2e1f37b0 2019-08-08 stsp void *patch_arg;
8139 2e1f37b0 2019-08-08 stsp static const struct got_error *
8140 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8141 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8142 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8143 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8144 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8146 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8147 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8148 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8149 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8150 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8151 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8152 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8154 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8155 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8157 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8159 2e1f37b0 2019-08-08 stsp return err;
8160 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8162 2e1f37b0 2019-08-08 stsp goto done;
8164 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8166 2e1f37b0 2019-08-08 stsp goto done;
8168 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8170 2e1f37b0 2019-08-08 stsp goto done;
8172 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8174 2e1f37b0 2019-08-08 stsp goto done;
8176 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8178 2e1f37b0 2019-08-08 stsp goto done;
8180 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8182 2e1f37b0 2019-08-08 stsp goto done;
8184 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
8185 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
8187 2e1f37b0 2019-08-08 stsp goto done;
8189 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8190 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
8192 2e1f37b0 2019-08-08 stsp goto done;
8193 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8194 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
8196 2e1f37b0 2019-08-08 stsp goto done;
8198 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8199 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8200 2e1f37b0 2019-08-08 stsp goto done;
8202 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8203 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8204 2e1f37b0 2019-08-08 stsp goto done;
8206 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8207 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8208 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
8209 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8210 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8211 fe621944 2020-11-10 stsp nchanges++;
8213 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8214 2e1f37b0 2019-08-08 stsp int choice;
8215 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8216 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8217 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8218 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8220 2e1f37b0 2019-08-08 stsp goto done;
8221 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8222 2e1f37b0 2019-08-08 stsp have_content = 1;
8224 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8225 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8228 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8229 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8230 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8232 2e1f37b0 2019-08-08 stsp free(label1);
8234 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8235 2e1f37b0 2019-08-08 stsp if (staged_blob)
8236 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8237 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8238 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8239 fe621944 2020-11-10 stsp err = free_err;
8240 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8241 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8242 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8243 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8244 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8245 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8246 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8247 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8248 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8249 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8250 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8251 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8252 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8253 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8254 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8255 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8256 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8257 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8258 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8260 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8261 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8262 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8263 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8264 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8265 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8266 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8268 2e1f37b0 2019-08-08 stsp free(path1);
8269 2e1f37b0 2019-08-08 stsp free(path2);
8270 fda8017d 2020-07-23 stsp return err;
8273 fda8017d 2020-07-23 stsp static const struct got_error *
8274 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8275 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8276 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8277 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8278 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8279 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8280 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8282 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8283 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8284 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8285 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8286 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8287 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8288 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8289 fda8017d 2020-07-23 stsp struct stat sb;
8291 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8292 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8293 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8295 fda8017d 2020-07-23 stsp return err;
8297 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8298 fda8017d 2020-07-23 stsp return NULL;
8300 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8301 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8302 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8304 fda8017d 2020-07-23 stsp goto done;
8307 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
8308 fda8017d 2020-07-23 stsp if (f == NULL) {
8309 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8310 fda8017d 2020-07-23 stsp path_unstaged_content);
8311 fda8017d 2020-07-23 stsp goto done;
8313 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8314 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8315 fda8017d 2020-07-23 stsp goto done;
8317 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8318 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8319 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8321 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8322 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8323 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8324 fda8017d 2020-07-23 stsp goto done;
8326 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8327 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8328 fda8017d 2020-07-23 stsp goto done;
8330 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8331 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8332 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8333 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8334 fda8017d 2020-07-23 stsp progress_arg);
8336 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8338 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8340 67a66647 2021-05-31 stsp return err;
8342 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8343 67a66647 2021-05-31 stsp parent) == -1) {
8344 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8345 67a66647 2021-05-31 stsp base_path = NULL;
8346 67a66647 2021-05-31 stsp goto done;
8349 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8351 67a66647 2021-05-31 stsp goto done;
8352 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8353 67a66647 2021-05-31 stsp blob_base);
8355 67a66647 2021-05-31 stsp goto done;
8358 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8359 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8361 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8362 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8363 eec2f5a9 2021-06-03 stsp ondisk_path);
8365 eec2f5a9 2021-06-03 stsp goto done;
8368 06340621 2021-12-31 thomas fd = open(ondisk_path,
8369 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8370 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8371 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8372 eec2f5a9 2021-06-03 stsp goto done;
8374 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8375 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8376 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8377 eec2f5a9 2021-06-03 stsp close(fd);
8378 eec2f5a9 2021-06-03 stsp goto done;
8382 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8383 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8384 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8385 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8386 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8389 fda8017d 2020-07-23 stsp goto done;
8391 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8392 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8393 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8395 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8396 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8399 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8400 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8401 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8402 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8403 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8404 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8405 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8406 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8407 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8408 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8409 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8410 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8411 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8412 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8413 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8414 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8415 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8416 67a66647 2021-05-31 stsp free(blob_base_path);
8417 67a66647 2021-05-31 stsp free(parent);
8418 67a66647 2021-05-31 stsp free(base_path);
8419 2e1f37b0 2019-08-08 stsp return err;
8422 ad493afc 2019-08-03 stsp static const struct got_error *
8423 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8424 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8425 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8426 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8428 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8429 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8430 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8431 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8432 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8433 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8434 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8435 9bc94a15 2019-08-03 stsp struct stat sb;
8437 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8438 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8439 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8440 2e1f37b0 2019-08-08 stsp return NULL;
8442 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8443 ad493afc 2019-08-03 stsp if (ie == NULL)
8444 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8446 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8448 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8450 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8451 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8453 f69721c3 2019-10-21 stsp goto done;
8454 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8455 f69721c3 2019-10-21 stsp id_str) == -1) {
8456 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8457 f69721c3 2019-10-21 stsp goto done;
8460 ad493afc 2019-08-03 stsp switch (staged_status) {
8461 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8462 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8463 ad493afc 2019-08-03 stsp blob_id, 8192);
8466 ad493afc 2019-08-03 stsp /* fall through */
8467 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8468 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8469 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8470 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8471 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8472 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8475 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8478 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8479 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8480 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8481 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8482 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8483 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8486 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8487 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
8490 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8491 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8492 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8493 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8494 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8495 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8496 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8497 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8498 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8500 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8501 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8502 36bf999c 2020-07-23 stsp char *staged_target;
8503 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8504 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8506 36bf999c 2020-07-23 stsp goto done;
8507 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8508 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8509 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8510 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8511 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8512 36bf999c 2020-07-23 stsp free(staged_target);
8514 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8515 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8516 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8517 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8518 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8519 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8520 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8524 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8527 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8528 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8529 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8530 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8533 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8534 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8535 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8536 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8537 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8540 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8542 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8543 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8547 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8548 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8549 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8550 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8553 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8557 ad493afc 2019-08-03 stsp free(ondisk_path);
8558 ad493afc 2019-08-03 stsp if (blob_base)
8559 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8560 ad493afc 2019-08-03 stsp if (blob_staged)
8561 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8562 f69721c3 2019-10-21 stsp free(id_str);
8563 f69721c3 2019-10-21 stsp free(label_orig);
8564 ad493afc 2019-08-03 stsp return err;
8567 ad493afc 2019-08-03 stsp const struct got_error *
8568 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8569 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8570 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8571 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8572 ad493afc 2019-08-03 stsp struct got_repository *repo)
8574 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8575 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8576 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8577 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8578 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8580 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8582 ad493afc 2019-08-03 stsp return err;
8584 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8586 ad493afc 2019-08-03 stsp goto done;
8588 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8589 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8590 ad493afc 2019-08-03 stsp upa.repo = repo;
8591 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8592 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8593 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8594 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8595 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8596 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8597 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
8599 ad493afc 2019-08-03 stsp goto done;
8602 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8603 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8604 ad493afc 2019-08-03 stsp err = sync_err;
8606 ad493afc 2019-08-03 stsp free(fileindex_path);
8607 ad493afc 2019-08-03 stsp if (fileindex)
8608 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8609 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8610 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8611 ad493afc 2019-08-03 stsp err = unlockerr;
8612 ad493afc 2019-08-03 stsp return err;
8615 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8616 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8617 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8618 b2118c49 2020-07-28 stsp void *info_arg;
8619 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8620 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8621 b2118c49 2020-07-28 stsp void *cancel_arg;
8624 b2118c49 2020-07-28 stsp static const struct got_error *
8625 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8627 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8628 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8629 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8630 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8631 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8632 b2118c49 2020-07-28 stsp int stage;
8634 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8635 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8637 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8638 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8639 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8642 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8643 b2118c49 2020-07-28 stsp return NULL;
8645 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8646 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8647 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8649 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8650 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8651 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8652 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8653 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8654 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8657 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8658 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8659 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8662 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8663 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8666 b2118c49 2020-07-28 stsp const struct got_error *
8667 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8668 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8669 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8670 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8673 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8674 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8675 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8676 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8678 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8680 b2118c49 2020-07-28 stsp return err;
8682 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8684 b2118c49 2020-07-28 stsp goto done;
8686 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8687 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8688 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8689 b2118c49 2020-07-28 stsp arg.paths = paths;
8690 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8691 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8692 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8695 b2118c49 2020-07-28 stsp free(fileindex_path);
8696 b2118c49 2020-07-28 stsp if (fileindex)
8697 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8698 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8699 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8700 b2118c49 2020-07-28 stsp err = unlockerr;
8701 b2118c49 2020-07-28 stsp return err;