2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 ec22038e 2019-03-10 stsp #include <uuid.h>
36 7154f6ce 2019-03-27 stsp #include <util.h>
38 86c3caaf 2018-03-09 stsp #include "got_error.h"
39 86c3caaf 2018-03-09 stsp #include "got_repository.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 9d31a1d8 2018-03-11 stsp #include "got_object.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 e6209546 2019-08-22 stsp #include "got_cancel.h"
44 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 234035bc 2019-06-01 stsp #include "got_diff.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
55 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
56 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
57 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
61 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
95 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
114 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 09fe317a 2018-03-11 stsp return err;
120 024e9686 2019-05-14 stsp static const struct got_error *
121 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
123 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
124 024e9686 2019-05-14 stsp char *refstr = NULL;
126 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
127 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
128 024e9686 2019-05-14 stsp if (refstr == NULL)
129 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
131 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
132 024e9686 2019-05-14 stsp if (refstr == NULL)
133 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
135 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 024e9686 2019-05-14 stsp free(refstr);
137 024e9686 2019-05-14 stsp return err;
140 86c3caaf 2018-03-09 stsp const struct got_error *
141 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
142 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
144 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
145 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
146 ec22038e 2019-03-10 stsp uuid_t uuid;
147 ec22038e 2019-03-10 stsp uint32_t uuid_status;
148 65596e15 2018-12-24 stsp int obj_type;
149 7ac97322 2018-03-11 stsp char *path_got = NULL;
150 1451e70d 2018-03-10 stsp char *formatstr = NULL;
151 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
152 65596e15 2018-12-24 stsp char *basestr = NULL;
153 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
155 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
160 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
162 65596e15 2018-12-24 stsp return err;
163 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
165 65596e15 2018-12-24 stsp return err;
166 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
169 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
170 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
171 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
174 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
175 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
180 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
181 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
185 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
190 056e7441 2018-03-11 stsp /* Create an empty lock file. */
191 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
195 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
196 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
200 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
201 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
205 65596e15 2018-12-24 stsp /* Record our base commit. */
206 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
209 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
213 1451e70d 2018-03-10 stsp /* Store path to repository. */
214 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
219 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
220 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
225 ec22038e 2019-03-10 stsp /* Generate UUID. */
226 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
227 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
228 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
231 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
233 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
236 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
240 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
241 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
245 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
250 65596e15 2018-12-24 stsp free(commit_id);
251 7ac97322 2018-03-11 stsp free(path_got);
252 1451e70d 2018-03-10 stsp free(formatstr);
253 0bb8a95e 2018-03-12 stsp free(absprefix);
254 65596e15 2018-12-24 stsp free(basestr);
255 ec22038e 2019-03-10 stsp free(uuidstr);
256 86c3caaf 2018-03-09 stsp return err;
259 247140b2 2019-02-05 stsp const struct got_error *
260 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 e5dc7198 2018-12-29 stsp const char *path_prefix)
263 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
265 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
266 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
269 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
270 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
271 e5dc7198 2018-12-29 stsp free(absprefix);
272 e5dc7198 2018-12-29 stsp return NULL;
275 bc70eb79 2019-05-09 stsp const char *
276 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
278 36a38700 2019-05-10 stsp return worktree->head_ref_name;
281 024e9686 2019-05-14 stsp const struct got_error *
282 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
283 024e9686 2019-05-14 stsp struct got_reference *head_ref)
285 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
286 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
288 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
290 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
291 024e9686 2019-05-14 stsp path_got = NULL;
295 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
296 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
297 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
301 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
305 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
306 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
308 024e9686 2019-05-14 stsp free(path_got);
310 024e9686 2019-05-14 stsp free(head_ref_name);
311 024e9686 2019-05-14 stsp return err;
314 b72f483a 2019-02-05 stsp struct got_object_id *
315 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
317 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
320 507dc3bb 2018-12-29 stsp const struct got_error *
321 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
324 507dc3bb 2018-12-29 stsp const struct got_error *err;
325 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
326 507dc3bb 2018-12-29 stsp char *id_str = NULL;
327 507dc3bb 2018-12-29 stsp char *path_got = NULL;
329 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
331 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
332 507dc3bb 2018-12-29 stsp path_got = NULL;
336 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
338 507dc3bb 2018-12-29 stsp return err;
340 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
345 507dc3bb 2018-12-29 stsp /* Record our base commit. */
346 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
349 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
353 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
354 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
355 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
356 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
361 507dc3bb 2018-12-29 stsp got_object_close(obj);
362 507dc3bb 2018-12-29 stsp free(id_str);
363 507dc3bb 2018-12-29 stsp free(path_got);
364 507dc3bb 2018-12-29 stsp return err;
367 50b0790e 2020-09-11 stsp const struct got_gotconfig *
368 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
370 50b0790e 2020-09-11 stsp return worktree->gotconfig;
373 9d31a1d8 2018-03-11 stsp static const struct got_error *
374 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
376 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
379 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
380 86c3caaf 2018-03-09 stsp return NULL;
383 21908da4 2019-01-13 stsp static const struct got_error *
384 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
386 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
387 4a1ddfc2 2019-01-12 stsp char *abspath;
389 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
392 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
393 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 ddcd8544 2019-03-11 stsp struct stat sb;
395 ddcd8544 2019-03-11 stsp err = NULL;
396 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
397 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
398 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
399 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
400 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
403 4a1ddfc2 2019-01-12 stsp free(abspath);
404 68c76935 2019-02-19 stsp return err;
407 68c76935 2019-02-19 stsp static const struct got_error *
408 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
410 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
411 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
412 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
413 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
418 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
420 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
423 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
425 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
428 68c76935 2019-02-19 stsp if (flen1 == 0) {
429 68c76935 2019-02-19 stsp if (flen2 != 0)
432 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
433 68c76935 2019-02-19 stsp if (flen1 != 0)
436 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
437 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
447 68c76935 2019-02-19 stsp return err;
450 68c76935 2019-02-19 stsp static const struct got_error *
451 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
453 68c76935 2019-02-19 stsp struct stat sb;
454 68c76935 2019-02-19 stsp size_t size1, size2;
458 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
459 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
460 68c76935 2019-02-19 stsp size1 = sb.st_size;
462 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
463 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
464 68c76935 2019-02-19 stsp size2 = sb.st_size;
466 68c76935 2019-02-19 stsp if (size1 != size2) {
468 68c76935 2019-02-19 stsp return NULL;
471 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
472 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
473 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
474 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
476 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
479 0e039681 2021-11-15 stsp static const struct got_error *
480 0e039681 2021-11-15 stsp copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
482 0e039681 2021-11-15 stsp uint8_t fbuf[65536];
483 0e039681 2021-11-15 stsp size_t flen;
484 0e039681 2021-11-15 stsp ssize_t outlen;
486 0e039681 2021-11-15 stsp *outsize = 0;
488 0e039681 2021-11-15 stsp if (fseek(f, 0L, SEEK_SET) == -1)
489 0e039681 2021-11-15 stsp return got_ferror(f, GOT_ERR_IO);
492 0e039681 2021-11-15 stsp flen = fread(fbuf, 1, sizeof(fbuf), f);
493 0e039681 2021-11-15 stsp if (flen == 0) {
494 0e039681 2021-11-15 stsp if (ferror(f))
495 0e039681 2021-11-15 stsp return got_error_from_errno("fread");
496 0e039681 2021-11-15 stsp if (feof(f))
499 0e039681 2021-11-15 stsp outlen = write(outfd, fbuf, flen);
500 0e039681 2021-11-15 stsp if (outlen == -1)
501 0e039681 2021-11-15 stsp return got_error_from_errno("write");
502 0e039681 2021-11-15 stsp if (outlen != flen)
503 0e039681 2021-11-15 stsp return got_error(GOT_ERR_IO);
504 0e039681 2021-11-15 stsp *outsize += outlen;
507 0e039681 2021-11-15 stsp return NULL;
510 0e039681 2021-11-15 stsp static const struct got_error *
511 0e039681 2021-11-15 stsp merge_binary_file(int *overlapcnt, int merged_fd,
512 0e039681 2021-11-15 stsp FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 0e039681 2021-11-15 stsp const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 0e039681 2021-11-15 stsp const char *ondisk_path)
516 0e039681 2021-11-15 stsp const struct got_error *err = NULL;
517 0e039681 2021-11-15 stsp int same_content, changed_deriv, changed_deriv2;
518 0e039681 2021-11-15 stsp int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 0e039681 2021-11-15 stsp off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 0e039681 2021-11-15 stsp char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 0e039681 2021-11-15 stsp char *base_path_orig = NULL, *base_path_deriv = NULL;
522 0e039681 2021-11-15 stsp char *base_path_deriv2 = NULL;
524 0e039681 2021-11-15 stsp *overlapcnt = 0;
526 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_deriv2);
528 0e039681 2021-11-15 stsp return err;
530 0e039681 2021-11-15 stsp if (same_content)
531 0e039681 2021-11-15 stsp return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
533 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_orig);
535 0e039681 2021-11-15 stsp return err;
536 0e039681 2021-11-15 stsp changed_deriv = !same_content;
537 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv2, f_orig);
539 0e039681 2021-11-15 stsp return err;
540 0e039681 2021-11-15 stsp changed_deriv2 = !same_content;
542 0e039681 2021-11-15 stsp if (changed_deriv && changed_deriv2) {
543 0e039681 2021-11-15 stsp *overlapcnt = 1;
544 0e039681 2021-11-15 stsp if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
548 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
552 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
556 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 0e039681 2021-11-15 stsp base_path_orig);
560 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 0e039681 2021-11-15 stsp base_path_deriv);
564 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 0e039681 2021-11-15 stsp base_path_deriv2);
568 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
571 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
574 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
577 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "Binary files differ and cannot be "
578 0e039681 2021-11-15 stsp "merged automatically:\n") < 0) {
579 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
582 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 0e039681 2021-11-15 stsp label_deriv ? " " : "",
585 0e039681 2021-11-15 stsp label_deriv ? label_deriv : "",
586 0e039681 2021-11-15 stsp path_deriv) < 0) {
587 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
590 0e039681 2021-11-15 stsp if (size_orig > 0) {
591 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_ORIG,
593 0e039681 2021-11-15 stsp label_orig ? " " : "",
594 0e039681 2021-11-15 stsp label_orig ? label_orig : "",
595 0e039681 2021-11-15 stsp path_orig) < 0) {
596 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
600 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
602 0e039681 2021-11-15 stsp path_deriv2,
603 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_END,
604 0e039681 2021-11-15 stsp label_deriv2 ? " " : "",
605 0e039681 2021-11-15 stsp label_deriv2 ? label_deriv2 : "") < 0) {
606 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
609 0e039681 2021-11-15 stsp } else if (changed_deriv)
610 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 0e039681 2021-11-15 stsp else if (changed_deriv2)
612 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
614 0e039681 2021-11-15 stsp if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
615 0e039681 2021-11-15 stsp err == NULL)
616 0e039681 2021-11-15 stsp err = got_error_from_errno2("unlink", path_orig);
617 0e039681 2021-11-15 stsp if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_orig);
619 0e039681 2021-11-15 stsp if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv);
621 0e039681 2021-11-15 stsp if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv2);
623 0e039681 2021-11-15 stsp free(path_orig);
624 0e039681 2021-11-15 stsp free(path_deriv);
625 0e039681 2021-11-15 stsp free(path_deriv2);
626 0e039681 2021-11-15 stsp free(base_path_orig);
627 0e039681 2021-11-15 stsp free(base_path_deriv);
628 0e039681 2021-11-15 stsp free(base_path_deriv2);
629 0e039681 2021-11-15 stsp return err;
633 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
634 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
635 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
636 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
637 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
639 6353ad76 2019-02-08 stsp static const struct got_error *
640 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
643 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
647 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
648 6353ad76 2019-02-08 stsp int merged_fd = -1;
649 db590691 2021-06-02 stsp FILE *f_merged = NULL;
650 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
651 234035bc 2019-06-01 stsp int overlapcnt = 0;
652 3524bbf9 2020-10-20 stsp char *parent = NULL;
654 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
656 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
658 3524bbf9 2020-10-20 stsp return err;
660 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
665 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
669 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
672 0e039681 2021-11-15 stsp if (err->code != GOT_ERR_FILE_BINARY)
674 0e039681 2021-11-15 stsp err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 0e039681 2021-11-15 stsp f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
676 0e039681 2021-11-15 stsp ondisk_path);
681 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
682 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
686 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
687 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
691 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
692 db590691 2021-06-02 stsp if (f_merged == NULL) {
693 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
696 db590691 2021-06-02 stsp merged_fd = -1;
698 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
699 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
700 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
706 db590691 2021-06-02 stsp if (fchmod(fileno(f_merged), st_mode) != 0) {
707 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
711 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
712 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
713 230a42bd 2019-05-11 jcs ondisk_path);
718 fdcb7daf 2019-12-15 stsp if (merged_path)
719 fdcb7daf 2019-12-15 stsp unlink(merged_path);
721 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
723 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
725 6353ad76 2019-02-08 stsp free(merged_path);
726 af54ae4a 2019-02-19 stsp free(base_path);
727 3524bbf9 2020-10-20 stsp free(parent);
728 af57b12a 2020-07-23 stsp return err;
731 af57b12a 2020-07-23 stsp static const struct got_error *
732 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
733 af57b12a 2020-07-23 stsp size_t target_len)
735 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
736 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
737 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
738 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
739 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
740 af57b12a 2020-07-23 stsp ondisk_path);
741 af57b12a 2020-07-23 stsp return NULL;
745 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
747 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
748 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
750 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
751 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
753 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
754 993e2a1b 2020-07-23 stsp * has deleted this symlink.
756 11cc08c1 2020-07-23 stsp static const struct got_error *
757 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
758 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
761 11cc08c1 2020-07-23 stsp const struct got_error *err;
762 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 11cc08c1 2020-07-23 stsp FILE *f = NULL;
765 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
767 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
769 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
770 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
775 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
777 3818e3c4 2020-11-01 naddy goto done;
779 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
780 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
784 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
787 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
788 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
789 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
790 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
791 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
792 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
797 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
798 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
801 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
802 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
806 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
807 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
808 11cc08c1 2020-07-23 stsp free(path);
809 11cc08c1 2020-07-23 stsp free(id_str);
810 11cc08c1 2020-07-23 stsp free(label_deriv);
811 11cc08c1 2020-07-23 stsp return err;
814 d219f183 2020-07-23 stsp /* forward declaration */
815 d219f183 2020-07-23 stsp static const struct got_error *
816 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
818 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
819 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
822 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
823 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
824 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
825 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
827 af57b12a 2020-07-23 stsp static const struct got_error *
828 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
829 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
830 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
831 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
834 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
835 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
836 af57b12a 2020-07-23 stsp struct stat sb;
837 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
838 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
839 11cc08c1 2020-07-23 stsp int have_local_change = 0;
840 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
842 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
843 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
845 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
846 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
847 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
848 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
849 af57b12a 2020-07-23 stsp ondisk_path);
852 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
854 526a746f 2020-07-23 stsp if (blob_orig) {
855 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
860 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
861 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
862 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 11cc08c1 2020-07-23 stsp have_local_change = 1;
865 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
866 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
867 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
868 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
871 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
872 11cc08c1 2020-07-23 stsp if (ancestor_target) {
873 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
874 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
876 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
877 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
879 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
882 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
883 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
884 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
885 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
888 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
891 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
892 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
893 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
894 af57b12a 2020-07-23 stsp strlen(deriv_target));
897 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
899 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
900 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
902 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
905 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
906 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
907 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
910 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
916 af57b12a 2020-07-23 stsp free(ancestor_target);
917 eec2f5a9 2021-06-03 stsp return err;
920 eec2f5a9 2021-06-03 stsp static const struct got_error *
921 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
923 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
924 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
925 eec2f5a9 2021-06-03 stsp ssize_t target_len;
929 eec2f5a9 2021-06-03 stsp *outfile = NULL;
931 eec2f5a9 2021-06-03 stsp f = got_opentemp();
932 eec2f5a9 2021-06-03 stsp if (f == NULL)
933 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
934 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
936 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
939 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
940 eec2f5a9 2021-06-03 stsp if (n != target_len) {
941 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
944 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
945 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
948 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
949 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
956 eec2f5a9 2021-06-03 stsp *outfile = f;
957 14c901f1 2019-08-08 stsp return err;
961 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
963 14c901f1 2019-08-08 stsp * acts as the second derived version.
965 14c901f1 2019-08-08 stsp static const struct got_error *
966 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
968 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
969 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
970 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
973 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
974 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
976 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
979 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
981 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
983 ed6b5030 2020-10-20 stsp return err;
985 67a66647 2021-05-31 stsp if (blob_orig) {
986 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
987 67a66647 2021-05-31 stsp parent) == -1) {
988 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
989 67a66647 2021-05-31 stsp base_path = NULL;
993 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
996 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
997 67a66647 2021-05-31 stsp blob_orig);
1000 67a66647 2021-05-31 stsp free(base_path);
1003 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1004 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1005 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1007 db590691 2021-06-02 stsp f_orig = got_opentemp();
1008 db590691 2021-06-02 stsp if (f_orig == NULL) {
1009 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1010 db590691 2021-06-02 stsp goto done;
1014 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1015 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1016 14c901f1 2019-08-08 stsp base_path = NULL;
1017 14c901f1 2019-08-08 stsp goto done;
1020 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1022 14c901f1 2019-08-08 stsp goto done;
1023 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1024 14c901f1 2019-08-08 stsp blob_deriv);
1026 14c901f1 2019-08-08 stsp goto done;
1028 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1030 14c901f1 2019-08-08 stsp goto done;
1031 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1032 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1033 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1034 14c901f1 2019-08-08 stsp goto done;
1038 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1039 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1041 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1042 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1044 eec2f5a9 2021-06-03 stsp goto done;
1047 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1048 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1049 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1050 eec2f5a9 2021-06-03 stsp goto done;
1052 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1053 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1054 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1055 eec2f5a9 2021-06-03 stsp close(fd);
1056 eec2f5a9 2021-06-03 stsp goto done;
1060 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1061 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1062 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1064 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1065 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1066 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1067 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1068 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1069 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1070 14c901f1 2019-08-08 stsp free(base_path);
1071 67a66647 2021-05-31 stsp if (blob_orig_path) {
1072 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1073 67a66647 2021-05-31 stsp free(blob_orig_path);
1075 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1076 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1077 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1079 6353ad76 2019-02-08 stsp free(id_str);
1080 818c7501 2019-07-11 stsp free(label_deriv);
1081 ed6b5030 2020-10-20 stsp free(parent);
1082 4a1ddfc2 2019-01-12 stsp return err;
1085 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1086 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1087 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1088 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1090 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1091 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1093 65b05cec 2020-07-23 stsp *new_iep = NULL;
1095 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1097 054041d0 2020-06-24 stsp return err;
1099 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1100 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1102 054041d0 2020-06-24 stsp goto done;
1104 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1107 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1109 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1110 13d9040b 2019-03-27 stsp return err;
1113 1ebedb77 2019-10-19 stsp static mode_t
1114 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1116 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1118 1ebedb77 2019-10-19 stsp if (executable) {
1119 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1120 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1121 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1122 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1123 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1124 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1127 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1130 8ba819a3 2020-07-23 stsp /* forward declaration */
1131 13d9040b 2019-03-27 stsp static const struct got_error *
1132 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1133 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1134 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1135 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1136 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1137 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1140 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1141 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1143 8ba819a3 2020-07-23 stsp static const struct got_error *
1144 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1145 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1147 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1148 5a1fbc73 2020-07-23 stsp ssize_t elen;
1149 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1152 c6e8a826 2021-04-05 stsp *did_something = 0;
1155 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1156 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1157 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1158 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1159 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1160 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1162 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1163 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1164 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
1165 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1167 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1168 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1169 5a1fbc73 2020-07-23 stsp if (elen == -1)
1170 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1172 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1173 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1174 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1177 c6e8a826 2021-04-05 stsp *did_something = 1;
1178 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1179 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1180 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1181 5a1fbc73 2020-07-23 stsp return err;
1184 3c1ec782 2020-07-23 stsp static const struct got_error *
1185 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1186 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1188 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1189 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1190 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1192 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1194 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1195 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1196 3c1ec782 2020-07-23 stsp return NULL;
1200 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1201 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1202 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1203 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1204 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1206 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1207 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1208 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1210 ce031e9e 2020-10-20 stsp return err;
1211 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1212 ce031e9e 2020-10-20 stsp free(parent);
1213 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1215 ce031e9e 2020-10-20 stsp free(parent);
1216 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1217 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1218 3c1ec782 2020-07-23 stsp free(abspath);
1219 3c1ec782 2020-07-23 stsp return err;
1221 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1222 3c1ec782 2020-07-23 stsp free(abspath);
1224 3c1ec782 2020-07-23 stsp return err;
1226 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1228 3c1ec782 2020-07-23 stsp return err;
1231 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1232 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1233 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1234 3c1ec782 2020-07-23 stsp return NULL;
1237 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1238 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1239 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1240 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1241 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1242 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1244 3c1ec782 2020-07-23 stsp free(path_got);
1245 3c1ec782 2020-07-23 stsp return NULL;
1248 5a1fbc73 2020-07-23 stsp static const struct got_error *
1249 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1250 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1251 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1252 5267b9e4 2021-09-26 stsp int path_is_unversioned, int allow_bad_symlinks,
1253 5267b9e4 2021-09-26 stsp struct got_repository *repo,
1254 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1256 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1257 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1258 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1259 906c123b 2020-07-23 stsp char *path_got = NULL;
1260 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1261 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1263 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1266 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1267 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1268 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1269 8ba819a3 2020-07-23 stsp * in the blob object.
1272 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1273 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1274 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1275 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1276 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1277 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1278 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1279 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1280 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1281 3b9f0f87 2020-07-23 stsp progress_arg);
1283 8ba819a3 2020-07-23 stsp if (len > 0) {
1284 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1285 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1286 8ba819a3 2020-07-23 stsp len - hdrlen);
1287 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1288 8ba819a3 2020-07-23 stsp hdrlen = 0;
1290 8ba819a3 2020-07-23 stsp } while (len != 0);
1291 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1293 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1294 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1296 3c1ec782 2020-07-23 stsp return err;
1298 5267b9e4 2021-09-26 stsp if (*is_bad_symlink && !allow_bad_symlinks) {
1299 906c123b 2020-07-23 stsp /* install as a regular file */
1300 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1301 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1302 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1303 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1304 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1305 906c123b 2020-07-23 stsp goto done;
1308 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1309 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1310 c6e8a826 2021-04-05 stsp int symlink_replaced;
1311 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1312 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1313 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1314 c90c8ce3 2020-07-23 stsp goto done;
1316 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1317 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1319 f35fa46a 2020-07-23 stsp goto done;
1320 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1321 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1322 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1323 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1324 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1325 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1327 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1328 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1331 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1334 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1335 f4994adc 2020-10-20 stsp char *parent;
1336 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1338 f4994adc 2020-10-20 stsp goto done;
1339 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1340 f4994adc 2020-10-20 stsp free(parent);
1342 f35fa46a 2020-07-23 stsp goto done;
1344 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1345 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1347 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1348 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1349 f35fa46a 2020-07-23 stsp goto done;
1353 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1354 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1355 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1356 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1357 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1358 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1359 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1360 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1361 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1362 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1363 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1364 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1365 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1367 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1368 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1370 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1371 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1372 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1374 906c123b 2020-07-23 stsp free(path_got);
1375 8ba819a3 2020-07-23 stsp return err;
1378 8ba819a3 2020-07-23 stsp static const struct got_error *
1379 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1380 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1381 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1382 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1383 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1384 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1386 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1387 507dc3bb 2018-12-29 stsp int fd = -1;
1388 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1389 507dc3bb 2018-12-29 stsp int update = 0;
1390 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1392 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1393 8bd0cdad 2021-12-31 stsp O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1394 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1395 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1396 f5375317 2020-10-20 stsp char *parent;
1397 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1399 f5375317 2020-10-20 stsp return err;
1400 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1401 f5375317 2020-10-20 stsp free(parent);
1403 21908da4 2019-01-13 stsp return err;
1404 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1405 8bd0cdad 2021-12-31 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1406 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1407 21908da4 2019-01-13 stsp if (fd == -1)
1408 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1409 230a42bd 2019-05-11 jcs ondisk_path);
1410 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1411 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1412 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1413 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1414 3b9f0f87 2020-07-23 stsp goto done;
1416 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1417 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1418 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1419 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1420 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1421 507dc3bb 2018-12-29 stsp goto done;
1423 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1424 507dc3bb 2018-12-29 stsp ondisk_path);
1426 507dc3bb 2018-12-29 stsp goto done;
1427 507dc3bb 2018-12-29 stsp update = 1;
1430 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1433 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1434 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1435 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1436 3818e3c4 2020-11-01 naddy goto done;
1439 bd6aa359 2020-07-23 stsp if (progress_cb) {
1440 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1441 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1443 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1444 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1447 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1448 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1450 bd6aa359 2020-07-23 stsp goto done;
1453 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1455 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1456 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1459 9d31a1d8 2018-03-11 stsp if (len > 0) {
1460 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1461 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1462 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1463 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1464 b87c6f83 2018-12-24 stsp goto done;
1465 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1466 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1467 b87c6f83 2018-12-24 stsp goto done;
1469 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1471 9d31a1d8 2018-03-11 stsp } while (len != 0);
1473 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1474 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1475 816dc654 2019-02-16 stsp goto done;
1478 507dc3bb 2018-12-29 stsp if (update) {
1479 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1480 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1481 f6d8c0ac 2020-11-09 stsp goto done;
1483 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1484 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1485 230a42bd 2019-05-11 jcs ondisk_path);
1486 68ed9ba5 2019-02-10 stsp goto done;
1488 63df146d 2020-11-09 stsp free(tmppath);
1489 63df146d 2020-11-09 stsp tmppath = NULL;
1493 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1494 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1495 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1496 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1497 507dc3bb 2018-12-29 stsp free(tmppath);
1498 6353ad76 2019-02-08 stsp return err;
1501 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1502 6353ad76 2019-02-08 stsp static const struct got_error *
1503 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1505 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1506 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1507 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1508 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1509 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1511 7154f6ce 2019-03-27 stsp int i = 0;
1512 9bdd68dd 2020-01-02 naddy char *line = NULL;
1513 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1514 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1516 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1517 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1518 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1519 7154f6ce 2019-03-27 stsp if (feof(f))
1521 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1525 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1526 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1528 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1533 9bdd68dd 2020-01-02 naddy free(line);
1535 7154f6ce 2019-03-27 stsp return err;
1538 e2b1e152 2019-07-13 stsp static int
1539 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1541 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1542 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1545 1ebedb77 2019-10-19 stsp static int
1546 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1548 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1549 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1550 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1551 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1552 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1553 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1556 c363b2c1 2019-08-03 stsp static unsigned char
1557 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1559 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1560 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1561 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1562 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1563 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1564 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1565 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1567 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1571 a919d5c4 2020-07-23 stsp static const struct got_error *
1572 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1573 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1574 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1576 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1577 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1578 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1579 a919d5c4 2020-07-23 stsp ssize_t elen;
1580 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1581 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1582 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1584 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1586 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1588 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1590 a919d5c4 2020-07-23 stsp return err;
1591 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1593 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1594 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1596 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1598 a919d5c4 2020-07-23 stsp if (len > 0) {
1599 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1600 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1601 a919d5c4 2020-07-23 stsp len - hdrlen);
1602 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1603 a919d5c4 2020-07-23 stsp hdrlen = 0;
1605 a919d5c4 2020-07-23 stsp } while (len != 0);
1606 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1608 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1609 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1610 a919d5c4 2020-07-23 stsp if (elen == -1)
1611 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1613 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1614 a919d5c4 2020-07-23 stsp if (elen == -1)
1615 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1618 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1619 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1621 a919d5c4 2020-07-23 stsp return NULL;
1624 7154f6ce 2019-03-27 stsp static const struct got_error *
1625 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1626 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1627 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1629 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1630 6353ad76 2019-02-08 stsp struct got_object_id id;
1631 6353ad76 2019-02-08 stsp size_t hdrlen;
1632 1338848f 2019-12-13 stsp int fd = -1;
1633 6353ad76 2019-02-08 stsp FILE *f = NULL;
1634 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1635 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1636 6353ad76 2019-02-08 stsp size_t flen, blen;
1637 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1639 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1640 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1643 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1644 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1645 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1647 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1648 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1649 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1650 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1651 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1653 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1654 882ef1b9 2019-12-13 stsp goto done;
1656 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1657 3d35a492 2019-12-13 stsp goto done;
1660 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1661 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1662 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1663 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1664 5c02d2a5 2021-09-26 stsp else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1665 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1666 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1667 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1668 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1669 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1670 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1672 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1673 3d35a492 2019-12-13 stsp goto done;
1675 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1676 1338848f 2019-12-13 stsp goto done;
1680 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1681 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1682 1338848f 2019-12-13 stsp goto done;
1685 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1686 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1687 1338848f 2019-12-13 stsp goto done;
1688 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1689 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1690 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1691 1338848f 2019-12-13 stsp goto done;
1694 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1695 f179e66d 2020-07-23 stsp goto done;
1697 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1698 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1699 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1700 1338848f 2019-12-13 stsp goto done;
1703 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1704 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1705 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1707 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1709 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1711 1338848f 2019-12-13 stsp goto done;
1713 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1714 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1715 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1716 a919d5c4 2020-07-23 stsp goto done;
1719 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1720 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1721 ab0d4361 2019-12-13 stsp if (fd == -1) {
1722 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1723 ab0d4361 2019-12-13 stsp goto done;
1727 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1728 6353ad76 2019-02-08 stsp if (f == NULL) {
1729 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1730 6353ad76 2019-02-08 stsp goto done;
1733 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1735 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1736 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1738 7154f6ce 2019-03-27 stsp goto done;
1739 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1740 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1741 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1742 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1743 7154f6ce 2019-03-27 stsp goto done;
1745 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1746 6353ad76 2019-02-08 stsp if (flen != 0)
1747 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1749 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1750 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1751 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1753 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1754 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1755 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1756 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1760 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1763 6353ad76 2019-02-08 stsp hdrlen = 0;
1766 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1767 7154f6ce 2019-03-27 stsp rewind(f);
1768 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1769 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1770 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1773 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1774 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1775 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1776 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1777 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1778 9d31a1d8 2018-03-11 stsp return err;
1782 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1783 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1785 e2b1e152 2019-07-13 stsp static const struct got_error *
1786 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1787 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1789 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1790 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1791 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1793 e2b1e152 2019-07-13 stsp return NULL;
1796 9d31a1d8 2018-03-11 stsp static const struct got_error *
1797 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1798 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1799 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1800 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1801 0584f854 2019-04-06 stsp void *progress_arg)
1803 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1804 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1805 6353ad76 2019-02-08 stsp char *ondisk_path;
1806 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1807 b8f41171 2019-02-10 stsp struct stat sb;
1809 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1810 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1813 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1814 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1815 a76c42e6 2019-08-03 stsp goto done;
1817 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1820 abb4604f 2019-07-27 stsp goto done;
1821 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1822 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1824 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1825 f6764181 2021-09-24 stsp if (errno != ENOENT) {
1826 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1827 f6764181 2021-09-24 stsp ondisk_path);
1828 f6764181 2021-09-24 stsp goto done;
1830 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1831 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1833 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1834 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1836 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1840 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1842 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1843 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1844 b8f41171 2019-02-10 stsp goto done;
1846 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1848 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1849 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1851 5036ab18 2020-04-18 stsp goto done;
1854 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1855 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1856 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1858 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1859 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1860 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1861 c6e8a826 2021-04-05 stsp * updating contents of this file.
1863 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1864 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1865 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1866 c6e8a826 2021-04-05 stsp /* Same commit. */
1867 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1868 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1870 e2b1e152 2019-07-13 stsp goto done;
1871 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1873 6353ad76 2019-02-08 stsp goto done;
1875 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1876 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1877 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1878 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1879 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1880 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1882 0f58026f 2021-04-05 stsp goto done;
1883 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1885 b8f41171 2019-02-10 stsp goto done;
1889 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1891 6353ad76 2019-02-08 stsp goto done;
1893 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1894 234035bc 2019-06-01 stsp int update_timestamps;
1895 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1896 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1897 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1898 234035bc 2019-06-01 stsp struct got_object_id id2;
1899 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1900 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1902 f69721c3 2019-10-21 stsp goto done;
1904 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1905 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1906 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1907 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1908 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1909 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1910 f69721c3 2019-10-21 stsp goto done;
1912 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1913 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1914 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1915 234035bc 2019-06-01 stsp goto done;
1918 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1919 36bf999c 2020-07-23 stsp char *link_target;
1920 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1922 36bf999c 2020-07-23 stsp goto done;
1923 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1924 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1925 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1926 36bf999c 2020-07-23 stsp free(link_target);
1928 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1929 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1930 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1931 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1933 f69721c3 2019-10-21 stsp free(label_orig);
1934 234035bc 2019-06-01 stsp if (blob2)
1935 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1937 909d120e 2019-09-22 stsp goto done;
1939 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1940 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1941 234035bc 2019-06-01 stsp * unmodified files again.
1943 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1944 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1945 234035bc 2019-06-01 stsp update_timestamps);
1946 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1947 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1948 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1949 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1950 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1952 1ee397ad 2019-07-12 stsp goto done;
1953 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1954 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1956 13d9040b 2019-03-27 stsp goto done;
1958 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1959 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1960 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1961 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1962 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1963 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
1964 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
1966 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1967 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
1968 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
1969 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1970 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1973 13d9040b 2019-03-27 stsp goto done;
1976 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
1977 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
1978 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
1980 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
1981 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
1982 054041d0 2020-06-24 stsp &blob->id);
1985 13d9040b 2019-03-27 stsp goto done;
1987 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
1988 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
1989 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
1992 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1994 6353ad76 2019-02-08 stsp free(ondisk_path);
1995 8da9e5f4 2019-01-12 stsp return err;
1998 90285c3b 2019-01-08 stsp static const struct got_error *
1999 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2001 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2002 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2004 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2005 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2007 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2008 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2009 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2011 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2012 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2014 1c4cdd89 2021-06-20 stsp goto done;
2015 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2016 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2017 fddefe3b 2020-10-20 stsp free(ondisk_path);
2018 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2019 1c4cdd89 2021-06-20 stsp parent = NULL;
2020 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2021 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2022 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2023 fddefe3b 2020-10-20 stsp ondisk_path);
2026 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2032 90285c3b 2019-01-08 stsp free(ondisk_path);
2033 1c4cdd89 2021-06-20 stsp free(parent);
2034 90285c3b 2019-01-08 stsp return err;
2037 708d8e67 2019-03-27 stsp static const struct got_error *
2038 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2039 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2040 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2042 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2043 708d8e67 2019-03-27 stsp unsigned char status;
2044 708d8e67 2019-03-27 stsp struct stat sb;
2045 708d8e67 2019-03-27 stsp char *ondisk_path;
2047 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2048 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2050 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2052 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2054 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2056 5a58a424 2020-06-23 stsp goto done;
2058 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2059 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2060 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2061 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2062 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2063 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2064 993e2a1b 2020-07-23 stsp goto done;
2066 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2067 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2068 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2069 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2071 993e2a1b 2020-07-23 stsp goto done;
2072 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2073 993e2a1b 2020-07-23 stsp ie->path);
2074 993e2a1b 2020-07-23 stsp goto done;
2077 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2078 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2079 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2081 5a58a424 2020-06-23 stsp goto done;
2083 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2084 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2086 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2087 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2089 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2091 5a58a424 2020-06-23 stsp goto done;
2092 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2093 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2095 5a58a424 2020-06-23 stsp goto done;
2097 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2100 5a58a424 2020-06-23 stsp free(ondisk_path);
2101 fc6346c4 2019-03-27 stsp return err;
2104 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2105 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2106 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2107 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2108 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2109 8da9e5f4 2019-01-12 stsp void *progress_arg;
2110 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2111 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2114 512f0d0e 2019-01-02 stsp static const struct got_error *
2115 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2116 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2118 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2120 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2121 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2123 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2124 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2127 8da9e5f4 2019-01-12 stsp static const struct got_error *
2128 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2130 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2132 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2133 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2135 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2136 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2139 9d31a1d8 2018-03-11 stsp static const struct got_error *
2140 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2142 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2143 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2144 8da9e5f4 2019-01-12 stsp char *path;
2146 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2147 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2149 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2150 63c5ca5d 2019-08-24 stsp return NULL;
2152 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2153 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2155 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2157 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2158 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2160 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2161 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2163 8da9e5f4 2019-01-12 stsp free(path);
2164 0cd1c46a 2019-03-11 stsp return err;
2167 b2118c49 2020-07-28 stsp const struct got_error *
2168 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2170 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2172 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2173 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2174 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2175 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2178 b2118c49 2020-07-28 stsp return NULL;
2181 818c7501 2019-07-11 stsp static const struct got_error *
2182 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2184 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2185 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2187 517bab73 2019-03-11 stsp *refname = NULL;
2189 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2191 b2118c49 2020-07-28 stsp return err;
2193 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2194 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2195 0647c563 2019-03-11 stsp *refname = NULL;
2197 517bab73 2019-03-11 stsp free(uuidstr);
2198 517bab73 2019-03-11 stsp return err;
2201 818c7501 2019-07-11 stsp const struct got_error *
2202 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2204 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2207 818c7501 2019-07-11 stsp static const struct got_error *
2208 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2210 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2211 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2214 818c7501 2019-07-11 stsp static const struct got_error *
2215 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2217 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2220 818c7501 2019-07-11 stsp static const struct got_error *
2221 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2223 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2224 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2227 818c7501 2019-07-11 stsp static const struct got_error *
2228 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2230 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2231 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2234 0ebf8283 2019-07-24 stsp static const struct got_error *
2235 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2237 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2238 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2241 0ebf8283 2019-07-24 stsp static const struct got_error *
2242 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2244 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2245 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2248 0ebf8283 2019-07-24 stsp static const struct got_error *
2249 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2251 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2252 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2255 0ebf8283 2019-07-24 stsp static const struct got_error *
2256 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2258 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2259 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2262 0ebf8283 2019-07-24 stsp const struct got_error *
2263 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2264 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2266 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2267 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2268 0ebf8283 2019-07-24 stsp *path = NULL;
2269 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2271 0ebf8283 2019-07-24 stsp return NULL;
2274 f259c4c1 2021-09-24 stsp static const struct got_error *
2275 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2277 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2278 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2281 f259c4c1 2021-09-24 stsp static const struct got_error *
2282 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2284 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2285 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2289 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2290 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2292 517bab73 2019-03-11 stsp static const struct got_error *
2293 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2295 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2296 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2297 517bab73 2019-03-11 stsp char *refname;
2299 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2301 517bab73 2019-03-11 stsp return err;
2303 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2305 0cd1c46a 2019-03-11 stsp goto done;
2307 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2309 0cd1c46a 2019-03-11 stsp free(refname);
2311 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2312 3e3a69f1 2019-07-25 stsp return err;
2315 3e3a69f1 2019-07-25 stsp static const struct got_error *
2316 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2318 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2320 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2321 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2322 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2323 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2325 9d31a1d8 2018-03-11 stsp return err;
2329 ebf99748 2019-05-09 stsp static const struct got_error *
2330 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2331 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2333 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2334 ebf99748 2019-05-09 stsp FILE *index = NULL;
2336 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2337 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2338 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2339 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2341 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2343 ebf99748 2019-05-09 stsp goto done;
2345 00fe21f2 2021-12-31 stsp index = fopen(*fileindex_path, "rbe");
2346 ebf99748 2019-05-09 stsp if (index == NULL) {
2347 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2348 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2350 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2351 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2352 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2355 ebf99748 2019-05-09 stsp if (err) {
2356 ebf99748 2019-05-09 stsp free(*fileindex_path);
2357 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2358 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2359 ebf99748 2019-05-09 stsp *fileindex = NULL;
2361 ebf99748 2019-05-09 stsp return err;
2364 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2365 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2366 c932eeeb 2019-05-22 stsp const char *path;
2367 c932eeeb 2019-05-22 stsp size_t path_len;
2368 c932eeeb 2019-05-22 stsp const char *entry_name;
2369 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2370 a484d721 2019-06-10 stsp void *progress_arg;
2373 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2374 c932eeeb 2019-05-22 stsp static const struct got_error *
2375 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2377 1ee397ad 2019-07-12 stsp const struct got_error *err;
2378 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2380 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2381 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2382 c932eeeb 2019-05-22 stsp return NULL;
2383 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2384 102ff934 2019-06-11 stsp return NULL;
2386 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2387 a769b60b 2021-06-27 stsp return NULL;
2389 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2390 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2391 c932eeeb 2019-05-22 stsp return NULL;
2393 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2394 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2395 edd02c5e 2019-07-11 stsp ie->path);
2397 1ee397ad 2019-07-12 stsp return err;
2399 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2400 c932eeeb 2019-05-22 stsp return NULL;
2403 a615e0e7 2020-12-16 stsp static const struct got_error *
2404 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2405 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2406 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2408 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2410 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2411 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2412 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2413 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2414 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2415 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2417 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2418 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2421 9c6338c4 2019-06-02 stsp static const struct got_error *
2422 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2424 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2425 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2426 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2427 867630bb 2020-01-17 stsp struct timespec timeout;
2429 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2430 9c6338c4 2019-06-02 stsp fileindex_path);
2432 9c6338c4 2019-06-02 stsp goto done;
2434 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2436 9c6338c4 2019-06-02 stsp goto done;
2438 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2439 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2440 9c6338c4 2019-06-02 stsp fileindex_path);
2441 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2445 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2446 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2447 867630bb 2020-01-17 stsp * was recorded in the file index.
2449 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2450 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2451 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2453 9c6338c4 2019-06-02 stsp if (new_index)
2454 9c6338c4 2019-06-02 stsp fclose(new_index);
2455 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2456 9c6338c4 2019-06-02 stsp return err;
2459 6ced4176 2019-07-12 stsp static const struct got_error *
2460 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2461 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2462 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2464 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2465 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2466 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2467 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2469 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2470 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2471 6ced4176 2019-07-12 stsp *tree_id = NULL;
2473 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2474 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2475 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2476 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2477 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2478 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2479 6ced4176 2019-07-12 stsp goto done;
2481 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2482 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2484 6ced4176 2019-07-12 stsp goto done;
2485 6ced4176 2019-07-12 stsp return NULL;
2488 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2490 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2491 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2492 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2493 6ced4176 2019-07-12 stsp goto done;
2496 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2497 6ced4176 2019-07-12 stsp in_repo_path);
2499 6ced4176 2019-07-12 stsp goto done;
2501 6ced4176 2019-07-12 stsp free(in_repo_path);
2502 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2504 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2506 6ced4176 2019-07-12 stsp goto done;
2508 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2509 6ced4176 2019-07-12 stsp /* Check out a single file. */
2510 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2511 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2512 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2513 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2514 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2515 6ced4176 2019-07-12 stsp goto done;
2517 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2518 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2519 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2520 6ced4176 2019-07-12 stsp goto done;
2523 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2524 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2526 6ced4176 2019-07-12 stsp return err;
2527 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2528 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2529 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2530 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2531 6ced4176 2019-07-12 stsp goto done;
2534 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2535 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2537 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2538 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2539 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2540 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2541 6ced4176 2019-07-12 stsp goto done;
2543 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2544 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2545 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2546 6ced4176 2019-07-12 stsp goto done;
2551 6ced4176 2019-07-12 stsp free(in_repo_path);
2552 6ced4176 2019-07-12 stsp if (err) {
2553 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2554 6ced4176 2019-07-12 stsp free(*tree_relpath);
2555 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2556 6ced4176 2019-07-12 stsp free(*tree_id);
2557 6ced4176 2019-07-12 stsp *tree_id = NULL;
2559 07ed472d 2019-07-12 stsp return err;
2562 07ed472d 2019-07-12 stsp static const struct got_error *
2563 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2564 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2565 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2566 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2568 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2569 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2570 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2571 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2572 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2574 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2575 7f47418f 2019-12-20 stsp if (err) {
2576 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2577 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2578 7f47418f 2019-12-20 stsp goto done;
2579 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2580 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2582 7f47418f 2019-12-20 stsp return err;
2585 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2586 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2588 07ed472d 2019-07-12 stsp goto done;
2590 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2592 07ed472d 2019-07-12 stsp goto done;
2594 07ed472d 2019-07-12 stsp if (entry_name &&
2595 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2596 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2597 07ed472d 2019-07-12 stsp goto done;
2600 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2601 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2602 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2603 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2604 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2605 07ed472d 2019-07-12 stsp arg.repo = repo;
2606 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2607 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2608 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2609 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2610 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2611 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2614 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2615 07ed472d 2019-07-12 stsp if (commit)
2616 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2617 6ced4176 2019-07-12 stsp return err;
2620 9d31a1d8 2018-03-11 stsp const struct got_error *
2621 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2622 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2623 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2624 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2626 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2627 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2628 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2629 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2630 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2631 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2632 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2633 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2634 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2635 f2ea84fa 2019-07-27 stsp int entry_type;
2636 f2ea84fa 2019-07-27 stsp char *relpath;
2637 f2ea84fa 2019-07-27 stsp char *entry_name;
2638 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2639 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2641 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2643 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2645 9d31a1d8 2018-03-11 stsp return err;
2647 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2648 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2649 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2650 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2651 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2652 f2ea84fa 2019-07-27 stsp goto done;
2655 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2656 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2657 f2ea84fa 2019-07-27 stsp if (err) {
2658 f2ea84fa 2019-07-27 stsp free(tpd);
2659 6ced4176 2019-07-12 stsp goto done;
2662 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2663 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2664 f2ea84fa 2019-07-27 stsp if (err) {
2665 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2666 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2667 f2ea84fa 2019-07-27 stsp free(tpd);
2668 f2ea84fa 2019-07-27 stsp goto done;
2671 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2673 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2677 51514078 2018-12-25 stsp * Read the file index.
2678 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2679 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2681 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2683 c4cdcb68 2019-04-03 stsp goto done;
2685 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2686 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2687 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2689 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2690 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2691 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2695 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2696 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2697 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2698 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2699 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2700 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2701 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2702 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2706 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2708 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2709 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2710 af12c6b9 2019-06-04 stsp err = sync_err;
2712 9c6338c4 2019-06-02 stsp free(fileindex_path);
2714 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2715 9d31a1d8 2018-03-11 stsp if (commit)
2716 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2717 5ade8233 2019-07-12 stsp if (fileindex)
2718 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2719 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2720 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2721 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2722 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2723 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2724 f2ea84fa 2019-07-27 stsp free(tpd);
2726 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2727 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2728 9d31a1d8 2018-03-11 stsp err = unlockerr;
2729 f8d1f275 2019-02-04 stsp return err;
2732 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2733 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2734 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2735 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2736 234035bc 2019-06-01 stsp void *progress_arg;
2737 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2738 234035bc 2019-06-01 stsp void *cancel_arg;
2739 f69721c3 2019-10-21 stsp const char *label_orig;
2740 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2741 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2744 234035bc 2019-06-01 stsp static const struct got_error *
2745 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2746 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2747 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2748 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2750 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2751 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2752 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2753 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2754 234035bc 2019-06-01 stsp struct stat sb;
2755 234035bc 2019-06-01 stsp unsigned char status;
2756 234035bc 2019-06-01 stsp int local_changes_subsumed;
2757 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2758 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2760 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2761 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2762 d6c87207 2019-08-02 stsp strlen(path2));
2763 1ee397ad 2019-07-12 stsp if (ie == NULL)
2764 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2765 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2767 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2768 234035bc 2019-06-01 stsp path2) == -1)
2769 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2771 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2774 234035bc 2019-06-01 stsp goto done;
2776 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2777 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2778 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2779 234035bc 2019-06-01 stsp goto done;
2781 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2782 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2783 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2784 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2785 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2786 234035bc 2019-06-01 stsp goto done;
2789 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2790 36bf999c 2020-07-23 stsp char *link_target2;
2791 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2793 36bf999c 2020-07-23 stsp goto done;
2794 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2795 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2796 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2797 36bf999c 2020-07-23 stsp free(link_target2);
2801 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2802 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2803 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2804 1af628f4 2021-06-03 stsp goto done;
2806 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2807 1af628f4 2021-06-03 stsp f_orig, blob1);
2809 1af628f4 2021-06-03 stsp goto done;
2811 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2812 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2813 1af628f4 2021-06-03 stsp goto done;
2814 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2815 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2817 1af628f4 2021-06-03 stsp goto done;
2819 c0df5966 2021-12-31 stsp fd = open(ondisk_path,
2820 c0df5966 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2821 1af628f4 2021-06-03 stsp if (fd == -1) {
2822 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2823 1af628f4 2021-06-03 stsp ondisk_path);
2824 1af628f4 2021-06-03 stsp goto done;
2826 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2827 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2828 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2829 1af628f4 2021-06-03 stsp ondisk_path);
2830 1af628f4 2021-06-03 stsp close(fd);
2831 1af628f4 2021-06-03 stsp goto done;
2833 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2835 1af628f4 2021-06-03 stsp goto done;
2836 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2837 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2838 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2839 1af628f4 2021-06-03 stsp goto done;
2841 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2842 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2843 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2844 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2845 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2847 234035bc 2019-06-01 stsp } else if (blob1) {
2848 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2849 d6c87207 2019-08-02 stsp strlen(path1));
2850 1ee397ad 2019-07-12 stsp if (ie == NULL)
2851 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2852 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2854 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2855 2b92fad7 2019-06-02 stsp path1) == -1)
2856 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2858 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2861 2b92fad7 2019-06-02 stsp goto done;
2863 2b92fad7 2019-06-02 stsp switch (status) {
2864 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2865 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2866 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2868 1ee397ad 2019-07-12 stsp goto done;
2869 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2871 2b92fad7 2019-06-02 stsp goto done;
2873 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2875 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2876 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2877 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2878 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2880 1ee397ad 2019-07-12 stsp goto done;
2882 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2884 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2885 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2886 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2887 72840534 2022-01-19 stsp off_t blob1_size;
2889 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2890 0a22ca1a 2020-09-23 stsp * exists in the repository.
2892 72840534 2022-01-19 stsp err = got_object_blob_file_create(&id, &blob1_f,
2893 72840534 2022-01-19 stsp &blob1_size, path1);
2895 0a22ca1a 2020-09-23 stsp goto done;
2896 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2897 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2898 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2900 0a22ca1a 2020-09-23 stsp goto done;
2901 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2904 0a22ca1a 2020-09-23 stsp goto done;
2906 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2909 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2910 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2912 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2913 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2916 0a22ca1a 2020-09-23 stsp goto done;
2919 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2920 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2921 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2922 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2924 1ee397ad 2019-07-12 stsp goto done;
2926 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2927 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2929 1ee397ad 2019-07-12 stsp goto done;
2934 234035bc 2019-06-01 stsp } else if (blob2) {
2935 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2936 234035bc 2019-06-01 stsp path2) == -1)
2937 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2938 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2939 d6c87207 2019-08-02 stsp strlen(path2));
2941 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2942 7f91a133 2019-12-13 stsp -1, NULL, repo);
2944 234035bc 2019-06-01 stsp goto done;
2945 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2946 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2947 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2948 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2949 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2950 1ee397ad 2019-07-12 stsp status, path2);
2951 234035bc 2019-06-01 stsp goto done;
2953 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2954 36bf999c 2020-07-23 stsp char *link_target2;
2955 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2958 36bf999c 2020-07-23 stsp goto done;
2959 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2960 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2961 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2962 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2963 36bf999c 2020-07-23 stsp free(link_target2);
2964 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2965 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2966 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2967 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2968 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2969 526a746f 2020-07-23 stsp a->progress_arg);
2971 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2972 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2975 dfe9fba0 2020-07-23 stsp goto done;
2976 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2977 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2978 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
2979 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2981 234035bc 2019-06-01 stsp goto done;
2984 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2985 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2986 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2987 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2988 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2989 5267b9e4 2021-09-26 stsp 0, 1, a->allow_bad_symlinks, repo,
2990 5267b9e4 2021-09-26 stsp a->progress_cb, a->progress_arg);
2992 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2993 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2994 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2997 234035bc 2019-06-01 stsp goto done;
2998 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3000 234035bc 2019-06-01 stsp goto done;
3001 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3002 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3003 3969253a 2020-03-07 stsp if (err) {
3004 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3005 3969253a 2020-03-07 stsp goto done;
3007 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3008 2b92fad7 2019-06-02 stsp if (err) {
3009 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3010 2b92fad7 2019-06-02 stsp goto done;
3012 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3013 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3014 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3019 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3020 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3021 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3022 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3023 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3024 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3025 1af628f4 2021-06-03 stsp free(id_str);
3026 54d5be07 2021-06-03 stsp free(label_deriv2);
3027 234035bc 2019-06-01 stsp free(ondisk_path);
3028 234035bc 2019-06-01 stsp return err;
3031 69de9dd4 2021-09-03 stsp static const struct got_error *
3032 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3034 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3036 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3037 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3038 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3039 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3040 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3042 69de9dd4 2021-09-03 stsp return NULL;
3045 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3046 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3047 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3048 234035bc 2019-06-01 stsp struct got_repository *repo;
3051 234035bc 2019-06-01 stsp static const struct got_error *
3052 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3053 69de9dd4 2021-09-03 stsp struct got_blob_object *blob2, struct got_object_id *id1,
3054 69de9dd4 2021-09-03 stsp struct got_object_id *id2, const char *path1, const char *path2,
3055 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3057 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3058 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3059 234035bc 2019-06-01 stsp unsigned char status;
3060 234035bc 2019-06-01 stsp struct stat sb;
3061 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3062 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3063 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3064 234035bc 2019-06-01 stsp char *ondisk_path;
3066 69de9dd4 2021-09-03 stsp if (id == NULL)
3067 69de9dd4 2021-09-03 stsp return NULL;
3069 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3070 69de9dd4 2021-09-03 stsp if (ie == NULL)
3071 69de9dd4 2021-09-03 stsp return NULL;
3073 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3075 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3077 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3078 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3079 5546d466 2021-09-02 stsp free(ondisk_path);
3081 234035bc 2019-06-01 stsp return err;
3082 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3083 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3085 234035bc 2019-06-01 stsp return NULL;
3088 818c7501 2019-07-11 stsp static const struct got_error *
3089 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3090 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3091 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3092 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3093 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3095 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3096 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3097 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3098 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3099 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3100 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3102 03415a1a 2019-06-02 stsp if (commit_id1) {
3103 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3104 03415a1a 2019-06-02 stsp worktree->path_prefix);
3105 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3106 03415a1a 2019-06-02 stsp goto done;
3108 69d57f3d 2020-07-31 stsp if (tree_id1) {
3109 69d57f3d 2020-07-31 stsp char *id_str;
3111 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3113 03415a1a 2019-06-02 stsp goto done;
3115 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3117 f69721c3 2019-10-21 stsp goto done;
3119 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3120 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3121 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3122 f69721c3 2019-10-21 stsp free(id_str);
3123 f69721c3 2019-10-21 stsp goto done;
3125 f69721c3 2019-10-21 stsp free(id_str);
3128 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3129 234035bc 2019-06-01 stsp worktree->path_prefix);
3131 234035bc 2019-06-01 stsp goto done;
3133 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3135 234035bc 2019-06-01 stsp goto done;
3137 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3138 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3139 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3140 69de9dd4 2021-09-03 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3141 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3143 69de9dd4 2021-09-03 stsp goto done;
3145 234035bc 2019-06-01 stsp arg.worktree = worktree;
3146 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3147 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3148 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3149 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3150 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3151 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3152 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3153 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3154 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3155 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3156 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3157 af12c6b9 2019-06-04 stsp err = sync_err;
3159 234035bc 2019-06-01 stsp if (tree1)
3160 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3161 234035bc 2019-06-01 stsp if (tree2)
3162 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3163 f69721c3 2019-10-21 stsp free(label_orig);
3164 818c7501 2019-07-11 stsp return err;
3167 818c7501 2019-07-11 stsp const struct got_error *
3168 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3169 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3170 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3171 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3173 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3174 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3175 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3177 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3179 818c7501 2019-07-11 stsp return err;
3181 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3183 818c7501 2019-07-11 stsp goto done;
3185 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3186 69de9dd4 2021-09-03 stsp worktree);
3188 818c7501 2019-07-11 stsp goto done;
3190 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3191 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3192 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3194 818c7501 2019-07-11 stsp if (fileindex)
3195 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3196 818c7501 2019-07-11 stsp free(fileindex_path);
3197 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3198 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3199 234035bc 2019-06-01 stsp err = unlockerr;
3200 234035bc 2019-06-01 stsp return err;
3203 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3204 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3205 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3206 927df6b7 2019-02-10 stsp const char *status_path;
3207 927df6b7 2019-02-10 stsp size_t status_path_len;
3208 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3209 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3210 f8d1f275 2019-02-04 stsp void *status_arg;
3211 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3212 f8d1f275 2019-02-04 stsp void *cancel_arg;
3213 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3214 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3215 f2a9dc41 2019-12-13 tracey int report_unchanged;
3216 3143d852 2020-06-25 stsp int no_ignores;
3219 f8d1f275 2019-02-04 stsp static const struct got_error *
3220 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3221 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3222 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3223 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3225 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3226 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3227 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3228 927df6b7 2019-02-10 stsp struct stat sb;
3229 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3230 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3231 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3233 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3235 98eaaa12 2019-08-03 stsp return err;
3237 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3238 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3239 98eaaa12 2019-08-03 stsp return NULL;
3241 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3242 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3243 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3245 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3246 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3247 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3249 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3250 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3251 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3252 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3253 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3256 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3257 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3260 927df6b7 2019-02-10 stsp static const struct got_error *
3261 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3262 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3264 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3265 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3266 f8d1f275 2019-02-04 stsp char *abspath;
3268 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3269 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3271 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3272 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3273 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3274 927df6b7 2019-02-10 stsp return NULL;
3276 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3277 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3278 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3279 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3281 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3282 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3283 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3286 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3287 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3288 f8d1f275 2019-02-04 stsp free(abspath);
3289 9d31a1d8 2018-03-11 stsp return err;
3292 f8d1f275 2019-02-04 stsp static const struct got_error *
3293 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3295 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3296 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3297 2ec1f75b 2019-03-26 stsp unsigned char status;
3299 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3300 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3302 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3303 927df6b7 2019-02-10 stsp return NULL;
3305 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3306 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3307 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3308 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3310 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3311 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3312 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3316 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3318 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3320 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3321 6841da00 2019-08-08 stsp free((char *)pe->path);
3322 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3326 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3328 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3330 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3331 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3332 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3333 6841da00 2019-08-08 stsp free((char *)pe->path);
3335 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3338 6841da00 2019-08-08 stsp static const struct got_error *
3339 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3341 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3342 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3343 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3344 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3345 6841da00 2019-08-08 stsp size_t linesize = 0;
3346 6841da00 2019-08-08 stsp ssize_t linelen;
3348 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3349 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3350 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3351 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3353 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3354 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3355 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3357 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3358 bd8de430 2019-10-04 stsp if (line[0] == '#')
3361 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3362 bd8de430 2019-10-04 stsp if (line[0] == '!')
3365 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3366 6841da00 2019-08-08 stsp line) == -1) {
3367 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3368 6841da00 2019-08-08 stsp goto done;
3370 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3372 6841da00 2019-08-08 stsp goto done;
3374 6841da00 2019-08-08 stsp if (ferror(f)) {
3375 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3376 6841da00 2019-08-08 stsp goto done;
3379 6841da00 2019-08-08 stsp dirpath = strdup(path);
3380 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3381 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3382 6841da00 2019-08-08 stsp goto done;
3384 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3386 6841da00 2019-08-08 stsp free(line);
3387 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3388 6841da00 2019-08-08 stsp free(dirpath);
3389 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3391 6841da00 2019-08-08 stsp return err;
3395 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3397 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3399 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3400 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3401 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3402 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3404 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3405 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3407 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3409 bd8de430 2019-10-04 stsp pattern += 3;
3411 bd8de430 2019-10-04 stsp while (*p) {
3412 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3413 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3414 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3415 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3417 bd8de430 2019-10-04 stsp while (*p == '/')
3427 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3428 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3429 6841da00 2019-08-08 stsp * ignores backwards.
3431 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3432 6841da00 2019-08-08 stsp while (pe) {
3433 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3434 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3435 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3436 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3437 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3438 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3439 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3440 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3441 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3446 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3452 6841da00 2019-08-08 stsp static const struct got_error *
3453 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3454 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3456 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3457 6841da00 2019-08-08 stsp char *ignorespath;
3458 886cec17 2019-12-15 stsp int fd = -1;
3459 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3461 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3462 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3463 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3465 886cec17 2019-12-15 stsp if (dirfd != -1) {
3466 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3467 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3468 886cec17 2019-12-15 stsp if (fd == -1) {
3469 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3470 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3471 886cec17 2019-12-15 stsp ignorespath);
3473 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3474 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3475 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3476 886cec17 2019-12-15 stsp ignorespath);
3479 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3483 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3484 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3485 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3486 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3487 886cec17 2019-12-15 stsp ignorespath);
3489 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3492 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3493 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3494 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3495 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3496 6841da00 2019-08-08 stsp free(ignorespath);
3497 6841da00 2019-08-08 stsp return err;
3500 f8d1f275 2019-02-04 stsp static const struct got_error *
3501 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3502 62da3196 2021-10-01 stsp int dirfd)
3504 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3505 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3506 f8d1f275 2019-02-04 stsp char *path = NULL;
3508 62da3196 2021-10-01 stsp if (ignore != NULL)
3509 62da3196 2021-10-01 stsp *ignore = 0;
3511 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3512 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3514 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3515 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3516 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3518 f8d1f275 2019-02-04 stsp path = de->d_name;
3521 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3522 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3523 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3524 62da3196 2021-10-01 stsp *ignore = 1;
3525 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3526 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3527 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3528 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3529 f8d1f275 2019-02-04 stsp if (parent_path[0])
3530 f8d1f275 2019-02-04 stsp free(path);
3531 b72f483a 2019-02-05 stsp return err;
3534 347d1d3e 2019-07-12 stsp static const struct got_error *
3535 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3537 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3538 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3540 3143d852 2020-06-25 stsp if (a->no_ignores)
3541 3143d852 2020-06-25 stsp return NULL;
3543 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3544 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3546 3143d852 2020-06-25 stsp return err;
3548 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3549 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3551 3143d852 2020-06-25 stsp return err;
3554 3143d852 2020-06-25 stsp static const struct got_error *
3555 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3556 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3557 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3558 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3560 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3561 abb4604f 2019-07-27 stsp struct stat sb;
3563 0e33f8e0 2021-09-01 stsp if (!no_ignores && match_ignores(ignores, path))
3564 ff56836b 2021-07-08 stsp return NULL;
3566 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3568 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3569 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3571 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3572 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3573 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3574 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3575 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3578 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3579 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3580 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3582 abb4604f 2019-07-27 stsp return NULL;
3585 3143d852 2020-06-25 stsp static const struct got_error *
3586 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3587 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3589 3143d852 2020-06-25 stsp const struct got_error *err;
3590 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3592 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3593 3143d852 2020-06-25 stsp ".cvsignore");
3595 3143d852 2020-06-25 stsp return err;
3597 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3598 3143d852 2020-06-25 stsp ".gitignore");
3600 3143d852 2020-06-25 stsp return err;
3602 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3603 3143d852 2020-06-25 stsp if (err) {
3604 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3605 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3606 3143d852 2020-06-25 stsp return err;
3608 3143d852 2020-06-25 stsp for (;;) {
3609 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3610 3143d852 2020-06-25 stsp ".cvsignore");
3613 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3614 3143d852 2020-06-25 stsp ".gitignore");
3617 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3618 3143d852 2020-06-25 stsp if (err) {
3619 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3620 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3623 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3625 b737c85a 2020-06-26 stsp free(parent_path);
3626 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3627 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3630 b737c85a 2020-06-26 stsp free(parent_path);
3631 b737c85a 2020-06-26 stsp free(next_parent_path);
3632 3143d852 2020-06-25 stsp return err;
3635 abb4604f 2019-07-27 stsp static const struct got_error *
3636 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3637 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3638 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3639 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3640 f2a9dc41 2019-12-13 tracey int report_unchanged)
3642 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3643 6fc93f37 2019-12-13 stsp int fd = -1;
3644 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3645 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3646 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3647 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3649 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3651 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3652 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3653 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3655 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3656 6fc93f37 2019-12-13 stsp if (fd == -1) {
3657 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3658 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
3659 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3661 ff56836b 2021-07-08 stsp if (!no_ignores) {
3662 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3663 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3665 ff56836b 2021-07-08 stsp goto done;
3667 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3668 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3669 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3672 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3673 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3674 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3675 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3676 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3677 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3678 abb4604f 2019-07-27 stsp arg.status_path = path;
3679 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3680 abb4604f 2019-07-27 stsp arg.repo = repo;
3681 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3682 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3683 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3684 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3685 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3686 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3687 022fae89 2019-12-06 tracey if (!no_ignores) {
3688 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3689 3143d852 2020-06-25 stsp worktree->root_path, path);
3691 3143d852 2020-06-25 stsp goto done;
3693 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3694 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3695 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3698 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3699 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3700 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3701 927df6b7 2019-02-10 stsp free(ondisk_path);
3702 347d1d3e 2019-07-12 stsp return err;
3705 347d1d3e 2019-07-12 stsp const struct got_error *
3706 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3707 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3708 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3709 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3711 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3712 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3713 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3714 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3716 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3718 347d1d3e 2019-07-12 stsp return err;
3720 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3721 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3722 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3723 f6343036 2021-06-22 stsp no_ignores, 0);
3727 f8d1f275 2019-02-04 stsp free(fileindex_path);
3728 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3729 f8d1f275 2019-02-04 stsp return err;
3732 6c7ab921 2019-03-18 stsp const struct got_error *
3733 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3734 6c7ab921 2019-03-18 stsp const char *arg)
3736 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3737 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3738 6c7ab921 2019-03-18 stsp size_t len;
3739 00bb5ea0 2020-07-23 stsp struct stat sb;
3740 01740607 2020-11-04 stsp char *abspath = NULL;
3741 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3743 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3745 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3746 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3747 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3749 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3750 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3751 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3752 00bb5ea0 2020-07-23 stsp goto done;
3754 727173c3 2020-11-06 stsp sb.st_mode = 0;
3756 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3758 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3759 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3760 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3761 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3763 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3764 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3765 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3766 00bb5ea0 2020-07-23 stsp goto done;
3770 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3771 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3773 d0710d08 2019-07-22 stsp goto done;
3774 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3775 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3776 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3777 00bb5ea0 2020-07-23 stsp goto done;
3780 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3781 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3782 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3783 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3784 00bb5ea0 2020-07-23 stsp goto done;
3786 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3787 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3788 01740607 2020-11-04 stsp goto done;
3790 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3791 01740607 2020-11-04 stsp sizeof(canonpath));
3793 00bb5ea0 2020-07-23 stsp goto done;
3794 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3795 01740607 2020-11-04 stsp if (resolved == NULL) {
3796 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3797 01740607 2020-11-04 stsp goto done;
3802 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3803 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3804 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3805 6c7ab921 2019-03-18 stsp goto done;
3808 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3809 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3810 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3812 f6d88e1a 2019-05-29 stsp goto done;
3814 f6d88e1a 2019-05-29 stsp path = strdup("");
3815 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3816 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3817 f6d88e1a 2019-05-29 stsp goto done;
3821 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3822 6c7ab921 2019-03-18 stsp len = strlen(path);
3823 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3824 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3828 01740607 2020-11-04 stsp free(abspath);
3829 6c7ab921 2019-03-18 stsp free(resolved);
3830 d0710d08 2019-07-22 stsp free(cwd);
3831 6c7ab921 2019-03-18 stsp if (err == NULL)
3832 6c7ab921 2019-03-18 stsp *wt_path = path;
3834 6c7ab921 2019-03-18 stsp free(path);
3835 d00136be 2019-03-26 stsp return err;
3838 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3839 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3840 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3841 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3842 4e68cba3 2019-11-23 stsp void *progress_arg;
3843 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3846 4e68cba3 2019-11-23 stsp static const struct got_error *
3847 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3848 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3849 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3850 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3852 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3853 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3854 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3855 6d022e97 2019-08-04 stsp struct stat sb;
3856 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3858 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3859 dbb83fbd 2019-12-12 stsp relpath) == -1)
3860 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3862 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3864 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3865 12463d8b 2019-12-13 stsp de_name, a->repo);
3867 dbb83fbd 2019-12-12 stsp goto done;
3868 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3869 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3870 dbb83fbd 2019-12-12 stsp goto done;
3871 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3873 dbb83fbd 2019-12-12 stsp goto done;
3876 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3877 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
3878 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
3880 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3881 dbb83fbd 2019-12-12 stsp goto done;
3884 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3886 4e68cba3 2019-11-23 stsp goto done;
3887 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3888 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3889 3969253a 2020-03-07 stsp if (err) {
3890 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3891 3969253a 2020-03-07 stsp goto done;
3893 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3894 07f5b47a 2019-06-02 stsp if (err) {
3895 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3896 4e68cba3 2019-11-23 stsp goto done;
3899 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3901 dbb83fbd 2019-12-12 stsp return err;
3902 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3903 dbb83fbd 2019-12-12 stsp return NULL;
3904 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3907 d00136be 2019-03-26 stsp const struct got_error *
3908 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3909 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3910 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3911 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3913 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3914 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3915 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3916 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3917 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3919 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3921 d00136be 2019-03-26 stsp return err;
3923 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3925 d00136be 2019-03-26 stsp goto done;
3927 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3928 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3929 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3930 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3931 4e68cba3 2019-11-23 stsp saa.repo = repo;
3933 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3934 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3935 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3939 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3940 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3941 af12c6b9 2019-06-04 stsp err = sync_err;
3943 fb399478 2019-07-12 stsp free(fileindex_path);
3944 d00136be 2019-03-26 stsp if (fileindex)
3945 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3946 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3947 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3948 d00136be 2019-03-26 stsp err = unlockerr;
3949 6c7ab921 2019-03-18 stsp return err;
3952 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3953 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3954 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3955 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3956 f2a9dc41 2019-12-13 tracey void *progress_arg;
3957 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3958 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3959 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3960 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
3961 766841c2 2020-08-13 stsp const char *status_codes;
3964 f2a9dc41 2019-12-13 tracey static const struct got_error *
3965 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3966 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3967 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3968 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3970 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3971 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3972 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3973 17ed4618 2019-06-02 stsp struct stat sb;
3974 20a2ad1c 2020-10-20 stsp char *ondisk_path;
3976 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
3977 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
3978 4e12cd97 2022-01-25 stsp return NULL;
3979 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
3982 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3983 17ed4618 2019-06-02 stsp if (ie == NULL)
3984 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
3986 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3987 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3988 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3989 9acbc4fa 2019-08-03 stsp return NULL;
3990 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3993 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3994 f2a9dc41 2019-12-13 tracey relpath) == -1)
3995 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3997 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4000 f2a9dc41 2019-12-13 tracey goto done;
4002 766841c2 2020-08-13 stsp if (a->status_codes) {
4003 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4005 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4006 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4009 766841c2 2020-08-13 stsp if (i == ncodes) {
4010 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4011 766841c2 2020-08-13 stsp free(ondisk_path);
4012 766841c2 2020-08-13 stsp return NULL;
4014 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4015 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4016 766841c2 2020-08-13 stsp static char msg[64];
4017 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4018 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4019 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4020 766841c2 2020-08-13 stsp goto done;
4024 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4025 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4026 f2a9dc41 2019-12-13 tracey goto done;
4027 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4028 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4029 f2a9dc41 2019-12-13 tracey goto done;
4031 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4032 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4033 4e12cd97 2022-01-25 stsp goto done;
4035 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4036 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4037 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4038 f2a9dc41 2019-12-13 tracey goto done;
4042 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4043 20a2ad1c 2020-10-20 stsp size_t root_len;
4045 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4046 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
4047 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4048 12463d8b 2019-12-13 stsp ondisk_path);
4049 12463d8b 2019-12-13 stsp goto done;
4051 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
4052 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4053 12463d8b 2019-12-13 stsp goto done;
4056 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4058 20a2ad1c 2020-10-20 stsp char *parent;
4059 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4061 2513f20a 2020-10-20 stsp goto done;
4062 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4063 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4064 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4065 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4066 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4067 20a2ad1c 2020-10-20 stsp ondisk_path);
4070 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4071 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4074 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4076 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4077 f2a9dc41 2019-12-13 tracey if (err)
4078 f2a9dc41 2019-12-13 tracey return err;
4079 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4080 f2a9dc41 2019-12-13 tracey return NULL;
4081 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4082 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4085 2ec1f75b 2019-03-26 stsp const struct got_error *
4086 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4087 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4088 766841c2 2020-08-13 stsp const char *status_codes,
4089 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4090 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4092 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4093 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4094 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4095 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4096 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4098 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4100 2ec1f75b 2019-03-26 stsp return err;
4102 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4104 2ec1f75b 2019-03-26 stsp goto done;
4106 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4107 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4108 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4109 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4110 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4111 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4112 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4113 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4114 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4116 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4117 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4118 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4122 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4123 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4124 af12c6b9 2019-06-04 stsp err = sync_err;
4126 fb399478 2019-07-12 stsp free(fileindex_path);
4127 2ec1f75b 2019-03-26 stsp if (fileindex)
4128 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4129 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4130 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4131 2ec1f75b 2019-03-26 stsp err = unlockerr;
4132 33aa809d 2019-08-08 stsp return err;
4135 33aa809d 2019-08-08 stsp static const struct got_error *
4136 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4138 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4139 33aa809d 2019-08-08 stsp char *line = NULL;
4140 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4141 33aa809d 2019-08-08 stsp ssize_t linelen;
4143 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4144 33aa809d 2019-08-08 stsp if (linelen == -1) {
4145 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4146 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4147 33aa809d 2019-08-08 stsp goto done;
4149 33aa809d 2019-08-08 stsp return NULL;
4151 33aa809d 2019-08-08 stsp if (outfile) {
4152 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4153 33aa809d 2019-08-08 stsp if (n != linelen) {
4154 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4155 33aa809d 2019-08-08 stsp goto done;
4158 33aa809d 2019-08-08 stsp if (rejectfile) {
4159 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4160 33aa809d 2019-08-08 stsp if (n != linelen)
4161 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4164 33aa809d 2019-08-08 stsp free(line);
4165 2ec1f75b 2019-03-26 stsp return err;
4168 33aa809d 2019-08-08 stsp static const struct got_error *
4169 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4171 33aa809d 2019-08-08 stsp char *line = NULL;
4172 33aa809d 2019-08-08 stsp size_t linesize = 0;
4173 33aa809d 2019-08-08 stsp ssize_t linelen;
4175 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4176 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4177 500467ff 2019-09-25 hiltjo if (ferror(f))
4178 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4179 500467ff 2019-09-25 hiltjo return NULL;
4181 33aa809d 2019-08-08 stsp free(line);
4182 33aa809d 2019-08-08 stsp return NULL;
4185 33aa809d 2019-08-08 stsp static const struct got_error *
4186 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4187 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4188 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4190 33aa809d 2019-08-08 stsp const struct got_error *err;
4192 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4193 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4194 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4196 33aa809d 2019-08-08 stsp return err;
4197 33aa809d 2019-08-08 stsp (*line_cur1)++;
4199 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4200 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4201 33aa809d 2019-08-08 stsp if (rejectfile)
4202 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4204 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4206 33aa809d 2019-08-08 stsp return err;
4207 33aa809d 2019-08-08 stsp (*line_cur2)++;
4209 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4210 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4211 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4213 33aa809d 2019-08-08 stsp return err;
4214 33aa809d 2019-08-08 stsp (*line_cur2)++;
4216 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4217 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4218 33aa809d 2019-08-08 stsp if (rejectfile)
4219 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4221 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4223 33aa809d 2019-08-08 stsp return err;
4224 33aa809d 2019-08-08 stsp (*line_cur1)++;
4227 f1e81a05 2019-08-10 stsp return NULL;
4230 f1e81a05 2019-08-10 stsp static const struct got_error *
4231 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4232 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4234 f1e81a05 2019-08-10 stsp const struct got_error *err;
4236 f1e81a05 2019-08-10 stsp if (outfile) {
4237 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4238 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4239 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4241 f1e81a05 2019-08-10 stsp return err;
4242 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4245 f1e81a05 2019-08-10 stsp if (rejectfile) {
4246 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4247 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4248 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4250 f1e81a05 2019-08-10 stsp return err;
4251 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4255 33aa809d 2019-08-08 stsp return NULL;
4258 33aa809d 2019-08-08 stsp static const struct got_error *
4259 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4260 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4261 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4262 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4263 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4265 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4266 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4267 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4268 33aa809d 2019-08-08 stsp FILE *hunkfile;
4269 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4270 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4273 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4275 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4276 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4277 fe621944 2020-11-10 stsp start_old = cc.left.start;
4278 fe621944 2020-11-10 stsp end_old = cc.left.end;
4279 fe621944 2020-11-10 stsp start_new = cc.right.start;
4280 fe621944 2020-11-10 stsp end_new = cc.right.end;
4282 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4283 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4284 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4286 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4287 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4288 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4290 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4291 fe621944 2020-11-10 stsp if (diff_state == NULL)
4292 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4293 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4295 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4296 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4297 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4298 33aa809d 2019-08-08 stsp goto done;
4301 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4302 fe621944 2020-11-10 stsp diff_result, &cc);
4303 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4304 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4305 33aa809d 2019-08-08 stsp goto done;
4308 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4309 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4310 33aa809d 2019-08-08 stsp goto done;
4313 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4314 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4316 33aa809d 2019-08-08 stsp goto done;
4318 33aa809d 2019-08-08 stsp switch (*choice) {
4319 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4320 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4321 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4323 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4324 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4325 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4327 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4330 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4334 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4335 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4336 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4337 33aa809d 2019-08-08 stsp return err;
4340 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4341 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4342 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4343 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4344 1f1abb7e 2019-08-08 stsp void *progress_arg;
4345 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4346 33aa809d 2019-08-08 stsp void *patch_arg;
4347 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4348 f259c4c1 2021-09-24 stsp int unlink_added_files;
4351 e20a8b6f 2019-06-04 stsp static const struct got_error *
4352 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4353 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4354 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4355 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4356 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4358 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4359 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4360 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4361 1ebedb77 2019-10-19 stsp int fd2 = -1;
4362 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4363 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4364 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4365 fe621944 2020-11-10 stsp struct stat sb2;
4366 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4367 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4368 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4370 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4372 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4374 33aa809d 2019-08-08 stsp return err;
4376 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4377 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4378 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4379 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4380 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4381 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4382 fa3cef63 2020-07-23 stsp goto done;
4384 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4385 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4386 c0df5966 2021-12-31 stsp if (link_len == -1) {
4387 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4390 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4391 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4394 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4395 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4396 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4397 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4398 fa3cef63 2020-07-23 stsp goto done;
4400 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4401 fa3cef63 2020-07-23 stsp sizeof(link_target));
4402 fa3cef63 2020-07-23 stsp if (link_len == -1)
4403 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4404 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4405 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4408 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4409 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4410 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4411 fa3cef63 2020-07-23 stsp goto done;
4414 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4415 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4416 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4417 fa3cef63 2020-07-23 stsp goto done;
4422 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4423 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4424 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4425 fa3cef63 2020-07-23 stsp goto done;
4427 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4428 fa3cef63 2020-07-23 stsp if (n != link_len) {
4429 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4430 fa3cef63 2020-07-23 stsp goto done;
4432 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4433 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4434 fa3cef63 2020-07-23 stsp goto done;
4436 fa3cef63 2020-07-23 stsp rewind(f2);
4439 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4441 33aa809d 2019-08-08 stsp goto done;
4443 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4445 33aa809d 2019-08-08 stsp goto done;
4447 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4449 33aa809d 2019-08-08 stsp goto done;
4451 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4454 33aa809d 2019-08-08 stsp goto done;
4456 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4458 33aa809d 2019-08-08 stsp goto done;
4460 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4461 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4462 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4463 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4465 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4466 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4467 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4468 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4469 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4470 fe621944 2020-11-10 stsp nchanges++;
4472 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4473 33aa809d 2019-08-08 stsp int choice;
4474 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4475 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4476 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4477 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4478 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4479 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4481 33aa809d 2019-08-08 stsp goto done;
4482 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4483 33aa809d 2019-08-08 stsp have_content = 1;
4484 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4487 1ebedb77 2019-10-19 stsp if (have_content) {
4488 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4489 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4490 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4492 1ebedb77 2019-10-19 stsp goto done;
4494 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4495 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4496 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4497 fa3cef63 2020-07-23 stsp goto done;
4502 33aa809d 2019-08-08 stsp free(id_str);
4504 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4505 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4506 fe621944 2020-11-10 stsp if (err == NULL)
4507 fe621944 2020-11-10 stsp err = free_err;
4508 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4509 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4510 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4511 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4512 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4513 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4514 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4515 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4516 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4517 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4518 33aa809d 2019-08-08 stsp if (err || !have_content) {
4519 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4520 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4521 33aa809d 2019-08-08 stsp free(*path_outfile);
4522 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4524 33aa809d 2019-08-08 stsp free(path1);
4525 33aa809d 2019-08-08 stsp return err;
4528 33aa809d 2019-08-08 stsp static const struct got_error *
4529 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4530 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4531 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4532 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4534 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4535 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4536 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4537 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4538 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4539 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4540 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4541 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4542 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4543 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4545 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4546 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4547 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4548 d3bcc3d1 2019-08-08 stsp return NULL;
4550 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4551 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4552 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4554 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4555 65084dad 2019-08-08 stsp if (ie == NULL)
4556 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4558 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4559 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4560 a129376b 2019-03-28 stsp if (err) {
4561 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4562 a129376b 2019-03-28 stsp goto done;
4563 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4564 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4565 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4566 e20a8b6f 2019-06-04 stsp goto done;
4569 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4570 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4571 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4572 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4573 a129376b 2019-03-28 stsp goto done;
4576 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4577 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4578 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4579 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4580 a129376b 2019-03-28 stsp goto done;
4583 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4584 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4585 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4586 a129376b 2019-03-28 stsp goto done;
4591 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4592 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4593 a9fa2909 2019-07-27 stsp if (err) {
4594 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4595 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4596 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4597 a9fa2909 2019-07-27 stsp goto done;
4599 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4601 a9fa2909 2019-07-27 stsp goto done;
4603 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4605 a9fa2909 2019-07-27 stsp goto done;
4607 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4608 1233e6b6 2020-10-19 stsp free(te_name);
4609 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4610 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4611 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4612 a9fa2909 2019-07-27 stsp goto done;
4616 a129376b 2019-03-28 stsp switch (status) {
4617 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4618 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4619 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4620 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4621 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4623 33aa809d 2019-08-08 stsp goto done;
4624 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4627 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4628 1f1abb7e 2019-08-08 stsp ie->path);
4630 1ee397ad 2019-07-12 stsp goto done;
4631 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4632 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
4633 f259c4c1 2021-09-24 stsp if (asprintf(&ondisk_path, "%s/%s",
4634 f259c4c1 2021-09-24 stsp got_worktree_get_root_path(a->worktree),
4635 f259c4c1 2021-09-24 stsp relpath) == -1) {
4636 f259c4c1 2021-09-24 stsp err = got_error_from_errno("asprintf");
4637 f259c4c1 2021-09-24 stsp goto done;
4639 f259c4c1 2021-09-24 stsp if (unlink(ondisk_path) == -1) {
4640 f259c4c1 2021-09-24 stsp err = got_error_from_errno2("unlink",
4641 f259c4c1 2021-09-24 stsp ondisk_path);
4646 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4647 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4648 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4649 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4650 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4652 33aa809d 2019-08-08 stsp goto done;
4653 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4656 33aa809d 2019-08-08 stsp /* fall through */
4657 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4658 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4659 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4660 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4661 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4662 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4663 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4664 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4665 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4667 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4668 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4669 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4671 65084dad 2019-08-08 stsp goto done;
4673 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4674 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4675 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4676 a129376b 2019-03-28 stsp goto done;
4679 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4680 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4681 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4682 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4683 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4684 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4685 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4687 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4688 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4689 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4690 369fd7e5 2020-07-23 stsp path_content);
4693 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4694 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4695 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4696 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4698 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4699 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4700 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4701 369fd7e5 2020-07-23 stsp goto done;
4705 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4706 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4707 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4708 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4709 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4710 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4712 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4714 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4715 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4716 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4717 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4720 a129376b 2019-03-28 stsp goto done;
4721 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4722 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4723 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4724 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4725 437adc9d 2020-12-10 yzhong blob->id.sha1,
4726 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4728 2e1fa222 2020-07-23 stsp goto done;
4730 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4731 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4732 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4741 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4742 33aa809d 2019-08-08 stsp free(path_content);
4743 e20a8b6f 2019-06-04 stsp free(parent_path);
4744 a129376b 2019-03-28 stsp free(tree_path);
4746 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4748 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4749 a129376b 2019-03-28 stsp free(tree_id);
4750 e20a8b6f 2019-06-04 stsp return err;
4753 e20a8b6f 2019-06-04 stsp const struct got_error *
4754 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4755 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4756 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4757 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4758 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4760 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4761 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4762 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4763 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4764 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4765 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4767 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4769 e20a8b6f 2019-06-04 stsp return err;
4771 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4773 e20a8b6f 2019-06-04 stsp goto done;
4775 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4776 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4777 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4778 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4779 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4780 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4781 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4782 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
4783 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4784 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4785 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
4789 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4790 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4791 e20a8b6f 2019-06-04 stsp err = sync_err;
4793 fb399478 2019-07-12 stsp free(fileindex_path);
4794 a129376b 2019-03-28 stsp if (fileindex)
4795 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4796 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4797 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4798 a129376b 2019-03-28 stsp err = unlockerr;
4799 c4296144 2019-05-09 stsp return err;
4802 cf066bf8 2019-05-09 stsp static void
4803 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4805 24519714 2019-05-09 stsp free(ct->path);
4806 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4807 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4808 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4809 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4810 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4811 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4815 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4816 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4817 24519714 2019-05-09 stsp struct got_repository *repo;
4818 24519714 2019-05-09 stsp struct got_worktree *worktree;
4819 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4820 5f8a88c6 2019-08-03 stsp int have_staged_files;
4821 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4824 c4296144 2019-05-09 stsp static const struct got_error *
4825 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
4826 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
4827 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4828 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4830 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
4831 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4832 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4833 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4834 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4835 768aea60 2019-05-09 stsp struct stat sb;
4837 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
4838 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4839 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4840 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4841 5f8a88c6 2019-08-03 stsp return NULL;
4843 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4844 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4846 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4847 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4848 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4849 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4850 5f8a88c6 2019-08-03 stsp return NULL;
4853 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4854 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4855 036813ee 2019-05-09 stsp goto done;
4857 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4858 036813ee 2019-05-09 stsp parent_path = strdup("");
4859 69960a46 2019-05-09 stsp if (parent_path == NULL)
4860 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4862 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4864 69960a46 2019-05-09 stsp return err;
4867 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4868 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4869 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4870 c4296144 2019-05-09 stsp goto done;
4873 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4874 768aea60 2019-05-09 stsp relpath) == -1) {
4875 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4876 768aea60 2019-05-09 stsp goto done;
4879 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4880 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4881 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4882 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4883 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4884 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4885 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4886 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4888 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4889 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4892 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4893 0aeb8099 2020-07-23 stsp goto done;
4895 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4896 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4897 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4898 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4899 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4900 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4901 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4902 12463d8b 2019-12-13 stsp ct->ondisk_path);
4903 12463d8b 2019-12-13 stsp goto done;
4905 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4906 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4907 768aea60 2019-05-09 stsp goto done;
4909 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4912 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4913 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4914 44d03001 2019-05-09 stsp relpath) == -1) {
4915 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4916 44d03001 2019-05-09 stsp goto done;
4919 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4920 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4921 35213c7c 2020-07-23 stsp int is_bad_symlink;
4922 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4923 35213c7c 2020-07-23 stsp ssize_t target_len;
4924 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4925 35213c7c 2020-07-23 stsp sizeof(target_path));
4926 35213c7c 2020-07-23 stsp if (target_len == -1) {
4927 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4928 35213c7c 2020-07-23 stsp ct->ondisk_path);
4929 35213c7c 2020-07-23 stsp goto done;
4931 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4932 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4934 35213c7c 2020-07-23 stsp goto done;
4935 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4936 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4937 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4938 35213c7c 2020-07-23 stsp goto done;
4943 cf066bf8 2019-05-09 stsp ct->status = status;
4944 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4945 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4946 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4947 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4948 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4949 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4950 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4951 b416585c 2019-05-13 stsp goto done;
4953 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4954 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4955 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4956 f0b75401 2019-08-03 stsp goto done;
4959 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4960 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4961 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4962 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4963 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4964 036813ee 2019-05-09 stsp goto done;
4967 24519714 2019-05-09 stsp ct->path = strdup(path);
4968 24519714 2019-05-09 stsp if (ct->path == NULL) {
4969 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4970 24519714 2019-05-09 stsp goto done;
4972 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4974 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4975 ed175427 2019-05-09 stsp free_commitable(ct);
4976 24519714 2019-05-09 stsp free(parent_path);
4977 036813ee 2019-05-09 stsp free(path);
4978 a129376b 2019-03-28 stsp return err;
4981 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4982 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4983 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4984 036813ee 2019-05-09 stsp struct got_repository *);
4986 ed175427 2019-05-09 stsp static const struct got_error *
4987 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4988 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4989 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4990 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4991 afa376bf 2019-05-09 stsp struct got_repository *repo)
4993 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4994 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4995 036813ee 2019-05-09 stsp char *subpath;
4997 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4998 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4999 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5001 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5003 ed175427 2019-05-09 stsp return err;
5005 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5006 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5007 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5008 036813ee 2019-05-09 stsp free(subpath);
5009 036813ee 2019-05-09 stsp return err;
5012 036813ee 2019-05-09 stsp static const struct got_error *
5013 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5015 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5016 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5018 036813ee 2019-05-09 stsp *match = 0;
5020 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5021 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5022 0f63689d 2019-05-10 stsp return NULL;
5025 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5027 0f63689d 2019-05-10 stsp return err;
5028 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5029 036813ee 2019-05-09 stsp free(ct_parent_path);
5030 036813ee 2019-05-09 stsp return err;
5033 768aea60 2019-05-09 stsp static mode_t
5034 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5036 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5037 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5039 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5042 036813ee 2019-05-09 stsp static const struct got_error *
5043 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5044 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5046 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5048 036813ee 2019-05-09 stsp *new_te = NULL;
5050 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5052 036813ee 2019-05-09 stsp goto done;
5054 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5056 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5057 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5058 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5060 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5062 036813ee 2019-05-09 stsp if (err && *new_te) {
5063 56e0773d 2019-11-28 stsp free(*new_te);
5064 036813ee 2019-05-09 stsp *new_te = NULL;
5066 036813ee 2019-05-09 stsp return err;
5069 036813ee 2019-05-09 stsp static const struct got_error *
5070 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5071 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5073 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5074 102b254e 2020-10-19 stsp char *ct_name = NULL;
5076 036813ee 2019-05-09 stsp *new_te = NULL;
5078 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5079 036813ee 2019-05-09 stsp if (*new_te == NULL)
5080 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5082 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5084 036813ee 2019-05-09 stsp goto done;
5085 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5086 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5087 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5088 036813ee 2019-05-09 stsp goto done;
5091 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5093 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5094 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5095 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5097 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5099 102b254e 2020-10-19 stsp free(ct_name);
5100 036813ee 2019-05-09 stsp if (err && *new_te) {
5101 56e0773d 2019-11-28 stsp free(*new_te);
5102 036813ee 2019-05-09 stsp *new_te = NULL;
5104 036813ee 2019-05-09 stsp return err;
5107 036813ee 2019-05-09 stsp static const struct got_error *
5108 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5109 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5111 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5112 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5114 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5116 036813ee 2019-05-09 stsp return err;
5117 036813ee 2019-05-09 stsp if (new_pe == NULL)
5118 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5119 036813ee 2019-05-09 stsp return NULL;
5122 afa376bf 2019-05-09 stsp static const struct got_error *
5123 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5124 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5126 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5127 5f8a88c6 2019-08-03 stsp unsigned char status;
5129 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5130 0ff8d236 2021-09-28 stsp return NULL;
5132 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5133 afa376bf 2019-05-09 stsp ct_path++;
5135 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5136 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5138 5f8a88c6 2019-08-03 stsp status = ct->status;
5140 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5141 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5144 036813ee 2019-05-09 stsp static const struct got_error *
5145 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5146 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5148 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5149 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5150 44d03001 2019-05-09 stsp char *te_path;
5152 44d03001 2019-05-09 stsp *modified = 0;
5154 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5155 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5156 44d03001 2019-05-09 stsp te->name) == -1)
5157 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5159 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5160 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5161 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5162 44d03001 2019-05-09 stsp strlen(te_path));
5163 62d463ca 2020-10-20 naddy if (*modified)
5167 44d03001 2019-05-09 stsp free(te_path);
5168 44d03001 2019-05-09 stsp return err;
5171 44d03001 2019-05-09 stsp static const struct got_error *
5172 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5173 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5174 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5176 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5177 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5179 036813ee 2019-05-09 stsp *ctp = NULL;
5181 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5182 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5183 036813ee 2019-05-09 stsp char *ct_name = NULL;
5184 036813ee 2019-05-09 stsp int path_matches;
5186 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5187 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5188 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5189 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5192 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5193 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5197 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5200 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5202 036813ee 2019-05-09 stsp return err;
5203 036813ee 2019-05-09 stsp if (!path_matches)
5206 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5208 d34b633e 2020-10-19 stsp return err;
5210 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5211 d34b633e 2020-10-19 stsp free(ct_name);
5214 d34b633e 2020-10-19 stsp free(ct_name);
5216 036813ee 2019-05-09 stsp *ctp = ct;
5220 2b496619 2019-07-10 stsp return err;
5223 2b496619 2019-07-10 stsp static const struct got_error *
5224 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5225 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5226 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5227 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5228 2b496619 2019-07-10 stsp struct got_repository *repo)
5230 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5231 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5232 2b496619 2019-07-10 stsp char *subtree_path;
5233 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5234 ba580f68 2020-03-22 stsp int nentries;
5236 2b496619 2019-07-10 stsp *new_tep = NULL;
5238 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5239 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5240 2b496619 2019-07-10 stsp child_path) == -1)
5241 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5243 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5244 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5245 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5246 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5248 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5249 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5250 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5251 2b496619 2019-07-10 stsp goto done;
5253 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5254 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5255 2b496619 2019-07-10 stsp if (err) {
5256 56e0773d 2019-11-28 stsp free(new_te);
5257 2b496619 2019-07-10 stsp goto done;
5259 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5262 2b496619 2019-07-10 stsp free(subtree_path);
5263 2b496619 2019-07-10 stsp if (err == NULL)
5264 2b496619 2019-07-10 stsp *new_tep = new_te;
5265 036813ee 2019-05-09 stsp return err;
5268 036813ee 2019-05-09 stsp static const struct got_error *
5269 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5270 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5271 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5272 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5273 036813ee 2019-05-09 stsp struct got_repository *repo)
5275 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5276 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5277 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5278 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5280 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5281 ba580f68 2020-03-22 stsp *nentries = 0;
5283 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5284 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5285 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5286 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5288 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5289 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5290 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5293 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5294 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5297 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5298 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5300 036813ee 2019-05-09 stsp goto done;
5302 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5303 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5304 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5306 036813ee 2019-05-09 stsp goto done;
5307 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5309 afa376bf 2019-05-09 stsp goto done;
5310 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5311 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5313 2f51b5b3 2019-05-09 stsp goto done;
5314 ba580f68 2020-03-22 stsp (*nentries)++;
5316 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5317 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5318 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5319 2b496619 2019-07-10 stsp == NULL) {
5320 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5321 2b496619 2019-07-10 stsp child_path, path_base_tree,
5322 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5325 2b496619 2019-07-10 stsp goto done;
5326 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5328 2b496619 2019-07-10 stsp goto done;
5329 ba580f68 2020-03-22 stsp (*nentries)++;
5334 2f51b5b3 2019-05-09 stsp if (base_tree) {
5335 56e0773d 2019-11-28 stsp int i, nbase_entries;
5336 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5337 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5338 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5339 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5341 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5342 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5343 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5344 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5346 63c5ca5d 2019-08-24 stsp goto done;
5347 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5349 63c5ca5d 2019-08-24 stsp goto done;
5350 ba580f68 2020-03-22 stsp (*nentries)++;
5354 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5355 44d03001 2019-05-09 stsp int modified;
5356 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5358 036813ee 2019-05-09 stsp goto done;
5359 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5360 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5362 2f51b5b3 2019-05-09 stsp goto done;
5363 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5364 44d03001 2019-05-09 stsp if (modified) {
5365 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5366 ba580f68 2020-03-22 stsp int nsubentries;
5367 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5368 ba580f68 2020-03-22 stsp &nsubentries, te,
5369 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5370 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5372 44d03001 2019-05-09 stsp goto done;
5373 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5374 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5375 ba580f68 2020-03-22 stsp free(new_id);
5378 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5379 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5380 56e0773d 2019-11-28 stsp free(new_id);
5382 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5384 036813ee 2019-05-09 stsp goto done;
5385 ba580f68 2020-03-22 stsp (*nentries)++;
5389 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5390 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5392 f66c734c 2019-09-22 stsp goto done;
5394 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5395 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5396 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5397 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5398 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5399 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5401 2f51b5b3 2019-05-09 stsp goto done;
5402 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5404 2f51b5b3 2019-05-09 stsp goto done;
5405 ba580f68 2020-03-22 stsp (*nentries)++;
5407 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5408 b416585c 2019-05-13 stsp status_arg);
5410 afa376bf 2019-05-09 stsp goto done;
5412 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5413 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5415 2f51b5b3 2019-05-09 stsp goto done;
5416 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5418 2f51b5b3 2019-05-09 stsp goto done;
5419 ba580f68 2020-03-22 stsp (*nentries)++;
5424 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5425 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5427 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5428 2e1fa222 2020-07-23 stsp return err;
5431 2e1fa222 2020-07-23 stsp static const struct got_error *
5432 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5433 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5434 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5435 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5437 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5438 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5439 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5441 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5442 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5443 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5445 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5447 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5448 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5449 437adc9d 2020-12-10 yzhong if (err)
5450 437adc9d 2020-12-10 yzhong goto done;
5453 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5454 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5455 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5456 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5457 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5458 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5459 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5460 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5462 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5463 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5464 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5465 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5466 72fd46fa 2019-09-06 stsp !have_staged_files);
5468 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5469 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5470 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5471 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5472 72fd46fa 2019-09-06 stsp !have_staged_files);
5474 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5476 437adc9d 2020-12-10 yzhong goto done;
5477 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5478 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5479 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5480 3969253a 2020-03-07 stsp if (err) {
5481 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5482 437adc9d 2020-12-10 yzhong goto done;
5484 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5485 3969253a 2020-03-07 stsp if (err) {
5486 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5487 437adc9d 2020-12-10 yzhong goto done;
5490 437adc9d 2020-12-10 yzhong free(relpath);
5491 437adc9d 2020-12-10 yzhong relpath = NULL;
5494 437adc9d 2020-12-10 yzhong free(relpath);
5495 d56d26ce 2019-05-10 stsp return err;
5499 d56d26ce 2019-05-10 stsp static const struct got_error *
5500 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5501 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5502 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5503 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5504 735ef5ac 2019-08-03 stsp int ood_errcode)
5506 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5507 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5509 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5510 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5511 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5512 1a36436d 2019-06-10 stsp return NULL;
5514 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5515 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5517 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5518 735ef5ac 2019-08-03 stsp in_repo_path);
5519 9bead371 2019-07-28 stsp if (err) {
5520 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5521 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5522 1a36436d 2019-06-10 stsp goto done;
5523 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5524 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5526 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5527 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5528 735ef5ac 2019-08-03 stsp in_repo_path);
5529 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5530 1a36436d 2019-06-10 stsp goto done;
5531 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5535 1a36436d 2019-06-10 stsp return err;
5538 c4296144 2019-05-09 stsp const struct got_error *
5539 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5540 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5541 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
5542 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
5543 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
5544 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5545 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5546 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5547 afa376bf 2019-05-09 stsp struct got_repository *repo)
5549 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5550 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5551 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5552 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5553 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5554 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5555 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5556 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5557 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
5558 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5559 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5560 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5562 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5564 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5566 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5568 588edf97 2019-05-10 stsp goto done;
5570 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5572 588edf97 2019-05-10 stsp goto done;
5574 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5575 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5580 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5581 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5585 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5586 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5587 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5588 cf066bf8 2019-05-09 stsp char *ondisk_path;
5590 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5591 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5592 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5595 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5596 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5597 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5600 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5601 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5602 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5603 cf066bf8 2019-05-09 stsp goto done;
5605 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5606 2e1fa222 2020-07-23 stsp free(ondisk_path);
5608 cf066bf8 2019-05-09 stsp goto done;
5611 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5612 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5613 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5615 036813ee 2019-05-09 stsp goto done;
5617 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5619 de18fc63 2019-05-09 stsp goto done;
5620 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5621 f259c4c1 2021-09-24 stsp nparents++;
5622 f259c4c1 2021-09-24 stsp if (parent_id2) {
5623 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
5625 f259c4c1 2021-09-24 stsp goto done;
5626 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5627 f259c4c1 2021-09-24 stsp nparents++;
5629 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5630 f259c4c1 2021-09-24 stsp nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5631 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5632 33ad4cbe 2019-05-12 jcs free(logmsg);
5634 9d40349a 2019-05-09 stsp goto done;
5636 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5637 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5638 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5639 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5640 09f5bd90 2019-05-09 stsp goto done;
5642 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5643 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5645 09f5bd90 2019-05-09 stsp goto done;
5646 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5648 ebf99748 2019-05-09 stsp goto done;
5649 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5650 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5651 09f5bd90 2019-05-09 stsp goto done;
5653 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5654 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5656 f2c16586 2019-05-09 stsp goto done;
5657 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5659 f2c16586 2019-05-09 stsp goto done;
5661 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5663 09f5bd90 2019-05-09 stsp goto done;
5665 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5667 ebf99748 2019-05-09 stsp goto done;
5669 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
5670 588edf97 2019-05-10 stsp if (head_tree)
5671 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5672 588edf97 2019-05-10 stsp if (head_commit)
5673 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5674 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5675 2f17228e 2019-05-12 stsp if (head_ref2) {
5676 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5677 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5678 2f17228e 2019-05-12 stsp err = unlockerr;
5679 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5681 39cd0ff6 2019-07-12 stsp return err;
5684 5c1e53bc 2019-07-28 stsp static const struct got_error *
5685 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5686 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5688 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5689 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5691 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5692 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5693 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5695 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5696 5c1e53bc 2019-07-28 stsp ct_path++;
5698 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5699 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5703 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5704 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5706 5c1e53bc 2019-07-28 stsp return NULL;
5709 f0b75401 2019-08-03 stsp static const struct got_error *
5710 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5712 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5714 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5715 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5716 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5719 f0b75401 2019-08-03 stsp return NULL;
5722 5f8a88c6 2019-08-03 stsp static const struct got_error *
5723 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5724 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5726 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5727 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5729 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5730 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5732 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5733 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5734 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5735 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5736 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5737 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5740 5f8a88c6 2019-08-03 stsp return NULL;
5743 39cd0ff6 2019-07-12 stsp const struct got_error *
5744 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5745 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5746 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5747 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5748 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5749 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5751 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5752 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5753 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5754 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5755 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5756 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5757 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5758 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5759 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5761 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5763 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5765 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5767 39cd0ff6 2019-07-12 stsp goto done;
5769 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5771 39cd0ff6 2019-07-12 stsp goto done;
5773 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5775 39cd0ff6 2019-07-12 stsp goto done;
5777 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5779 39cd0ff6 2019-07-12 stsp goto done;
5781 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5782 5f8a88c6 2019-08-03 stsp &have_staged_files);
5783 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5784 5f8a88c6 2019-08-03 stsp goto done;
5785 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5786 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5788 5f8a88c6 2019-08-03 stsp goto done;
5791 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5792 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5793 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5794 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5795 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5796 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5797 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5798 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5799 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5801 5c1e53bc 2019-07-28 stsp goto done;
5804 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5805 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5806 39cd0ff6 2019-07-12 stsp goto done;
5809 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5810 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5812 5c1e53bc 2019-07-28 stsp goto done;
5815 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5816 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5817 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5819 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5820 f0b75401 2019-08-03 stsp ct_path++;
5821 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5822 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5823 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5825 b50cabdf 2019-07-12 stsp goto done;
5829 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5830 f259c4c1 2021-09-24 stsp head_commit_id, NULL, worktree, author, committer,
5831 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5833 39cd0ff6 2019-07-12 stsp goto done;
5835 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5836 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5837 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5838 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5839 39cd0ff6 2019-07-12 stsp err = sync_err;
5841 39cd0ff6 2019-07-12 stsp if (fileindex)
5842 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5843 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5844 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5845 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5846 39cd0ff6 2019-07-12 stsp err = unlockerr;
5847 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5848 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5849 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5851 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5852 c4296144 2019-05-09 stsp return err;
5855 8656d6c4 2019-05-20 stsp const char *
5856 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5858 8656d6c4 2019-05-20 stsp return ct->path;
5861 8656d6c4 2019-05-20 stsp unsigned int
5862 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5864 8656d6c4 2019-05-20 stsp return ct->status;
5867 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5868 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5869 818c7501 2019-07-11 stsp struct got_repository *repo;
5872 818c7501 2019-07-11 stsp static const struct got_error *
5873 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5875 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5876 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5877 818c7501 2019-07-11 stsp unsigned char status;
5878 818c7501 2019-07-11 stsp struct stat sb;
5879 818c7501 2019-07-11 stsp char *ondisk_path;
5881 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5882 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5883 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5884 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5886 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5888 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5890 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5891 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5892 818c7501 2019-07-11 stsp free(ondisk_path);
5894 818c7501 2019-07-11 stsp return err;
5896 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5897 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5898 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5899 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5901 818c7501 2019-07-11 stsp return NULL;
5904 818c7501 2019-07-11 stsp const struct got_error *
5905 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5906 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5907 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5908 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5910 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5911 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5912 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5913 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5914 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5915 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5916 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5918 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5919 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5920 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5922 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5924 818c7501 2019-07-11 stsp return err;
5926 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5928 818c7501 2019-07-11 stsp goto done;
5930 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5931 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5932 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5935 818c7501 2019-07-11 stsp goto done;
5937 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5939 818c7501 2019-07-11 stsp goto done;
5941 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5943 818c7501 2019-07-11 stsp goto done;
5945 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5947 818c7501 2019-07-11 stsp goto done;
5949 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5952 e51d7b55 2020-01-04 stsp goto done;
5954 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5956 818c7501 2019-07-11 stsp goto done;
5957 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5958 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5959 e51d7b55 2020-01-04 stsp goto done;
5962 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5963 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5965 818c7501 2019-07-11 stsp goto done;
5966 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5968 818c7501 2019-07-11 stsp goto done;
5970 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5972 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5974 818c7501 2019-07-11 stsp goto done;
5976 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5978 818c7501 2019-07-11 stsp goto done;
5980 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5981 818c7501 2019-07-11 stsp worktree->base_commit_id);
5983 818c7501 2019-07-11 stsp goto done;
5984 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5986 818c7501 2019-07-11 stsp goto done;
5988 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5990 818c7501 2019-07-11 stsp goto done;
5992 818c7501 2019-07-11 stsp free(fileindex_path);
5993 818c7501 2019-07-11 stsp free(tmp_branch_name);
5994 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5995 818c7501 2019-07-11 stsp free(branch_ref_name);
5996 818c7501 2019-07-11 stsp if (branch_ref)
5997 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5998 818c7501 2019-07-11 stsp if (wt_branch)
5999 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6000 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6001 818c7501 2019-07-11 stsp if (err) {
6002 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6003 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6004 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6006 818c7501 2019-07-11 stsp if (*tmp_branch) {
6007 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6008 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6010 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6011 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6012 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6014 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6016 818c7501 2019-07-11 stsp return err;
6019 818c7501 2019-07-11 stsp const struct got_error *
6020 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6021 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6022 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6023 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6025 818c7501 2019-07-11 stsp const struct got_error *err;
6026 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6027 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6028 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6029 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6030 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6032 818c7501 2019-07-11 stsp *commit_id = NULL;
6033 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6034 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6035 3e3a69f1 2019-07-25 stsp *branch = NULL;
6036 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6038 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6040 3e3a69f1 2019-07-25 stsp return err;
6042 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6044 f032f1f7 2019-08-04 stsp goto done;
6046 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6047 f032f1f7 2019-08-04 stsp &have_staged_files);
6048 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6049 f032f1f7 2019-08-04 stsp goto done;
6050 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6051 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6052 3e3a69f1 2019-07-25 stsp goto done;
6055 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6057 f032f1f7 2019-08-04 stsp goto done;
6059 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6061 818c7501 2019-07-11 stsp goto done;
6063 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6065 818c7501 2019-07-11 stsp goto done;
6067 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6069 818c7501 2019-07-11 stsp goto done;
6071 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6073 818c7501 2019-07-11 stsp goto done;
6075 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6076 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6078 818c7501 2019-07-11 stsp goto done;
6080 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6082 818c7501 2019-07-11 stsp goto done;
6084 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6086 818c7501 2019-07-11 stsp goto done;
6088 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6089 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6091 818c7501 2019-07-11 stsp goto done;
6093 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6095 818c7501 2019-07-11 stsp goto done;
6097 818c7501 2019-07-11 stsp free(commit_ref_name);
6098 818c7501 2019-07-11 stsp free(branch_ref_name);
6099 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6100 818c7501 2019-07-11 stsp if (commit_ref)
6101 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6102 818c7501 2019-07-11 stsp if (branch_ref)
6103 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6104 818c7501 2019-07-11 stsp if (err) {
6105 818c7501 2019-07-11 stsp free(*commit_id);
6106 818c7501 2019-07-11 stsp *commit_id = NULL;
6107 818c7501 2019-07-11 stsp if (*tmp_branch) {
6108 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6109 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6111 818c7501 2019-07-11 stsp if (*new_base_branch) {
6112 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6113 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6115 818c7501 2019-07-11 stsp if (*branch) {
6116 818c7501 2019-07-11 stsp got_ref_close(*branch);
6117 818c7501 2019-07-11 stsp *branch = NULL;
6119 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6120 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6121 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6123 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6125 818c7501 2019-07-11 stsp return err;
6128 818c7501 2019-07-11 stsp const struct got_error *
6129 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6131 818c7501 2019-07-11 stsp const struct got_error *err;
6132 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6134 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6136 818c7501 2019-07-11 stsp return err;
6138 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6139 818c7501 2019-07-11 stsp free(tmp_branch_name);
6140 818c7501 2019-07-11 stsp return NULL;
6143 818c7501 2019-07-11 stsp static const struct got_error *
6144 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6145 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6147 0ebf8283 2019-07-24 stsp *logmsg = arg;
6148 818c7501 2019-07-11 stsp return NULL;
6151 818c7501 2019-07-11 stsp static const struct got_error *
6152 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6153 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6154 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6155 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6157 818c7501 2019-07-11 stsp return NULL;
6160 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6161 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6162 01757395 2019-07-12 stsp void *progress_arg;
6163 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6166 01757395 2019-07-12 stsp static const struct got_error *
6167 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6169 01757395 2019-07-12 stsp const struct got_error *err;
6170 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6172 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6174 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6176 01757395 2019-07-12 stsp return err;
6178 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6179 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6180 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6181 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6182 01757395 2019-07-12 stsp return NULL;
6184 01757395 2019-07-12 stsp p = strdup(path);
6185 01757395 2019-07-12 stsp if (p == NULL)
6186 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6188 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6189 01757395 2019-07-12 stsp if (err || new == NULL)
6191 01757395 2019-07-12 stsp return err;
6195 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6197 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6199 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6200 01757395 2019-07-12 stsp free((char *)pe->path);
6202 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6205 0ebf8283 2019-07-24 stsp static const struct got_error *
6206 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6207 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6209 818c7501 2019-07-11 stsp const struct got_error *err;
6210 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6212 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6213 818c7501 2019-07-11 stsp if (err) {
6214 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6215 818c7501 2019-07-11 stsp goto done;
6216 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6218 818c7501 2019-07-11 stsp goto done;
6219 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6221 818c7501 2019-07-11 stsp goto done;
6222 de05890f 2020-03-05 stsp } else if (is_rebase) {
6223 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6226 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6228 818c7501 2019-07-11 stsp goto done;
6229 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6230 818c7501 2019-07-11 stsp free(stored_id);
6231 818c7501 2019-07-11 stsp if (cmp != 0) {
6232 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6233 818c7501 2019-07-11 stsp goto done;
6237 0ebf8283 2019-07-24 stsp if (commit_ref)
6238 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6239 0ebf8283 2019-07-24 stsp return err;
6242 0ebf8283 2019-07-24 stsp static const struct got_error *
6243 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6244 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6245 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6246 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6247 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6248 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6250 0ebf8283 2019-07-24 stsp const struct got_error *err;
6251 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6252 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6253 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6255 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6257 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6259 0ebf8283 2019-07-24 stsp return err;
6261 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6262 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6263 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6264 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6265 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6266 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6267 818c7501 2019-07-11 stsp if (commit_ref)
6268 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6269 818c7501 2019-07-11 stsp return err;
6272 818c7501 2019-07-11 stsp const struct got_error *
6273 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6274 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6275 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6276 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6277 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6278 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6280 0ebf8283 2019-07-24 stsp const struct got_error *err;
6281 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6283 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6285 0ebf8283 2019-07-24 stsp return err;
6287 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6289 0ebf8283 2019-07-24 stsp goto done;
6291 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6292 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6293 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6295 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6296 0ebf8283 2019-07-24 stsp return err;
6299 0ebf8283 2019-07-24 stsp const struct got_error *
6300 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6301 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6302 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6303 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6304 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6305 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6307 0ebf8283 2019-07-24 stsp const struct got_error *err;
6308 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6310 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6312 0ebf8283 2019-07-24 stsp return err;
6314 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6316 0ebf8283 2019-07-24 stsp goto done;
6318 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6319 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6320 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6322 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6323 0ebf8283 2019-07-24 stsp return err;
6326 0ebf8283 2019-07-24 stsp static const struct got_error *
6327 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6328 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6329 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6330 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6331 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6333 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6334 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6335 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6336 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6337 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6338 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6339 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6341 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6342 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6344 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6346 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6348 3e3a69f1 2019-07-25 stsp return err;
6350 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6351 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6352 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6353 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6355 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6356 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6358 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6359 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6360 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6361 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6362 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6363 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6365 01757395 2019-07-12 stsp if (merged_paths) {
6366 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6367 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6368 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6369 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6372 01757395 2019-07-12 stsp goto done;
6375 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6376 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6378 01757395 2019-07-12 stsp goto done;
6381 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6382 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6383 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6385 a0e95631 2019-07-12 stsp goto done;
6386 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6387 a0e95631 2019-07-12 stsp goto done;
6390 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6392 edd02c5e 2019-07-11 stsp goto done;
6394 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6396 818c7501 2019-07-11 stsp goto done;
6398 5943eee2 2019-08-13 stsp if (new_logmsg) {
6399 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6400 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6401 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6402 5943eee2 2019-08-13 stsp goto done;
6405 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6407 5943eee2 2019-08-13 stsp goto done;
6410 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6411 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6412 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
6413 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6414 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6416 a0e95631 2019-07-12 stsp goto done;
6418 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6420 a0e95631 2019-07-12 stsp goto done;
6422 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6424 a0e95631 2019-07-12 stsp goto done;
6426 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6427 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6428 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6429 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6430 a0e95631 2019-07-12 stsp err = sync_err;
6432 a0e95631 2019-07-12 stsp free(fileindex_path);
6433 a0e95631 2019-07-12 stsp free(head_commit_id);
6434 a0e95631 2019-07-12 stsp if (head_ref)
6435 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6436 818c7501 2019-07-11 stsp if (err) {
6437 818c7501 2019-07-11 stsp free(*new_commit_id);
6438 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6440 818c7501 2019-07-11 stsp return err;
6443 818c7501 2019-07-11 stsp const struct got_error *
6444 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6445 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6446 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6447 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6448 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6450 0ebf8283 2019-07-24 stsp const struct got_error *err;
6451 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6452 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6453 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6455 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6457 0ebf8283 2019-07-24 stsp return err;
6459 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6461 0ebf8283 2019-07-24 stsp goto done;
6462 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6464 0ebf8283 2019-07-24 stsp goto done;
6465 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6466 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6467 0ebf8283 2019-07-24 stsp goto done;
6470 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6471 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6473 0ebf8283 2019-07-24 stsp if (commit_ref)
6474 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6475 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6476 0ebf8283 2019-07-24 stsp free(commit_id);
6477 0ebf8283 2019-07-24 stsp return err;
6480 0ebf8283 2019-07-24 stsp const struct got_error *
6481 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6482 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6483 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6484 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6485 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6486 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6488 0ebf8283 2019-07-24 stsp const struct got_error *err;
6489 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6490 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6492 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6494 0ebf8283 2019-07-24 stsp return err;
6496 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6498 0ebf8283 2019-07-24 stsp goto done;
6500 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6501 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6503 0ebf8283 2019-07-24 stsp if (commit_ref)
6504 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6505 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6506 0ebf8283 2019-07-24 stsp return err;
6509 0ebf8283 2019-07-24 stsp const struct got_error *
6510 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6511 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6513 3e3a69f1 2019-07-25 stsp if (fileindex)
6514 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6515 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6518 69844fba 2019-07-11 stsp static const struct got_error *
6519 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6521 69844fba 2019-07-11 stsp const struct got_error *err;
6522 69844fba 2019-07-11 stsp struct got_reference *ref;
6524 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6525 69844fba 2019-07-11 stsp if (err) {
6526 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6527 69844fba 2019-07-11 stsp return NULL;
6528 69844fba 2019-07-11 stsp return err;
6531 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6532 69844fba 2019-07-11 stsp got_ref_close(ref);
6533 69844fba 2019-07-11 stsp return err;
6536 69844fba 2019-07-11 stsp static const struct got_error *
6537 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6539 69844fba 2019-07-11 stsp const struct got_error *err;
6540 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6541 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6543 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6545 69844fba 2019-07-11 stsp goto done;
6546 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6548 69844fba 2019-07-11 stsp goto done;
6550 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6552 69844fba 2019-07-11 stsp goto done;
6553 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6555 69844fba 2019-07-11 stsp goto done;
6557 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6559 69844fba 2019-07-11 stsp goto done;
6560 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6562 69844fba 2019-07-11 stsp goto done;
6564 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6566 69844fba 2019-07-11 stsp goto done;
6567 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6569 69844fba 2019-07-11 stsp goto done;
6572 69844fba 2019-07-11 stsp free(tmp_branch_name);
6573 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6574 69844fba 2019-07-11 stsp free(branch_ref_name);
6575 69844fba 2019-07-11 stsp free(commit_ref_name);
6576 e600f124 2021-03-21 stsp return err;
6579 e600f124 2021-03-21 stsp const struct got_error *
6580 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6581 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6583 e600f124 2021-03-21 stsp const struct got_error *err;
6584 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6585 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6586 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6587 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6588 e600f124 2021-03-21 stsp char *refname = NULL;
6590 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6591 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6592 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6593 e600f124 2021-03-21 stsp branch_name += 11;
6595 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6597 e600f124 2021-03-21 stsp return err;
6599 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6600 e600f124 2021-03-21 stsp new_id_str) == -1) {
6601 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6602 e600f124 2021-03-21 stsp goto done;
6605 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6607 e600f124 2021-03-21 stsp goto done;
6609 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6611 e600f124 2021-03-21 stsp goto done;
6613 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6615 e600f124 2021-03-21 stsp free(new_id_str);
6616 e600f124 2021-03-21 stsp free(refname);
6617 e600f124 2021-03-21 stsp free(old_commit_id);
6619 e600f124 2021-03-21 stsp got_ref_close(ref);
6620 69844fba 2019-07-11 stsp return err;
6623 818c7501 2019-07-11 stsp const struct got_error *
6624 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6625 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6626 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6627 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6629 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6630 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6631 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6633 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6635 818c7501 2019-07-11 stsp return err;
6637 e600f124 2021-03-21 stsp if (create_backup) {
6638 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6639 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6641 e600f124 2021-03-21 stsp goto done;
6644 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6646 818c7501 2019-07-11 stsp goto done;
6648 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6650 818c7501 2019-07-11 stsp goto done;
6652 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6654 818c7501 2019-07-11 stsp goto done;
6656 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6658 a615e0e7 2020-12-16 stsp goto done;
6660 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6662 a615e0e7 2020-12-16 stsp goto done;
6663 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6664 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6665 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6666 a615e0e7 2020-12-16 stsp err = sync_err;
6668 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6669 a615e0e7 2020-12-16 stsp free(fileindex_path);
6670 818c7501 2019-07-11 stsp free(new_head_commit_id);
6671 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6672 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6673 818c7501 2019-07-11 stsp err = unlockerr;
6674 818c7501 2019-07-11 stsp return err;
6677 818c7501 2019-07-11 stsp const struct got_error *
6678 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6679 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6680 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6681 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
6683 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6684 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6685 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6686 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6687 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6688 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6690 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6692 818c7501 2019-07-11 stsp return err;
6694 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6695 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6697 818c7501 2019-07-11 stsp goto done;
6699 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6701 818c7501 2019-07-11 stsp goto done;
6704 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6705 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6706 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6708 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6710 818c7501 2019-07-11 stsp goto done;
6712 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6714 818c7501 2019-07-11 stsp goto done;
6716 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6717 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6719 ca355955 2019-07-12 stsp goto done;
6721 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6723 ca355955 2019-07-12 stsp goto done;
6725 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6727 818c7501 2019-07-11 stsp goto done;
6729 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6730 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6731 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6732 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6733 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6734 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6735 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6736 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
6737 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6738 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
6740 1f1abb7e 2019-08-08 stsp goto sync;
6742 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6743 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6745 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6746 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6747 ca355955 2019-07-12 stsp err = sync_err;
6749 818c7501 2019-07-11 stsp got_ref_close(resolved);
6750 a3a2faf2 2019-07-12 stsp free(tree_id);
6751 818c7501 2019-07-11 stsp free(commit_id);
6752 0ebf8283 2019-07-24 stsp if (fileindex)
6753 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6754 0ebf8283 2019-07-24 stsp free(fileindex_path);
6756 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6757 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6758 0ebf8283 2019-07-24 stsp err = unlockerr;
6759 0ebf8283 2019-07-24 stsp return err;
6762 0ebf8283 2019-07-24 stsp const struct got_error *
6763 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6764 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6765 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6766 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6768 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6769 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6770 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6771 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6772 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6773 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6774 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6775 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6777 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6778 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6779 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6780 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6782 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6784 0ebf8283 2019-07-24 stsp return err;
6786 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6788 0ebf8283 2019-07-24 stsp goto done;
6790 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6791 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6792 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6795 0ebf8283 2019-07-24 stsp goto done;
6797 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6799 0ebf8283 2019-07-24 stsp goto done;
6801 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6803 0ebf8283 2019-07-24 stsp goto done;
6805 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6806 0ebf8283 2019-07-24 stsp worktree);
6808 0ebf8283 2019-07-24 stsp goto done;
6810 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6813 0ebf8283 2019-07-24 stsp goto done;
6815 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6817 0ebf8283 2019-07-24 stsp goto done;
6819 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6821 0ebf8283 2019-07-24 stsp goto done;
6823 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6824 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6826 0ebf8283 2019-07-24 stsp goto done;
6827 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6829 0ebf8283 2019-07-24 stsp goto done;
6830 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6831 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6832 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6833 0ebf8283 2019-07-24 stsp goto done;
6836 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6837 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6839 0ebf8283 2019-07-24 stsp goto done;
6840 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6842 0ebf8283 2019-07-24 stsp goto done;
6844 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6846 0ebf8283 2019-07-24 stsp goto done;
6848 0ebf8283 2019-07-24 stsp free(fileindex_path);
6849 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6850 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6851 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6852 0ebf8283 2019-07-24 stsp if (wt_branch)
6853 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6854 0ebf8283 2019-07-24 stsp if (err) {
6855 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6856 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6857 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6859 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6860 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6861 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6863 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6864 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6865 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6866 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6868 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6870 0ebf8283 2019-07-24 stsp return err;
6873 0ebf8283 2019-07-24 stsp const struct got_error *
6874 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6875 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6877 3e3a69f1 2019-07-25 stsp if (fileindex)
6878 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6879 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6882 0ebf8283 2019-07-24 stsp const struct got_error *
6883 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6884 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6886 0ebf8283 2019-07-24 stsp const struct got_error *err;
6887 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6889 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6891 0ebf8283 2019-07-24 stsp return err;
6893 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6894 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6895 0ebf8283 2019-07-24 stsp return NULL;
6898 0ebf8283 2019-07-24 stsp const struct got_error *
6899 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6900 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6901 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6902 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6904 0ebf8283 2019-07-24 stsp const struct got_error *err;
6905 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6906 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6907 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6908 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6909 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6910 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6912 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6913 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6914 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6915 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6917 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6919 3e3a69f1 2019-07-25 stsp return err;
6921 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6923 f032f1f7 2019-08-04 stsp goto done;
6925 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6926 f032f1f7 2019-08-04 stsp &have_staged_files);
6927 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6928 f032f1f7 2019-08-04 stsp goto done;
6929 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6930 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6931 3e3a69f1 2019-07-25 stsp goto done;
6934 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6936 f032f1f7 2019-08-04 stsp goto done;
6938 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6940 0ebf8283 2019-07-24 stsp goto done;
6942 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6944 0ebf8283 2019-07-24 stsp goto done;
6946 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6947 0ebf8283 2019-07-24 stsp worktree);
6949 0ebf8283 2019-07-24 stsp goto done;
6951 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6953 0ebf8283 2019-07-24 stsp goto done;
6955 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6957 0ebf8283 2019-07-24 stsp goto done;
6958 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6960 0ebf8283 2019-07-24 stsp goto done;
6962 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6964 0ebf8283 2019-07-24 stsp goto done;
6965 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6967 0ebf8283 2019-07-24 stsp goto done;
6969 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6971 0ebf8283 2019-07-24 stsp goto done;
6973 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6974 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6975 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6976 0ebf8283 2019-07-24 stsp if (commit_ref)
6977 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6978 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6979 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6980 0ebf8283 2019-07-24 stsp if (err) {
6981 0ebf8283 2019-07-24 stsp free(*commit_id);
6982 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6983 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6984 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6985 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6986 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6987 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6989 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6990 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6991 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6993 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6995 0ebf8283 2019-07-24 stsp return err;
6998 0ebf8283 2019-07-24 stsp static const struct got_error *
6999 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7001 0ebf8283 2019-07-24 stsp const struct got_error *err;
7002 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7003 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7005 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7007 0ebf8283 2019-07-24 stsp goto done;
7008 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7010 0ebf8283 2019-07-24 stsp goto done;
7012 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7013 0ebf8283 2019-07-24 stsp worktree);
7015 0ebf8283 2019-07-24 stsp goto done;
7016 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7018 0ebf8283 2019-07-24 stsp goto done;
7020 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7022 0ebf8283 2019-07-24 stsp goto done;
7023 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7025 0ebf8283 2019-07-24 stsp goto done;
7027 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7029 0ebf8283 2019-07-24 stsp goto done;
7030 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7032 0ebf8283 2019-07-24 stsp goto done;
7034 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7035 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7036 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7037 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7038 0ebf8283 2019-07-24 stsp return err;
7041 0ebf8283 2019-07-24 stsp const struct got_error *
7042 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7043 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7044 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7045 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7047 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7048 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7049 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7050 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7051 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7053 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7055 0ebf8283 2019-07-24 stsp return err;
7057 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7058 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7060 0ebf8283 2019-07-24 stsp goto done;
7062 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7064 0ebf8283 2019-07-24 stsp goto done;
7066 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7068 0ebf8283 2019-07-24 stsp goto done;
7070 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7071 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7073 0ebf8283 2019-07-24 stsp goto done;
7075 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7077 0ebf8283 2019-07-24 stsp goto done;
7079 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7081 0ebf8283 2019-07-24 stsp goto done;
7083 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7084 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7085 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7086 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7087 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7088 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7089 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7090 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7091 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7092 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7094 1f1abb7e 2019-08-08 stsp goto sync;
7096 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7097 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7099 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7100 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7101 0ebf8283 2019-07-24 stsp err = sync_err;
7103 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7104 0ebf8283 2019-07-24 stsp free(tree_id);
7105 818c7501 2019-07-11 stsp free(fileindex_path);
7107 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7108 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7109 818c7501 2019-07-11 stsp err = unlockerr;
7110 818c7501 2019-07-11 stsp return err;
7113 0ebf8283 2019-07-24 stsp const struct got_error *
7114 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7115 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7116 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7118 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7119 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7120 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7121 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7123 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7125 0ebf8283 2019-07-24 stsp return err;
7127 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7128 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7130 e600f124 2021-03-21 stsp goto done;
7132 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7133 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7135 0ebf8283 2019-07-24 stsp goto done;
7137 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7139 0ebf8283 2019-07-24 stsp goto done;
7141 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7143 0ebf8283 2019-07-24 stsp goto done;
7145 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7147 0ebf8283 2019-07-24 stsp goto done;
7149 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7151 a615e0e7 2020-12-16 stsp goto done;
7153 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7155 a615e0e7 2020-12-16 stsp goto done;
7156 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7157 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7158 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7159 a615e0e7 2020-12-16 stsp err = sync_err;
7161 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7162 a615e0e7 2020-12-16 stsp free(fileindex_path);
7163 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7164 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7165 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7166 0ebf8283 2019-07-24 stsp err = unlockerr;
7167 0ebf8283 2019-07-24 stsp return err;
7170 0ebf8283 2019-07-24 stsp const struct got_error *
7171 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7172 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7174 0ebf8283 2019-07-24 stsp const struct got_error *err;
7175 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7177 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7179 0ebf8283 2019-07-24 stsp return err;
7181 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7183 0ebf8283 2019-07-24 stsp goto done;
7185 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7187 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7188 2822a352 2019-10-15 stsp return err;
7191 2822a352 2019-10-15 stsp const struct got_error *
7192 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7193 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7194 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7195 2822a352 2019-10-15 stsp struct got_repository *repo)
7197 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7198 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7199 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7201 2822a352 2019-10-15 stsp *fileindex = NULL;
7202 2822a352 2019-10-15 stsp *branch_ref = NULL;
7203 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7205 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7207 2822a352 2019-10-15 stsp return err;
7209 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7210 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7211 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7212 2822a352 2019-10-15 stsp "update -b or different branch name required");
7213 2822a352 2019-10-15 stsp goto done;
7216 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7218 2822a352 2019-10-15 stsp goto done;
7220 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7221 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7222 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7223 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7226 2822a352 2019-10-15 stsp goto done;
7228 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7230 2822a352 2019-10-15 stsp goto done;
7232 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7233 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7235 2822a352 2019-10-15 stsp if (err) {
7236 2822a352 2019-10-15 stsp if (*branch_ref) {
7237 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7238 2822a352 2019-10-15 stsp *branch_ref = NULL;
7240 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7241 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7242 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7244 2822a352 2019-10-15 stsp if (*fileindex) {
7245 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7246 2822a352 2019-10-15 stsp *fileindex = NULL;
7248 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7250 0cb83759 2019-08-03 stsp return err;
7253 2822a352 2019-10-15 stsp const struct got_error *
7254 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7255 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7256 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7257 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7258 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7260 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7261 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7262 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7264 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7266 2822a352 2019-10-15 stsp goto done;
7268 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7270 2822a352 2019-10-15 stsp goto done;
7272 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7273 2822a352 2019-10-15 stsp worktree->path_prefix);
7275 2822a352 2019-10-15 stsp goto done;
7277 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7279 2822a352 2019-10-15 stsp goto done;
7281 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7282 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7284 2822a352 2019-10-15 stsp goto sync;
7286 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7288 2822a352 2019-10-15 stsp goto sync;
7290 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7292 6e210706 2021-01-22 stsp goto sync;
7294 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7296 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7297 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7298 2822a352 2019-10-15 stsp err = sync_err;
7301 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7302 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7303 2822a352 2019-10-15 stsp err = unlockerr;
7304 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7306 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7307 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7308 2822a352 2019-10-15 stsp err = unlockerr;
7309 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7311 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7312 2822a352 2019-10-15 stsp free(fileindex_path);
7313 2822a352 2019-10-15 stsp free(tree_id);
7315 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7316 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7317 2822a352 2019-10-15 stsp err = unlockerr;
7318 2822a352 2019-10-15 stsp return err;
7321 2822a352 2019-10-15 stsp const struct got_error *
7322 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7323 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7324 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7326 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7328 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7330 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7332 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7333 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7334 8b692cd0 2019-10-21 stsp err = unlockerr;
7335 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7337 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7338 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7339 8b692cd0 2019-10-21 stsp err = unlockerr;
7340 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7342 f259c4c1 2021-09-24 stsp return err;
7345 f259c4c1 2021-09-24 stsp const struct got_error *
7346 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
7347 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
7349 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
7350 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7352 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7354 f259c4c1 2021-09-24 stsp goto done;
7356 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7358 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
7359 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7360 f259c4c1 2021-09-24 stsp err = sync_err;
7362 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7363 f259c4c1 2021-09-24 stsp free(fileindex_path);
7364 8b692cd0 2019-10-21 stsp return err;
7367 f259c4c1 2021-09-24 stsp static const struct got_error *
7368 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7370 f259c4c1 2021-09-24 stsp const struct got_error *err;
7371 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7373 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7375 f259c4c1 2021-09-24 stsp goto done;
7376 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
7378 f259c4c1 2021-09-24 stsp goto done;
7380 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7382 f259c4c1 2021-09-24 stsp goto done;
7383 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
7385 f259c4c1 2021-09-24 stsp goto done;
7388 f259c4c1 2021-09-24 stsp free(branch_refname);
7389 f259c4c1 2021-09-24 stsp free(commit_refname);
7390 f259c4c1 2021-09-24 stsp return err;
7393 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
7394 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
7395 f259c4c1 2021-09-24 stsp const char *branch_name;
7398 f259c4c1 2021-09-24 stsp static const struct got_error *
7399 f259c4c1 2021-09-24 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7400 f259c4c1 2021-09-24 stsp void *arg)
7402 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
7404 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7405 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
7406 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
7408 f259c4c1 2021-09-24 stsp return NULL;
7412 f259c4c1 2021-09-24 stsp const struct got_error *
7413 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
7414 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
7415 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
7416 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
7417 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7418 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7420 f259c4c1 2021-09-24 stsp const struct got_error *err;
7421 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7423 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7425 f259c4c1 2021-09-24 stsp goto done;
7427 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7428 f259c4c1 2021-09-24 stsp worktree);
7430 f259c4c1 2021-09-24 stsp goto done;
7432 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7433 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
7434 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
7436 f259c4c1 2021-09-24 stsp free(fileindex_path);
7437 f259c4c1 2021-09-24 stsp return err;
7440 f259c4c1 2021-09-24 stsp const struct got_error *
7441 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
7442 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7443 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
7444 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
7445 0ff8d236 2021-09-28 stsp struct got_repository *repo,
7446 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
7449 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
7450 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
7451 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
7452 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
7453 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
7454 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
7455 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7456 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
7457 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7459 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
7461 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
7463 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7465 f259c4c1 2021-09-24 stsp goto done;
7467 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7469 f259c4c1 2021-09-24 stsp goto done;
7471 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7473 f259c4c1 2021-09-24 stsp goto done;
7475 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7476 f259c4c1 2021-09-24 stsp &have_staged_files);
7477 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7478 f259c4c1 2021-09-24 stsp goto done;
7479 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7480 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7481 f259c4c1 2021-09-24 stsp goto done;
7484 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
7485 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
7486 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
7487 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
7488 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
7489 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7490 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7491 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7493 f259c4c1 2021-09-24 stsp goto done;
7495 f259c4c1 2021-09-24 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7496 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7497 f259c4c1 2021-09-24 stsp "merge of %s cannot proceed", branch_name);
7498 f259c4c1 2021-09-24 stsp goto done;
7501 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7502 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7503 f259c4c1 2021-09-24 stsp const char *ct_path = ct->in_repo_path;
7505 f259c4c1 2021-09-24 stsp while (ct_path[0] == '/')
7506 f259c4c1 2021-09-24 stsp ct_path++;
7507 f259c4c1 2021-09-24 stsp err = check_out_of_date(ct_path, ct->status,
7508 f259c4c1 2021-09-24 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7509 f259c4c1 2021-09-24 stsp head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7511 f259c4c1 2021-09-24 stsp goto done;
7515 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
7516 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
7517 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
7518 f259c4c1 2021-09-24 stsp head_commit_id, branch_tip, worktree, author, committer,
7519 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7521 f259c4c1 2021-09-24 stsp goto done;
7523 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
7524 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
7525 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7526 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7527 f259c4c1 2021-09-24 stsp err = sync_err;
7529 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7530 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7531 f259c4c1 2021-09-24 stsp free_commitable(ct);
7533 f259c4c1 2021-09-24 stsp got_pathlist_free(&commitable_paths);
7534 f259c4c1 2021-09-24 stsp free(fileindex_path);
7535 f259c4c1 2021-09-24 stsp return err;
7538 f259c4c1 2021-09-24 stsp const struct got_error *
7539 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
7540 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
7542 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7543 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7545 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7547 f259c4c1 2021-09-24 stsp goto done;
7549 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7551 f259c4c1 2021-09-24 stsp goto done;
7552 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7553 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7554 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7555 f259c4c1 2021-09-24 stsp err = sync_err;
7557 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7558 f259c4c1 2021-09-24 stsp free(fileindex_path);
7559 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7560 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7561 f259c4c1 2021-09-24 stsp err = unlockerr;
7562 f259c4c1 2021-09-24 stsp return err;
7565 f259c4c1 2021-09-24 stsp const struct got_error *
7566 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7567 f259c4c1 2021-09-24 stsp struct got_repository *repo)
7569 f259c4c1 2021-09-24 stsp const struct got_error *err;
7570 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
7571 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
7573 f259c4c1 2021-09-24 stsp *in_progress = 0;
7575 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7577 f259c4c1 2021-09-24 stsp return err;
7578 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7579 793fcac3 2021-09-24 stsp free(branch_refname);
7580 f259c4c1 2021-09-24 stsp if (err) {
7581 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
7582 f259c4c1 2021-09-24 stsp return err;
7584 f259c4c1 2021-09-24 stsp *in_progress = 1;
7586 f259c4c1 2021-09-24 stsp return NULL;
7589 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
7590 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7591 f259c4c1 2021-09-24 stsp struct got_reference *branch, struct got_repository *repo)
7593 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
7594 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7595 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7596 f259c4c1 2021-09-24 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7597 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL;
7598 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7599 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
7601 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7603 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
7605 f259c4c1 2021-09-24 stsp return err;
7607 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7609 f259c4c1 2021-09-24 stsp goto done;
7611 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
7612 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
7613 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
7614 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7617 f259c4c1 2021-09-24 stsp goto done;
7619 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7621 f259c4c1 2021-09-24 stsp return err;
7623 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7625 f259c4c1 2021-09-24 stsp return err;
7627 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7630 f259c4c1 2021-09-24 stsp goto done;
7632 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7634 f259c4c1 2021-09-24 stsp goto done;
7636 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7637 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7638 f259c4c1 2021-09-24 stsp goto done;
7641 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
7643 f259c4c1 2021-09-24 stsp goto done;
7645 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7647 f259c4c1 2021-09-24 stsp goto done;
7648 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
7650 f259c4c1 2021-09-24 stsp goto done;
7652 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7654 f259c4c1 2021-09-24 stsp goto done;
7655 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
7657 f259c4c1 2021-09-24 stsp goto done;
7660 f259c4c1 2021-09-24 stsp free(branch_refname);
7661 f259c4c1 2021-09-24 stsp free(commit_refname);
7662 f259c4c1 2021-09-24 stsp free(fileindex_path);
7663 f259c4c1 2021-09-24 stsp if (branch_ref)
7664 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
7665 f259c4c1 2021-09-24 stsp if (commit_ref)
7666 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
7667 f259c4c1 2021-09-24 stsp if (wt_branch)
7668 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
7669 f259c4c1 2021-09-24 stsp free(wt_branch_tip);
7670 f259c4c1 2021-09-24 stsp if (err) {
7671 f259c4c1 2021-09-24 stsp if (*fileindex) {
7672 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
7673 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7675 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
7677 f259c4c1 2021-09-24 stsp return err;
7680 f259c4c1 2021-09-24 stsp const struct got_error *
7681 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
7682 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7683 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7685 f259c4c1 2021-09-24 stsp const struct got_error *err;
7686 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
7687 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7688 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7689 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7691 f259c4c1 2021-09-24 stsp *branch_name = NULL;
7692 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
7693 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7695 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
7697 f259c4c1 2021-09-24 stsp return err;
7699 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7701 f259c4c1 2021-09-24 stsp goto done;
7703 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7704 f259c4c1 2021-09-24 stsp &have_staged_files);
7705 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7706 f259c4c1 2021-09-24 stsp goto done;
7707 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7708 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7709 f259c4c1 2021-09-24 stsp goto done;
7712 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7714 f259c4c1 2021-09-24 stsp goto done;
7716 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7718 f259c4c1 2021-09-24 stsp goto done;
7720 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7722 f259c4c1 2021-09-24 stsp goto done;
7724 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
7725 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7726 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
7727 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
7728 f259c4c1 2021-09-24 stsp goto done;
7730 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7731 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
7732 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
7733 f259c4c1 2021-09-24 stsp goto done;
7736 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7738 f259c4c1 2021-09-24 stsp goto done;
7740 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
7742 f259c4c1 2021-09-24 stsp goto done;
7744 f259c4c1 2021-09-24 stsp free(commit_refname);
7745 f259c4c1 2021-09-24 stsp free(branch_refname);
7746 f259c4c1 2021-09-24 stsp free(fileindex_path);
7747 f259c4c1 2021-09-24 stsp if (commit_ref)
7748 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
7749 f259c4c1 2021-09-24 stsp if (branch_ref)
7750 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
7751 f259c4c1 2021-09-24 stsp if (err) {
7752 f259c4c1 2021-09-24 stsp if (*branch_name) {
7753 f259c4c1 2021-09-24 stsp free(*branch_name);
7754 f259c4c1 2021-09-24 stsp *branch_name = NULL;
7756 f259c4c1 2021-09-24 stsp free(*branch_tip);
7757 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
7758 f259c4c1 2021-09-24 stsp if (*fileindex) {
7759 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
7760 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7762 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
7764 f259c4c1 2021-09-24 stsp return err;
7767 f259c4c1 2021-09-24 stsp const struct got_error *
7768 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
7769 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7770 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7772 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7773 f259c4c1 2021-09-24 stsp struct got_object_id *commit_id = NULL;
7774 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7775 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
7776 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
7778 f259c4c1 2021-09-24 stsp err = got_object_id_by_path(&tree_id, repo,
7779 f259c4c1 2021-09-24 stsp worktree->base_commit_id, worktree->path_prefix);
7781 f259c4c1 2021-09-24 stsp goto done;
7783 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7785 f259c4c1 2021-09-24 stsp goto done;
7787 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7789 f259c4c1 2021-09-24 stsp goto done;
7791 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
7792 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
7793 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
7794 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
7795 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
7796 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
7797 f259c4c1 2021-09-24 stsp rfa.repo = repo;
7798 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
7799 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7800 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7802 f259c4c1 2021-09-24 stsp goto sync;
7804 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7805 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7807 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7808 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7809 f259c4c1 2021-09-24 stsp err = sync_err;
7811 f259c4c1 2021-09-24 stsp free(tree_id);
7812 f259c4c1 2021-09-24 stsp free(commit_id);
7813 f259c4c1 2021-09-24 stsp if (fileindex)
7814 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7815 f259c4c1 2021-09-24 stsp free(fileindex_path);
7817 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7818 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7819 f259c4c1 2021-09-24 stsp err = unlockerr;
7820 f259c4c1 2021-09-24 stsp return err;
7823 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7824 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7825 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7826 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7827 2db2652d 2019-08-07 stsp struct got_repository *repo;
7828 2db2652d 2019-08-07 stsp int have_changes;
7831 2db2652d 2019-08-07 stsp const struct got_error *
7832 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7833 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7834 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7835 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7837 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7838 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7839 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7840 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7841 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7842 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7844 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7845 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7846 8b13ce36 2019-08-08 stsp return NULL;
7847 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7848 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7850 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7851 735ef5ac 2019-08-03 stsp if (ie == NULL)
7852 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7854 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7855 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7856 735ef5ac 2019-08-03 stsp relpath) == -1)
7857 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7859 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7860 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7861 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7862 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7865 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7866 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7867 3aa5969e 2019-08-06 stsp goto done;
7868 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7869 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7870 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7871 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7872 735ef5ac 2019-08-03 stsp goto done;
7875 2db2652d 2019-08-07 stsp a->have_changes = 1;
7877 735ef5ac 2019-08-03 stsp p = in_repo_path;
7878 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7880 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7881 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7882 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7884 735ef5ac 2019-08-03 stsp free(in_repo_path);
7885 dc424a06 2019-08-07 stsp return err;
7888 2db2652d 2019-08-07 stsp struct stage_path_arg {
7889 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7890 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7891 2db2652d 2019-08-07 stsp struct got_repository *repo;
7892 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7893 2db2652d 2019-08-07 stsp void *status_arg;
7894 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7895 2db2652d 2019-08-07 stsp void *patch_arg;
7896 7b5dc508 2019-10-28 stsp int staged_something;
7897 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7900 2db2652d 2019-08-07 stsp static const struct got_error *
7901 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7902 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7903 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7904 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7906 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7907 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7908 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7909 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7910 0cb83759 2019-08-03 stsp uint32_t stage;
7911 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7912 0aeb8099 2020-07-23 stsp struct stat sb;
7914 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7915 8b13ce36 2019-08-08 stsp return NULL;
7917 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7918 d3e7c587 2019-08-03 stsp if (ie == NULL)
7919 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7921 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7922 2db2652d 2019-08-07 stsp relpath)== -1)
7923 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7925 0cb83759 2019-08-03 stsp switch (status) {
7926 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7927 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7928 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7929 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7930 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7933 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7934 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7935 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7936 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7937 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7940 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7943 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7944 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7945 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7946 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7947 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7951 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7952 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7955 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7956 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7957 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7958 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7960 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7961 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7962 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7963 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7964 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7965 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7966 35213c7c 2020-07-23 stsp ssize_t target_len;
7967 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7968 35213c7c 2020-07-23 stsp sizeof(target_path));
7969 35213c7c 2020-07-23 stsp if (target_len == -1) {
7970 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7971 35213c7c 2020-07-23 stsp ondisk_path);
7974 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7975 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7976 35213c7c 2020-07-23 stsp a->worktree->root_path);
7979 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7980 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7981 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7985 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7986 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7987 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7989 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7990 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7992 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7993 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7995 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7996 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7998 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7999 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8000 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8002 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8003 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8005 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8006 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8007 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8008 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8011 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8013 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8014 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8018 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8019 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8020 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8021 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8023 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8024 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8027 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8029 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8030 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8032 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8033 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8036 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8040 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8041 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8042 dc424a06 2019-08-07 stsp free(path_content);
8043 2db2652d 2019-08-07 stsp free(ondisk_path);
8044 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8045 0ebf8283 2019-07-24 stsp return err;
8048 0cb83759 2019-08-03 stsp const struct got_error *
8049 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8050 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8051 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8052 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8053 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8055 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8056 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8057 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8058 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8059 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8060 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8061 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8062 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8064 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8066 0cb83759 2019-08-03 stsp return err;
8068 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8069 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8071 735ef5ac 2019-08-03 stsp goto done;
8072 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8074 735ef5ac 2019-08-03 stsp goto done;
8075 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8077 0cb83759 2019-08-03 stsp goto done;
8079 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8080 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8081 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8082 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8083 2db2652d 2019-08-07 stsp oka.repo = repo;
8084 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8085 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8086 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8087 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
8089 735ef5ac 2019-08-03 stsp goto done;
8091 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8092 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8093 2db2652d 2019-08-07 stsp goto done;
8096 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8097 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8098 2db2652d 2019-08-07 stsp spa.repo = repo;
8099 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8100 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8101 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8102 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8103 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8104 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8105 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8106 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8107 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
8109 2db2652d 2019-08-07 stsp goto done;
8111 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8112 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8113 7b5dc508 2019-10-28 stsp goto done;
8116 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8117 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8118 0cb83759 2019-08-03 stsp err = sync_err;
8120 735ef5ac 2019-08-03 stsp if (head_ref)
8121 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8122 735ef5ac 2019-08-03 stsp free(head_commit_id);
8123 0cb83759 2019-08-03 stsp free(fileindex_path);
8124 0cb83759 2019-08-03 stsp if (fileindex)
8125 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8126 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8127 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8128 0cb83759 2019-08-03 stsp err = unlockerr;
8129 0cb83759 2019-08-03 stsp return err;
8132 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8133 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8134 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8135 ad493afc 2019-08-03 stsp struct got_repository *repo;
8136 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8137 ad493afc 2019-08-03 stsp void *progress_arg;
8138 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8139 2e1f37b0 2019-08-08 stsp void *patch_arg;
8142 2e1f37b0 2019-08-08 stsp static const struct got_error *
8143 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8144 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8145 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8146 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8147 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8149 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8150 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8151 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8152 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8153 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8154 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8155 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8157 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8158 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8160 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8162 2e1f37b0 2019-08-08 stsp return err;
8163 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8165 2e1f37b0 2019-08-08 stsp goto done;
8167 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8169 2e1f37b0 2019-08-08 stsp goto done;
8171 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8173 2e1f37b0 2019-08-08 stsp goto done;
8175 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8177 2e1f37b0 2019-08-08 stsp goto done;
8179 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8181 2e1f37b0 2019-08-08 stsp goto done;
8183 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8185 2e1f37b0 2019-08-08 stsp goto done;
8187 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
8188 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
8190 2e1f37b0 2019-08-08 stsp goto done;
8192 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8193 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
8195 2e1f37b0 2019-08-08 stsp goto done;
8196 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8197 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
8199 2e1f37b0 2019-08-08 stsp goto done;
8201 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8202 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8203 2e1f37b0 2019-08-08 stsp goto done;
8205 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8206 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8207 2e1f37b0 2019-08-08 stsp goto done;
8209 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8210 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8211 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
8212 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8213 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8214 fe621944 2020-11-10 stsp nchanges++;
8216 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8217 2e1f37b0 2019-08-08 stsp int choice;
8218 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8219 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8220 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8221 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8223 2e1f37b0 2019-08-08 stsp goto done;
8224 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8225 2e1f37b0 2019-08-08 stsp have_content = 1;
8227 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8228 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8231 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8232 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8233 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8235 2e1f37b0 2019-08-08 stsp free(label1);
8237 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8238 2e1f37b0 2019-08-08 stsp if (staged_blob)
8239 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8240 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8241 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8242 fe621944 2020-11-10 stsp err = free_err;
8243 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8244 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8245 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8246 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8247 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8248 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8249 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8250 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8251 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8252 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8253 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8254 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8255 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8256 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8257 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8258 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8259 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8260 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8261 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8263 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8264 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8265 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8266 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8267 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8268 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8269 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8271 2e1f37b0 2019-08-08 stsp free(path1);
8272 2e1f37b0 2019-08-08 stsp free(path2);
8273 fda8017d 2020-07-23 stsp return err;
8276 fda8017d 2020-07-23 stsp static const struct got_error *
8277 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8278 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8279 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8280 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8281 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8282 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8283 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8285 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8286 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8287 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8288 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8289 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8290 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8291 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8292 fda8017d 2020-07-23 stsp struct stat sb;
8294 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8295 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8296 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8298 fda8017d 2020-07-23 stsp return err;
8300 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8301 fda8017d 2020-07-23 stsp return NULL;
8303 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8304 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8305 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8307 fda8017d 2020-07-23 stsp goto done;
8310 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
8311 fda8017d 2020-07-23 stsp if (f == NULL) {
8312 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8313 fda8017d 2020-07-23 stsp path_unstaged_content);
8314 fda8017d 2020-07-23 stsp goto done;
8316 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8317 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8318 fda8017d 2020-07-23 stsp goto done;
8320 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8321 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8322 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8324 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8325 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8326 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8327 fda8017d 2020-07-23 stsp goto done;
8329 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8330 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8331 fda8017d 2020-07-23 stsp goto done;
8333 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8334 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8335 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8336 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8337 fda8017d 2020-07-23 stsp progress_arg);
8339 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8341 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8343 67a66647 2021-05-31 stsp return err;
8345 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8346 67a66647 2021-05-31 stsp parent) == -1) {
8347 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8348 67a66647 2021-05-31 stsp base_path = NULL;
8349 67a66647 2021-05-31 stsp goto done;
8352 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8354 67a66647 2021-05-31 stsp goto done;
8355 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8356 67a66647 2021-05-31 stsp blob_base);
8358 67a66647 2021-05-31 stsp goto done;
8361 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8362 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8364 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8365 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8366 eec2f5a9 2021-06-03 stsp ondisk_path);
8368 eec2f5a9 2021-06-03 stsp goto done;
8371 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
8372 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8373 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8374 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8375 eec2f5a9 2021-06-03 stsp goto done;
8377 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8378 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8379 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8380 eec2f5a9 2021-06-03 stsp close(fd);
8381 eec2f5a9 2021-06-03 stsp goto done;
8385 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8386 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8387 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8388 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8389 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8392 fda8017d 2020-07-23 stsp goto done;
8394 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8395 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8396 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8398 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8399 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8402 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8403 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8404 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8405 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8406 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8407 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8408 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8409 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8410 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8411 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8412 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8413 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8414 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8415 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8416 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8417 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8418 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8419 67a66647 2021-05-31 stsp free(blob_base_path);
8420 67a66647 2021-05-31 stsp free(parent);
8421 67a66647 2021-05-31 stsp free(base_path);
8422 2e1f37b0 2019-08-08 stsp return err;
8425 ad493afc 2019-08-03 stsp static const struct got_error *
8426 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8427 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8428 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8429 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8431 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8432 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8433 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8434 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8435 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8436 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8437 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8438 9bc94a15 2019-08-03 stsp struct stat sb;
8440 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8441 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8442 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8443 2e1f37b0 2019-08-08 stsp return NULL;
8445 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8446 ad493afc 2019-08-03 stsp if (ie == NULL)
8447 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8449 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8451 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8453 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8454 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8456 f69721c3 2019-10-21 stsp goto done;
8457 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8458 f69721c3 2019-10-21 stsp id_str) == -1) {
8459 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8460 f69721c3 2019-10-21 stsp goto done;
8463 ad493afc 2019-08-03 stsp switch (staged_status) {
8464 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8465 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8466 ad493afc 2019-08-03 stsp blob_id, 8192);
8469 ad493afc 2019-08-03 stsp /* fall through */
8470 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8471 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8472 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8473 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8474 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8475 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8478 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8481 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8482 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8483 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8484 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8485 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8486 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8489 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8490 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
8493 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8494 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8495 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8496 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8497 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8498 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8499 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8500 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8501 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8503 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8504 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8505 36bf999c 2020-07-23 stsp char *staged_target;
8506 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8507 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8509 36bf999c 2020-07-23 stsp goto done;
8510 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8511 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8512 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8513 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8514 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8515 36bf999c 2020-07-23 stsp free(staged_target);
8517 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8518 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8519 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8520 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8521 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8522 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8523 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8527 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8530 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8531 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8532 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8533 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8536 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8537 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8538 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8539 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8540 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8543 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8545 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8546 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8550 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8551 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8552 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8553 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8556 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8560 ad493afc 2019-08-03 stsp free(ondisk_path);
8561 ad493afc 2019-08-03 stsp if (blob_base)
8562 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8563 ad493afc 2019-08-03 stsp if (blob_staged)
8564 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8565 f69721c3 2019-10-21 stsp free(id_str);
8566 f69721c3 2019-10-21 stsp free(label_orig);
8567 ad493afc 2019-08-03 stsp return err;
8570 ad493afc 2019-08-03 stsp const struct got_error *
8571 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8572 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8573 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8574 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8575 ad493afc 2019-08-03 stsp struct got_repository *repo)
8577 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8578 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8579 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8580 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8581 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8583 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8585 ad493afc 2019-08-03 stsp return err;
8587 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8589 ad493afc 2019-08-03 stsp goto done;
8591 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8592 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8593 ad493afc 2019-08-03 stsp upa.repo = repo;
8594 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8595 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8596 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8597 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8598 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8599 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8600 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
8602 ad493afc 2019-08-03 stsp goto done;
8605 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8606 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8607 ad493afc 2019-08-03 stsp err = sync_err;
8609 ad493afc 2019-08-03 stsp free(fileindex_path);
8610 ad493afc 2019-08-03 stsp if (fileindex)
8611 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8612 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8613 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8614 ad493afc 2019-08-03 stsp err = unlockerr;
8615 ad493afc 2019-08-03 stsp return err;
8618 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8619 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8620 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8621 b2118c49 2020-07-28 stsp void *info_arg;
8622 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8623 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8624 b2118c49 2020-07-28 stsp void *cancel_arg;
8627 b2118c49 2020-07-28 stsp static const struct got_error *
8628 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8630 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8631 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8632 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8633 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8634 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8635 b2118c49 2020-07-28 stsp int stage;
8637 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8638 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8640 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8641 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8642 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8645 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8646 b2118c49 2020-07-28 stsp return NULL;
8648 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8649 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8650 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8652 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8653 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8654 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8655 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8656 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8657 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8660 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8661 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8662 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8665 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8666 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8669 b2118c49 2020-07-28 stsp const struct got_error *
8670 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8671 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8672 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8673 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8676 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8677 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8678 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8679 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8681 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8683 b2118c49 2020-07-28 stsp return err;
8685 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8687 b2118c49 2020-07-28 stsp goto done;
8689 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8690 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8691 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8692 b2118c49 2020-07-28 stsp arg.paths = paths;
8693 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8694 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8695 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8698 b2118c49 2020-07-28 stsp free(fileindex_path);
8699 b2118c49 2020-07-28 stsp if (fileindex)
8700 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8701 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8702 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8703 b2118c49 2020-07-28 stsp err = unlockerr;
8704 b2118c49 2020-07-28 stsp return err;